Apache DataFusion SQL Query Engine
Fix join without constraints (#2240)
Daniël Heresaa609f6dc8f8
diff --git a/datafusion/core/src/physical_plan/hash_join.rs b/datafusion/core/src/physical_plan/hash_join.rsindex 44b8cc97d9..488222accc 100644--- a/datafusion/core/src/physical_plan/hash_join.rs+++ b/datafusion/core/src/physical_plan/hash_join.rs@@ -189,6 +189,12 @@ impl HashJoinExec {) -> Result<Self> {let left_schema = left.schema();let right_schema = right.schema();+ if on.is_empty() {+ return Err(DataFusionError::Plan(+ "On constraints in HashJoinExec should be non-empty".to_string(),+ ));+ }+check_join_is_valid(&left_schema, &right_schema, &on)?;let (schema, column_indices) =diff --git a/datafusion/core/src/sql/planner.rs b/datafusion/core/src/sql/planner.rsindex 536682cf08..cff38d47b4 100644--- a/datafusion/core/src/sql/planner.rs+++ b/datafusion/core/src/sql/planner.rs@@ -524,7 +524,13 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {keys.into_iter().unzip();// return the logical plan representing the join- if filter.is_empty() {+ if left_keys.is_empty() {+ // When we don't have join keys, use cross join+ let join = LogicalPlanBuilder::from(left).cross_join(&right)?;++ join.filter(filter.into_iter().reduce(Expr::and).unwrap())?+ .build()+ } else if filter.is_empty() {let join = LogicalPlanBuilder::from(left).join(&right,join_type,@@ -537,13 +543,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {join_type,(left_keys, right_keys),)?;- let join_filter_init = filter.remove(0);- join.filter(- filter- .into_iter()- .fold(join_filter_init, |acc, e| acc.and(e)),- )?- .build()+ join.filter(filter.into_iter().reduce(Expr::and).unwrap())?+ .build()}// Left join with all non-equijoin expressions from the right// l left join rdiff --git a/datafusion/core/tests/sql/joins.rs b/datafusion/core/tests/sql/joins.rsindex d8f5d4ad8a..4859041579 100644--- a/datafusion/core/tests/sql/joins.rs+++ b/datafusion/core/tests/sql/joins.rs@@ -293,6 +293,24 @@ async fn left_join_not_null_filter_on_join_column() -> Result<()> {Ok(())}+#[tokio::test]+async fn self_join_non_equijoin() -> Result<()> {+ let ctx = create_join_context_with_nulls()?;+ let sql =+ "SELECT x.t1_id, y.t1_id FROM t1 x JOIN t1 y ON x.t1_id = 11 AND y.t1_id = 44";+ let expected = vec![+ "+-------+-------+",+ "| t1_id | t1_id |",+ "+-------+-------+",+ "| 11 | 44 |",+ "+-------+-------+",+ ];++ let actual = execute_to_batches(&ctx, sql).await;+ assert_batches_eq!(expected, &actual);+ Ok(())+}+#[tokio::test]async fn right_join_null_filter() -> Result<()> {let ctx = create_join_context_with_nulls()?;