Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6890d2371b42bad8e07beecc6d9a7e6b7c2878a460503a137File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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 \ | ||
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.