2010YOUY01 opened a new pull request, #17319:
URL: https://github.com/apache/datafusion/pull/17319
## 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.
-->
<!--
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.
-->
The following join query now it's using NLJ. Given the equal join operators
like Hash Join, SortMergeJoin supports a special null comparison behavior:
`NULL=NULL` -> `true` instead of the default `NULL`, this query can be safely
re-written to equal join with equi-join operator's `null_equals_null` option
enabled.
Given HJ is faster than NLJ in general, this optimization can make similar
queries more efficient.
```SQL
SELECT *
FROM t1
JOIN t2 ON t1.val IS NOT DISTINCT FROM t2.val;
+---------------+------------------------------------------------------------+
| plan_type | plan
|
+---------------+------------------------------------------------------------+
| physical_plan | ┌───────────────────────────┐
|
| | │ ProjectionExec │
|
| | │ -------------------- │
|
| | │ t1_id: id │
|
| | │ t2_id: id │
|
| | │ val: val │
|
| | └─────────────┬─────────────┘
|
| | ┌─────────────┴─────────────┐
|
| | │ NestedLoopJoinExec ├──────────────┐
|
| | └─────────────┬─────────────┘ │
|
| | ┌─────────────┴─────────────┐┌─────────────┴─────────────┐
|
| | │ DataSourceExec ││ DataSourceExec │
|
| | │ -------------------- ││ -------------------- │
|
| | │ bytes: 288 ││ bytes: 288 │
|
| | │ format: memory ││ format: memory │
|
| | │ rows: 1 ││ rows: 1 │
|
| | └───────────────────────────┘└───────────────────────────┘
|
| |
|
+---------------+------------------------------------------------------------+
```
I've verified that DuckDB is already doing so
```
D explain SELECT *
FROM t1
JOIN t2 ON t1.val IS NOT DISTINCT FROM t2.val;
┌─────────────────────────────┐
│┌───────────────────────────┐│
││ Physical Plan ││
│└───────────────────────────┘│
└─────────────────────────────┘
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ id │
│ val │
│ id │
│ val │
│ │
│ ~18 Rows │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ HASH_JOIN │
│ ──────────────────── │
│ Join Type: INNER │
│ │
│ Conditions: ├──────────────┐
│ val IS NOT DISTINCT FROM │ │
│ val │ │
│ │ │
│ ~18 Rows │ │
└─────────────┬─────────────┘ │
┌─────────────┴─────────────┐┌─────────────┴─────────────┐
│ SEQ_SCAN ││ SEQ_SCAN │
│ ──────────────────── ││ ──────────────────── │
│ Table: t1 ││ Table: t2 │
│ Type: Sequential Scan ││ Type: Sequential Scan │
│ ││ │
│ Projections: ││ Projections: │
│ val ││ val │
│ id ││ id │
│ ││ │
│ ~6 Rows ││ ~6 Rows │
└───────────────────────────┘└───────────────────────────┘
```
## What changes are included in this PR?
Modified logical optimizer pass `ExtractEquijoinPredicate` to also handle
`IS NOT DISTINCT FROM` case.
INDF expressions will only be extracted to equal join condition, if the join
predicate has 0 equality expr, and several INDF comparisons. See the
implementation for more details.
## 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)?
-->
sqllogictests (mostly check the plans include HashJoin if the conversion is
possible)
## Are there any user-facing changes?
No
<!--
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.
-->
--
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]