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

   ### Background
   
   Nereids propagates constant values across equality predicates and adds a 
predicate for each value newly inferred from a conjunction. An inlined CTE can 
expose a constant projection to its parent join. A materialized CTE is read 
through a consumer that does not expose its producer's constant traits.
   
   ### 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
   WITH s AS (
       SELECT id, NULL AS r FROM (SELECT 1 AS id UNION ALL SELECT 2) t
   ),
   k AS (SELECT id, r FROM s),
   b AS (
       SELECT id, NULL AS r FROM (SELECT 1 AS id UNION ALL SELECT 2) t
   ),
   unused AS (SELECT id FROM k)
   SELECT a.id FROM k a JOIN b ON b.id = a.id AND b.r <=> a.r
   ```
   
   With CTE materialization enabled and an inline reference threshold of 1, the 
added reference materializes `k` while `b` is inlined. The upstream master 
baseline returned zero rows. The fixed build returns `1` and `2`.
   
   ### Details
   
   - **Frequency:** Always for the reproduction above with the asymmetric CTE 
plan.
   - **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 new null-safe join and ordinary equality control.
   - 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.
   
   ### Notes for Reviewers
   
   The rule trace shows the fixed join retaining `r#19 <=> r#17` and adding 
`r#17 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.
   


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