[enhancement](Nereids)support max function (#11795)
- add aggregate function max to Nereids - add function P0 regression test for Nereids
This commit is contained in:
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
16
regression-test/data/nereids_syntax_p0/function.out
Normal file
16
regression-test/data/nereids_syntax_p0/function.out
Normal 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
|
||||
|
||||
47
regression-test/suites/nereids_syntax_p0/function.groovy
Normal file
47
regression-test/suites/nereids_syntax_p0/function.groovy
Normal 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;
|
||||
"""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user