Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
eramitmittal authored Feb 8, 2025
2 parents f5fb6cc + 8cff889 commit 92af000
Show file tree
Hide file tree
Showing 681 changed files with 66,257 additions and 3,574 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/Java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
fetch-depth: 0
ref: ${{ inputs.git_ref }}

- run: make format-check
- run: |
python3 -m pip install --user clang_format==11.0.1
make format-check
java-linux-amd64:
name: Java Linux (amd64)
Expand Down
6 changes: 3 additions & 3 deletions CMakeLists.txt

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions src/duckdb/extension/core_functions/aggregate/algebraic/avg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ struct AvgState {
}
};

struct IntervalAvgState {
int64_t count;
interval_t value;

void Initialize() {
this->count = 0;
this->value = interval_t();
}

void Combine(const IntervalAvgState &other) {
this->count += other.count;
this->value = AddOperator::Operation<interval_t, interval_t, interval_t>(this->value, other.value);
}
};

struct KahanAvgState {
uint64_t count;
double value;
Expand Down Expand Up @@ -105,6 +120,20 @@ struct IntegerAverageOperationHugeint : public BaseSumOperation<AverageSetOperat
}
};

struct DiscreteAverageOperation : public BaseSumOperation<AverageSetOperation, AddToHugeint> {
template <class T, class STATE>
static void Finalize(STATE &state, T &target, AggregateFinalizeData &finalize_data) {
if (state.count == 0) {
finalize_data.ReturnNull();
} else {
uint64_t remainder;
target = Hugeint::Cast<T>(Hugeint::DivModPositive(state.value, state.count, remainder));
// Round the result
target += (remainder > (state.count / 2));
}
}
};

struct HugeintAverageOperation : public BaseSumOperation<AverageSetOperation, HugeintAdd> {
template <class T, class STATE>
static void Finalize(STATE &state, T &target, AggregateFinalizeData &finalize_data) {
Expand Down Expand Up @@ -139,6 +168,45 @@ struct KahanAverageOperation : public BaseSumOperation<AverageSetOperation, Kaha
}
};

struct IntervalAverageOperation : public BaseSumOperation<AverageSetOperation, IntervalAdd> {
// Override BaseSumOperation::Initialize because
// IntervalAvgState does not have an assignment constructor from 0
static void Initialize(IntervalAvgState &state) {
AverageSetOperation::Initialize<IntervalAvgState>(state);
}

template <class RESULT_TYPE, class STATE>
static void Finalize(STATE &state, RESULT_TYPE &target, AggregateFinalizeData &finalize_data) {
if (state.count == 0) {
finalize_data.ReturnNull();
} else {
// DivideOperator does not borrow fractions right,
// TODO: Maybe it should?
// Copy PG implementation.
const auto &value = state.value;
const auto count = UnsafeNumericCast<int64_t>(state.count);

target.months = value.months / count;
auto months_remainder = value.months % count;

target.days = value.days / count;
auto days_remainder = value.days % count;

target.micros = value.micros / count;
auto micros_remainder = value.micros % count;

// Shift the remainders right
months_remainder *= Interval::DAYS_PER_MONTH;
target.days += months_remainder / count;
days_remainder += months_remainder % count;

days_remainder *= Interval::MICROS_PER_DAY;
micros_remainder += days_remainder / count;
target.micros += micros_remainder;
}
}
};

AggregateFunction GetAverageAggregate(PhysicalType type) {
switch (type) {
case PhysicalType::INT16: {
Expand All @@ -157,6 +225,10 @@ AggregateFunction GetAverageAggregate(PhysicalType type) {
return AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, hugeint_t, double, HugeintAverageOperation>(
LogicalType::HUGEINT, LogicalType::DOUBLE);
}
case PhysicalType::INTERVAL: {
return AggregateFunction::UnaryAggregate<IntervalAvgState, interval_t, interval_t, IntervalAverageOperation>(
LogicalType::INTERVAL, LogicalType::INTERVAL);
}
default:
throw InternalException("Unimplemented average aggregate");
}
Expand All @@ -183,8 +255,17 @@ AggregateFunctionSet AvgFun::GetFunctions() {
avg.AddFunction(GetAverageAggregate(PhysicalType::INT32));
avg.AddFunction(GetAverageAggregate(PhysicalType::INT64));
avg.AddFunction(GetAverageAggregate(PhysicalType::INT128));
avg.AddFunction(GetAverageAggregate(PhysicalType::INTERVAL));
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<double>, double, double, NumericAverageOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE));

avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, int64_t, DiscreteAverageOperation>(
LogicalType::TIMESTAMP, LogicalType::TIMESTAMP));
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, int64_t, DiscreteAverageOperation>(
LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP_TZ));
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, int64_t, DiscreteAverageOperation>(
LogicalType::TIME, LogicalType::TIME));

