[feature](Nereids) support binary arithmetic function (#18213)
support binary arithmetic functions like: add(op1, op2) -> op1 + op2 subtract(op1, op2) -> op1 - op2 multiply(op1, op2) -> op1 * op2 divide(op1, op2) -> op1 / op2 mod(op1, op2) -> op1 % op2
This commit is contained in:
@ -0,0 +1,63 @@
|
||||
// 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.rules.analysis;
|
||||
|
||||
import org.apache.doris.nereids.trees.expressions.Add;
|
||||
import org.apache.doris.nereids.trees.expressions.BitAnd;
|
||||
import org.apache.doris.nereids.trees.expressions.BitNot;
|
||||
import org.apache.doris.nereids.trees.expressions.BitOr;
|
||||
import org.apache.doris.nereids.trees.expressions.BitXor;
|
||||
import org.apache.doris.nereids.trees.expressions.Divide;
|
||||
import org.apache.doris.nereids.trees.expressions.Expression;
|
||||
import org.apache.doris.nereids.trees.expressions.IntegralDivide;
|
||||
import org.apache.doris.nereids.trees.expressions.Mod;
|
||||
import org.apache.doris.nereids.trees.expressions.Multiply;
|
||||
import org.apache.doris.nereids.trees.expressions.Subtract;
|
||||
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* bind arithmetic function
|
||||
*/
|
||||
public class ArithmeticFunctionBinder {
|
||||
private static final NullLiteral DUMMY_EXPRESSION = new NullLiteral();
|
||||
private static final Map<String, Expression> FUNCTION_TO_EXPRESSION = ImmutableMap.<String, Expression>builder()
|
||||
.put("add", new Add(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("subtract", new Subtract(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("multiply", new Multiply(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("divide", new Divide(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("int_divide", new IntegralDivide(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("mod", new Mod(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("bitand", new BitAnd(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("bitor", new BitOr(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("bitxor", new BitXor(DUMMY_EXPRESSION, DUMMY_EXPRESSION))
|
||||
.put("bitnot", new BitNot(DUMMY_EXPRESSION))
|
||||
.build();
|
||||
|
||||
public boolean isBinaryArithmetic(String functionName) {
|
||||
return FUNCTION_TO_EXPRESSION.containsKey(functionName.toLowerCase());
|
||||
}
|
||||
|
||||
public Expression bindBinaryArithmetic(String functionName, List<Expression> children) {
|
||||
return FUNCTION_TO_EXPRESSION.get(functionName.toLowerCase()).withChildren(children);
|
||||
}
|
||||
}
|
||||
@ -96,6 +96,14 @@ public class FunctionBinder extends DefaultExpressionRewriter<CascadesContext> {
|
||||
.build()
|
||||
: (List) unboundFunction.getArguments();
|
||||
|
||||
// we will change arithmetic function like add(), subtract(), bitnot() to the corresponding objects rather than
|
||||
// BoundFunction.
|
||||
ArithmeticFunctionBinder functionBinder = new ArithmeticFunctionBinder();
|
||||
if (functionBinder.isBinaryArithmetic(unboundFunction.getName())) {
|
||||
return functionBinder.bindBinaryArithmetic(unboundFunction.getName(), unboundFunction.children())
|
||||
.accept(this, context);
|
||||
}
|
||||
|
||||
FunctionBuilder builder = functionRegistry.findFunctionBuilder(functionName, arguments);
|
||||
BoundFunction boundFunction = builder.build(functionName, arguments);
|
||||
return TypeCoercionUtils.processBoundFunction(boundFunction);
|
||||
|
||||
Reference in New Issue
Block a user