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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 221 additions & 10 deletions src/duckdb/extension/core_functions/aggregate/algebraic/avg.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include "core_functions/aggregate/algebraic_functions.hpp"
#include "core_functions/aggregate/sum_helpers.hpp"
#include "duckdb/common/bignum.hpp"
#include "duckdb/common/types/decimal_helper.hpp"
#include "duckdb/common/types/string_heap.hpp"
#include "duckdb/common/types/decimal.hpp"
#include "duckdb/common/types/hugeint.hpp"
#include "duckdb/common/types/time.hpp"
#include "duckdb/common/vector/struct_vector.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/common/likely.hpp"
#include "duckdb/function/function_set.hpp"
#include "duckdb/planner/expression.hpp"
#include "duckdb/planner/expression/bound_aggregate_expression.hpp"

#include <cmath>

namespace duckdb {

namespace {
Expand Down Expand Up @@ -55,19 +62,21 @@ struct KahanAvgState {
};

struct AverageDecimalBindData : public FunctionData {
explicit AverageDecimalBindData(double scale) : scale(scale) {
AverageDecimalBindData(double scale, uint32_t decimal_scale) : scale(scale), decimal_scale(decimal_scale) {
}

//! 10^decimal_scale, the divisor of the unscaled sum
double scale;
uint32_t decimal_scale;

public:
unique_ptr<FunctionData> Copy() const override {
return make_uniq<AverageDecimalBindData>(scale);
return make_uniq<AverageDecimalBindData>(scale, decimal_scale);
};

bool Equals(const FunctionData &other_p) const override {
auto &other = other_p.Cast<AverageDecimalBindData>();
return scale == other.scale;
return scale == other.scale && decimal_scale == other.decimal_scale;
}
};

Expand Down Expand Up @@ -496,6 +505,193 @@ struct TimeTZAverageOperation : public BaseSumOperation<AverageSetOperation, Add
}
};

//! The average of the bignum_t tier of decimals: an exact BIGNUM sum, divided once at the end like the hugeint tier
struct BignumAvgState {
uint64_t count;
BignumIntermediate value;
};

//! The sum as a long double: through a hugeint when it fits, so that the same values in a fixed-size and a wide
//! decimal average to the same result
static long double BignumSumToLongDouble(const BignumIntermediate &value) {
const auto start = value.GetStartDataPos();
const auto size = value.size - start;
if (size <= sizeof(hugeint_t)) {
uhugeint_t magnitude = 0;
for (idx_t i = start; i < value.size; i++) {
magnitude = (magnitude << 8) | uhugeint_t(value.GetAbsoluteByte(NumericCast<int64_t>(i)));
}
if (magnitude <= uhugeint_t(NumericLimits<hugeint_t>::Maximum())) {
auto sum = hugeint_t(magnitude.upper, magnitude.lower);
return Hugeint::Cast<long double>(value.is_negative ? -sum : sum);
}
}
long double result = 0;
for (idx_t i = start; i < value.size; i++) {
result = result * 256 + value.GetAbsoluteByte(NumericCast<int64_t>(i));
}
return value.is_negative ? -result : result;
}

//! The digits a quotient carries beyond the 17 significant digits of a DOUBLE, so that the quotient and a sticky
//! digit for a nonzero remainder round to the DOUBLE nearest to the exact average
static constexpr idx_t AVERAGE_GUARD_DIGITS = 40;

//! The average as the DOUBLE nearest to the exact quotient: the sum is scaled up by enough digits for the quotient to
//! carry the guard digits beyond those of the count, a nonzero remainder (which lies strictly between two quotients)
//! is kept as a trailing nonzero digit, and the decimal text is parsed once, so the result is rounded once
static double BignumExactAverage(const BignumIntermediate &sum, uint64_t count, uint32_t scale) {
StringHeap heap;
BignumArithmeticState state;
const idx_t extra_digits = AVERAGE_GUARD_DIGITS + std::to_string(count).size();
const string power_blob = DecimalHelper<bignum_t>::PowerOfTen(extra_digits);
const bignum_t power_value {string_t(power_blob)};
const BignumIntermediate power(power_value);
const bignum_t scaled_value {BignumIntermediate::Multiply(heap, state, sum, power)};
const BignumIntermediate scaled_sum(scaled_value);
const string count_blob = DecimalHelper<bignum_t>::FromInteger(uhugeint_t(count));
const bignum_t count_value {string_t(count_blob)};
const BignumIntermediate divisor(count_value);
string_t quotient;
string_t remainder;
if (!BignumIntermediate::TryDivMod(heap, state, scaled_sum, divisor, quotient, remainder)) {
throw InternalException("avg: division of the wide decimal sum by the count failed");
}
auto text = Bignum::BignumToVarchar(bignum_t(quotient));
text += DecimalHelper<bignum_t>::IsZero(bignum_t(remainder)) ? "0" : "1";
text += "e-" + std::to_string(idx_t(scale) + extra_digits + 1);
double result;
if (!TryCast::Operation(string_t(text), result, false)) {
throw InternalException("avg: could not convert the wide decimal average '%s' to a DOUBLE", text);
}
return result;
}

struct BignumAverageOperation {
template <class STATE>
static void Initialize(STATE &state) {
state.count = 0;
state.value = BignumIntermediate();
}

template <class INPUT_TYPE, class STATE, class OP>
static void Operation(STATE &state, const INPUT_TYPE &input, AggregateUnaryInput &unary_input) {
if (state.count == 0) {
state.value.Initialize(unary_input.input.allocator);
}
state.count++;
BignumIntermediate rhs(input);
state.value.AddInPlace(unary_input.input.allocator, rhs);
}

template <class INPUT_TYPE, class STATE, class OP>
static void ConstantOperation(STATE &state, const INPUT_TYPE &input, AggregateUnaryInput &unary_input,
idx_t count) {
for (idx_t i = 0; i < count; i++) {
Operation<INPUT_TYPE, STATE, OP>(state, input, unary_input);
}
}

template <class STATE, class OP>
static void Combine(const STATE &source, STATE &target, AggregateInputData &input) {
if (source.count == 0) {
return;
}
if (target.count == 0) {
target.value.Initialize(input.allocator);
}
target.count += source.count;
target.value.AddInPlace(input.allocator, source.value);
}

template <class T, class STATE>
static void Finalize(STATE &state, T &target, AggregateFinalizeData &finalize_data) {
if (state.count == 0) {
finalize_data.ReturnNull();
return;
}
long double divident = GetAverageDivident<long double>(state.count, finalize_data.input.bind_data);
const auto sum = BignumSumToLongDouble(state.value);
if (std::isfinite(sum) && std::isfinite(divident)) {
target = sum / divident;
return;
}
// beyond the range of a long double (a sum or a scale factor near the DOUBLE range) the exact sum is divided
// by the count instead, so that the average stays finite and a subnormal average keeps its value
auto &bind_data = finalize_data.input.bind_data->Cast<AverageDecimalBindData>();
target = BignumExactAverage(state.value, state.count, bind_data.decimal_scale);
}

static bool IgnoreNull() {
return true;
}
};

//! The states are exported as STRUCT(count UBIGINT, value BIGNUM), the value is NULL for an empty state
static LogicalType BignumAvgStateType() {
return LogicalType::STRUCT({{"count", LogicalType::UBIGINT}, {"value", LogicalType::BIGNUM}});
}

AggregateStateLayout BignumAvgStateLayout(AggregateLayoutInput &input) {
return AggregateStateLayout(BignumAvgStateType(), AlignValue<idx_t>(sizeof(BignumAvgState)));
}

void BignumAvgExportState(Vector &state_vector, AggregateFinalizeInputData &, Vector &result, idx_t count,
idx_t offset) {
auto states = state_vector.Values<BignumAvgState *>();
auto &entries = StructVector::GetEntries(result);
auto count_data = FlatVector::GetDataMutable<uint64_t>(entries[0]);
auto value_data = FlatVector::GetDataMutable<string_t>(entries[1]);
auto &value_validity = FlatVector::ValidityMutable(entries[1]);
for (idx_t i = 0; i < count; i++) {
auto &state = *states[i].GetValue();
count_data[offset + i] = state.count;
if (state.count == 0) {
value_validity.SetInvalid(offset + i);
continue;
}
value_data[offset + i] = BignumAggregates::WriteBignum(state.value, entries[1]);
}
}

void BignumAvgImportState(AggregateImportInputData &input) {
auto &input_vec = input.input_vec;
auto count = input_vec.size();
UnifiedVectorFormat struct_format;
input_vec.ToUnifiedFormat(struct_format);
auto &entries = StructVector::GetEntries(input_vec);
UnifiedVectorFormat count_format;
UnifiedVectorFormat value_format;
entries[0].ToUnifiedFormat(count_format);
entries[1].ToUnifiedFormat(value_format);
auto count_data = count_format.GetData<uint64_t>(count_format);
auto value_data = value_format.GetData<string_t>(value_format);
for (idx_t i = 0; i < count; i++) {
auto &state = *reinterpret_cast<BignumAvgState *>(input.dest_buffer + i * input.layout.total_state_size);
BignumAverageOperation::Initialize(state);
const auto struct_idx = struct_format.sel->get_index(i);
const auto count_idx = count_format.sel->get_index(i);
const auto value_idx = value_format.sel->get_index(i);
if (!struct_format.validity.RowIsValid(struct_idx) || !count_format.validity.RowIsValid(count_idx) ||
count_data[count_idx] == 0) {
// an empty state, e.g. to_aggregate_state(NULL, ...)
continue;
}
if (!value_format.validity.RowIsValid(value_idx)) {
throw InvalidInputException("Invalid avg state - the value cannot be NULL when the count is not 0");
}
state.count = count_data[count_idx];
state.value = BignumAggregates::ImportBignum(value_data[value_idx], input.allocator);
}
}

AggregateFunction GetBignumAverageAggregate() {
auto function = AggregateFunction::UnaryAggregate<BignumAvgState, bignum_t, double, BignumAverageOperation>(
LogicalTypeId::DECIMAL, LogicalType::DOUBLE);
function.SetStateExportCallbacks(BignumAvgStateLayout, BignumAvgExportState, BignumAvgImportState);
return function;
}

static AggregateFunction NameXParameter(AggregateFunction fun) {
fun.GetSignature().GetParameter(0).SetName("x");
return fun;
Expand Down Expand Up @@ -541,21 +737,39 @@ AggregateFunction GetAverageAggregate(PhysicalType type) {
function.SetFallible(); // interval addition throws on overflow of any field
return NameXParameter(std::move(function));
}
case PhysicalType::DOUBLE: {
return NameXParameter(
AggregateFunction::UnaryAggregate<AvgState<double>, double, double, NumericAverageOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE));
}
default:
throw InternalException("Unimplemented average aggregate");
}
}

