Skip to content

Commit 434a23b

Browse files
Fix: Align sort_merge_join filter output with join schema to fix right-anti panic (#18800)
## 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 #18787. ## Rationale for this change Sort-merge joins assumed both inputs had the same width when applying filter results. With different column counts (like right-anti joins) we built batches that no longer matched the output schema, so Arrow panicked during concat_batches. <!-- 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. --> ## What changes are included in this PR? - Fix the null-row and filtered-row builders in SortMergeJoinStream to slice the streamed vs buffered columns using the correct widths, keeping every batch consistent with self.schema. - Merge the semi/anti projection branch so it always projects the streamed-side columns expected in the output. <!-- 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. --> ## Are these changes tested? Manual testing and ran previous tests. <img width="1092" height="552" alt="Screenshot 2025-11-18 at 5 04 20 PM" src="https://github.com/user-attachments/assets/1272a368-f694-4d7b-907b-b605a4996419" /> <!-- 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)? --> ## 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. --> --------- Co-authored-by: Raz Luvaton <16746759+rluvaton@users.noreply.github.com>
1 parent 6751f44 commit 434a23b

File tree

3 files changed

+102
-37
lines changed

3 files changed

+102
-37
lines changed

datafusion/core/tests/fuzz_cases/join_fuzz.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,16 @@ async fn test_right_semi_join_1k() {
245245

246246
#[tokio::test]
247247
async fn test_right_semi_join_1k_filtered() {
248-
JoinFuzzTestCase::new(
249-
make_staggered_batches_i32(1000, false),
250-
make_staggered_batches_i32(1000, false),
251-
JoinType::RightSemi,
252-
Some(Box::new(col_lt_col_filter)),
253-
)
254-
.run_test(&[HjSmj, NljHj], false)
255-
.await
248+
for (left_extra, right_extra) in [(true, true), (false, true), (true, false)] {
249+
JoinFuzzTestCase::new(
250+
make_staggered_batches_i32(1000, left_extra),
251+
make_staggered_batches_i32(1000, right_extra),
252+
JoinType::RightSemi,
253+
Some(Box::new(col_lt_col_filter)),
254+
)
255+
.run_test(&[HjSmj, NljHj], false)
256+
.await
257+
}
256258
}
257259

258260
#[tokio::test]
@@ -299,14 +301,16 @@ async fn test_right_anti_join_1k() {
299301

300302
#[tokio::test]
301303
async fn test_right_anti_join_1k_filtered() {
302-
JoinFuzzTestCase::new(
303-
make_staggered_batches_i32(1000, false),
304-
make_staggered_batches_i32(1000, false),
305-
JoinType::RightAnti,
306-
Some(Box::new(col_lt_col_filter)),
307-
)
308-
.run_test(&[HjSmj, NljHj], false)
309-
.await
304+
for (left_extra, right_extra) in [(true, true), (false, true), (true, false)] {
305+
JoinFuzzTestCase::new(
306+
make_staggered_batches_i32(1000, left_extra),
307+
make_staggered_batches_i32(1000, right_extra),
308+
JoinType::RightAnti,
309+
Some(Box::new(col_lt_col_filter)),
310+
)
311+
.run_test(&[HjSmj, NljHj], false)
312+
.await
313+
}
310314
}
311315

312316
#[tokio::test]
@@ -564,26 +568,30 @@ async fn test_left_anti_join_1k_binary_filtered() {
564568

565569
#[tokio::test]
566570
async fn test_right_anti_join_1k_binary() {
567-
JoinFuzzTestCase::new(
568-
make_staggered_batches_binary(1000, false),
569-
make_staggered_batches_binary(1000, false),
570-
JoinType::RightAnti,
571-
None,
572-
)
573-
.run_test(&[HjSmj, NljHj], false)
574-
.await
571+
for (left_extra, right_extra) in [(true, true), (false, true), (true, false)] {
572+
JoinFuzzTestCase::new(
573+
make_staggered_batches_binary(1000, left_extra),
574+
make_staggered_batches_binary(1000, right_extra),
575+
JoinType::RightAnti,
576+
None,
577+
)
578+
.run_test(&[HjSmj, NljHj], false)
579+
.await
580+
}
575581
}
576582

577583
#[tokio::test]
578584
async fn test_right_anti_join_1k_binary_filtered() {
579-
JoinFuzzTestCase::new(
580-
make_staggered_batches_binary(1000, false),
581-
make_staggered_batches_binary(1000, false),
582-
JoinType::RightAnti,
583-
Some(Box::new(col_lt_col_filter)),
584-
)
585-
.run_test(&[HjSmj, NljHj], false)
586-
.await
585+
for (left_extra, right_extra) in [(true, true), (false, true), (true, false)] {
586+
JoinFuzzTestCase::new(
587+
make_staggered_batches_binary(1000, left_extra),
588+
make_staggered_batches_binary(1000, right_extra),
589+
JoinType::RightAnti,
590+
Some(Box::new(col_lt_col_filter)),
591+
)
592+
.run_test(&[HjSmj, NljHj], false)
593+
.await
594+
}
587595
}
588596

589597
#[tokio::test]

datafusion/physical-plan/src/joins/sort_merge_join/stream.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,14 +1595,16 @@ impl SortMergeJoinStream {
15951595
&self.schema,
15961596
&[filtered_record_batch, null_joined_streamed_batch],
15971597
)?;
1598-
} else if matches!(self.join_type, JoinType::LeftSemi | JoinType::LeftAnti) {
1598+
} else if matches!(
1599+
self.join_type,
1600+
JoinType::LeftSemi
1601+
| JoinType::LeftAnti
1602+
| JoinType::RightAnti
1603+
| JoinType::RightSemi
1604+
) {
15991605
let output_column_indices = (0..left_columns_length).collect::<Vec<_>>();
16001606
filtered_record_batch =
16011607
filtered_record_batch.project(&output_column_indices)?;
1602-
} else if matches!(self.join_type, JoinType::RightAnti | JoinType::RightSemi) {
1603-
let output_column_indices = (0..right_columns_length).collect::<Vec<_>>();
1604-
filtered_record_batch =
1605-
filtered_record_batch.project(&output_column_indices)?;
16061608
} else if matches!(self.join_type, JoinType::Full)
16071609
&& corrected_mask.false_count() > 0
16081610
{

datafusion/physical-plan/src/joins/sort_merge_join/tests.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,61 @@ async fn join_right_anti_two_with_filter() -> Result<()> {
11091109
Ok(())
11101110
}
11111111

1112+
#[tokio::test]
1113+
async fn join_right_anti_filtered_with_mismatched_columns() -> Result<()> {
1114+
let left = build_table_two_cols(("a1", &vec![31, 31]), ("b1", &vec![32, 33]));
1115+
let right = build_table(
1116+
("a2", &vec![31, 31]),
1117+
("b2", &vec![32, 35]),
1118+
("c2", &vec![108, 109]),
1119+
);
1120+
let on = vec![
1121+
(
1122+
Arc::new(Column::new_with_schema("a1", &left.schema())?) as _,
1123+
Arc::new(Column::new_with_schema("a2", &right.schema())?) as _,
1124+
),
1125+
(
1126+
Arc::new(Column::new_with_schema("b1", &left.schema())?) as _,
1127+
Arc::new(Column::new_with_schema("b2", &right.schema())?) as _,
1128+
),
1129+
];
1130+
1131+
let filter = JoinFilter::new(
1132+
Arc::new(BinaryExpr::new(
1133+
Arc::new(Column::new("b1", 0)),
1134+
Operator::LtEq,
1135+
Arc::new(Column::new("c2", 1)),
1136+
)),
1137+
vec![
1138+
ColumnIndex {
1139+
index: 1,
1140+
side: JoinSide::Left,
1141+
},
1142+
ColumnIndex {
1143+
index: 2,
1144+
side: JoinSide::Right,
1145+
},
1146+
],
1147+
Arc::new(Schema::new(vec![
1148+
Field::new("b1", DataType::Int32, false),
1149+
Field::new("c2", DataType::Int32, false),
1150+
])),
1151+
);
1152+
1153+
let (_, batches) =
1154+
join_collect_with_filter(left, right, on, filter, RightAnti).await?;
1155+
1156+
let expected = [
1157+
"+----+----+-----+",
1158+
"| a2 | b2 | c2 |",
1159+
"+----+----+-----+",
1160+
"| 31 | 35 | 109 |",
1161+
"+----+----+-----+",
1162+
];
1163+
assert_batches_eq!(expected, &batches);
1164+
Ok(())
1165+
}
1166+
11121167
#[tokio::test]
11131168
async fn join_right_anti_with_nulls() -> Result<()> {
11141169
let left = build_table_i32_nullable(

0 commit comments

Comments
 (0)