Repository navigation
Commit 5191bcc
authored
fix: preserve join predicates during reordering (#27066)
> [!IMPORTANT]
>
> **Fork CI setup:** For pull requests from a fork to `main`, enable
workflows in your fork's **Actions** tab before you push.
> See the [fork CI setup
guide](https://datafusion.apache.org/contributor-guide/#pull-requests-from-forks).
## 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 #26058.
- Closes #25704.
## 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.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of
the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
Join reordering can drop predicates and return incorrect results. For
example:
```SQL
SELECT *
FROM (VALUES (1)) AS a(x)
CROSS JOIN (VALUES (1)) AS k(x)
JOIN (VALUES (1)) AS b(x)
ON a.x = b.x
AND a.x + k.x = b.x;
```
This query should return no rows because 1 + 1 = 1 is false. However,
EliminateCrossJoin can reorder the inputs into (a JOIN b) CROSS JOIN k
and drop the second condition, incorrectly returning one row.
Rebuilding joins can also overwrite individual keys’ NULL comparison
semantics when ordinary equality and NULL-safe equality are mixed,
causing missing or extra rows.
## 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.
-->
- Preserve original join predicates and retain conditions that rebuilt
joins cannot enforce as residual filters.
- Track ordinary and NULL-safe join keys separately, removing predicates
only when rebuilt joins enforce the same comparison semantics.
- Convert the existing NULL semantics unit test into four table-driven
scenarios.
## What is the testing strategy for this PR?
<!--
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
Briefly describe how this PR is tested, and point to the specific tests
you added. For example: 'This new feature is covered by the
`sqllogictest` cases added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change
is already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the
changed code is exercised.
-->
Add five regression cases in joins.slt covering CASE predicate
preservation, mixed NULL semantics in both directions, NULL-safe
residual predicates, and ordinary equality on columns already joined
with NULL-safe equality.
Validation passed:
- Optimizer unit, integration, and documentation tests.
- SQL logic tests selected by joins.slt.
- cargo fmt --all.
- cargo clippy --all-targets --all-features -- -D warnings.
## 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.
-->
Affected queries now preserve all join conditions and their NULL
comparison semantics, returning the correct rows. No public API changes.1 parent 3f4a37f commit 5191bcc
3 files changed
Lines changed: 241 additions & 118 deletions
File tree
- datafusion
- optimizer/src
- sqllogictest/test_files
0 commit comments