be/src/exprs/aggregate/aggregate_function_window.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/Processors/Transforms/WindowTransform.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #include "exprs/aggregate/aggregate_function_window.h" |
22 | | |
23 | | #include <string> |
24 | | |
25 | | #include "exprs/aggregate/aggregate_function_simple_factory.h" |
26 | | #include "exprs/aggregate/helpers.h" |
27 | | |
28 | | namespace doris { |
29 | | |
30 | | // Defined in separate translation units to reduce per-file template instantiation cost: |
31 | | // aggregate_function_window_lag.cpp |
32 | | // aggregate_function_window_lead.cpp |
33 | | // aggregate_function_window_first.cpp |
34 | | // aggregate_function_window_last.cpp |
35 | | // aggregate_function_window_nth_value.cpp |
36 | | AggregateFunctionPtr create_aggregate_function_window_lag(const std::string& name, |
37 | | const DataTypes& argument_types, |
38 | | const DataTypePtr& result_type, |
39 | | const bool result_is_nullable, |
40 | | const AggregateFunctionAttr& attr); |
41 | | AggregateFunctionPtr create_aggregate_function_window_lead(const std::string& name, |
42 | | const DataTypes& argument_types, |
43 | | const DataTypePtr& result_type, |
44 | | const bool result_is_nullable, |
45 | | const AggregateFunctionAttr& attr); |
46 | | AggregateFunctionPtr create_aggregate_function_window_first(const std::string& name, |
47 | | const DataTypes& argument_types, |
48 | | const DataTypePtr& result_type, |
49 | | const bool result_is_nullable, |
50 | | const AggregateFunctionAttr& attr); |
51 | | AggregateFunctionPtr create_aggregate_function_window_first_ignore_null( |
52 | | const std::string& name, const DataTypes& argument_types, const DataTypePtr& result_type, |
53 | | const bool result_is_nullable, const AggregateFunctionAttr& attr); |
54 | | AggregateFunctionPtr create_aggregate_function_window_last(const std::string& name, |
55 | | const DataTypes& argument_types, |
56 | | const DataTypePtr& result_type, |
57 | | const bool result_is_nullable, |
58 | | const AggregateFunctionAttr& attr); |
59 | | AggregateFunctionPtr create_aggregate_function_window_last_ignore_null( |
60 | | const std::string& name, const DataTypes& argument_types, const DataTypePtr& result_type, |
61 | | const bool result_is_nullable, const AggregateFunctionAttr& attr); |
62 | | AggregateFunctionPtr create_aggregate_function_window_nth_value(const std::string& name, |
63 | | const DataTypes& argument_types, |
64 | | const DataTypePtr& result_type, |
65 | | const bool result_is_nullable, |
66 | | const AggregateFunctionAttr& attr); |
67 | | |
68 | | template <typename AggregateFunctionTemplate> |
69 | | AggregateFunctionPtr create_empty_arg_window(const std::string& name, |
70 | | const DataTypes& argument_types, |
71 | | const DataTypePtr& result_type, |
72 | | const bool result_is_nullable, |
73 | 339 | const AggregateFunctionAttr& attr) { |
74 | 339 | if (!argument_types.empty()) { |
75 | 0 | throw doris::Exception( |
76 | 0 | Status::InternalError("create_window: argument_types must be empty")); |
77 | 0 | } |
78 | 339 | std::unique_ptr<IAggregateFunction> result = |
79 | 339 | std::make_unique<AggregateFunctionTemplate>(argument_types); |
80 | 339 | CHECK_AGG_FUNCTION_SERIALIZED_TYPE(AggregateFunctionTemplate); |
81 | 339 | return AggregateFunctionPtr(result.release()); |
82 | 339 | } _ZN5doris23create_empty_arg_windowINS_23WindowFunctionDenseRankEEESt10shared_ptrINS_18IAggregateFunctionEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIS2_IKNS_9IDataTypeEESaISG_EERKSG_bRKNS_21AggregateFunctionAttrE Line | Count | Source | 73 | 15 | const AggregateFunctionAttr& attr) { | 74 | 15 | if (!argument_types.empty()) { | 75 | 0 | throw doris::Exception( | 76 | 0 | Status::InternalError("create_window: argument_types must be empty")); | 77 | 0 | } | 78 | 15 | std::unique_ptr<IAggregateFunction> result = | 79 | 15 | std::make_unique<AggregateFunctionTemplate>(argument_types); | 80 | 15 | CHECK_AGG_FUNCTION_SERIALIZED_TYPE(AggregateFunctionTemplate); | 81 | 15 | return AggregateFunctionPtr(result.release()); | 82 | 15 | } |
_ZN5doris23create_empty_arg_windowINS_18WindowFunctionRankEEESt10shared_ptrINS_18IAggregateFunctionEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIS2_IKNS_9IDataTypeEESaISG_EERKSG_bRKNS_21AggregateFunctionAttrE Line | Count | Source | 73 | 179 | const AggregateFunctionAttr& attr) { | 74 | 179 | if (!argument_types.empty()) { | 75 | 0 | throw doris::Exception( | 76 | 0 | Status::InternalError("create_window: argument_types must be empty")); | 77 | 0 | } | 78 | 179 | std::unique_ptr<IAggregateFunction> result = | 79 | 179 | std::make_unique<AggregateFunctionTemplate>(argument_types); | 80 | 179 | CHECK_AGG_FUNCTION_SERIALIZED_TYPE(AggregateFunctionTemplate); | 81 | 179 | return AggregateFunctionPtr(result.release()); | 82 | 179 | } |
_ZN5doris23create_empty_arg_windowINS_25WindowFunctionPercentRankEEESt10shared_ptrINS_18IAggregateFunctionEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIS2_IKNS_9IDataTypeEESaISG_EERKSG_bRKNS_21AggregateFunctionAttrE Line | Count | Source | 73 | 6 | const AggregateFunctionAttr& attr) { | 74 | 6 | if (!argument_types.empty()) { | 75 | 0 | throw doris::Exception( | 76 | 0 | Status::InternalError("create_window: argument_types must be empty")); | 77 | 0 | } | 78 | 6 | std::unique_ptr<IAggregateFunction> result = | 79 | 6 | std::make_unique<AggregateFunctionTemplate>(argument_types); | 80 | 6 | CHECK_AGG_FUNCTION_SERIALIZED_TYPE(AggregateFunctionTemplate); | 81 | 6 | return AggregateFunctionPtr(result.release()); | 82 | 6 | } |
_ZN5doris23create_empty_arg_windowINS_23WindowFunctionRowNumberEEESt10shared_ptrINS_18IAggregateFunctionEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIS2_IKNS_9IDataTypeEESaISG_EERKSG_bRKNS_21AggregateFunctionAttrE Line | Count | Source | 73 | 133 | const AggregateFunctionAttr& attr) { | 74 | 133 | if (!argument_types.empty()) { | 75 | 0 | throw doris::Exception( | 76 | 0 | Status::InternalError("create_window: argument_types must be empty")); | 77 | 0 | } | 78 | 133 | std::unique_ptr<IAggregateFunction> result = | 79 | 133 | std::make_unique<AggregateFunctionTemplate>(argument_types); | 80 | 133 | CHECK_AGG_FUNCTION_SERIALIZED_TYPE(AggregateFunctionTemplate); | 81 | 133 | return AggregateFunctionPtr(result.release()); | 82 | 133 | } |
_ZN5doris23create_empty_arg_windowINS_22WindowFunctionCumeDistEEESt10shared_ptrINS_18IAggregateFunctionEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIS2_IKNS_9IDataTypeEESaISG_EERKSG_bRKNS_21AggregateFunctionAttrE Line | Count | Source | 73 | 6 | const AggregateFunctionAttr& attr) { | 74 | 6 | if (!argument_types.empty()) { | 75 | 0 | throw doris::Exception( | 76 | 0 | Status::InternalError("create_window: argument_types must be empty")); | 77 | 0 | } | 78 | 6 | std::unique_ptr<IAggregateFunction> result = | 79 | 6 | std::make_unique<AggregateFunctionTemplate>(argument_types); | 80 | 6 | CHECK_AGG_FUNCTION_SERIALIZED_TYPE(AggregateFunctionTemplate); | 81 | 6 | return AggregateFunctionPtr(result.release()); | 82 | 6 | } |
|
83 | | |
84 | 6 | void register_aggregate_function_window_rank(AggregateFunctionSimpleFactory& factory) { |
85 | 6 | factory.register_function("dense_rank", create_empty_arg_window<WindowFunctionDenseRank>); |
86 | 6 | factory.register_function("rank", create_empty_arg_window<WindowFunctionRank>); |
87 | 6 | factory.register_function("percent_rank", create_empty_arg_window<WindowFunctionPercentRank>); |
88 | 6 | factory.register_function("row_number", create_empty_arg_window<WindowFunctionRowNumber>); |
89 | 6 | factory.register_function("ntile", creator_without_type::creator<WindowFunctionNTile>); |
90 | 6 | factory.register_function("cume_dist", create_empty_arg_window<WindowFunctionCumeDist>); |
91 | 6 | } |
92 | | |
93 | | void register_aggregate_function_window_lead_lag_first_last( |
94 | 6 | AggregateFunctionSimpleFactory& factory) { |
95 | 6 | factory.register_function_both("lead", create_aggregate_function_window_lead); |
96 | 6 | factory.register_function_both("lag", create_aggregate_function_window_lag); |
97 | | // FE rewrites first_value(k1, false) → first_value(k1), so argument_types.size() == 2 |
98 | | // means arg_ignore_null = true. Dispatch at registration to avoid runtime branching |
99 | | // that would double template instantiations. |
100 | 6 | factory.register_function_both( |
101 | 6 | "first_value", |
102 | 6 | [](const std::string& name, const DataTypes& argument_types, |
103 | 6 | const DataTypePtr& result_type, const bool result_is_nullable, |
104 | 213 | const AggregateFunctionAttr& attr) -> AggregateFunctionPtr { |
105 | 213 | if (argument_types.size() == 2) { |
106 | 11 | return create_aggregate_function_window_first_ignore_null( |
107 | 11 | name, argument_types, result_type, result_is_nullable, attr); |
108 | 11 | } |
109 | 202 | return create_aggregate_function_window_first(name, argument_types, result_type, |
110 | 202 | result_is_nullable, attr); |
111 | 213 | }); |
112 | 6 | factory.register_function_both( |
113 | 6 | "last_value", |
114 | 6 | [](const std::string& name, const DataTypes& argument_types, |
115 | 6 | const DataTypePtr& result_type, const bool result_is_nullable, |
116 | 92 | const AggregateFunctionAttr& attr) -> AggregateFunctionPtr { |
117 | 92 | if (argument_types.size() == 2) { |
118 | 9 | return create_aggregate_function_window_last_ignore_null( |
119 | 9 | name, argument_types, result_type, result_is_nullable, attr); |
120 | 9 | } |
121 | 83 | return create_aggregate_function_window_last(name, argument_types, result_type, |
122 | 83 | result_is_nullable, attr); |
123 | 92 | }); |
124 | | // nth_value always has 2 args (column, N) from FE. |
125 | | // WindowFunctionNthValueImpl does not implement ignore-null logic, |
126 | | // so register directly without dispatch. |
127 | 6 | factory.register_function_both("nth_value", create_aggregate_function_window_nth_value); |
128 | 6 | } |
129 | | |
130 | | } // namespace doris |