Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
915 changes: 458 additions & 457 deletions CMakeLists.txt

Large diffs are not rendered by default.

84 changes: 21 additions & 63 deletions src/duckdb/extension/core_functions/aggregate/algebraic/avg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,39 @@ namespace {

template <class T>
struct AvgState {
static constexpr const char *STATE_NAMES[] = {"count", "value"};
using STATE_TYPE = StructStateType<uint64_t, T>;

uint64_t count;
T value;

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

void Combine(const AvgState<T> &other) {
this->count += other.count;
this->value += other.value;
}
};

struct IntervalAvgState {
static constexpr const char *STATE_NAMES[] = {"count", "value"};
using STATE_TYPE = StructStateType<int64_t, interval_t>;

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 {
static constexpr const char *STATE_NAMES[] = {"count", "value", "err"};
using STATE_TYPE = StructStateType<uint64_t, double, double>;

uint64_t count;
double value;
double err;

void Initialize() {
this->count = 0;
this->err = 0.0;
}

void Combine(const KahanAvgState &other) {
this->count += other.count;
KahanAddInternal(other.value, this->value, this->err);
Expand All @@ -75,10 +70,6 @@ struct AverageDecimalBindData : public FunctionData {
};

struct AverageSetOperation {
template <class STATE>
static void Initialize(STATE &state) {
state.Initialize();
}
template <class STATE>
static void Combine(const STATE &source, STATE &target, AggregateInputData &) {
target.Combine(source);
Expand Down Expand Up @@ -172,12 +163,6 @@ 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) {
Expand Down Expand Up @@ -239,47 +224,27 @@ struct TimeTZAverageOperation : public BaseSumOperation<AverageSetOperation, Add
}
};

LogicalType GetAvgStateType(const BoundAggregateFunction &function) {
child_list_t<LogicalType> children;
children.emplace_back("count", LogicalType::UBIGINT);
children.emplace_back("value", function.GetArguments()[0]);
return LogicalType::STRUCT(std::move(children));
}

LogicalType GetKahanAvgStateType(const BoundAggregateFunction &function) {
child_list_t<LogicalType> children;
children.emplace_back("count", LogicalType::UBIGINT);
children.emplace_back("value", LogicalType::DOUBLE);
children.emplace_back("err", LogicalType::DOUBLE);
return LogicalType::STRUCT(std::move(children));
}

AggregateFunction GetAverageAggregate(PhysicalType type) {
switch (type) {
case PhysicalType::INT16: {
return AggregateFunction::UnaryAggregate<AvgState<int64_t>, int16_t, double, IntegerAverageOperation>(
LogicalType::SMALLINT, LogicalType::DOUBLE)
.SetStructStateExport(GetAvgStateType);
LogicalType::SMALLINT, LogicalType::DOUBLE);
}
case PhysicalType::INT32: {
return AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int32_t, double, IntegerAverageOperationHugeint>(
LogicalType::INTEGER, LogicalType::DOUBLE)
.SetStructStateExport(GetAvgStateType);
LogicalType::INTEGER, LogicalType::DOUBLE);
}
case PhysicalType::INT64: {
return AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, double, IntegerAverageOperationHugeint>(
LogicalType::BIGINT, LogicalType::DOUBLE)
.SetStructStateExport(GetAvgStateType);
LogicalType::BIGINT, LogicalType::DOUBLE);
}
case PhysicalType::INT128: {
return AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, hugeint_t, double, HugeintAverageOperation>(
LogicalType::HUGEINT, LogicalType::DOUBLE)
.SetStructStateExport(GetAvgStateType);
LogicalType::HUGEINT, LogicalType::DOUBLE);
}
case PhysicalType::INTERVAL: {
return AggregateFunction::UnaryAggregate<IntervalAvgState, interval_t, interval_t, IntervalAverageOperation>(
LogicalType::INTERVAL, LogicalType::INTERVAL)
.SetStructStateExport(GetAvgStateType);
LogicalType::INTERVAL, LogicalType::INTERVAL);
}
default:
throw InternalException("Unimplemented average aggregate");
Expand Down Expand Up @@ -313,31 +278,24 @@ AggregateFunctionSet AvgFun::GetFunctions() {
avg.AddFunction(GetAverageAggregate(PhysicalType::INT128));
avg.AddFunction(GetAverageAggregate(PhysicalType::INTERVAL));
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<double>, double, double, NumericAverageOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE)
.SetStructStateExport(GetAvgStateType));
LogicalType::DOUBLE, LogicalType::DOUBLE));

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

