Skip to content

Commit

Permalink
[Impl](Nereids) add propagateNullLiteral trait for special functions (a…
Browse files Browse the repository at this point in the history
…pache#42256)

Add a trait PropagateNullLiteral, which ensures that if any of its input
parameters is a null literal, the output will also be a null literal.
Note that PropagateNullable also has this property. Due to limitations
in the GeneratedExpressionRelations map, most functions that implement
alwaysNullable are included in PropagateNullLiteral. Therefore, defining
a function that is PropagateNullable but not PropagateNullLiteral is not
currently allowed.
function lists:
Acos AppendTrailingCharIfAbsent Asin BitmapFromArray BitmapFromBase64
BitmapFromString BitmapMax BitmapMin BitmapSubsetInRange
BitmapSubsetLimit ConvertTz CryptoFunction DayFloor Date DateFormat
Divide DigitalMasking Dlog10 Dsqrt Fmod FromBase64 FromDays
GetJsonDouble GetJsonString HourCeil HourFloor IntegralDivide Lpad Ln
Log10 Log2 Mod MonthCeil MonthFloor Pmod ParseUrl Rpad Repeat
RegexpExtract RegexpExtractAll RegexpReplace RegexpReplaceOne SecondCeil
SplitPart Sqrt StAngle StAngleSphere StAreaSquareKm StAreaSquareMeters
StDistanceSphere StGeomFromWKB StGeomfromtext StGeometryfromtext
StLinefromtext StPoint StPolygon StPolyfromtext StX StY StrToDate
SubReplace ToDate ToDateV2
  • Loading branch information
LiBinfeng-01 committed Nov 8, 2024
1 parent 16a83fb commit 04daffb
Show file tree
Hide file tree
Showing 73 changed files with 345 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.apache.doris.nereids.trees.expressions.WhenClause;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateParam;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
Expand Down Expand Up @@ -562,7 +563,7 @@ public Expr visitTableGeneratingFunction(TableGeneratingFunction function,
@Override
public Expr visitBinaryArithmetic(BinaryArithmetic binaryArithmetic, PlanTranslatorContext context) {
NullableMode nullableMode = NullableMode.DEPEND_ON_ARGUMENT;
if (binaryArithmetic instanceof AlwaysNullable) {
if (binaryArithmetic instanceof AlwaysNullable || binaryArithmetic instanceof PropagateNullLiteral) {
nullableMode = NullableMode.ALWAYS_NULLABLE;
} else if (binaryArithmetic instanceof AlwaysNotNullable) {
nullableMode = NullableMode.ALWAYS_NOT_NULLABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
import org.apache.doris.nereids.trees.expressions.WhenClause;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.generator.TableGeneratingFunction;
Expand Down Expand Up @@ -715,7 +716,8 @@ private Optional<Expression> preProcess(Expression expression) {
if (expression instanceof AggregateFunction || expression instanceof TableGeneratingFunction) {
return Optional.of(expression);
}
if (expression instanceof PropagateNullable && ExpressionUtils.hasNullLiteral(expression.getArguments())) {
if (ExpressionUtils.hasNullLiteral(expression.getArguments())
&& (expression instanceof PropagateNullLiteral || expression instanceof PropagateNullable)) {
return Optional.of(new NullLiteral(expression.getDataType()));
}
if (!ExpressionUtils.isAllLiteral(expression.getArguments())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.doris.common.Config;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.DecimalV3Type;
Expand All @@ -35,7 +36,7 @@
/**
* Divide Expression.
*/
public class Divide extends BinaryArithmetic implements AlwaysNullable {
public class Divide extends BinaryArithmetic implements AlwaysNullable, PropagateNullLiteral {

public Divide(Expression left, Expression right) {
super(ImmutableList.of(left, right), Operator.DIVIDE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.doris.nereids.trees.expressions;

import org.apache.doris.catalog.Env;
import org.apache.doris.nereids.exceptions.NotSupportedException;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
Expand All @@ -30,8 +29,6 @@
import org.apache.doris.nereids.trees.expressions.literal.DateLiteral;
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.types.DataType;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
Expand Down Expand Up @@ -59,13 +56,11 @@ public enum ExpressionEvaluator {
* Evaluate the value of the expression.
*/
public Expression eval(Expression expression) {

if (!(expression.isConstant() || expression.foldable()) || expression instanceof AggregateFunction) {
return expression;
}

String fnName = null;
DataType ret = expression.getDataType();
if (expression instanceof BinaryArithmetic) {
BinaryArithmetic arithmetic = (BinaryArithmetic) expression;
fnName = arithmetic.getLegacyOperator().getName();
Expand All @@ -77,14 +72,6 @@ public Expression eval(Expression expression) {
fnName = function.getName();
}

if ((Env.getCurrentEnv().isNullResultWithOneNullParamFunction(fnName))) {
for (Expression e : expression.children()) {
if (e instanceof NullLiteral) {
return new NullLiteral(ret);
}
}
}

return invoke(expression, fnName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.analysis.ArithmeticExpr.Operator;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;

import com.google.common.base.Preconditions;
Expand All @@ -30,7 +31,7 @@
/**
* A DIV B
*/
public class IntegralDivide extends BinaryArithmetic implements AlwaysNullable {
public class IntegralDivide extends BinaryArithmetic implements AlwaysNullable, PropagateNullLiteral {

public IntegralDivide(Expression left, Expression right) {
super(ImmutableList.of(left, right), Operator.INT_DIVIDE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.analysis.ArithmeticExpr.Operator;
import org.apache.doris.nereids.exceptions.UnboundException;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DecimalV3Type;

Expand All @@ -31,7 +32,7 @@
/**
* Mod Expression.
*/
public class Mod extends BinaryArithmetic implements AlwaysNullable {
public class Mod extends BinaryArithmetic implements AlwaysNullable, PropagateNullLiteral {

public Mod(Expression left, Expression right) {
super(ImmutableList.of(left, right), Operator.MOD);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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;

/**
* If input has NullLiteral, then output would be NullLiteral when folding constant
*
* e.g. Acos(NULL) ==> NULL.
*/
public interface PropagateNullLiteral {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DoubleType;
Expand All @@ -34,7 +35,7 @@
* ScalarFunction 'acos'. This class is generated by GenerateFunction.
*/
public class Acos extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.StringType;
Expand All @@ -35,7 +36,7 @@
* ScalarFunction 'append_trailing_char_if_absent'. This class is generated by GenerateFunction.
*/
public class AppendTrailingCharIfAbsent extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DoubleType;
Expand All @@ -34,7 +35,7 @@
* ScalarFunction 'asin'. This class is generated by GenerateFunction.
*/
public class Asin extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.ArrayType;
Expand All @@ -39,7 +40,7 @@
* ScalarFunction 'bitmap_from_array'. This class is generated by GenerateFunction.
*/
public class BitmapFromArray extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BitmapType.INSTANCE).args(ArrayType.of(BigIntType.INSTANCE)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BitmapType;
Expand All @@ -36,7 +37,7 @@
* ScalarFunction 'bitmap_from_string'. This class is generated by GenerateFunction.
*/
public class BitmapFromBase64 extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BitmapType.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BitmapType;
Expand All @@ -36,7 +37,7 @@
* ScalarFunction 'bitmap_from_string'. This class is generated by GenerateFunction.
*/
public class BitmapFromString extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BitmapType.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
Expand All @@ -35,7 +36,7 @@
* ScalarFunction 'bitmap_max'. This class is generated by GenerateFunction.
*/
public class BitmapMax extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE).args(BitmapType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
Expand All @@ -35,7 +36,7 @@
* ScalarFunction 'bitmap_min'. This class is generated by GenerateFunction.
*/
public class BitmapMin extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE).args(BitmapType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.TernaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
Expand All @@ -35,7 +36,7 @@
* ScalarFunction 'bitmap_subset_in_range'. This class is generated by GenerateFunction.
*/
public class BitmapSubsetInRange extends ScalarFunction
implements TernaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements TernaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BitmapType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.shape.TernaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.BigIntType;
Expand All @@ -35,7 +36,7 @@
* ScalarFunction 'bitmap_subset_limit'. This class is generated by GenerateFunction.
*/
public class BitmapSubsetLimit extends ScalarFunction
implements TernaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
implements TernaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BitmapType.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.Monotonic;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
Expand All @@ -41,7 +42,7 @@
* ScalarFunction 'convert_tz'. This class is generated by GenerateFunction.
*/
public class ConvertTz extends ScalarFunction
implements TernaryExpression, ExplicitlyCastableSignature, AlwaysNullable, Monotonic {
implements TernaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral, Monotonic {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.qe.ConnectContext;

Expand All @@ -30,7 +31,7 @@

/** CryptoFunction */
public abstract class CryptoFunction extends ScalarFunction
implements ExplicitlyCastableSignature, AlwaysNullable {
implements ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {

public CryptoFunction(String name, Expression... arguments) {
super(name, arguments);
Expand Down
Loading

0 comments on commit 04daffb

Please sign in to comment.