From f737ff742ee50d5348f8d3be35b7f59ebe4deed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E4=BF=8A=E6=9D=B0?= <971308896@qq.com> Date: Wed, 24 Aug 2022 10:54:51 +0800 Subject: [PATCH] [enhancement](Nereids)support max function (#11795) - add aggregate function max to Nereids - add function P0 regression test for Nereids --- .../doris/analysis/FunctionCallExpr.java | 1 + .../nereids/rules/analysis/BindFunction.java | 7 +++ .../trees/expressions/functions/Max.java | 55 +++++++++++++++++++ .../expressions/ExpressionParserTest.java | 6 ++ .../data/nereids_syntax_p0/function.out | 16 ++++++ .../suites/nereids_syntax_p0/function.groovy | 47 ++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/Max.java create mode 100644 regression-test/data/nereids_syntax_p0/function.out create mode 100644 regression-test/suites/nereids_syntax_p0/function.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java index 39b91eb2eb..363eda3bd0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java @@ -1402,6 +1402,7 @@ public class FunctionCallExpr extends Expr { fn = getBuiltinFunction(fnName.getFunction(), childTypes, Function.CompareMode.IS_NONSTRICT_SUPERTYPE_OF); type = fn.getReturnType(); } else if (fnName.getFunction().equalsIgnoreCase("year") + || fnName.getFunction().equalsIgnoreCase("max") || fnName.getFunction().equalsIgnoreCase("min") || fnName.getFunction().equalsIgnoreCase("avg")) { Type childType = getChild(0).type; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java index 875d68ea3f..384f57f78c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java @@ -26,6 +26,7 @@ import org.apache.doris.nereids.trees.expressions.NamedExpression; import org.apache.doris.nereids.trees.expressions.TimestampArithmetic; import org.apache.doris.nereids.trees.expressions.functions.Avg; import org.apache.doris.nereids.trees.expressions.functions.Count; +import org.apache.doris.nereids.trees.expressions.functions.Max; import org.apache.doris.nereids.trees.expressions.functions.Min; import org.apache.doris.nereids.trees.expressions.functions.Substring; import org.apache.doris.nereids.trees.expressions.functions.Sum; @@ -102,6 +103,12 @@ public class BindFunction implements AnalysisRuleFactory { return new Count(); } return new Count(unboundFunction.getArguments().get(0)); + } else if (name.equalsIgnoreCase("max")) { + List arguments = unboundFunction.getArguments(); + if (arguments.size() != 1) { + return unboundFunction; + } + return new Max(unboundFunction.getArguments().get(0)); } else if (name.equalsIgnoreCase("min")) { List arguments = unboundFunction.getArguments(); if (arguments.size() != 1) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/Max.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/Max.java new file mode 100644 index 0000000000..abb781f4ec --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/Max.java @@ -0,0 +1,55 @@ +// 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; + +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression; +import org.apache.doris.nereids.types.DataType; + +import com.google.common.base.Preconditions; + +import java.util.List; + +/** max agg function. */ +public class Max extends AggregateFunction implements UnaryExpression { + + public Max(Expression child) { + super("max", child); + } + + @Override + public DataType getDataType() { + return child().getDataType(); + } + + @Override + public boolean nullable() { + return child().nullable(); + } + + @Override + public Expression withChildren(List children) { + Preconditions.checkArgument(children.size() == 1); + return new Max(children.get(0)); + } + + @Override + public DataType getIntermediateType() { + return getDataType(); + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionParserTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionParserTest.java index e6dd5de565..6047bac3ff 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionParserTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionParserTest.java @@ -149,6 +149,12 @@ public class ExpressionParserTest extends ParserTestBase { String min = "select min(a), min(b) as m from test1"; assertSql(min); + + String max = "select max(a), max(b) as m from test1"; + assertSql(max); + + String maxAndMin = "select max(a), min(b) from test1"; + assertSql(maxAndMin); } @Test diff --git a/regression-test/data/nereids_syntax_p0/function.out b/regression-test/data/nereids_syntax_p0/function.out new file mode 100644 index 0000000000..cac9a7c5b1 --- /dev/null +++ b/regression-test/data/nereids_syntax_p0/function.out @@ -0,0 +1,16 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !max -- +10 2959704 + +-- !min -- +7 1705830 + +-- !max_and_min -- +2959704 7 + +-- !count -- +3 3 + +-- !avg -- +2.5E-323 1.1644193E-317 + diff --git a/regression-test/suites/nereids_syntax_p0/function.groovy b/regression-test/suites/nereids_syntax_p0/function.groovy new file mode 100644 index 0000000000..f0b44ae2e6 --- /dev/null +++ b/regression-test/suites/nereids_syntax_p0/function.groovy @@ -0,0 +1,47 @@ +// 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("function") { + sql """ + SET enable_vectorized_engine=true + """ + + sql """ + SET enable_nereids_planner=true + """ + + order_qt_max """ + SELECT max(lo_discount), max(lo_extendedprice) AS max_extendedprice FROM lineorder; + """ + + order_qt_min """ + SELECT min(lo_discount), min(lo_extendedprice) AS min_extendedprice FROM lineorder; + """ + + order_qt_max_and_min """ + SELECT max(lo_extendedprice), min(lo_discount) FROM lineorder; + """ + + order_qt_count """ + SELECT count(c_city), count(*) AS custdist FROM customer; + """ + + order_qt_avg """ + SELECT avg(lo_tax), avg(lo_extendedprice) AS avg_extendedprice FROM lineorder; + """ +} +