This is an automated email from the ASF dual-hosted git repository.

englefly pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new ece7e6e1292 [fix](nereids) Preserve NULL in inferred constant 
predicates (#71233)
ece7e6e1292 is described below

commit ece7e6e12920dae7408707eea40e7b497ca19da5
Author: minghong <[email protected]>
AuthorDate: Thu Dec 3 14:30:00 2026 +0800

    [fix](nereids) Preserve NULL in inferred constant predicates (#71233)
    
    ### Background
    
    Nereids propagates constant values across equality predicates and adds a
    predicate for each value newly inferred from a conjunction. A projected
    literal can expose a constant value to a join even when the opposite
    input provides no constant for its matching column.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: #49982 (introduced the constant propagation code)
    PR Type: Bugfix
    
    Problem Summary:
    
    A null-safe join can lose all matching rows when one input exposes a
    constant NULL and the other needs a predicate inferred from the join.
    Generate an IS NULL predicate for that inferred value and preserve it
    across propagation iterations.
    
    ```sql
    SELECT a.id
    FROM (
        SELECT 1 AS id, CAST(NULL AS INT) AS r
        UNION ALL
        SELECT 2, 7
    ) a
    JOIN (SELECT 1 AS id, CAST(NULL AS INT) AS r) b
        ON a.id = b.id AND a.r <=> b.r
    ```
    
    The right input exposes `b.r` as a constant NULL. The master baseline
    returned zero rows. The fixed build returns `1`.
    
    ### Details
    
    - **Frequency:** Always for the reproduction above with constant
    propagation enabled.
    - **Root cause:** `ConstantPropagation.replaceAndConstants()` emits an
    ordinary equality for every newly inferred literal. The NULL case
    therefore adds `a.r = NULL`, which cannot match a row.
    - **Why existing tests missed it:**
    `ConstantPropagationTest.testLogicalJoin` covers propagation of non-NULL
    constants. The `test_null_uniform_join` regression covers ordinary
    equality with NULL inputs. Neither exercised a null-safe join with a
    constant visible on only one side.
    - **Fix:** Emit IS NULL for a newly inferred NULL and retain an existing
    conjunct that enforces the same value across iterations. Non-NULL
    literals continue through the existing comparison coercion.
    - **Other paths and branches:** The correction applies to conjunctions
    handled by the shared FE rule, including join and filter predicates. The
    faulty construction is present in the 4.0, 4.1, and 4.2 branches. The
    rule is absent on the checked 3.1 branch.
    - **Behavior change:** Rows matching a null-safe equality survive when
    predicate inference propagates a NULL value. Ordinary equality still
    rejects NULL matches.
    
    ### Testing
    
    - Built the baseline and fixed FE with `./build.sh --fe`. The fixed FE
    passed Checkstyle.
    - Ran `./run-regression-test.sh --run -d
    nereids_rules_p0/constant_propagation` on the fixed FE. All three suites
    passed, including the reduced `test_null_safe_constant_propagation`
    suite. The direct join returns `1` with null-safe equality and zero rows
    with ordinary equality.
    - Re-ran the new suite against the unmodified master FE with the same
    generated expected results. The null-safe query failed on the first
    expected row because the result was empty.
    - Both regression runs used the same prebuilt BE from `9b9016645356` in
    the isolated local cluster.
    - Ran `./run-fe-ut.sh --run
    org.apache.doris.nereids.rules.rewrite.ConstantPropagationTest`. All 13
    tests passed.
    
    ### Notes for Reviewers
    
    The rule trace shows the fixed join retaining `a.r <=> b.r` and adding
    `a.r IS NULL` as an additional conjunct. The baseline replaces the join
    with a logical empty relation in the same rule.
    
    ### Release note
    
    Fix missing rows when Nereids propagates a NULL constant through a
    null-safe join predicate.
    
    ### Check List (For Author)
    
    - Behavior changed: Yes (see Details)
    - Does this need documentation: No
    - Pre-submission status: Ready
    
    ### AI Disclosure
    
    - Tool: OpenAI Codex (GPT-6)
    - Generated: FE fix, regression test, and PR description
    - Human review: The author discussed the intended NULL comparison
    semantics. Review of the final diff has not been reported.
---
 .../nereids/rules/rewrite/ConstantPropagation.java | 33 +++++++++++++---------
 .../test_null_safe_constant_propagation.out        |  6 ++++
 .../test_null_safe_constant_propagation.groovy     | 31 ++++++++++++++++++++
 3 files changed, 57 insertions(+), 13 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConstantPropagation.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConstantPropagation.java
index 21910bdbb2e..b5f9144ccb3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConstantPropagation.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ConstantPropagation.java
@@ -32,6 +32,7 @@ import 
org.apache.doris.nereids.trees.expressions.EqualPredicate;
 import org.apache.doris.nereids.trees.expressions.EqualTo;
 import org.apache.doris.nereids.trees.expressions.ExprId;
 import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.IsNull;
 import org.apache.doris.nereids.trees.expressions.Match;
 import org.apache.doris.nereids.trees.expressions.NamedExpression;
 import org.apache.doris.nereids.trees.expressions.Or;
@@ -496,8 +497,7 @@ public class ConstantPropagation extends 
DefaultPlanRewriter<CascadesContext> im
         Set<Slot> inputSlots = expression.getInputSlots();
         ImmutableEqualSet<Slot> newEqualSet = 
equalAndConstantOptions.get().first;
         Map<Slot, Literal> newConstants = equalAndConstantOptions.get().second;
-        // myInferConstantSlots : the slots that are inferred by this 
expression, not inferred by parent
-        // myInferConstantSlots[slot] = true means expression had contains 
conjunct `slot = constant`
+        // Track whether each constant inferred by this expression already has 
an enforcing conjunct.
         Map<Slot, Boolean> myInferConstantSlots = 
Maps.newLinkedHashMapWithExpectedSize(
                 Math.max(0, newConstants.size() - parentConstants.size()));
         for (Slot slot : newConstants.keySet()) {
@@ -524,13 +524,18 @@ public class ConstantPropagation extends 
DefaultPlanRewriter<CascadesContext> im
                 builder.add(newChild);
             }
         }
-        // if the expression infer `slot = constant`, but not contains 
conjunct `slot = constant`, need to add it
+        // Add a conjunct for each inferred constant that is not already 
enforced.
         for (Map.Entry<Slot, Boolean> entry : myInferConstantSlots.entrySet()) 
{
             // if this expression don't contain the slot, no add it, to avoid 
the expression size increase too long
             if (!entry.getValue() && inputSlots.contains(entry.getKey())) {
                 Slot slot = entry.getKey();
-                EqualTo equal = new EqualTo(slot, newConstants.get(slot), 
true);
-                
builder.add(TypeCoercionUtils.processComparisonPredicate(equal));
+                Literal value = newConstants.get(slot);
+                if (value.isNullLiteral()) {
+                    // NULL inferred through null-safe equality must still 
match NULL.
+                    builder.add(new IsNull(slot));
+                } else {
+                    
builder.add(TypeCoercionUtils.processComparisonPredicate(new EqualTo(slot, 
value, true)));
+                }
             }
         }
         return expression.withChildren(builder.build());
@@ -553,15 +558,17 @@ public class ConstantPropagation extends 
DefaultPlanRewriter<CascadesContext> im
 
     private boolean needReplaceWithConstant(Expression expression, Map<Slot, 
Literal> constants,
             Map<Slot, Boolean> myInferConstantSlots) {
-        if (expression instanceof EqualTo && expression.child(0) instanceof 
Slot) {
+        if ((expression instanceof EqualTo || expression instanceof IsNull) && 
expression.child(0) instanceof Slot) {
             Slot slot = (Slot) expression.child(0);
-            // my infer constant slots contain this slot and hadn't replaced 
with slot=constant yet,
-            // so let it alone and don't replace it.
-            if (myInferConstantSlots.containsKey(slot)
-                    && !myInferConstantSlots.get(slot)
-                    && expression.child(1).equals(constants.get(slot))) {
-                myInferConstantSlots.put(slot, true);
-                return false;
+            if (Boolean.FALSE.equals(myInferConstantSlots.get(slot))) {
+                Literal value = constants.get(slot);
+                boolean enforcesConstant = expression instanceof IsNull
+                        ? value.isNullLiteral() : 
expression.child(1).equals(value);
+                if (enforcesConstant) {
+                    // Preserve one enforcing conjunct across propagation 
iterations.
+                    myInferConstantSlots.put(slot, true);
+                    return false;
+                }
             }
         }
 
diff --git 
a/regression-test/data/nereids_rules_p0/constant_propagation/test_null_safe_constant_propagation.out
 
b/regression-test/data/nereids_rules_p0/constant_propagation/test_null_safe_constant_propagation.out
new file mode 100644
index 00000000000..03e0e97f054
--- /dev/null
+++ 
b/regression-test/data/nereids_rules_p0/constant_propagation/test_null_safe_constant_propagation.out
@@ -0,0 +1,6 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !null_safe_equality --
+1
+
+-- !ordinary_equality --
+
diff --git 
a/regression-test/suites/nereids_rules_p0/constant_propagation/test_null_safe_constant_propagation.groovy
 
b/regression-test/suites/nereids_rules_p0/constant_propagation/test_null_safe_constant_propagation.groovy
new file mode 100644
index 00000000000..999bcc94904
--- /dev/null
+++ 
b/regression-test/suites/nereids_rules_p0/constant_propagation/test_null_safe_constant_propagation.groovy
@@ -0,0 +1,31 @@
+// 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.
+
+suite("test_null_safe_constant_propagation") {
+    // b.r is uniformly NULL, so the conjunction lets constant propagation 
infer NULL for a.r.
+    def joinInputs = """
+        FROM (
+            SELECT 1 AS id, CAST(NULL AS INT) AS r
+            UNION ALL
+            SELECT 2, 7
+        ) a
+        JOIN (SELECT 1 AS id, CAST(NULL AS INT) AS r) b ON a.id = b.id
+    """
+
+    order_qt_null_safe_equality "SELECT a.id ${joinInputs} AND a.r <=> b.r"
+    order_qt_ordinary_equality "SELECT a.id ${joinInputs} AND a.r = b.r"
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to