-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Enhancement](doris-future) Support "REGR_" aggregation functions (PART II) #41240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
96d4a20
add regr_intercept and regr_slope aggregate functions
Yoruet 5882639
delete unused files
Yoruet 7d3d24e
fix bug in regr_slope and regr_intercept
Yoruet 5bc299e
update RegrSlope and RegrIntercept to be nullable
Yoruet 19829a7
change the implementation of RegrSlope and RegrIntercept
Yoruet 875fb28
update the be of regr_slope and regr_intercept
Yoruet 4207554
Modification details
Yoruet 2a35625
fix bug in be of regr_slope and regr_intercept
Yoruet 64711ad
X
zhiqiang-hhhh 86ed7a3
Merge branch 'master' of https://github.com/yoruet/doris into pr/Yoru…
zhiqiang-hhhh 567f5e3
update doris-cluster.conf
Yoruet 2ca9f40
Merge branch 'zhiqiang-hhhh-regr_intercept' and change other files
Yoruet 40b3f80
Code Formatter / Clang Formatter
Yoruet 13d4676
fix fe of regr_slope and add test_regr_slope.groovy
Yoruet 29f6e52
union regr_slope and regr_intercept to regr_union
Yoruet 43c58df
using sub class in regr_slope and regr_intercept
Yoruet 9b85156
code format
Yoruet 72c33e2
Move get_slope method to parent class
Yoruet 68d8d74
code format
Yoruet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
be/src/vec/aggregate_functions/aggregate_function_regr_union.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include "vec/aggregate_functions/aggregate_function_regr_union.h" | ||
|
|
||
| #include "common/status.h" | ||
| #include "vec/aggregate_functions/aggregate_function.h" | ||
| #include "vec/aggregate_functions/aggregate_function_simple_factory.h" | ||
| #include "vec/aggregate_functions/helpers.h" | ||
| #include "vec/core/types.h" | ||
| #include "vec/data_types/data_type.h" | ||
| #include "vec/data_types/data_type_nullable.h" | ||
|
|
||
| namespace doris::vectorized { | ||
|
|
||
| template <typename T, template <typename> class StatFunctionTemplate> | ||
| AggregateFunctionPtr type_dispatch_for_aggregate_function_regr(const DataTypes& argument_types, | ||
| const bool& result_is_nullable, | ||
| bool y_nullable_input, | ||
| bool x_nullable_input) { | ||
| if (y_nullable_input) { | ||
| if (x_nullable_input) { | ||
| return creator_without_type::create_ignore_nullable< | ||
| AggregateFunctionRegrSimple<StatFunctionTemplate<T>, true, true>>( | ||
| argument_types, result_is_nullable); | ||
| } else { | ||
| return creator_without_type::create_ignore_nullable< | ||
| AggregateFunctionRegrSimple<StatFunctionTemplate<T>, true, false>>( | ||
| argument_types, result_is_nullable); | ||
| } | ||
| } else { | ||
| if (x_nullable_input) { | ||
| return creator_without_type::create_ignore_nullable< | ||
| AggregateFunctionRegrSimple<StatFunctionTemplate<T>, false, true>>( | ||
| argument_types, result_is_nullable); | ||
| } else { | ||
| return creator_without_type::create_ignore_nullable< | ||
| AggregateFunctionRegrSimple<StatFunctionTemplate<T>, false, false>>( | ||
| argument_types, result_is_nullable); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template <template <typename> class StatFunctionTemplate> | ||
| AggregateFunctionPtr create_aggregate_function_regr(const std::string& name, | ||
| const DataTypes& argument_types, | ||
| const bool result_is_nullable) { | ||
| if (argument_types.size() != 2) { | ||
| LOG(WARNING) << "aggregate function " << name << " requires exactly 2 arguments"; | ||
| return nullptr; | ||
| } | ||
| if (!result_is_nullable) { | ||
| LOG(WARNING) << "aggregate function " << name << " requires nullable result type"; | ||
| return nullptr; | ||
| } | ||
|
|
||
| bool y_nullable_input = argument_types[0]->is_nullable(); | ||
| bool x_nullable_input = argument_types[1]->is_nullable(); | ||
| WhichDataType y_type(remove_nullable(argument_types[0])); | ||
| WhichDataType x_type(remove_nullable(argument_types[1])); | ||
|
|
||
| #define DISPATCH(TYPE) \ | ||
| if (x_type.idx == TypeIndex::TYPE && y_type.idx == TypeIndex::TYPE) \ | ||
| return type_dispatch_for_aggregate_function_regr<TYPE, StatFunctionTemplate>( \ | ||
| argument_types, result_is_nullable, y_nullable_input, x_nullable_input); | ||
| FOR_NUMERIC_TYPES(DISPATCH) | ||
| #undef DISPATCH | ||
|
|
||
| LOG(WARNING) << "unsupported input types " << argument_types[0]->get_name() << " and " | ||
| << argument_types[1]->get_name() << " for aggregate function " << name; | ||
| return nullptr; | ||
| } | ||
|
|
||
| void register_aggregate_function_regr_union(AggregateFunctionSimpleFactory& factory) { | ||
| factory.register_function_both("regr_slope", create_aggregate_function_regr<RegrSlopeFunc>); | ||
| factory.register_function_both("regr_intercept", | ||
| create_aggregate_function_regr<RegrInterceptFunc>); | ||
| } | ||
| } // namespace doris::vectorized |
216 changes: 216 additions & 0 deletions
216
be/src/vec/aggregate_functions/aggregate_function_regr_union.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <type_traits> | ||
|
|
||
| #include "common/exception.h" | ||
| #include "common/status.h" | ||
| #include "vec/aggregate_functions/aggregate_function.h" | ||
| #include "vec/columns/column_nullable.h" | ||
| #include "vec/columns/column_vector.h" | ||
| #include "vec/common/assert_cast.h" | ||
| #include "vec/core/field.h" | ||
| #include "vec/core/types.h" | ||
| #include "vec/data_types/data_type.h" | ||
| #include "vec/data_types/data_type_nullable.h" | ||
| #include "vec/data_types/data_type_number.h" | ||
| #include "vec/io/io_helper.h" | ||
|
|
||
| namespace doris::vectorized { | ||
|
|
||
| template <typename T> | ||
| struct AggregateFunctionRegrData { | ||
| using Type = T; | ||
| UInt64 count = 0; | ||
| Float64 sum_x {}; | ||
| Float64 sum_y {}; | ||
| Float64 sum_of_x_mul_y {}; | ||
| Float64 sum_of_x_squared {}; | ||
|
|
||
| void write(BufferWritable& buf) const { | ||
| write_binary(sum_x, buf); | ||
| write_binary(sum_y, buf); | ||
| write_binary(sum_of_x_mul_y, buf); | ||
| write_binary(sum_of_x_squared, buf); | ||
| write_binary(count, buf); | ||
| } | ||
|
|
||
| void read(BufferReadable& buf) { | ||
| read_binary(sum_x, buf); | ||
| read_binary(sum_y, buf); | ||
| read_binary(sum_of_x_mul_y, buf); | ||
| read_binary(sum_of_x_squared, buf); | ||
| read_binary(count, buf); | ||
| } | ||
|
|
||
| void reset() { | ||
| sum_x = {}; | ||
| sum_y = {}; | ||
| sum_of_x_mul_y = {}; | ||
| sum_of_x_squared = {}; | ||
| count = 0; | ||
| } | ||
|
|
||
| void merge(const AggregateFunctionRegrData& rhs) { | ||
| if (rhs.count == 0) { | ||
| return; | ||
| } | ||
| sum_x += rhs.sum_x; | ||
| sum_y += rhs.sum_y; | ||
| sum_of_x_mul_y += rhs.sum_of_x_mul_y; | ||
| sum_of_x_squared += rhs.sum_of_x_squared; | ||
| count += rhs.count; | ||
| } | ||
|
|
||
| void add(T value_y, T value_x) { | ||
| sum_x += value_x; | ||
| sum_y += value_y; | ||
| sum_of_x_mul_y += value_x * value_y; | ||
| sum_of_x_squared += value_x * value_x; | ||
| count += 1; | ||
| } | ||
|
|
||
| Float64 get_slope() const { | ||
| Float64 denominator = count * sum_of_x_squared - sum_x * sum_x; | ||
| if (count < 2 || denominator == 0.0) { | ||
| return std::numeric_limits<Float64>::quiet_NaN(); | ||
| } | ||
| Float64 slope = (count * sum_of_x_mul_y - sum_x * sum_y) / denominator; | ||
| return slope; | ||
| } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct RegrSlopeFunc : AggregateFunctionRegrData<T> { | ||
| static constexpr const char* name = "regr_slope"; | ||
|
|
||
| Float64 get_result() const { return this->get_slope(); } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct RegrInterceptFunc : AggregateFunctionRegrData<T> { | ||
| static constexpr const char* name = "regr_intercept"; | ||
|
|
||
| Float64 get_result() const { | ||
| auto slope = this->get_slope(); | ||
| if (std::isnan(slope)) { | ||
| return slope; | ||
| } else { | ||
| Float64 intercept = (this->sum_y - slope * this->sum_x) / this->count; | ||
| return intercept; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template <typename RegrFunc, bool y_nullable, bool x_nullable> | ||
| class AggregateFunctionRegrSimple | ||
| : public IAggregateFunctionDataHelper< | ||
| RegrFunc, AggregateFunctionRegrSimple<RegrFunc, y_nullable, x_nullable>> { | ||
| public: | ||
| using Type = typename RegrFunc::Type; | ||
| using XInputCol = ColumnVector<Type>; | ||
| using YInputCol = ColumnVector<Type>; | ||
| using ResultCol = ColumnVector<Float64>; | ||
|
|
||
| explicit AggregateFunctionRegrSimple(const DataTypes& argument_types_) | ||
| : IAggregateFunctionDataHelper< | ||
| RegrFunc, AggregateFunctionRegrSimple<RegrFunc, y_nullable, x_nullable>>( | ||
| argument_types_) { | ||
| DCHECK(!argument_types_.empty()); | ||
| } | ||
|
|
||
| String get_name() const override { return RegrFunc::name; } | ||
|
|
||
| DataTypePtr get_return_type() const override { | ||
| return make_nullable(std::make_shared<DataTypeFloat64>()); | ||
| } | ||
|
|
||
| void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, | ||
| Arena*) const override { | ||
| bool y_null = false; | ||
| bool x_null = false; | ||
| const YInputCol* y_nested_column = nullptr; | ||
| const XInputCol* x_nested_column = nullptr; | ||
|
|
||
| if constexpr (y_nullable) { | ||
| const ColumnNullable& y_column_nullable = | ||
| assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[0]); | ||
| y_null = y_column_nullable.is_null_at(row_num); | ||
| y_nested_column = assert_cast<const YInputCol*, TypeCheckOnRelease::DISABLE>( | ||
| y_column_nullable.get_nested_column_ptr().get()); | ||
| } else { | ||
| y_nested_column = assert_cast<const YInputCol*, TypeCheckOnRelease::DISABLE>( | ||
| (*columns[0]).get_ptr().get()); | ||
| } | ||
|
|
||
| if constexpr (x_nullable) { | ||
| const ColumnNullable& x_column_nullable = | ||
| assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*columns[1]); | ||
| x_null = x_column_nullable.is_null_at(row_num); | ||
| x_nested_column = assert_cast<const XInputCol*, TypeCheckOnRelease::DISABLE>( | ||
| x_column_nullable.get_nested_column_ptr().get()); | ||
| } else { | ||
| x_nested_column = assert_cast<const XInputCol*, TypeCheckOnRelease::DISABLE>( | ||
| (*columns[1]).get_ptr().get()); | ||
| } | ||
|
|
||
| if (x_null || y_null) { | ||
| return; | ||
| } | ||
|
|
||
| Type y_value = y_nested_column->get_data()[row_num]; | ||
| Type x_value = x_nested_column->get_data()[row_num]; | ||
|
|
||
| this->data(place).add(y_value, x_value); | ||
| } | ||
|
|
||
| void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); } | ||
|
|
||
| void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, | ||
| Arena*) const override { | ||
| this->data(place).merge(this->data(rhs)); | ||
| } | ||
|
|
||
| void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { | ||
| this->data(place).write(buf); | ||
| } | ||
|
|
||
| void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, | ||
| Arena*) const override { | ||
| this->data(place).read(buf); | ||
| } | ||
|
|
||
| void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { | ||
| const auto& data = this->data(place); | ||
| auto& dst_column_with_nullable = assert_cast<ColumnNullable&>(to); | ||
| auto& dst_column = assert_cast<ResultCol&>(dst_column_with_nullable.get_nested_column()); | ||
| Float64 result = data.get_result(); | ||
| if (std::isnan(result)) { | ||
| dst_column_with_nullable.get_null_map_data().push_back(1); | ||
| dst_column.insert_default(); | ||
| } else { | ||
| dst_column_with_nullable.get_null_map_data().push_back(0); | ||
| dst_column.get_data().push_back(result); | ||
| } | ||
| } | ||
| }; | ||
| } // namespace doris::vectorized | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.