return avg;
}

AggregateFunction FAvgFun::GetFunction() {
auto function = AggregateFunction::UnaryAggregate<KahanAvgState, double, double, KahanAverageOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE)
.SetStructStateExport(GetKahanAvgStateType);
return function;
return AggregateFunction::UnaryAggregate<KahanAvgState, double, double, KahanAverageOperation>(LogicalType::DOUBLE,
LogicalType::DOUBLE);
}

} // namespace duckdb
28 changes: 1 addition & 27 deletions src/duckdb/extension/core_functions/aggregate/algebraic/corr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,8 @@

namespace duckdb {

LogicalType GetCorrStateType() {
child_list_t<LogicalType> covar_children;
covar_children.emplace_back("count", LogicalType::UBIGINT);
covar_children.emplace_back("meanx", LogicalType::DOUBLE);
covar_children.emplace_back("meany", LogicalType::DOUBLE);
covar_children.emplace_back("co_moment", LogicalType::DOUBLE);
auto cov_pop_type = LogicalType::STRUCT(std::move(covar_children));

child_list_t<LogicalType> stddev_types;
stddev_types.emplace_back("count", LogicalType::UBIGINT);
stddev_types.emplace_back("mean", LogicalType::DOUBLE);
stddev_types.emplace_back("dsquared", LogicalType::DOUBLE);
auto stddev_type = LogicalType::STRUCT(std::move(stddev_types));

child_list_t<LogicalType> state_children;
state_children.emplace_back("cov_pop", std::move(cov_pop_type));
state_children.emplace_back("dev_pop_x", stddev_type);
state_children.emplace_back("dev_pop_y", stddev_type);
return LogicalType::STRUCT(std::move(state_children));
}

LogicalType GetCorrExportStateType(const BoundAggregateFunction &) {
return GetCorrStateType();
}

AggregateFunction CorrFun::GetFunction() {
return AggregateFunction::BinaryAggregate<CorrState, double, double, double, CorrOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::DOUBLE)
.SetStructStateExport(GetCorrExportStateType);
LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::DOUBLE);
}
} // namespace duckdb
19 changes: 2 additions & 17 deletions src/duckdb/extension/core_functions/aggregate/algebraic/covar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,14 @@

namespace duckdb {

namespace {

LogicalType GetCovarStateType(const BoundAggregateFunction &) {
child_list_t<LogicalType> child_types;
child_types.emplace_back("count", LogicalType::UBIGINT);
child_types.emplace_back("meanx", LogicalType::DOUBLE);
child_types.emplace_back("meany", LogicalType::DOUBLE);
child_types.emplace_back("co_moment", LogicalType::DOUBLE);
return LogicalType::STRUCT(std::move(child_types));
}

} // namespace

AggregateFunction CovarPopFun::GetFunction() {
return AggregateFunction::BinaryAggregate<CovarState, double, double, double, CovarPopOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::DOUBLE)
.SetStructStateExport(GetCovarStateType);
LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::DOUBLE);
}

AggregateFunction CovarSampFun::GetFunction() {
return AggregateFunction::BinaryAggregate<CovarState, double, double, double, CovarSampOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::DOUBLE)
.SetStructStateExport(GetCovarStateType);
LogicalType::DOUBLE, LogicalType::DOUBLE, LogicalType::DOUBLE);
}

} // namespace duckdb
27 changes: 5 additions & 22 deletions src/duckdb/extension/core_functions/aggregate/algebraic/stddev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,29 @@

