-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow non-equijoin filters in join condition #660
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6890d23
Allow non-equijoin filters in join condition
Dandandan 71b42ba
Revert change to query
Dandandan d8e07be
Fix, only do for inner join
Dandandan ecc6d9a
Add test
Dandandan 7e6b7c2
docs update
Dandandan 878a460
Update test name
Dandandan 503a137
Add negative test
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 hidden or 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 |
|---|---|---|
|
|
@@ -368,15 +368,34 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| // parse ON expression | ||
| let expr = self.sql_to_rex(sql_expr, &join_schema)?; | ||
|
|
||
| // expression that didn't match equi-join pattern | ||
| let mut filter = vec![]; | ||
|
|
||
| // extract join keys | ||
| extract_join_keys(&expr, &mut keys)?; | ||
| extract_join_keys(&expr, &mut keys, &mut filter); | ||
|
|
||
| let (left_keys, right_keys): (Vec<Column>, Vec<Column>) = | ||
| keys.into_iter().unzip(); | ||
| // return the logical plan representing the join | ||
| LogicalPlanBuilder::from(left) | ||
| .join(right, join_type, left_keys, right_keys)? | ||
| let join = LogicalPlanBuilder::from(left) | ||
| .join(right, join_type, left_keys, right_keys)?; | ||
|
|
||
| if filter.is_empty() { | ||
| join.build() | ||
| } else if join_type == JoinType::Inner { | ||
| join.filter( | ||
|
Contributor
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. this is a fancy way of creating an |
||
| filter | ||
| .iter() | ||
| .skip(1) | ||
| .fold(filter[0].clone(), |acc, e| acc.and(e.clone())), | ||
| )? | ||
| .build() | ||
| } else { | ||
| Err(DataFusionError::NotImplemented(format!( | ||
| "Unsupported expressions in {:?} JOIN: {:?}", | ||
| join_type, filter | ||
| ))) | ||
| } | ||
| } | ||
| JoinConstraint::Using(idents) => { | ||
| let keys: Vec<Column> = idents | ||
|
|
@@ -1549,39 +1568,41 @@ fn remove_join_expressions( | |
| } | ||
| } | ||
|
|
||
| /// Parse equijoin ON condition which could be a single Eq or multiple conjunctive Eqs | ||
| /// Extracts equijoin ON condition be a single Eq or multiple conjunctive Eqs | ||
| /// Filters matching this pattern are added to `accum` | ||
| /// Filters that don't match this pattern are added to `accum_filter` | ||
| /// Examples: | ||
| /// | ||
| /// Examples | ||
| /// foo = bar => accum=[(foo, bar)] accum_filter=[] | ||
| /// foo = bar AND bar = baz => accum=[(foo, bar), (bar, baz)] accum_filter=[] | ||
| /// foo = bar AND baz > 1 => accum=[(foo, bar)] accum_filter=[baz > 1] | ||
| /// | ||
| /// foo = bar | ||
| /// foo = bar AND bar = baz AND ... | ||
| /// | ||
| fn extract_join_keys(expr: &Expr, accum: &mut Vec<(Column, Column)>) -> Result<()> { | ||
| fn extract_join_keys( | ||
Dandandan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expr: &Expr, | ||
| accum: &mut Vec<(Column, Column)>, | ||
| accum_filter: &mut Vec<Expr>, | ||
| ) { | ||
| match expr { | ||
| Expr::BinaryExpr { left, op, right } => match op { | ||
| Operator::Eq => match (left.as_ref(), right.as_ref()) { | ||
| (Expr::Column(l), Expr::Column(r)) => { | ||
| accum.push((l.clone(), r.clone())); | ||
| Ok(()) | ||
| } | ||
| other => Err(DataFusionError::SQL(ParserError(format!( | ||
| "Unsupported expression '{:?}' in JOIN condition", | ||
| other | ||
| )))), | ||
| _other => { | ||
| accum_filter.push(expr.clone()); | ||
| } | ||
| }, | ||
| Operator::And => { | ||
| extract_join_keys(left, accum)?; | ||
| extract_join_keys(right, accum) | ||
| extract_join_keys(left, accum, accum_filter); | ||
| extract_join_keys(right, accum, accum_filter); | ||
| } | ||
| _other => { | ||
| accum_filter.push(expr.clone()); | ||
| } | ||
| other => Err(DataFusionError::SQL(ParserError(format!( | ||
| "Unsupported expression '{:?}' in JOIN condition", | ||
| other | ||
| )))), | ||
| }, | ||
| other => Err(DataFusionError::SQL(ParserError(format!( | ||
| "Unsupported expression '{:?}' in JOIN condition", | ||
| other | ||
| )))), | ||
| _other => { | ||
| accum_filter.push(expr.clone()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2701,6 +2722,20 @@ mod tests { | |
| quick_test(sql, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn equijoin_unsupported_expression() { | ||
| let sql = "SELECT id, order_id \ | ||
| FROM person \ | ||
| JOIN orders \ | ||
| ON id = customer_id AND order_id > 1 "; | ||
| let expected = "Projection: #person.id, #orders.order_id\ | ||
| \n Filter: #orders.order_id Gt Int64(1)\ | ||
| \n Join: #person.id = #orders.customer_id\ | ||
| \n TableScan: person projection=None\ | ||
| \n TableScan: orders projection=None"; | ||
| quick_test(sql, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn join_with_table_name() { | ||
| let sql = "SELECT id, order_id \ | ||
|
|
||
This file contains hidden or 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.
👍 this is the right behavior.