Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1915,6 +1915,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 @@ -1983,6 +1987,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