github-actions[bot] commented on code in PR #62204:
URL: https://github.com/apache/doris/pull/62204#discussion_r3054896902


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NestedColumnPruning.java:
##########
@@ -123,6 +170,40 @@ private static boolean containsVariant(Plan plan) {
         return hasVariant.get();
     }
 
+    /** Returns true when the plan tree contains IS NULL or IS NOT NULL on a 
nullable slot. */
+    private static boolean containsNullCheck(Plan plan) {
+        AtomicBoolean found = new AtomicBoolean(false);
+        plan.foreachUp(node -> {
+            if (found.get()) {
+                return;
+            }
+            Plan current = (Plan) node;
+            for (Expression expression : current.getExpressions()) {
+                if (expressionContainsNullCheck(expression)) {
+                    found.set(true);
+                    return;
+                }
+            }
+        });
+        return found.get();
+    }
+
+    private static boolean expressionContainsNullCheck(Expression expr) {
+        if (expr instanceof IsNull && expr.child(0).nullable()) {
+            return true;
+        }
+        if (expr instanceof Not && expr.child(0) instanceof IsNull
+                && expr.child(0).child(0).nullable()) {
+            return true;
+        }
+        for (Expression child : expr.children()) {
+            if (expressionContainsNullCheck(child)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private static Map<Integer, AccessPathInfo> pruneDataType(
             Map<Slot, List<CollectAccessPathResult>> slotToAccessPaths) {
         Map<Integer, AccessPathInfo> result = new LinkedHashMap<>();

Review Comment:
   This new string-like fast path accidentally disables the null-flag 
optimization for primitive string columns. 
`AccessPathExpressionCollector.visitIsNull()` now records `[str_col, NULL]`, 
but this branch only materializes an `AccessPathInfo` when 
`hasStringOffsetOnlyAccess()` is true; otherwise it `continue`s immediately. 
For a query like `SELECT 1 FROM t WHERE str_col IS NULL`, `result` never gets 
an entry for `str_col`, `SlotTypeReplacer` drops it, and the scan never 
receives the null-only access path. Please handle `hasNullCheckOnlyAccess()` 
before the early `continue` for string-like slots.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRule.java:
##########
@@ -0,0 +1,113 @@
+// 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.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Length;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.doris.qe.ConnectContext;
+
+import java.util.List;
+
+/**
+ * Rewrites comparisons with empty strings to equivalent length()-based 
expressions so that
+ * the NestedColumnPruning OFFSET optimization can apply.
+ *
+ * <ul>
+ *   <li>{@code str_col = ''}   → {@code length(str_col) = 0}</li>
+ *   <li>{@code str_col <> ''}  → {@code length(str_col) != 0}
+ *       (represented as {@code NOT(length(str_col) = 0)})</li>
+ * </ul>
+ *
+ * This is a semantics-preserving rewrite: for any non-NULL string, {@code s = 
''} is equivalent
+ * to {@code length(s) = 0}; for NULL, both sides evaluate to NULL.
+ *
+ * Only applies when the compared expression is a direct {@link SlotReference} 
of string-like type,
+ * so that the resulting {@code length(slot)} call can benefit from 
OFFSET-only column reading.
+ */
+public class StringEmptyToLengthRule implements ExpressionPatternRuleFactory {
+    public static final StringEmptyToLengthRule INSTANCE = new 
StringEmptyToLengthRule();
+
+    @Override
+    public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
+        return ImmutableList.of(
+                // str_col = '' → length(str_col) = 0
+                matchesType(EqualTo.class)
+                        .thenApply(ctx -> rewriteEqualToEmpty(ctx.expr))
+                        .toRule(ExpressionRuleType.STRING_EMPTY_TO_LENGTH),
+
+                // NOT(str_col = '') → NOT(length(str_col) = 0)  (i.e. str_col 
<> '')
+                matchesType(Not.class)
+                        .thenApply(ctx -> {
+                            Not not = ctx.expr;
+                            if (!(not.child() instanceof EqualTo)) {
+                                return not;
+                            }
+                            Expression rewritten = 
rewriteEqualToEmpty((EqualTo) not.child());
+                            if (rewritten == not.child()) {
+                                return not;
+                            }
+                            return new Not(rewritten);
+                        })
+                        .toRule(ExpressionRuleType.STRING_EMPTY_TO_LENGTH)
+        );
+    }
+
+    /**
+     * If {@code equalTo} compares a string-typed SlotReference against an 
empty-string literal,
+     * rewrites it to {@code length(slot) = 0}.  Returns the original 
expression unchanged otherwise.
+     */
+    private static Expression rewriteEqualToEmpty(EqualTo equalTo) {
+        if (ConnectContext.get().getStatementContext().isDelete()) {
+            return equalTo;
+        }
+        Expression left = equalTo.left();
+        Expression right = equalTo.right();
+
+        SlotReference slot = null;
+        if (isStringSlot(left) && isEmptyStringLiteral(right)) {
+            slot = (SlotReference) left;
+        } else if (isStringSlot(right) && isEmptyStringLiteral(left)) {
+            slot = (SlotReference) right;
+        }
+
+        if (slot == null) {
+            return equalTo;
+        }
+        return new EqualTo(new Length(slot), new IntegerLiteral(0));
+    }
+
+    private static boolean isStringSlot(Expression expr) {
+        return expr instanceof SlotReference && 
expr.getDataType().isStringLikeType();

Review Comment:
   `isStringSlot()` currently keys off `isStringLikeType()`, which includes 
`CHAR`. The rewrite comment proves equivalence for variable-length strings, but 
not for padded `CHAR` values. In Doris, `CharType` goes through distinct 
comparison coercion paths, so a predicate like `cast('' as char(3)) = ''` can 
diverge from `length(cast('' as char(3))) = 0` once padding is involved. 
Because this rule also feeds `ExpressionUtils.extractUniformSlot()`, the 
fallout is broader than just column pruning. Please restrict the rewrite to 
`STRING`/`VARCHAR` or add a regression test that demonstrates `CHAR` is safe 
here.



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

Reply via email to