1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/function/aggregate_state.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/function/function.hpp"
12#include "duckdb/storage/statistics/base_statistics.hpp"
13#include "duckdb/storage/statistics/node_statistics.hpp"
14
15namespace duckdb {
16
17enum class AggregateType : uint8_t { NON_DISTINCT = 1, DISTINCT = 2 };
18//! Whether or not the input order influences the result of the aggregate
19enum class AggregateOrderDependent : uint8_t { ORDER_DEPENDENT = 1, NOT_ORDER_DEPENDENT = 2 };
20
21class BoundAggregateExpression;
22
23struct AggregateInputData {
24 AggregateInputData(optional_ptr<FunctionData> bind_data_p, Allocator &allocator_p)
25 : bind_data(bind_data_p), allocator(allocator_p) {
26 }
27 optional_ptr<FunctionData> bind_data;
28 Allocator &allocator;
29};
30
31struct AggregateUnaryInput {
32 AggregateUnaryInput(AggregateInputData &input_p, ValidityMask &input_mask_p)
33 : input(input_p), input_mask(input_mask_p), input_idx(0) {
34 }
35
36 AggregateInputData &input;
37 ValidityMask &input_mask;
38 idx_t input_idx;
39
40 inline bool RowIsValid() {
41 return input_mask.RowIsValid(row_idx: input_idx);
42 }
43};
44
45struct AggregateBinaryInput {
46 AggregateBinaryInput(AggregateInputData &input_p, ValidityMask &left_mask_p, ValidityMask &right_mask_p)
47 : input(input_p), left_mask(left_mask_p), right_mask(right_mask_p) {
48 }
49
50 AggregateInputData &input;
51 ValidityMask &left_mask;
52 ValidityMask &right_mask;
53 idx_t lidx;
54 idx_t ridx;
55};
56
57struct AggregateFinalizeData {
58 AggregateFinalizeData(Vector &result_p, AggregateInputData &input_p)
59 : result(result_p), input(input_p), result_idx(0) {
60 }
61
62 Vector &result;
63 AggregateInputData &input;
64 idx_t result_idx;
65
66 inline void ReturnNull() {
67 switch (result.GetVectorType()) {
68 case VectorType::FLAT_VECTOR:
69 FlatVector::SetNull(vector&: result, idx: result_idx, is_null: true);
70 break;
71 case VectorType::CONSTANT_VECTOR:
72 ConstantVector::SetNull(vector&: result, is_null: true);
73 break;
74 default:
75 throw InternalException("Invalid result vector type for aggregate");
76 }
77 }
78
79 inline string_t ReturnString(string_t value) {
80 return StringVector::AddStringOrBlob(vector&: result, data: value);
81 }
82};
83
84struct AggregateStatisticsInput {
85 AggregateStatisticsInput(optional_ptr<FunctionData> bind_data_p, vector<BaseStatistics> &child_stats_p,
86 optional_ptr<NodeStatistics> node_stats_p)
87 : bind_data(bind_data_p), child_stats(child_stats_p), node_stats(node_stats_p) {
88 }
89
90 optional_ptr<FunctionData> bind_data;
91 vector<BaseStatistics> &child_stats;
92 optional_ptr<NodeStatistics> node_stats;
93};
94
95} // namespace duckdb
96