Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
147 changes: 146 additions & 1 deletion datafusion/physical-plan/src/joins/sort_merge_join/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ mod tests {

use arrow::array::{
builder::{BooleanBuilder, UInt64Builder},
BooleanArray, Date32Array, Date64Array, Int32Array, RecordBatch, UInt64Array,
BinaryArray, BooleanArray, Date32Array, Date64Array, FixedSizeBinaryArray,
Int32Array, RecordBatch, UInt64Array,
};
use arrow::compute::{concat_batches, filter_record_batch, SortOptions};
use arrow::datatypes::{DataType, Field, Schema};
Expand Down Expand Up @@ -685,6 +686,56 @@ mod tests {
TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()
}

fn build_binary_table(
a: (&str, &Vec<&[u8]>),
b: (&str, &Vec<i32>),
c: (&str, &Vec<i32>),
) -> Arc<dyn ExecutionPlan> {
let schema = Schema::new(vec![
Field::new(a.0, DataType::Binary, false),
Field::new(b.0, DataType::Int32, false),
Field::new(c.0, DataType::Int32, false),
]);

let batch = RecordBatch::try_new(
Arc::new(schema),
vec![
Arc::new(BinaryArray::from(a.1.clone())),
Arc::new(Int32Array::from(b.1.clone())),
Arc::new(Int32Array::from(c.1.clone())),
],
)
.unwrap();

let schema = batch.schema();
TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()
}

fn build_fixed_size_binary_table(
a: (&str, &Vec<&[u8]>),
b: (&str, &Vec<i32>),
c: (&str, &Vec<i32>),
) -> Arc<dyn ExecutionPlan> {
let schema = Schema::new(vec![
Field::new(a.0, DataType::FixedSizeBinary(3), false),
Field::new(b.0, DataType::Int32, false),
Field::new(c.0, DataType::Int32, false),
]);

let batch = RecordBatch::try_new(
Arc::new(schema),
vec![
Arc::new(FixedSizeBinaryArray::from(a.1.clone())),
Arc::new(Int32Array::from(b.1.clone())),
Arc::new(Int32Array::from(c.1.clone())),
],
)
.unwrap();

let schema = batch.schema();
TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()
}

/// returns a table with 3 columns of i32 in memory
pub fn build_table_i32_nullable(
a: (&str, &Vec<Option<i32>>),
Expand Down Expand Up @@ -1923,6 +1974,100 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn join_binary() -> Result<()> {
let left = build_binary_table(
(
"a1",
&vec![
&[0xc0, 0xff, 0xee],
&[0xde, 0xca, 0xde],
&[0xfa, 0xca, 0xde],
],
),
("b1", &vec![5, 10, 15]), // this has a repetition
("c1", &vec![7, 8, 9]),
);
let right = build_binary_table(
(
"a1",
&vec![
&[0xc0, 0xff, 0xee],
&[0xde, 0xca, 0xde],
&[0xfa, 0xca, 0xde],
],
),
("b2", &vec![105, 110, 115]),
("c2", &vec![70, 80, 90]),
);

let on = vec![(
Arc::new(Column::new_with_schema("a1", &left.schema())?) as _,
Arc::new(Column::new_with_schema("a1", &right.schema())?) as _,
)];

let (_, batches) = join_collect(left, right, on, Inner).await?;

// The output order is important as SMJ preserves sortedness
assert_snapshot!(batches_to_string(&batches), @r#"
+--------+----+----+--------+-----+----+
| a1 | b1 | c1 | a1 | b2 | c2 |
+--------+----+----+--------+-----+----+
| c0ffee | 5 | 7 | c0ffee | 105 | 70 |
| decade | 10 | 8 | decade | 110 | 80 |
| facade | 15 | 9 | facade | 115 | 90 |
+--------+----+----+--------+-----+----+
"#);
Ok(())
}

#[tokio::test]
async fn join_fixed_size_binary() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should include large binary test as well? 🤔 I noticed the amount of unit tests were getting out of hand, maybe I'll look into separating the tests into the other SMJ files.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be great if you could do so

let left = build_fixed_size_binary_table(
(
"a1",
&vec![
&[0xc0, 0xff, 0xee],
&[0xde, 0xca, 0xde],
&[0xfa, 0xca, 0xde],
],
),
("b1", &vec![5, 10, 15]), // this has a repetition
("c1", &vec![7, 8, 9]),
);
let right = build_fixed_size_binary_table(
(
"a1",
&vec![
&[0xc0, 0xff, 0xee],
&[0xde, 0xca, 0xde],
&[0xfa, 0xca, 0xde],
],
),
("b2", &vec![105, 110, 115]),
("c2", &vec![70, 80, 90]),
);

let on = vec![(
Arc::new(Column::new_with_schema("a1", &left.schema())?) as _,
Arc::new(Column::new_with_schema("a1", &right.schema())?) as _,
)];

let (_, batches) = join_collect(left, right, on, Inner).await?;

// The output order is important as SMJ preserves sortedness
assert_snapshot!(batches_to_string(&batches), @r#"
+--------+----+----+--------+-----+----+
| a1 | b1 | c1 | a1 | b2 | c2 |
+--------+----+----+--------+-----+----+
| c0ffee | 5 | 7 | c0ffee | 105 | 70 |
| decade | 10 | 8 | decade | 110 | 80 |
| facade | 15 | 9 | facade | 115 | 90 |
+--------+----+----+--------+-----+----+
"#);
Ok(())
}

#[tokio::test]
async fn join_left_sort_order() -> Result<()> {
let left = build_table(
Expand Down
8 changes: 8 additions & 0 deletions datafusion/physical-plan/src/joins/sort_merge_join/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,10 @@ fn compare_join_arrays(
DataType::Utf8 => compare_value!(StringArray),
DataType::Utf8View => compare_value!(StringViewArray),
DataType::LargeUtf8 => compare_value!(LargeStringArray),
DataType::Binary => compare_value!(BinaryArray),
DataType::BinaryView => compare_value!(BinaryViewArray),
DataType::FixedSizeBinary(_) => compare_value!(FixedSizeBinaryArray),
DataType::LargeBinary => compare_value!(LargeBinaryArray),
DataType::Decimal128(..) => compare_value!(Decimal128Array),
DataType::Timestamp(time_unit, None) => match time_unit {
TimeUnit::Second => compare_value!(TimestampSecondArray),
Expand Down Expand Up @@ -1986,6 +1990,10 @@ fn is_join_arrays_equal(
DataType::Utf8 => compare_value!(StringArray),
DataType::Utf8View => compare_value!(StringViewArray),
DataType::LargeUtf8 => compare_value!(LargeStringArray),
DataType::Binary => compare_value!(BinaryArray),
DataType::BinaryView => compare_value!(BinaryViewArray),
DataType::FixedSizeBinary(_) => compare_value!(FixedSizeBinaryArray),
DataType::LargeBinary => compare_value!(LargeBinaryArray),
DataType::Decimal128(..) => compare_value!(Decimal128Array),
DataType::Timestamp(time_unit, None) => match time_unit {
TimeUnit::Second => compare_value!(TimestampSecondArray),
Expand Down
58 changes: 55 additions & 3 deletions datafusion/sqllogictest/test_files/sort_merge_join.slt
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,61 @@ t2 as (
11 14
12 15

# return sql params back to default values
statement ok
set datafusion.optimizer.prefer_hash_join = true;
set datafusion.execution.batch_size = 8192;


######
## Tests for Binary, LargeBinary, BinaryView, FixedSizeBinary join keys
######
statement ok
set datafusion.execution.batch_size = 8192;
create table t1(x varchar, id1 int) as values ('aa', 1), ('bb', 2), ('aa', 3), (null, 4), ('ee', 5);

statement ok
create table t2(y varchar, id2 int) as values ('ee', 10), ('bb', 20), ('cc', 30), ('cc', 40), (null, 50);

# Binary join keys
query ?I?I
with t1 as (select arrow_cast(x, 'Binary') as x, id1 from t1),
t2 as (select arrow_cast(y, 'Binary') as y, id2 from t2)
select * from t1 join t2 on t1.x = t2.y order by id1, id2
----
6262 2 6262 20
6565 5 6565 10

# LargeBinary join keys
query ?I?I
with t1 as (select arrow_cast(x, 'LargeBinary') as x, id1 from t1),
t2 as (select arrow_cast(y, 'LargeBinary') as y, id2 from t2)
select * from t1 join t2 on t1.x = t2.y order by id1, id2
----
6262 2 6262 20
6565 5 6565 10

# BinaryView join keys
query ?I?I
with t1 as (select arrow_cast(x, 'BinaryView') as x, id1 from t1),
t2 as (select arrow_cast(y, 'BinaryView') as y, id2 from t2)
select * from t1 join t2 on t1.x = t2.y order by id1, id2
----
6262 2 6262 20
6565 5 6565 10

# FixedSizeBinary join keys
query ?I?I
with t1 as (select arrow_cast(arrow_cast(x, 'Binary'), 'FixedSizeBinary(2)') as x, id1 from t1),
t2 as (select arrow_cast(arrow_cast(y, 'Binary'), 'FixedSizeBinary(2)') as y, id2 from t2)
select * from t1 join t2 on t1.x = t2.y order by id1, id2
----
6262 2 6262 20
6565 5 6565 10

statement ok
drop table t1;

statement ok
drop table t2;

# return sql params back to default values
statement ok
set datafusion.optimizer.prefer_hash_join = true;