-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Enhancement](doris-future) Support REGR_SXX_SXY_SYY aggregation functions #39187
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,173 @@ struct AggregateFunctionRegrData { | |
| return slope; | ||
| } | ||
| }; | ||
| template <typename T> | ||
| struct AggregateFunctionRegrSxxData { | ||
| using Type = T; | ||
| UInt64 count = 0; | ||
| Float64 sum_x {}; | ||
| Float64 sum_of_x_squared {}; | ||
|
|
||
| void write(BufferWritable& buf) const { | ||
| write_binary(sum_x, buf); | ||
| write_binary(sum_of_x_squared, buf); | ||
| write_binary(count, buf); | ||
| } | ||
|
|
||
| void read(BufferReadable& buf) { | ||
| read_binary(sum_x, buf); | ||
| read_binary(sum_of_x_squared, buf); | ||
| read_binary(count, buf); | ||
| } | ||
|
|
||
| void reset() { | ||
| sum_x = {}; | ||
| sum_of_x_squared = {}; | ||
| count = 0; | ||
| } | ||
|
|
||
| void merge(const AggregateFunctionRegrSxxData& rhs) { | ||
| if (rhs.count == 0) { | ||
| return; | ||
| } | ||
| sum_x += rhs.sum_x; | ||
| 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_of_x_squared += value_x * value_x; | ||
| count += 1; | ||
| } | ||
|
|
||
| Float64 get_regr_sxx_result() const { | ||
| // count == 0 | ||
| // The result of a query for an empty table is a null value | ||
| Float64 result = sum_of_x_squared - (sum_x * sum_x / count); | ||
| return result; | ||
| } | ||
| }; | ||
| template <typename T> | ||
| struct AggregateFunctionRegrSxyData { | ||
| using Type = T; | ||
| UInt64 count = 0; | ||
| Float64 sum_x {}; | ||
| Float64 sum_y {}; | ||
| Float64 sum_of_x_mul_y {}; | ||
|
|
||
| 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(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(count, buf); | ||
| } | ||
|
|
||
| void reset() { | ||
| sum_x = {}; | ||
| sum_y = {}; | ||
| sum_of_x_mul_y = {}; | ||
| count = 0; | ||
| } | ||
|
|
||
| void merge(const AggregateFunctionRegrSxyData& 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; | ||
| 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; | ||
| count += 1; | ||
| } | ||
|
|
||
| Float64 get_regr_sxy_result() const { | ||
| // count == 0 | ||
| // The result of a query for an empty table is a null value | ||
| Float64 result = sum_of_x_mul_y - (sum_x * sum_y / count); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return result; | ||
| } | ||
| }; | ||
| template <typename T> | ||
| struct AggregateFunctionRegrSyyData { | ||
| using Type = T; | ||
| UInt64 count = 0; | ||
| Float64 sum_y {}; | ||
| Float64 sum_of_y_squared {}; | ||
|
|
||
| void write(BufferWritable& buf) const { | ||
| write_binary(sum_y, buf); | ||
| write_binary(sum_of_y_squared, buf); | ||
| write_binary(count, buf); | ||
| } | ||
|
|
||
| void read(BufferReadable& buf) { | ||
| read_binary(sum_y, buf); | ||
| read_binary(sum_of_y_squared, buf); | ||
| read_binary(count, buf); | ||
| } | ||
|
|
||
| void reset() { | ||
| sum_y = {}; | ||
| sum_of_y_squared = {}; | ||
| count = 0; | ||
| } | ||
|
|
||
| void merge(const AggregateFunctionRegrSyyData& rhs) { | ||
| if (rhs.count == 0) { | ||
| return; | ||
| } | ||
| sum_y += rhs.sum_y; | ||
| sum_of_y_squared += rhs.sum_of_y_squared; | ||
| count += rhs.count; | ||
| } | ||
|
|
||
| void add(T value_y, T value_x) { | ||
| sum_y += value_y; | ||
| sum_of_y_squared += value_y * value_y; | ||
| count += 1; | ||
| } | ||
|
|
||
| Float64 get_regr_syy_result() const { | ||
| // count == 0 | ||
| // The result of a query for an empty table is a null value | ||
| Float64 result = sum_of_y_squared - (sum_y * sum_y / count); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return result; | ||
| } | ||
| }; | ||
| template <typename T> | ||
| struct RegrSxxFunc : AggregateFunctionRegrSxxData<T> { | ||
| static constexpr const char* name = "regr_sxx"; | ||
|
|
||
| Float64 get_result() const { return this->get_regr_sxx_result(); } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct RegrSxyFunc : AggregateFunctionRegrSxyData<T> { | ||
| static constexpr const char* name = "regr_sxy"; | ||
|
|
||
| Float64 get_result() const { return this->get_regr_sxy_result(); } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct RegrSyyFunc : AggregateFunctionRegrSyyData<T> { | ||
| static constexpr const char* name = "regr_syy"; | ||
|
|
||
| Float64 get_result() const { return this->get_regr_syy_result(); } | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct RegrSlopeFunc : AggregateFunctionRegrData<T> { | ||
|
|
||
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
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
94 changes: 94 additions & 0 deletions
94
...-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/RegrSxx.java
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,94 @@ | ||
| // 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. | ||
|
|
||
| package org.apache.doris.nereids.trees.expressions.functions.agg; | ||
|
|
||
| import org.apache.doris.catalog.FunctionSignature; | ||
| import org.apache.doris.nereids.exceptions.AnalysisException; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable; | ||
| import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; | ||
| import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression; | ||
| import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; | ||
| import org.apache.doris.nereids.types.BigIntType; | ||
| import org.apache.doris.nereids.types.DataType; | ||
| import org.apache.doris.nereids.types.DoubleType; | ||
| import org.apache.doris.nereids.types.FloatType; | ||
| import org.apache.doris.nereids.types.IntegerType; | ||
| import org.apache.doris.nereids.types.SmallIntType; | ||
| import org.apache.doris.nereids.types.TinyIntType; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** regr_sxx agg function. */ | ||
| public class RegrSxx extends AggregateFunction | ||
| implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable { | ||
|
|
||
| public static final List<FunctionSignature> SIGNATURES = ImmutableList.of( | ||
| FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE, DoubleType.INSTANCE), | ||
| FunctionSignature.ret(DoubleType.INSTANCE).args(BigIntType.INSTANCE, BigIntType.INSTANCE), | ||
| FunctionSignature.ret(DoubleType.INSTANCE).args(IntegerType.INSTANCE, IntegerType.INSTANCE), | ||
| FunctionSignature.ret(DoubleType.INSTANCE).args(SmallIntType.INSTANCE, SmallIntType.INSTANCE), | ||
| FunctionSignature.ret(DoubleType.INSTANCE).args(TinyIntType.INSTANCE, TinyIntType.INSTANCE), | ||
| FunctionSignature.ret(DoubleType.INSTANCE).args(FloatType.INSTANCE, FloatType.INSTANCE)); | ||
|
|
||
| /** | ||
| * Constructor with 2 arguments. | ||
| */ | ||
| public RegrSxx(Expression arg1, Expression arg2) { | ||
| this(false, arg1, arg2); | ||
| } | ||
|
|
||
| /** | ||
| * Constructor with distinct flag and 2 arguments. | ||
| */ | ||
| public RegrSxx(boolean distinct, Expression arg1, Expression arg2) { | ||
| super("regr_sxx", distinct, arg1, arg2); | ||
| } | ||
|
|
||
| @Override | ||
| public void checkLegalityBeforeTypeCoercion() throws AnalysisException { | ||
| DataType arg0Type = left().getDataType(); | ||
| DataType arg1Type = right().getDataType(); | ||
| if ((!arg0Type.isNumericType() && !arg0Type.isNullType()) | ||
| || arg0Type.isOnlyMetricType()) { | ||
| throw new AnalysisException("regr_sxx requires numeric for first parameter: " + toSql()); | ||
| } else if ((!arg1Type.isNumericType() && !arg1Type.isNullType()) | ||
| || arg1Type.isOnlyMetricType()) { | ||
| throw new AnalysisException("regr_sxx requires numeric for second parameter: " + toSql()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public RegrSxx withDistinctAndChildren(boolean distinct, List<Expression> children) { | ||
| Preconditions.checkArgument(children.size() == 2); | ||
| return new RegrSxx(distinct, children.get(0), children.get(1)); | ||
| } | ||
|
|
||
| @Override | ||
| public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) { | ||
| return visitor.visitRegrSxx(this, context); | ||
| } | ||
|
|
||
| @Override | ||
| public List<FunctionSignature> getSignatures() { | ||
| return SIGNATURES; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.