diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/RegrSlope.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/RegrSlope.java index 872184ee2b0628..efce6e88076a7e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/RegrSlope.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/RegrSlope.java @@ -20,8 +20,8 @@ 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.functions.window.SupportWindowAnalytic; 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; @@ -40,9 +40,8 @@ /** * AggregateFunction 'regr_slope'. */ - -public class RegrSlope extends NullableAggregateFunction - implements BinaryExpression, ExplicitlyCastableSignature, SupportWindowAnalytic { +public class RegrSlope extends AggregateFunction + implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable { public static final List SIGNATURES = ImmutableList.of( FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE, DoubleType.INSTANCE), @@ -57,18 +56,14 @@ public class RegrSlope extends NullableAggregateFunction * Constructor with 2 arguments. */ public RegrSlope(Expression arg1, Expression arg2) { - this(false, true, arg1, arg2); + this(false, arg1, arg2); } /** * Constructor with distinct flag and 2 arguments. */ public RegrSlope(boolean distinct, Expression arg1, Expression arg2) { - this(distinct, true, arg1, arg2); - } - - public RegrSlope(boolean distinct, boolean alwaysNullable, Expression arg1, Expression arg2) { - super("regr_slope", distinct, alwaysNullable, arg1, arg2); + super("regr_slope", distinct, arg1, arg2); } @Override @@ -87,17 +82,12 @@ public void checkLegalityBeforeTypeCoercion() throws AnalysisException { @Override public RegrSlope withDistinctAndChildren(boolean distinct, List children) { Preconditions.checkArgument(children.size() == 2); - return new RegrSlope(distinct, alwaysNullable, children.get(0), children.get(1)); - } - - @Override - public NullableAggregateFunction withAlwaysNullable(boolean alwaysNullable) { - return new RegrSlope(distinct, alwaysNullable, children().get(0), children().get(1)); + return new RegrSlope(distinct, children.get(0), children.get(1)); } @Override public R accept(ExpressionVisitor visitor, C context) { - return visitor.visitNullableAggregateFunction(this, context); + return visitor.visitRegrSlope(this, context); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/AggregateFunctionVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/AggregateFunctionVisitor.java index 044f14e3b46c18..3f8c157b376bd6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/AggregateFunctionVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/AggregateFunctionVisitor.java @@ -273,7 +273,7 @@ default R visitRegrIntercept(RegrIntercept regrIntercept, C context) { } default R visitRegrSlope(RegrSlope regrSlope, C context) { - return visitNullableAggregateFunction(regrSlope, context); + return visitAggregateFunction(regrSlope, context); } default R visitRetention(Retention retention, C context) { diff --git a/regression-test/data/nereids_function_p0/agg_function/test_regr_intercept.out b/regression-test/data/nereids_function_p0/agg_function/test_regr_intercept.out deleted file mode 100644 index 1a8c12853de54b..00000000000000 --- a/regression-test/data/nereids_function_p0/agg_function/test_regr_intercept.out +++ /dev/null @@ -1,25 +0,0 @@ --- This file is automatically generated. You should know what you did if you want to edit this --- !sql -- -\N - --- !sql -- -10.0 - --- !sql -- -4.0 - --- !sql -- -5.512931034482759 - --- !sql -- -5.4887751937424145 - --- !sql -- -9.180592991913747 - --- !sql -- -9.180592991913747 - --- !sql -- -9.180592991913747 - diff --git a/regression-test/data/nereids_function_p0/agg_function/test_regr_slope.out b/regression-test/data/nereids_function_p0/agg_function/test_regr_slope.out deleted file mode 100644 index a8faed11d33318..00000000000000 --- a/regression-test/data/nereids_function_p0/agg_function/test_regr_slope.out +++ /dev/null @@ -1,25 +0,0 @@ --- This file is automatically generated. You should know what you did if you want to edit this --- !sql -- -\N - --- !sql -- -0.0 - --- !sql -- -0.0 - --- !sql -- -0.6853448275862069 - --- !sql -- -0.6987176230207446 - --- !sql -- -0.6442048517520216 - --- !sql -- -0.6442048517520216 - --- !sql -- -0.6442048517520216 - diff --git a/regression-test/data/query_p0/aggregate/test_regr_slope.out b/regression-test/data/query_p0/aggregate/test_regr_slope.out new file mode 100644 index 00000000000000..2c39290d95c935 --- /dev/null +++ b/regression-test/data/query_p0/aggregate/test_regr_slope.out @@ -0,0 +1,114 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql_empty_1 -- +\N + +-- !sql_empty_2 -- + +-- !sql_int_1 -- +0.0 + +-- !sql_int_2 -- +\N + +-- !sql_int_3 -- +0.6853448275862069 + +-- !sql_int_4 -- +0.6853448275862069 + +-- !sql_int_5 -- +0.6853448275862069 + +-- !sql_int_6 -- +0.0 + +-- !sql_int_7 -- +\N + +-- !sql_int_8 -- +0.7390029325513197 + +-- !sql_int_8 -- +\N +\N +9.0 +\N +\N + +-- !sql_int_9 -- +0.7401574803149606 + +-- !sql_int_9 -- +9.0 +\N +\N + +-- !sql_int_10 -- +0.7401574803149606 + +-- !sql_int_10 -- +9.0 +\N +\N + +-- !sql_double_1 -- +0.0 + +-- !sql_double_2 -- +\N + +-- !sql_double_3 -- +0.6987176230207431 + +-- !sql_double_3 -- +\N +\N +\N +\N +\N + +-- !sql_double_4 -- +0.6987176230207446 + +-- !sql_double_4 -- +\N +\N +\N +\N +\N + +-- !sql_double_5 -- +0.6987176230207446 + +-- !sql_double_6 -- +0.0 + +-- !sql_double_7 -- +\N + +-- !sql_double_8 -- +0.7390029325513197 + +-- !sql_double_8 -- +\N +\N +9.0 +\N +\N + +-- !sql_double_9 -- +0.7401574803149606 + +-- !sql_double_9 -- +9.0 +\N +\N + +-- !sql_double_10 -- +0.7401574803149606 + +-- !sql_double_10 -- +9.0 +\N +\N + diff --git a/regression-test/suites/nereids_function_p0/agg_function/test_regr_slope.groovy b/regression-test/suites/nereids_function_p0/agg_function/test_regr_slope.groovy deleted file mode 100644 index 783a822c5da113..00000000000000 --- a/regression-test/suites/nereids_function_p0/agg_function/test_regr_slope.groovy +++ /dev/null @@ -1,141 +0,0 @@ -// 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. - -suite("test_regr_slope") { - sql """ DROP TABLE IF EXISTS test_regr_slope_int """ - sql """ DROP TABLE IF EXISTS test_regr_slope_double """ - sql """ DROP TABLE IF EXISTS test_regr_slope_nullable_col """ - - - sql """ SET enable_nereids_planner=true """ - sql """ SET enable_fallback_to_original_planner=false """ - - sql """ - CREATE TABLE test_regr_slope_int ( - `id` int, - `x` int, - `y` int - ) ENGINE=OLAP - DUPLICATE KEY (`id`) - DISTRIBUTED BY HASH(`id`) BUCKETS 4 - PROPERTIES ( - "replication_allocation" = "tag.location.default: 1" - ); - """ - sql """ - CREATE TABLE test_regr_slope_double ( - `id` int, - `x` double, - `y` double - ) ENGINE=OLAP - DUPLICATE KEY (`id`) - DISTRIBUTED BY HASH(`id`) BUCKETS 4 - PROPERTIES ( - "replication_allocation" = "tag.location.default: 1" - ); - """ - sql """ - CREATE TABLE test_regr_slope_nullable_col ( - `id` int, - `x` int, - `y` int - ) ENGINE=OLAP - DUPLICATE KEY (`id`) - DISTRIBUTED BY HASH(`id`) BUCKETS 4 - PROPERTIES ( - "replication_allocation" = "tag.location.default: 1" - ); - """ - // no value - qt_sql "select regr_slope(y,x) from test_regr_slope_int" - sql """ TRUNCATE TABLE test_regr_slope_int """ - - sql """ - INSERT INTO test_regr_slope_int VALUES - (1, 18, 13), - (2, 14, 27), - (3, 12, 2), - (4, 5, 6), - (5, 10, 20); - """ - - sql """ - INSERT INTO test_regr_slope_double VALUES - (1, 18.27123456, 13.27123456), - (2, 14.65890846, 27.65890846), - (3, 12.25345846, 2.253458468), - (4, 5.890846835, 6.890846835), - (5, 10.14345678, 20.14345678); - """ - - sql """ - INSERT INTO test_regr_slope_nullable_col VALUES - (1, 18, 13), - (2, 14, 27), - (3, 5, 7), - (4, 10, 20); - """ - - // value is null - sql """SELECT regr_slope(NULL, NULL);""" - - // parameter is literal and columns - qt_sql "select regr_slope(10,x) from test_regr_slope_int" - - // literal and column - qt_sql "select regr_slope(4,x) from test_regr_slope_int" - - // int value - qt_sql "select regr_slope(y,x) from test_regr_slope_int" - sql """ TRUNCATE TABLE test_regr_slope_int """ - - // double value - qt_sql "select regr_slope(y,x) from test_regr_slope_double" - sql """ TRUNCATE TABLE test_regr_slope_double """ - - // nullable and non_nullable - qt_sql "select regr_slope(y,non_nullable(x)) from test_regr_slope_nullable_col" - - // non_nullable and nullable - qt_sql "select regr_slope(non_nullable(y),x) from test_regr_slope_nullable_col" - - // non_nullable and non_nullable - qt_sql "select regr_slope(non_nullable(y),non_nullable(x)) from test_regr_slope_nullable_col" - sql """ TRUNCATE TABLE test_regr_slope_nullable_col """ - - // exception test - test{ - sql """select regr_slope('range', 1);""" - exception "regr_slope requires numeric for first parameter" - } - - test{ - sql """select regr_slope(1, 'hello');""" - exception "regr_slope requires numeric for second parameter" - } - - test{ - sql """select regr_slope(y, 'hello') from test_regr_slope_int;""" - exception "regr_slope requires numeric for second parameter" - } - - test{ - sql """select regr_slope(1, true);""" - exception "regr_slope requires numeric for second parameter" - } - -} diff --git a/regression-test/suites/query_p0/aggregate/test_regr_slope.groovy b/regression-test/suites/query_p0/aggregate/test_regr_slope.groovy new file mode 100644 index 00000000000000..19397036234595 --- /dev/null +++ b/regression-test/suites/query_p0/aggregate/test_regr_slope.groovy @@ -0,0 +1,217 @@ +// 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. + +suite("test_regr_slope") { + sql """ DROP TABLE IF EXISTS test_regr_slope_int """ + sql """ DROP TABLE IF EXISTS test_regr_slope_double """ + sql """ DROP TABLE IF EXISTS test_regr_slope_nullable_col """ + + + sql """ SET enable_nereids_planner=true """ + sql """ SET enable_fallback_to_original_planner=false """ + + sql """ + CREATE TABLE test_regr_slope_int ( + `id` int, + `x` int, + `y` int + ) ENGINE=OLAP + DUPLICATE KEY (`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 4 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + sql """ + CREATE TABLE test_regr_slope_double ( + `id` int, + `x` double, + `y` double + ) ENGINE=OLAP + DUPLICATE KEY (`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 4 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + + // no value + // agg function without group by should return null + qt_sql_empty_1 "select regr_slope(y,x) from test_regr_slope_int" + // agg function with group by should return empty set + qt_sql_empty_2 "select regr_slope(y,x) from test_regr_slope_int group by id" + + sql """ TRUNCATE TABLE test_regr_slope_int """ + + sql """ + INSERT INTO test_regr_slope_int VALUES + (1, 18, 13), + (2, 14, 27), + (3, 12, 2), + (4, 5, 6), + (5, 10, 20); + """ + + sql """ + INSERT INTO test_regr_slope_double VALUES + (1, 18.27123456, 13.27123456), + (2, 14.65890846, 27.65890846), + (3, 12.25345846, 2.253458468), + (4, 5.890846835, 6.890846835), + (5, 10.14345678, 20.14345678); + """ + + // parameter is literal and columns + // _xxx can help use to distinguish the result + qt_sql_int_1 "select regr_slope(10, x) from test_regr_slope_int" + + // column and literal + qt_sql_int_2 "select regr_slope(x, 4) from test_regr_slope_int" + + // int value + qt_sql_int_3 "select regr_slope(y,x) from test_regr_slope_int" + + // qt_sql_int_3 tests Nullable input column, qt_sql_int_4 test non-Nullable input column + qt_sql_int_4 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_int" + // qt_sql_int_4 tests mix nullable + qt_sql_int_5 "select regr_slope(y, non_nullable(x)) from test_regr_slope_int" + + sql """ TRUNCATE TABLE test_regr_slope_int """ + + // Insert random null values + sql """ + INSERT INTO test_regr_slope_int VALUES + (1, 18, 13), + (1, NULL, 14), + (1, 19, NULL), + (2, 14, 27), + (2, NULL, NULL); + """ + + // Insert random value + // group of id = 3 shoud NOT be null + // group of id = 4 || 5 shoud be null since its size is 1 + sql """ + INSERT INTO test_regr_slope_int VALUES + (3, 12, 2), + (3, 13, 10), + (3, 14, 20), + (4, 5, 6), + (5, 10, 20); + """ + + // parameter is literal and columns + // _xxx can help use to distinguish the result + qt_sql_int_6 "select regr_slope(10, x) from test_regr_slope_int" + + // column and literal + qt_sql_int_7 "select regr_slope(x, 4) from test_regr_slope_int" + + // int value + qt_sql_int_8 "select regr_slope(y,x) from test_regr_slope_int" + qt_sql_int_8 "select regr_slope(y,x) from test_regr_slope_int group by id order by id" + + // qt_sql_int_3 tests Nullable input column, qt_sql_int_4 test non-Nullable input column + qt_sql_int_9 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_int where id >= 3" + qt_sql_int_9 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_int where id >= 3 group by id order by id" + // qt_sql_int_4 tests mix nullable + qt_sql_int_10 "select regr_slope(y, non_nullable(x)) from test_regr_slope_int where id >= 3" + qt_sql_int_10 "select regr_slope(y, non_nullable(x)) from test_regr_slope_int where id >= 3 group by id order by id" + + //// Repeat same thing for double //// + + // parameter is literal and columns + // _xxx can help use to distinguish the result + qt_sql_double_1 "select regr_slope(10, x) from test_regr_slope_double" + + // column and literal + qt_sql_double_2 "select regr_slope(x, 4) from test_regr_slope_double" + + // int value + qt_sql_double_3 "select regr_slope(y,x) from test_regr_slope_double" + qt_sql_double_3 "select regr_slope(y,x) from test_regr_slope_double group by id order by id" + + // qt_sql_int_3 tests Nullable input column, qt_sql_int_4 test non-Nullable input column + qt_sql_double_4 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_double" + qt_sql_double_4 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_double group by id order by id" + // qt_sql_int_4 tests mix nullable + qt_sql_double_5 "select regr_slope(y, non_nullable(x)) from test_regr_slope_double" + + sql """ TRUNCATE TABLE test_regr_slope_double """ + + // Insert random null values + sql """ + INSERT INTO test_regr_slope_double VALUES + (1, 18, 13), + (1, NULL, 14), + (1, 19, NULL), + (2, 14, 27), + (2, NULL, NULL); + """ + + // Insert random value + // group of id = 3 shoud NOT be null + // group of id = 4 || 5 shoud be null since its size is 1 + sql """ + INSERT INTO test_regr_slope_double VALUES + (3, 12, 2), + (3, 13, 10), + (3, 14, 20), + (4, 5, 6), + (5, 10, 20); + """ + + // parameter is literal and columns + // _xxx can help use to distinguish the result + qt_sql_double_6 "select regr_slope(10, x) from test_regr_slope_double" + + // column and literal + qt_sql_double_7 "select regr_slope(x, 4) from test_regr_slope_double" + + // int value + qt_sql_double_8 "select regr_slope(y,x) from test_regr_slope_double" + qt_sql_double_8 "select regr_slope(y,x) from test_regr_slope_double group by id order by id" + + // qt_sql_int_3 tests Nullable input column, qt_sql_int_4 test non-Nullable input column + qt_sql_double_9 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_double where id >= 3" + qt_sql_double_9 "select regr_slope(non_nullable(y), non_nullable(x)) from test_regr_slope_double where id >= 3 group by id order by id" + // qt_sql_int_4 tests mix nullable + qt_sql_double_10 "select regr_slope(y, non_nullable(x)) from test_regr_slope_double where id >= 3" + qt_sql_double_10 "select regr_slope(y, non_nullable(x)) from test_regr_slope_double where id >= 3 group by id order by id" + + // exception test + test{ + sql """select regr_slope('range', 1);""" + exception "regr_slope requires numeric for first parameter" + } + + test{ + sql """select regr_slope(1, 'hello');""" + exception "regr_slope requires numeric for second parameter" + } + + test{ + sql """select regr_slope(y, 'hello') from test_regr_slope_int;""" + exception "regr_slope requires numeric for second parameter" + } + + test{ + sql """select regr_slope(1, true);""" + exception "regr_slope requires numeric for second parameter" + } + +}