[enhancement](Nereids)support max function (#11795)

- add aggregate function max to Nereids
- add function P0 regression test for Nereids
This commit is contained in:
肖俊杰
2022-08-24 10:54:51 +08:00
committed by GitHub
parent 47dfd915c6
commit f737ff742e
6 changed files with 132 additions and 0 deletions

View File

@ -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;

View File

@ -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<Expression> arguments = unboundFunction.getArguments();
if (arguments.size() != 1) {
return unboundFunction;
}
return new Max(unboundFunction.getArguments().get(0));
} else if (name.equalsIgnoreCase("min")) {
List<Expression> arguments = unboundFunction.getArguments();
if (arguments.size() != 1) {

View File

@ -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<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new Max(children.get(0));
}
@Override
public DataType getIntermediateType() {
return getDataType();
}
}

View File

@ -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

View File

@ -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

View File

@ -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;
"""
}