be/src/exprs/aggregate/aggregate_function_quantile_state.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <stddef.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <string> |
24 | | |
25 | | #include "common/compiler_util.h" // IWYU pragma: keep |
26 | | #include "core/assert_cast.h" |
27 | | #include "core/column/column_complex.h" |
28 | | #include "core/column/column_nullable.h" |
29 | | #include "core/data_type/data_type_quantilestate.h" |
30 | | #include "core/types.h" |
31 | | #include "core/value/quantile_state.h" |
32 | | #include "exprs/aggregate/aggregate_function.h" |
33 | | |
34 | | namespace doris { |
35 | | class Arena; |
36 | | class BufferReadable; |
37 | | class BufferWritable; |
38 | | class IColumn; |
39 | | } // namespace doris |
40 | | |
41 | | namespace doris { |
42 | | |
43 | | struct AggregateFunctionQuantileStateUnionOp { |
44 | | static constexpr auto name = "quantile_union"; |
45 | | |
46 | 0 | static void add(QuantileState& res, const double& data, bool& is_first) { res.add_value(data); } |
47 | | |
48 | 12.0k | static void add(QuantileState& res, const QuantileState& data, bool& is_first) { |
49 | 12.0k | if (UNLIKELY(is_first)) { |
50 | 3 | res = data; |
51 | 3 | is_first = false; |
52 | 12.0k | } else { |
53 | 12.0k | res.merge(data); |
54 | 12.0k | } |
55 | 12.0k | } |
56 | | |
57 | 0 | static void merge(QuantileState& res, const QuantileState& data, bool& is_first) { |
58 | 0 | if (UNLIKELY(is_first)) { |
59 | 0 | res = data; |
60 | 0 | is_first = false; |
61 | 0 | } else { |
62 | 0 | res.merge(data); |
63 | 0 | } |
64 | 0 | } |
65 | | }; |
66 | | |
67 | | template <typename Op> |
68 | | struct AggregateFunctionQuantileStateData { |
69 | | using DataType = QuantileState; |
70 | | DataType value; |
71 | | bool is_first = true; |
72 | | |
73 | | template <typename T> |
74 | 12.0k | void add(const T& data) { |
75 | 12.0k | Op::add(value, data, is_first); |
76 | 12.0k | } |
77 | | |
78 | 0 | void merge(const DataType& data) { Op::merge(value, data, is_first); } |
79 | | |
80 | 0 | void write(BufferWritable& buf) const { |
81 | 0 | DataTypeQuantileState::serialize_as_stream(value, buf); |
82 | 0 | } |
83 | | |
84 | 0 | void read(BufferReadable& buf) { DataTypeQuantileState::deserialize_as_stream(value, buf); } |
85 | | |
86 | 0 | void reset() { is_first = true; } |
87 | | |
88 | | DataType& get() { return value; } |
89 | | |
90 | 12.0k | const DataType& get() const { return value; } |
91 | | }; |
92 | | |
93 | | template <bool arg_is_nullable, typename Op> |
94 | | class AggregateFunctionQuantileStateOp final |
95 | | : public IAggregateFunctionDataHelper< |
96 | | AggregateFunctionQuantileStateData<Op>, |
97 | | AggregateFunctionQuantileStateOp<arg_is_nullable, Op>> { |
98 | | public: |
99 | | using ResultDataType = QuantileState; |
100 | | using ColVecType = ColumnQuantileState; |
101 | | using ColVecResult = ColumnQuantileState; |
102 | | |
103 | 0 | String get_name() const override { return Op::name; }Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE8get_nameB5cxx11Ev Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE8get_nameB5cxx11Ev |
104 | | |
105 | | AggregateFunctionQuantileStateOp(const DataTypes& argument_types_, bool is_window_function) |
106 | 3 | : IAggregateFunctionDataHelper<AggregateFunctionQuantileStateData<Op>, |
107 | 3 | AggregateFunctionQuantileStateOp<arg_is_nullable, Op>>( |
108 | 3 | argument_types_), |
109 | 3 | _is_window_function(is_window_function) {}Unexecuted instantiation: _ZN5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EEb _ZN5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EEb Line | Count | Source | 106 | 3 | : IAggregateFunctionDataHelper<AggregateFunctionQuantileStateData<Op>, | 107 | 3 | AggregateFunctionQuantileStateOp<arg_is_nullable, Op>>( | 108 | 3 | argument_types_), | 109 | 3 | _is_window_function(is_window_function) {} |
|
110 | | |
111 | 0 | DataTypePtr get_return_type() const override { |
112 | 0 | return std::make_shared<DataTypeQuantileState>(); |
113 | 0 | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE15get_return_typeEv Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE15get_return_typeEv |
114 | | |
115 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
116 | 12.0k | Arena&) const override { |
117 | 12.0k | if constexpr (arg_is_nullable) { |
118 | 0 | auto& nullable_column = |
119 | 0 | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
120 | 0 | if (!nullable_column.is_null_at(row_num)) { |
121 | 0 | const auto& column = assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>( |
122 | 0 | nullable_column.get_nested_column()); |
123 | 0 | this->data(place).add(column.get_data()[row_num]); |
124 | 0 | } |
125 | 12.0k | } else { |
126 | 12.0k | const auto& column = |
127 | 12.0k | assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]); |
128 | 12.0k | this->data(place).add(column.get_data()[row_num]); |
129 | 12.0k | } |
130 | 12.0k | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE3addEPcPPKNS_7IColumnElRNS_5ArenaE _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE3addEPcPPKNS_7IColumnElRNS_5ArenaE Line | Count | Source | 116 | 12.0k | Arena&) const override { | 117 | | if constexpr (arg_is_nullable) { | 118 | | auto& nullable_column = | 119 | | assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); | 120 | | if (!nullable_column.is_null_at(row_num)) { | 121 | | const auto& column = assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>( | 122 | | nullable_column.get_nested_column()); | 123 | | this->data(place).add(column.get_data()[row_num]); | 124 | | } | 125 | 12.0k | } else { | 126 | 12.0k | const auto& column = | 127 | 12.0k | assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]); | 128 | 12.0k | this->data(place).add(column.get_data()[row_num]); | 129 | 12.0k | } | 130 | 12.0k | } |
|
131 | | |
132 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
133 | 0 | Arena&) const override { |
134 | 0 | this->data(place).merge(this->data(rhs).get()); |
135 | 0 | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE5mergeEPcPKcRNS_5ArenaE Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE5mergeEPcPKcRNS_5ArenaE |
136 | | |
137 | 0 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
138 | 0 | this->data(place).write(buf); |
139 | 0 | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE9serializeEPKcRNS_14BufferWritableE Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE9serializeEPKcRNS_14BufferWritableE |
140 | | |
141 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
142 | 0 | Arena&) const override { |
143 | 0 | this->data(place).read(buf); |
144 | 0 | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE |
145 | | |
146 | 12.0k | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
147 | 12.0k | auto& column = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to); |
148 | 12.0k | const auto& value = this->data(place).get(); |
149 | 12.0k | column.get_data().push_back(_is_window_function ? value.copy_for_result() : value); |
150 | 12.0k | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE18insert_result_intoEPKcRNS_7IColumnE _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE18insert_result_intoEPKcRNS_7IColumnE Line | Count | Source | 146 | 12.0k | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { | 147 | 12.0k | auto& column = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to); | 148 | 12.0k | const auto& value = this->data(place).get(); | 149 | 12.0k | column.get_data().push_back(_is_window_function ? value.copy_for_result() : value); | 150 | 12.0k | } |
|
151 | | |
152 | | void insert_result_into_range(ConstAggregateDataPtr __restrict place, IColumn& to, |
153 | 4 | const size_t start, const size_t end) const override { |
154 | 4 | if (start == end) { |
155 | 2 | return; |
156 | 2 | } |
157 | 2 | insert_result_into(place, to); |
158 | 2 | auto& data = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to).get_data(); |
159 | | // Rows with the same window result share one compact digest. |
160 | 2 | data.insert(data.end(), end - start - 1, data.back()); |
161 | 2 | } Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE24insert_result_into_rangeEPKcRNS_7IColumnEmm _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE24insert_result_into_rangeEPKcRNS_7IColumnEmm Line | Count | Source | 153 | 4 | const size_t start, const size_t end) const override { | 154 | 4 | if (start == end) { | 155 | 2 | return; | 156 | 2 | } | 157 | 2 | insert_result_into(place, to); | 158 | 2 | auto& data = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to).get_data(); | 159 | | // Rows with the same window result share one compact digest. | 160 | 2 | data.insert(data.end(), end - start - 1, data.back()); | 161 | 2 | } |
|
162 | | |
163 | 0 | void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE5resetEPc Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE5resetEPc |
164 | | |
165 | | private: |
166 | | const bool _is_window_function; |
167 | | }; |
168 | | |
169 | | AggregateFunctionPtr create_aggregate_function_quantile_state_union( |
170 | | const std::string& name, const DataTypes& argument_types, const DataTypePtr& result_type, |
171 | | const bool result_is_nullable, const AggregateFunctionAttr& attr); |
172 | | |
173 | | } // namespace doris |