Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SortMergeJoin with join filter filtering all rows out #10495

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion datafusion/physical-plan/src/joins/sort_merge_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,9 @@ impl SMJStream {
// If join filter exists, `self.output_size` is not accurate as we don't know the exact
// number of rows in the output record batch. If streamed row joined with buffered rows,
// once join filter is applied, the number of output rows may be more than 1.
if record_batch.num_rows() > self.output_size {
// If `record_batch` is empty, we should reset `self.output_size` to 0. It could be happened
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// when the join filter is applied and all rows are filtered out.
if record_batch.num_rows() == 0 || record_batch.num_rows() > self.output_size {
self.output_size = 0;
} else {
self.output_size -= record_batch.num_rows();
Expand Down
17 changes: 17 additions & 0 deletions datafusion/sqllogictest/test_files/sort_merge_join.slt
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,22 @@ DROP TABLE t1;
statement ok
DROP TABLE t2;

# Set batch size to 1 for sort merge join
comphead marked this conversation as resolved.
Show resolved Hide resolved
statement ok
set datafusion.execution.batch_size = 1;

query II
SELECT * FROM (
WITH
t1 AS (
SELECT 12 a, 12 b
),
t2 AS (
SELECT 12 a, 12 b
)
SELECT t1.* FROM t1 JOIN t2 on t1.a = t2.b WHERE t1.a > t2.b
) ORDER BY 1, 2;
----

statement ok
set datafusion.optimizer.prefer_hash_join = true;
Loading