Coverage Report

Created: 2026-11-03 16:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
18.1k
    static void add(QuantileState& res, const QuantileState& data, bool& is_first) {
49
18.1k
        if (UNLIKELY(is_first)) {
50
35
            res = data;
51
35
            is_first = false;
52
18.1k
        } else {
53
18.1k
            res.merge(data);
54
18.1k
        }
55
18.1k
    }
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
18.1k
    void add(const T& data) {
75
18.1k
        Op::add(value, data, is_first);
76
18.1k
    }
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
32
    void reset() {
87
32
        is_first = true;
88
32
        value.reset();
89
32
    }
90
91
    DataType& get() { return value; }
92
93
12.0k
    const DataType& get() const { return value; }
94
};
95
96
template <bool arg_is_nullable, typename Op>
97
class AggregateFunctionQuantileStateOp final
98
        : public IAggregateFunctionDataHelper<
99
                  AggregateFunctionQuantileStateData<Op>,
100
                  AggregateFunctionQuantileStateOp<arg_is_nullable, Op>> {
101
public:
102
    using ResultDataType = QuantileState;
103
    using ColVecType = ColumnQuantileState;
104
    using ColVecResult = ColumnQuantileState;
105
106
0
    String get_name() const override { return Op::name; }
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE8get_nameB5cxx11Ev
107
108
    AggregateFunctionQuantileStateOp(const DataTypes& argument_types_, bool is_window_function)
109
14
            : IAggregateFunctionDataHelper<AggregateFunctionQuantileStateData<Op>,
110
14
                                           AggregateFunctionQuantileStateOp<arg_is_nullable, Op>>(
111
14
                      argument_types_),
112
14
              _is_window_function(is_window_function) {}
_ZN5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EEb
Line
Count
Source
109
1
            : IAggregateFunctionDataHelper<AggregateFunctionQuantileStateData<Op>,
110
1
                                           AggregateFunctionQuantileStateOp<arg_is_nullable, Op>>(
111
1
                      argument_types_),
112
1
              _is_window_function(is_window_function) {}
_ZN5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EEb
Line
Count
Source
109
13
            : IAggregateFunctionDataHelper<AggregateFunctionQuantileStateData<Op>,
110
13
                                           AggregateFunctionQuantileStateOp<arg_is_nullable, Op>>(
111
13
                      argument_types_),
112
13
              _is_window_function(is_window_function) {}
113
114
0
    DataTypePtr get_return_type() const override {
115
0
        return std::make_shared<DataTypeQuantileState>();
116
0
    }
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE15get_return_typeEv
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE15get_return_typeEv
117
118
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
119
18.1k
             Arena&) const override {
120
18.1k
        if constexpr (arg_is_nullable) {
121
2
            auto& nullable_column =
122
2
                    assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]);
123
2
            if (!nullable_column.is_null_at(row_num)) {
124
1
                const auto& column = assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(
125
1
                        nullable_column.get_nested_column());
126
1
                this->data(place).add(column.get_data()[row_num]);
127
1
            }
128
18.1k
        } else {
129
18.1k
            const auto& column =
130
18.1k
                    assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]);
131
18.1k
            this->data(place).add(column.get_data()[row_num]);
132
18.1k
        }
133
18.1k
    }
_ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
119
2
             Arena&) const override {
120
2
        if constexpr (arg_is_nullable) {
121
2
            auto& nullable_column =
122
2
                    assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]);
123
2
            if (!nullable_column.is_null_at(row_num)) {
124
1
                const auto& column = assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(
125
1
                        nullable_column.get_nested_column());
126
1
                this->data(place).add(column.get_data()[row_num]);
127
1
            }
128
        } else {
129
            const auto& column =
130
                    assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]);
131
            this->data(place).add(column.get_data()[row_num]);
132
        }
133
2
    }
_ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
119
18.1k
             Arena&) const override {
120
        if constexpr (arg_is_nullable) {
121
            auto& nullable_column =
122
                    assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]);
123
            if (!nullable_column.is_null_at(row_num)) {
124
                const auto& column = assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(
125
                        nullable_column.get_nested_column());
126
                this->data(place).add(column.get_data()[row_num]);
127
            }
128
18.1k
        } else {
129
18.1k
            const auto& column =
130
18.1k
                    assert_cast<const ColVecType&, TypeCheckOnRelease::DISABLE>(*columns[0]);
131
18.1k
            this->data(place).add(column.get_data()[row_num]);
132
18.1k
        }
133
18.1k
    }
134
135
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
136
0
               Arena&) const override {
137
0
        this->data(place).merge(this->data(rhs).get());
138
0
    }
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE5mergeEPcPKcRNS_5ArenaE
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE5mergeEPcPKcRNS_5ArenaE
139
140
0
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
141
0
        this->data(place).write(buf);
142
0
    }
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE9serializeEPKcRNS_14BufferWritableE
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE9serializeEPKcRNS_14BufferWritableE
143
144
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
145
0
                     Arena&) const override {
146
0
        this->data(place).read(buf);
147
0
    }
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
148
149
12.0k
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
150
12.0k
        auto& column = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to);
151
12.0k
        const auto& value = this->data(place).get();
152
12.0k
        column.get_data().push_back(_is_window_function ? value.copy_for_result() : value);
153
12.0k
    }
_ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
149
2
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
150
2
        auto& column = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to);
151
2
        const auto& value = this->data(place).get();
152
2
        column.get_data().push_back(_is_window_function ? value.copy_for_result() : value);
153
2
    }
_ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
149
12.0k
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
150
12.0k
        auto& column = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to);
151
12.0k
        const auto& value = this->data(place).get();
152
12.0k
        column.get_data().push_back(_is_window_function ? value.copy_for_result() : value);
153
12.0k
    }
154
155
    void insert_result_into_range(ConstAggregateDataPtr __restrict place, IColumn& to,
156
4
                                  const size_t start, const size_t end) const override {
157
4
        if (start == end) {
158
2
            return;
159
2
        }
160
2
        insert_result_into(place, to);
161
2
        auto& data = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to).get_data();
162
        // Rows with the same window result share one compact digest.
163
2
        data.insert(data.end(), end - start - 1, data.back());
164
2
    }
Unexecuted instantiation: _ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE24insert_result_into_rangeEPKcRNS_7IColumnEmm
_ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE24insert_result_into_rangeEPKcRNS_7IColumnEmm
Line
Count
Source
156
4
                                  const size_t start, const size_t end) const override {
157
4
        if (start == end) {
158
2
            return;
159
2
        }
160
2
        insert_result_into(place, to);
161
2
        auto& data = assert_cast<ColVecResult&, TypeCheckOnRelease::DISABLE>(to).get_data();
162
        // Rows with the same window result share one compact digest.
163
2
        data.insert(data.end(), end - start - 1, data.back());
164
2
    }
165
166
32
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
_ZNK5doris32AggregateFunctionQuantileStateOpILb1ENS_37AggregateFunctionQuantileStateUnionOpEE5resetEPc
Line
Count
Source
166
1
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
_ZNK5doris32AggregateFunctionQuantileStateOpILb0ENS_37AggregateFunctionQuantileStateUnionOpEE5resetEPc
Line
Count
Source
166
31
    void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); }
167
168
private:
169
    const bool _is_window_function;
170
};
171
172
AggregateFunctionPtr create_aggregate_function_quantile_state_union(
173
        const std::string& name, const DataTypes& argument_types, const DataTypePtr& result_type,
174
        const bool result_is_nullable, const AggregateFunctionAttr& attr);
175
176
} // namespace doris