//! 10^scale as a double, through the hugeint table where it reaches so that both decimal tiers divide identically
static double DecimalScaleFactor(uint32_t scale) {
if (scale < Hugeint::CACHED_POWERS_OF_TEN) {
return Hugeint::Cast<double>(Hugeint::POWERS_OF_TEN[scale]);
}
return std::pow(10.0, static_cast<double>(scale));
}

unique_ptr<FunctionData> BindDecimalAvg(BindAggregateFunctionInput &input) {
auto &function = input.GetBoundFunction();
auto &arguments = input.GetArguments();
auto decimal_type = BindDecimalArgument(input.GetClientContext(), arguments[0], "avg");
function.ReplaceImplementation(GetAverageAggregate(decimal_type.InternalType()));
if (decimal_type.InternalType() == PhysicalType::VARCHAR) {
// the bignum_t tier of decimals sums its BIGNUM blobs
function.ReplaceImplementation(GetBignumAverageAggregate());
} else {
function.ReplaceImplementation(GetAverageAggregate(decimal_type.InternalType()));
}
function.SetName("avg");
function.GetArguments()[0] = decimal_type;
function.SetReturnType(LogicalType::DOUBLE);
return make_uniq<AverageDecimalBindData>(
Hugeint::Cast<double>(Hugeint::POWERS_OF_TEN[DecimalType::GetScale(decimal_type)]));
const auto scale = DecimalType::GetScale(decimal_type);
return make_uniq<AverageDecimalBindData>(DecimalScaleFactor(scale), scale);
}

