You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "englefly (via GitHub)" <gi...@apache.org> on 2026/04/08 05:21:08 UTC

[PR] Is null opt [doris]

englefly opened a new pull request, #62204:
URL: https://github.com/apache/doris/pull/62204

   ### What problem does this PR solve?
   
   Issue Number: close #xxx
   
   Related PR: #xxx
   
   Problem Summary:
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Test <!-- At least one of them must be included. -->
       - [ ] Regression test
       - [ ] Unit Test
       - [ ] Manual test (add detailed scripts or steps below)
       - [ ] No need to test or manual test. Explain why:
           - [ ] This is a refactor/code format and no logic has been changed.
           - [ ] Previous test can cover this change.
           - [ ] No code files have been changed.
           - [ ] Other reason <!-- Add your reason?  -->
   
   - Behavior changed:
       - [ ] No.
       - [ ] Yes. <!-- Explain the behavior change -->
   
   - Does this need documentation?
       - [ ] No.
       - [ ] Yes. <!-- Add document PR link here. eg: https://github.com/apache/doris-website/pull/1214 -->
   
   ### Check List (For Reviewer who merge this PR)
   
   - [ ] Confirm the release note
   - [ ] Confirm test cases
   - [ ] Confirm document
   - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into -->
   
   


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] Is null opt [doris]

Posted by "englefly (via GitHub)" <gi...@apache.org>.
englefly commented on PR #62204:
URL: https://github.com/apache/doris/pull/62204#issuecomment-4210605633

   /review
   


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] Is null opt [doris]

Posted by "hello-stephen (via GitHub)" <gi...@apache.org>.
hello-stephen commented on PR #62204:
URL: https://github.com/apache/doris/pull/62204#issuecomment-4204016123

   
   Thank you for your contribution to Apache Doris.
   Don't know what should be done next? See [How to process your PR](https://cwiki.apache.org/confluence/display/DORIS/How+to+process+your+PR).
   
   Please clearly describe your PR:
   1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
   2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
   3. What features were added. Why was this function added?
   4. Which code was refactored and why was this part of the code refactored?
   5. Which functions were optimized and what is the difference before and after the optimization?
       


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] Is null opt [doris]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] Is null opt [doris]

Posted by "englefly (via GitHub)" <gi...@apache.org>.
englefly closed pull request #62204: Is null opt
URL: https://github.com/apache/doris/pull/62204


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] Is null opt [doris]

Posted by "englefly (via GitHub)" <gi...@apache.org>.
englefly commented on PR #62204:
URL: https://github.com/apache/doris/pull/62204#issuecomment-4204016207

   run buildall


-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org