namespace duckdb {

namespace {

LogicalType GetStddevStateType(const BoundAggregateFunction &) {
child_list_t<LogicalType> child_types;
child_types.emplace_back("count", LogicalType::UBIGINT);
child_types.emplace_back("mean", LogicalType::DOUBLE);
child_types.emplace_back("dsquared", LogicalType::DOUBLE);
return LogicalType::STRUCT(std::move(child_types));
}

} // namespace

AggregateFunction StdDevSampFun::GetFunction() {
return AggregateFunction::UnaryAggregate<StddevState, double, double, STDDevSampOperation>(LogicalType::DOUBLE,
LogicalType::DOUBLE)
.SetStructStateExport(GetStddevStateType);
LogicalType::DOUBLE);
}

AggregateFunction StdDevPopFun::GetFunction() {
return AggregateFunction::UnaryAggregate<StddevState, double, double, STDDevPopOperation>(LogicalType::DOUBLE,
LogicalType::DOUBLE)
.SetStructStateExport(GetStddevStateType);
LogicalType::DOUBLE);
}

AggregateFunction VarPopFun::GetFunction() {
return AggregateFunction::UnaryAggregate<StddevState, double, double, VarPopOperation>(LogicalType::DOUBLE,
LogicalType::DOUBLE)
.SetStructStateExport(GetStddevStateType);
LogicalType::DOUBLE);
}

AggregateFunction VarSampFun::GetFunction() {
return AggregateFunction::UnaryAggregate<StddevState, double, double, VarSampOperation>(LogicalType::DOUBLE,
LogicalType::DOUBLE)
.SetStructStateExport(GetStddevStateType);
LogicalType::DOUBLE);
}

AggregateFunction StandardErrorOfTheMeanFun::GetFunction() {
return AggregateFunction::UnaryAggregate<StddevState, double, double, StandardErrorOfTheMeanOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE)
.SetStructStateExport(GetStddevStateType);
LogicalType::DOUBLE, LogicalType::DOUBLE);
}

} // namespace duckdb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ struct ApproxDistinctCountState {
};

struct ApproxCountDistinctFunction {
template <class STATE>
static void Initialize(STATE &state) {
new (&state) STATE();
}

template <class STATE, class OP>
static void Combine(const STATE &source, STATE &target, AggregateInputData &) {
target.hll.Merge(source.hll);
Expand All @@ -34,21 +29,6 @@ struct ApproxCountDistinctFunction {
}
};

void ApproxCountDistinctSimpleUpdateFunction(Vector inputs[], AggregateInputData &, idx_t input_count, data_ptr_t state,
idx_t count) {
D_ASSERT(input_count == 1);
auto &input = inputs[0];

if (count > STANDARD_VECTOR_SIZE) {
throw InternalException("ApproxCountDistinct - count must be at most vector size");
}
Vector hash_vec(LogicalType::HASH, count);
VectorOperations::Hash(input, hash_vec, count);

auto agg_state = reinterpret_cast<ApproxDistinctCountState *>(state);
agg_state->hll.Update(input, hash_vec, count);
}

void ApproxCountDistinctUpdateFunction(Vector inputs[], AggregateInputData &, idx_t input_count, Vector &state_vector,
idx_t count) {
D_ASSERT(input_count == 1);
Expand Down Expand Up @@ -80,8 +60,7 @@ AggregateFunction GetApproxCountDistinctFunction(const LogicalType &input_type)
AggregateFunction::StateInitialize<ApproxDistinctCountState, ApproxCountDistinctFunction>,
ApproxCountDistinctUpdateFunction,
AggregateFunction::StateCombine<ApproxDistinctCountState, ApproxCountDistinctFunction>,
AggregateFunction::StateFinalize<ApproxDistinctCountState, int64_t, ApproxCountDistinctFunction>,
ApproxCountDistinctSimpleUpdateFunction);
AggregateFunction::StateFinalize<ApproxDistinctCountState, int64_t, ApproxCountDistinctFunction>, nullptr);
fun.SetNullHandling(FunctionNullHandling::SPECIAL_HANDLING);
return fun;
}
Expand Down
Loading
Loading