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

morrysnow 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 865e3b84208 [fix](fe) Fix PullUpPredicates losing NULL rows in 
INTERSECT/EXCEPT (#62299)
865e3b84208 is described below

commit 865e3b84208bc500872df9d998a9c23c65d14243
Author: yujun <[email protected]>
AuthorDate: Mon Apr 13 11:15:02 2026 +0800

    [fix](fe) Fix PullUpPredicates losing NULL rows in INTERSECT/EXCEPT (#62299)
    
    ### What problem does this PR solve?
    
    Problem Summary:
    
    introduced by #39450
    
    `PullUpPredicates.getFiltersFromUnionConstExprs()` incorrectly removes
    `NullLiteral` from UNION ALL constant expressions without compensating
    with `OR IS NULL`, producing invalid pull-up predicates.
    
    For example, `SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT NULL`
    generates the pull-up predicate `n IN (1, 3)`, which is NOT true for the
    NULL row (`NULL IN (1, 3)` evaluates to NULL, not TRUE).
    
    When `InferPredicates` pushes this predicate from one INTERSECT child to
    another, the IN filter incorrectly eliminates NULL rows. Since INTERSECT
    treats NULL=NULL per SQL standard, this causes incorrect query results
    with missing NULLs.
    
    **Root Cause**: In `getFiltersFromUnionConstExprs()`, line 467:
    ```java
    options.removeIf(option -> option instanceof NullLiteral);
    ```
    removes NULL but does not add `OR IS NULL` to compensate.
    
    **Fix**: When NULL constants are present, generate `n IN (...) OR n IS
    NULL` instead of just `n IN (...)`, making the predicate valid for all
    rows including NULL.
    
    **Reproducing Query**:
    ```sql
    WITH
      tbl0(n) AS (SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT NULL),
      tbl1(n) AS (SELECT 2 UNION ALL SELECT NULL UNION ALL SELECT 1)
    (
      SELECT (n * 2) AS n FROM tbl0
      INTERSECT
      SELECT (n * 2) AS n FROM tbl1
    );
    -- Expected: {2, NULL}  Actual before fix: {2}
    ```
    
    ### Release note
    
    Fixed a bug where INTERSECT and EXCEPT queries could incorrectly drop
    NULL rows when the optimizer inferred and pushed down predicates from
    UNION ALL constant expressions.
    
    
    Co-authored-by: Copilot <[email protected]>
---
 .../nereids/rules/rewrite/PullUpPredicates.java    |  15 ++-
 .../set_operations/test_pullup_predicate_null.out  |  32 +++++
 .../test_pullup_predicate_null.groovy              | 137 +++++++++++++++++++++
 3 files changed, 181 insertions(+), 3 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpPredicates.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpPredicates.java
index 4720430df99..941cd07f20a 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpPredicates.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpPredicates.java
@@ -464,11 +464,20 @@ public class PullUpPredicates extends 
PlanVisitor<ImmutableSet<Expression>, Void
                     break;
                 }
             }
-            options.removeIf(option -> option instanceof NullLiteral);
+            boolean hasNull = options.removeIf(option -> option instanceof 
NullLiteral);
+            Expression predicate = null;
             if (options.size() > 1) {
-                filtersFromConstExprs.add(new InPredicate(compareExpr, 
options));
+                predicate = new InPredicate(compareExpr, options);
             } else if (options.size() == 1) {
-                filtersFromConstExprs.add(new EqualTo(compareExpr, 
options.iterator().next()));
+                predicate = new EqualTo(compareExpr, 
options.iterator().next());
+            }
+            if (hasNull && predicate != null) {
+                predicate = new Or(predicate, new IsNull(compareExpr));
+            } else if (hasNull) {
+                predicate = new IsNull(compareExpr);
+            }
+            if (predicate != null) {
+                filtersFromConstExprs.add(predicate);
             }
         }
         return ImmutableSet.copyOf(filtersFromConstExprs);
diff --git 
a/regression-test/data/query_p0/set_operations/test_pullup_predicate_null.out 
b/regression-test/data/query_p0/set_operations/test_pullup_predicate_null.out
new file mode 100644
index 00000000000..56f67996604
--- /dev/null
+++ 
b/regression-test/data/query_p0/set_operations/test_pullup_predicate_null.out
@@ -0,0 +1,32 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !intersect_null --
+\N
+2
+
+-- !except_null --
+6
+
+-- !except_null_only_left --
+\N
+
+-- !all_null_intersect --
+\N
+
+-- !union_distinct_null --
+\N
+1
+2
+
+-- !no_null_intersect --
+2
+
+-- !multi_col_null --
+1      \N
+
+-- !table_intersect_null --
+\N
+2
+
+-- !table_except_null --
+1
+
diff --git 
a/regression-test/suites/query_p0/set_operations/test_pullup_predicate_null.groovy
 
