Skip to content

Commit d966984

Browse files
authored
fix nested loop join with literal join filter (#5431)
* fix nested loop join with literal join filter * imrpove error message
1 parent 96aa2a6 commit d966984

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

datafusion/core/src/physical_plan/joins/hash_join.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,9 @@ impl HashJoinStream {
12191219
self.join_metrics.output_rows.add(batch.num_rows());
12201220
Some(result)
12211221
}
1222-
Err(_) => Some(Err(DataFusionError::Execution(
1223-
"Build left right indices error".to_string(),
1224-
))),
1222+
Err(err) => Some(Err(DataFusionError::Execution(format!(
1223+
"Fail to build join indices in HashJoinExec, error:{err}",
1224+
)))),
12251225
};
12261226
timer.done();
12271227
result

datafusion/core/src/physical_plan/joins/nested_loop_join.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,9 @@ fn join_left_and_right_batch(
475475
let mut left_indices_builder = UInt64Builder::new();
476476
let mut right_indices_builder = UInt32Builder::new();
477477
let left_right_indices = match indices_result {
478-
Err(_) => Err(DataFusionError::Execution(
479-
"Build left right indices error".to_string(),
480-
)),
478+
Err(err) => Err(DataFusionError::Execution(format!(
479+
"Fail to build join indices in NestedLoopJoinExec, error:{err}"
480+
))),
481481
Ok(indices) => {
482482
for (left_side, right_side) in indices {
483483
left_indices_builder

datafusion/core/src/physical_plan/joins/utils.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use arrow::array::{
2828
};
2929
use arrow::compute;
3030
use arrow::datatypes::{Field, Schema, UInt32Type, UInt64Type};
31-
use arrow::record_batch::RecordBatch;
31+
use arrow::record_batch::{RecordBatch, RecordBatchOptions};
3232
use datafusion_common::cast::as_boolean_array;
3333
use datafusion_common::ScalarValue;
3434
use datafusion_physical_expr::{EquivalentClass, PhysicalExpr};
@@ -790,6 +790,18 @@ pub(crate) fn build_batch_from_indices(
790790
right_indices: UInt32Array,
791791
column_indices: &[ColumnIndex],
792792
) -> Result<RecordBatch> {
793+
if schema.fields().is_empty() {
794+
let options = RecordBatchOptions::new()
795+
.with_match_field_names(true)
796+
.with_row_count(Some(left_indices.len()));
797+
798+
return Ok(RecordBatch::try_new_with_options(
799+
Arc::new(schema.clone()),
800+
vec![],
801+
&options,
802+
)?);
803+
}
804+
793805
// build the columns of the new [RecordBatch]:
794806
// 1. pick whether the column is from the left or right
795807
// 2. based on the pick, `take` items from the different RecordBatches

datafusion/core/tests/sqllogictests/test_files/join.slt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ CREATE TABLE grades(grade INT, min INT, max INT) AS VALUES
3535
(4, 56, 79),
3636
(5, 80, 100);
3737

38+
statement ok
39+
CREATE TABLE test1(a int, b int) as select 1 as a, 2 as b;
40+
41+
statement ok
42+
CREATE TABLE test2(a int, b int) as select 1 as a, 2 as b;
43+
3844
# Regression test: https://github.com/apache/arrow-datafusion/issues/4844
3945
query TII
4046
SELECT s.*, g.grade FROM students s join grades g on s.mark between g.min and g.max WHERE grade > 2 ORDER BY s.mark DESC
@@ -81,3 +87,9 @@ ON (
8187
----
8288
11 a 1
8389
44 d 4
90+
91+
# issue: https://github.com/apache/arrow-datafusion/issues/5382
92+
query IIII rowsort
93+
SELECT * FROM test2 FULL JOIN test1 ON true;
94+
----
95+
1 2 1 2

0 commit comments

Comments
 (0)