hareshkh opened a new pull request, #21121:
URL: https://github.com/apache/datafusion/pull/21121

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and 
enhancements and this helps us generate change logs for our releases. You can 
link an issue to this PR using the GitHub syntax. For example `Closes #123` 
indicates that this PR will close issue #123.
   -->
   
   - Closes #.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in 
the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your 
changes and offer better suggestions for fixes.  
   -->
   
   When a Substrait join expression contains both equal and 
is_not_distinct_from predicates (e.g. Spark pushes a null-safe filter into a 
join that already has a regular equality key), the 
split_eq_and_noneq_join_predicate_with_nulls_equality function uses a single 
nulls_equal_nulls boolean that gets overwritten per-predicate. Whichever 
operator is processed last determines the NullEquality for all keys, silently 
dropping NULL-matching rows.
   
   Since NullEquality is a join-level setting (not per-key) across all physical 
join implementations (HashJoinExec, SortMergeJoinExec, SymmetricHashJoinExec), 
the correct fix is to match DataFusion's own SQL planner behavior: demote IS 
NOT DISTINCT FROM keys to the join filter when mixed with Eq keys. This is 
already correctly handled for SQL as shown in 
[join_is_not_distinct_from.slt:L188](https://sourcegraph.com/r/github.com/apache/datafusion@2b7d4f9a5b005905b23128274ad37c3306ffcd15/-/blob/datafusion/sqllogictest/test_files/join_is_not_distinct_from.slt?L188)
   ```
   # Test mixed equal and IS NOT DISTINCT FROM conditions
   # The `IS NOT DISTINCT FROM` expr should NOT in HashJoin's `on` predicate
   query TT
   EXPLAIN SELECT t1.id AS t1_id, t2.id AS t2_id, t1.val, t2.val
   FROM t1
   JOIN t2 ON t1.id = t2.id AND t1.val IS NOT DISTINCT FROM t2.val
   ----
   logical_plan
   01)Projection: t1.id AS t1_id, t2.id AS t2_id, t1.val, t2.val
   02)--Inner Join: t1.id = t2.id Filter: t1.val IS NOT DISTINCT FROM t2.val
   03)----TableScan: t1 projection=[id, val]
   04)----TableScan: t2 projection=[id, val]
   ```
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is 
sometimes worth providing a summary of the individual changes in this PR.
   -->
   `datafusion/substrait/src/logical_plan/consumer/rel/join_rel.rs`:
     - Collect eq_keys and indistinct_keys separately instead of using a single 
vec with an overwritable boolean
     - When both are present (mixed case), use eq_keys as equijoin keys with 
NullEqualsNothing and reconstruct the IsNotDistinctFrom expressions into the 
join filter
     - Return NullEquality directly instead of converting from bool
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are 
they covered by existing tests)?
   -->
   Yes, three levels of coverage:
   
   1. Unit tests (join_rel.rs) — directly assert the output of 
split_eq_and_noneq_join_predicate_with_nulls_equality for eq-only, 
indistinct-only, mixed, and non-column-operand cases
   2. Integration test (consumer_integration.rs) — loads a JSON-encoded 
Substrait plan with a JoinRel containing both operators through 
from_substrait_plan, executes it, and asserts 6 rows (including NULL=NULL 
matches)
   3. Existing SLT (join_is_not_distinct_from.slt:179-205) — confirms the SQL 
planner already exhibits the same demotion behavior that this PR adds to the 
Substrait consumer
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 
change` label.
   -->
   No API changes. Substrait plans with mixed equal/is_not_distinct_from join 
predicates now correctly preserve null-safe semantics instead of silently 
dropping NULL-matching rows.
   


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