-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Implement right semi join and support in HashBuildProbeorder #3958
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
08f8755
Implement right semi join
Dandandan 084833a
Change error a bit
Dandandan 9d60e51
protobuf
Dandandan c531151
protobuf
Dandandan 6e230a8
protobuf
Dandandan 94461b5
Change column name to b2
Dandandan edad01f
Rename everything
Dandandan 1117dd6
Rename & fmt
Dandandan 03dc95a
Change display to leftanti
Dandandan ee86743
Fix last expected plan
Dandandan b3857f1
Merge remote-tracking branch 'upstream/master' into right_semi_join
Dandandan 85201d8
Commit generated file
Dandandan 6daeb85
Merge remote-tracking branch 'upstream/master' into right_semi_join
Dandandan 866c3dc
Merge remote-tracking branch 'upstream/master' into right_semi_join
Dandandan 7963202
generated
Dandandan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -765,6 +765,54 @@ fn build_join_indexes( | |
PrimitiveArray::<UInt32Type>::from(right), | ||
)) | ||
} | ||
JoinType::RightSemi => { | ||
let mut left_indices = UInt64BufferBuilder::new(0); | ||
let mut right_indices = UInt32BufferBuilder::new(0); | ||
|
||
// Visit all of the right rows | ||
for (row, hash_value) in hash_values.iter().enumerate() { | ||
// Get the hash and find it in the build index | ||
|
||
// For every item on the left and right we check if it matches | ||
// This possibly contains rows with hash collisions, | ||
// So we have to check here whether rows are equal or not | ||
// We only produce one row if there is a match | ||
if let Some((_, indices)) = | ||
left.0.get(*hash_value, |(hash, _)| *hash_value == *hash) | ||
{ | ||
for &i in indices { | ||
// Check hash collisions | ||
if equal_rows( | ||
i as usize, | ||
row, | ||
&left_join_values, | ||
&keys_values, | ||
*null_equals_null, | ||
)? { | ||
left_indices.append(i); | ||
right_indices.append(row as u32); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
let left = ArrayData::builder(DataType::UInt64) | ||
.len(left_indices.len()) | ||
.add_buffer(left_indices.finish()) | ||
.build() | ||
.unwrap(); | ||
let right = ArrayData::builder(DataType::UInt32) | ||
.len(right_indices.len()) | ||
.add_buffer(right_indices.finish()) | ||
.build() | ||
.unwrap(); | ||
|
||
Ok(( | ||
PrimitiveArray::<UInt64Type>::from(left), | ||
PrimitiveArray::<UInt32Type>::from(right), | ||
)) | ||
} | ||
JoinType::Left => { | ||
let mut left_indices = UInt64Builder::with_capacity(0); | ||
let mut right_indices = UInt32Builder::with_capacity(0); | ||
|
@@ -853,7 +901,11 @@ fn apply_join_filter( | |
)?; | ||
|
||
match join_type { | ||
JoinType::Inner | JoinType::Left | JoinType::Anti | JoinType::Semi => { | ||
JoinType::Inner | ||
| JoinType::Left | ||
| JoinType::Anti | ||
| JoinType::Semi | ||
| JoinType::RightSemi => { | ||
// For both INNER and LEFT joins, input arrays contains only indices for matched data. | ||
// Due to this fact it's correct to simply apply filter to intermediate batch and return | ||
// indices for left/right rows satisfying filter predicate | ||
|
@@ -1287,7 +1339,9 @@ impl HashJoinStream { | |
|
||
buffer | ||
} | ||
JoinType::Inner | JoinType::Right => BooleanBufferBuilder::new(0), | ||
JoinType::Inner | JoinType::Right | JoinType::RightSemi => { | ||
BooleanBufferBuilder::new(0) | ||
} | ||
} | ||
}); | ||
|
||
|
@@ -1324,7 +1378,7 @@ impl HashJoinStream { | |
visited_left_side.set_bit(x as usize, true); | ||
}); | ||
} | ||
JoinType::Inner | JoinType::Right => {} | ||
JoinType::Inner | JoinType::Right | JoinType::RightSemi => {} | ||
} | ||
} | ||
Some(result.map(|x| x.0)) | ||
|
@@ -1361,6 +1415,7 @@ impl HashJoinStream { | |
JoinType::Left | ||
| JoinType::Full | ||
| JoinType::Semi | ||
| JoinType::RightSemi | ||
| JoinType::Anti | ||
| JoinType::Inner | ||
| JoinType::Right => {} | ||
|
@@ -2116,6 +2171,48 @@ mod tests { | |
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn join_right_semi() -> Result<()> { | ||
let session_ctx = SessionContext::new(); | ||
let task_ctx = session_ctx.task_ctx(); | ||
let left = build_table( | ||
("a2", &vec![10, 20, 30, 40]), | ||
("b1", &vec![4, 5, 6, 5]), // 5 is double on the left | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. recommend calling this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense |
||
("c2", &vec![70, 80, 90, 100]), | ||
); | ||
let right = build_table( | ||
("a1", &vec![1, 2, 2, 3]), | ||
("b1", &vec![4, 5, 5, 7]), // 7 does not exist on the left | ||
Dandandan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
("c1", &vec![7, 8, 8, 9]), | ||
); | ||
|
||
let on = vec![( | ||
Column::new_with_schema("b1", &left.schema())?, | ||
Column::new_with_schema("b1", &right.schema())?, | ||
)]; | ||
|
||
let join = join(left, right, on, &JoinType::RightSemi, false)?; | ||
|
||
let columns = columns(&join.schema()); | ||
assert_eq!(columns, vec!["a1", "b1", "c1"]); | ||
|
||
let stream = join.execute(0, task_ctx)?; | ||
let batches = common::collect(stream).await?; | ||
|
||
let expected = vec![ | ||
"+----+----+----+", | ||
"| a1 | b1 | c1 |", | ||
"+----+----+----+", | ||
"| 1 | 4 | 7 |", | ||
"| 2 | 5 | 8 |", | ||
"| 2 | 5 | 8 |", | ||
"+----+----+----+", | ||
]; | ||
assert_batches_sorted_eq!(expected, &batches); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn join_anti() -> Result<()> { | ||
let session_ctx = SessionContext::new(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to change
JoinType::Semi
toJoinType::LeftSemi
explictly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might be better for clarity, but also a breaking change.
I think we also should change Anti to LeftAnti if we want to do this.
FYI @alamb @andygrove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add the new variants and mark the old ones as deprecated?