morrySnow commented on code in PR #56424: URL: https://github.com/apache/doris/pull/56424#discussion_r2378527801
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ReplaceNullWithFalseForCond.java: ########## @@ -0,0 +1,131 @@ +// 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.expression.rules; + +import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher; +import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory; +import org.apache.doris.nereids.rules.expression.ExpressionRuleType; +import org.apache.doris.nereids.trees.expressions.CaseWhen; +import org.apache.doris.nereids.trees.expressions.CompoundPredicate; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.WhenClause; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; +import org.apache.doris.nereids.types.DataType; + +import com.google.common.collect.ImmutableList; + +import java.util.List; +import java.util.Optional; + +/** + * Replace null literal with false literal for condition expression. + * Because in nereids, we use boolean type to represent three-value logic, + * so we need to replace null literal with false literal for condition expression. + * For example, in filter, join condition, case when predicate, etc. + * + * rule: if(null and a > 1, ...) => if(false and a > 1, ...) + * case when null and a > 1 then ... => case when false and a > 1 then ... + * null or (null and a > 1) or not(null) => false or (false and a > 1) or not(null) + */ +public class ReplaceNullWithFalseForCond implements ExpressionPatternRuleFactory { + + public static final ReplaceNullWithFalseForCond INSTANCE = new ReplaceNullWithFalseForCond(); + + @Override + public List<ExpressionPatternMatcher<? extends Expression>> buildRules() { + return ImmutableList.of( + matchesTopType(CaseWhen.class).then(this::rewrite) + .toRule(ExpressionRuleType.REPLACE_NULL_WITH_FALSE_FOR_COND), + matchesTopType(If.class).then(this::rewrite) + .toRule(ExpressionRuleType.REPLACE_NULL_WITH_FALSE_FOR_COND) + ); + } + + protected Expression rewrite(Expression expression) { + return replace(expression, false); + } + + /** + * replace null which its ancestors are all AND/OR/CASE WHEN/IF CONDITION. + * NOTICE: NOT's type is boolean too, if replace null to false in NOT, will get NOT(NULL) = NOT(FALSE) = TRUE, + * but it is wrong, NOT(NULL) = NULL. For null, only under the AND / OR, can rewrite it as FALSE. + */ + public static Expression replace(Expression expression, boolean replaceCaseThen) { + if (!expression.containsType(NullLiteral.class)) { + return expression; + } + if (expression.isNullLiteral()) { + DataType dataType = expression.getDataType(); + if (dataType.isBooleanType() || dataType.isNullType()) { + return BooleanLiteral.FALSE; Review Comment: i don't think we should return `BooleanLiteral.FALSE` for NullType -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
