Coverage Report

Created: 2026-03-13 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_distinct.cpp
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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/AggregateFunctions/AggregateFunctionDistinct.cpp
19
// and modified by Doris
20
21
#include "exprs/aggregate/aggregate_function_distinct.h"
22
23
#include <algorithm>
24
25
#include "core/data_type/data_type_nullable.h"
26
#include "exprs/aggregate/aggregate_function_combinator.h"
27
#include "exprs/aggregate/aggregate_function_simple_factory.h"
28
#include "exprs/aggregate/helpers.h"
29
30
namespace doris {
31
#include "common/compile_check_begin.h"
32
33
template <PrimitiveType T>
34
struct Reducer {
35
    template <bool stable>
36
    using Output = AggregateFunctionDistinctSingleNumericData<T, stable>;
37
    using AggregateFunctionDistinctNormal = AggregateFunctionDistinct<Output, false>;
38
};
39
40
template <PrimitiveType T>
41
using AggregateFunctionDistinctNumeric = typename Reducer<T>::AggregateFunctionDistinctNormal;
42
43
class AggregateFunctionCombinatorDistinct final : public IAggregateFunctionCombinator {
44
public:
45
0
    String get_name() const override { return "Distinct"; }
46
47
1.67k
    DataTypes transform_arguments(const DataTypes& arguments) const override {
48
1.67k
        if (arguments.empty()) {
49
0
            throw doris::Exception(
50
0
                    ErrorCode::INTERNAL_ERROR,
51
0
                    "Incorrect number of arguments for aggregate function with Distinct suffix");
52
0
        }
53
1.67k
        return arguments;
54
1.67k
    }
55
56
    AggregateFunctionPtr transform_aggregate_function(
57
            const AggregateFunctionPtr& nested_function, const DataTypes& arguments,
58
1.67k
            const bool result_is_nullable, const AggregateFunctionAttr& attr) const override {
59
1.67k
        DCHECK(nested_function != nullptr);
60
1.67k
        if (nested_function == nullptr) {
61
0
            return nullptr;
62
0
        }
63
64
1.67k
        if (arguments.size() == 1) {
65
1.64k
            AggregateFunctionPtr res(
66
1.64k
                    creator_with_type_list<TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT,
67
1.64k
                                           TYPE_LARGEINT>::
68
1.64k
                            create<AggregateFunctionDistinctNumeric>(arguments, result_is_nullable,
69
1.64k
                                                                     attr, nested_function));
70
1.64k
            if (res) {
71
931
                return res;
72
931
            }
73
74
713
            res = creator_without_type::create<
75
713
                    AggregateFunctionDistinct<AggregateFunctionDistinctSingleGenericData>>(
76
713
                    arguments, result_is_nullable, attr, nested_function);
77
713
            return res;
78
1.64k
        }
79
27
        return creator_without_type::create<
80
27
                AggregateFunctionDistinct<AggregateFunctionDistinctMultipleGenericData>>(
81
27
                arguments, result_is_nullable, attr, nested_function);
82
1.67k
    }
83
};
84
85
8
void register_aggregate_function_combinator_distinct(AggregateFunctionSimpleFactory& factory) {
86
8
    AggregateFunctionCreator creator = [&](const std::string& name, const DataTypes& types,
87
8
                                           const DataTypePtr& result_type,
88
8
                                           const bool result_is_nullable,
89
1.67k
                                           const AggregateFunctionAttr& attr) {
90
        // 1. we should get not nullable types;
91
1.67k
        DataTypes nested_types(types.size());
92
1.67k
        std::ranges::transform(types, nested_types.begin(),
93
1.69k
                               [](const auto& e) { return remove_nullable(e); });
94
1.67k
        auto function_combinator = std::make_shared<AggregateFunctionCombinatorDistinct>();
95
1.67k
        auto transform_arguments = function_combinator->transform_arguments(nested_types);
96
1.67k
        auto nested_function_name = name.substr(DISTINCT_FUNCTION_PREFIX.size());
97
1.67k
        auto nested_function = factory.get(nested_function_name, transform_arguments, result_type,
98
1.67k
                                           false, BeExecVersionManager::get_newest_version(), attr);
99
1.67k
        return function_combinator->transform_aggregate_function(nested_function, types,
100
1.67k
                                                                 result_is_nullable, attr);
101
1.67k
    };
102
8
    factory.register_distinct_function_combinator(creator, DISTINCT_FUNCTION_PREFIX);
103
8
    factory.register_distinct_function_combinator(creator, DISTINCT_FUNCTION_PREFIX, true);
104
8
}
105
} // namespace doris