return avg;
}

Expand Down
1 change: 1 addition & 0 deletions src/duckdb/extension/core_functions/function_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ static const StaticFunctionDefinition core_functions[] = {
DUCKDB_SCALAR_FUNCTION(MapConcatFun),
DUCKDB_SCALAR_FUNCTION(MapEntriesFun),
DUCKDB_SCALAR_FUNCTION(MapExtractFun),
DUCKDB_SCALAR_FUNCTION(MapExtractValueFun),
DUCKDB_SCALAR_FUNCTION(MapFromEntriesFun),
DUCKDB_SCALAR_FUNCTION(MapKeysFun),
DUCKDB_SCALAR_FUNCTION(MapValuesFun),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#pragma once

#include "duckdb/common/common.hpp"
#include "duckdb/common/types.hpp"
#include "duckdb/common/types/vector.hpp"
#include "duckdb/common/types/hugeint.hpp"
#include "duckdb/common/operator/add.hpp"
#include "duckdb/common/operator/multiply.hpp"
#include "duckdb/function/aggregate_state.hpp"
#include "duckdb/common/operator/cast_operators.hpp"

namespace duckdb {

Expand Down Expand Up @@ -77,6 +79,20 @@ struct HugeintAdd {
}
};

struct IntervalAdd {
template <class STATE, class T>
static void AddNumber(STATE &state, T input) {
state.value = AddOperator::Operation<interval_t, interval_t, interval_t>(state.value, input);
}

template <class STATE, class T>
static void AddConstant(STATE &state, T input, idx_t count) {
const auto count64 = Cast::Operation<idx_t, int64_t>(count);
input = MultiplyOperator::Operation<interval_t, int64_t, interval_t>(input, count64);
state.value = AddOperator::Operation<interval_t, interval_t, interval_t>(state.value, input);
}
};

struct KahanAdd {
template <class STATE, class T>
static void AddNumber(STATE &state, T input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ struct ElementAtFun {
static constexpr const char *Name = "element_at";
};

struct MapExtractValueFun {
static constexpr const char *Name = "map_extract_value";
static constexpr const char *Parameters = "map,key";
static constexpr const char *Description = "Returns the value for a given key or NULL if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map’s keys else an error is returned";
static constexpr const char *Example = "map_extract_value(map(['key'], ['val']), 'key')";

static ScalarFunction GetFunction();
};

struct MapFromEntriesFun {
static constexpr const char *Name = "map_from_entries";
static constexpr const char *Parameters = "map";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "duckdb/main/client_context.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/transaction/meta_transaction.hpp"
#include "duckdb/planner/expression/bound_cast_expression.hpp"

namespace duckdb {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ static void CanCastImplicitlyFunction(DataChunk &args, ExpressionState &state, V
}

unique_ptr<Expression> BindCanCastImplicitlyExpression(FunctionBindExpressionInput &input) {
auto &source_type = input.function.children[0]->return_type;
auto &target_type = input.function.children[1]->return_type;
auto &source_type = input.children[0]->return_type;
auto &target_type = input.children[1]->return_type;
if (source_type.id() == LogicalTypeId::UNKNOWN || source_type.id() == LogicalTypeId::SQLNULL ||
target_type.id() == LogicalTypeId::UNKNOWN || target_type.id() == LogicalTypeId::SQLNULL) {
// parameter - unknown return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static void TypeOfFunction(DataChunk &args, ExpressionState &state, Vector &resu
}

unique_ptr<Expression> BindTypeOfFunctionExpression(FunctionBindExpressionInput &input) {
auto &return_type = input.function.children[0]->return_type;
auto &return_type = input.children[0]->return_type;
if (return_type.id() == LogicalTypeId::UNKNOWN || return_type.id() == LogicalTypeId::SQLNULL) {
// parameter - unknown return type
return nullptr;
Expand Down
Loading

0 comments on commit 92af000

Please sign in to comment.