AggregateFunction GetTimestampAverage() {
Expand Down Expand Up @@ -602,10 +816,7 @@ AggregateFunctionSet AvgFun::GetFunctions() {
avg.AddFunction(GetAverageAggregate(PhysicalType::INT64));
avg.AddFunction(GetAverageAggregate(PhysicalType::INT128));
avg.AddFunction(GetAverageAggregate(PhysicalType::INTERVAL));
auto numeric_avg = AggregateFunction::UnaryAggregate<AvgState<double>, double, double, NumericAverageOperation>(
LogicalType::DOUBLE, LogicalType::DOUBLE);
numeric_avg.GetSignature().GetParameter(0).SetName("x");
avg.AddFunction(numeric_avg);
avg.AddFunction(GetAverageAggregate(PhysicalType::DOUBLE));

avg.AddFunction(GetTimestampAverage());
for (auto &type : {LogicalType::TIMESTAMP_S, LogicalType::TIMESTAMP_MS, LogicalType::DATE}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ AggregateFunction GetDecimalArgMinMaxFunction(const LogicalType &by_type, const
return GetArgMinMaxFunctionBy<OP, int32_t>(by_type, type, null_handling);
case PhysicalType::INT64:
return GetArgMinMaxFunctionBy<OP, int64_t>(by_type, type, null_handling);
case PhysicalType::VARCHAR:
// wide decimals are BIGNUM blobs
return GetArgMinMaxFunctionBy<OP, string_t>(by_type, type, null_handling);
default:
return GetArgMinMaxFunctionBy<OP, hugeint_t>(by_type, type, null_handling);
}
Expand Down
Loading
Loading