b/regression-test/suites/query_p0/set_operations/test_pullup_predicate_null.groovy
new file mode 100644
index 00000000000..2d944943940
--- /dev/null
+++ 
b/regression-test/suites/query_p0/set_operations/test_pullup_predicate_null.groovy
@@ -0,0 +1,137 @@
+// 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.
+
+// Regression test for PullUpPredicates.getFiltersFromUnionConstExprs() 
incorrectly
+// removing NULL from pulled-up predicates without compensating with OR IS 
NULL.
+// This caused INTERSECT/EXCEPT to lose NULL rows when InferPredicates pushed 
down
+// the incomplete IN(...) filter to set operation children.
+suite("test_pullup_predicate_null", "query,p0") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"
+
+    // Original reproducing case: INTERSECT with NULL in CTE constants
+    // tbl0 n*2 = {2, 6, NULL}, tbl1 n*2 = {4, NULL, 2}
+    // INTERSECT should return {2, NULL}
+    order_qt_intersect_null """
+        WITH
+            tbl0(n) AS (SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT NULL),
+            tbl1(n) AS (SELECT 2 UNION ALL SELECT NULL UNION ALL SELECT 1)
+        (
+            SELECT (n * 2) AS n FROM tbl0
+            INTERSECT
+            SELECT (n * 2) AS n FROM tbl1
+        )
+    """
+
+    // EXCEPT with NULL: {2, 6, NULL} EXCEPT {4, NULL, 2} = {6}
+    order_qt_except_null """
+        WITH
+            tbl0(n) AS (SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT NULL),
+            tbl1(n) AS (SELECT 2 UNION ALL SELECT NULL UNION ALL SELECT 1)
+        (
+            SELECT (n * 2) AS n FROM tbl0
+            EXCEPT
+            SELECT (n * 2) AS n FROM tbl1
+        )
+    """
+
+    // EXCEPT where NULL is only in first set: {1, NULL} EXCEPT {1, 2} = {NULL}
+    order_qt_except_null_only_left """
+        WITH
+            tbl0(n) AS (SELECT 1 UNION ALL SELECT NULL),
+            tbl1(n) AS (SELECT 1 UNION ALL SELECT 2)
+        (
+            SELECT n FROM tbl0
+            EXCEPT
+            SELECT n FROM tbl1
+        )
+    """
+
+    // All-NULL INTERSECT: {NULL} INTERSECT {NULL, 1} = {NULL}
+    order_qt_all_null_intersect """
+        SELECT * FROM (SELECT NULL a UNION ALL SELECT NULL) t1
+        INTERSECT
+        SELECT * FROM (SELECT NULL a UNION ALL SELECT 1) t2
+    """
+
+    // UNION DISTINCT with NULL from both sides
+    order_qt_union_distinct_null """
+        WITH
+            tbl0(n) AS (SELECT 1 UNION ALL SELECT NULL),
+            tbl1(n) AS (SELECT NULL UNION ALL SELECT 2)
+        (
+            SELECT n FROM tbl0
+            UNION
+            SELECT n FROM tbl1
+        )
+    """
+
+    // Non-null case: verify no regression
+    order_qt_no_null_intersect """
+        WITH
+            tbl0(n) AS (SELECT 1 UNION ALL SELECT 3),
+            tbl1(n) AS (SELECT 2 UNION ALL SELECT 1)
+        (
+            SELECT (n * 2) AS n FROM tbl0
+            INTERSECT
+            SELECT (n * 2) AS n FROM tbl1
+        )
+    """
+
+    // Multi-column with NULL in one column
+    order_qt_multi_col_null """
+        WITH
+            t1(a, b) AS (SELECT 1, NULL UNION ALL SELECT 2, 'y'),
+            t2(a, b) AS (SELECT 1, NULL UNION ALL SELECT 3, 'z')
+        (
+            SELECT a, b FROM t1
+            INTERSECT
+            SELECT a, b FROM t2
+        )
+    """
+
+    // Table-based test with NULL values
+    sql "DROP TABLE IF EXISTS test_pullup_null_t1"
+    sql """
+        CREATE TABLE test_pullup_null_t1 (
+            k1 INT NULL
+        ) DISTRIBUTED BY HASH(k1) BUCKETS 3
+        PROPERTIES ("replication_num" = "1")
+    """
+    sql "INSERT INTO test_pullup_null_t1 VALUES (1), (2), (NULL)"
+
+    sql "DROP TABLE IF EXISTS test_pullup_null_t2"
+    sql """
+        CREATE TABLE test_pullup_null_t2 (
+            k1 INT NULL
+        ) DISTRIBUTED BY HASH(k1) BUCKETS 3
+        PROPERTIES ("replication_num" = "1")
+    """
+    sql "INSERT INTO test_pullup_null_t2 VALUES (2), (3), (NULL)"
+
+    order_qt_table_intersect_null """
+        SELECT k1 FROM test_pullup_null_t1
+        INTERSECT
+        SELECT k1 FROM test_pullup_null_t2
+    """
+
+    order_qt_table_except_null """
+        SELECT k1 FROM test_pullup_null_t1
+        EXCEPT
+        SELECT k1 FROM test_pullup_null_t2
+    """
+}


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

Reply via email to