Apache DataFusion SQL Query Engine
Fix Internal Error for an INNER JOIN query (#11578)
Xin Lif9ee64a0ec98
diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rsindex e3620501d9..452c05be34 100644--- a/datafusion/expr/src/expr.rs+++ b/datafusion/expr/src/expr.rs@@ -1340,6 +1340,9 @@ impl Expr {////// returns `None` if the expression is not a `Column`///+ /// Note: None may be returned for expressions that are not `Column` but+ /// are convertible to `Column` such as `Cast` expressions.+ ////// Example/// ```/// # use datafusion_common::Column;@@ -1358,6 +1361,23 @@ impl Expr {}}+ /// Returns the inner `Column` if any. This is a specialized version of+ /// [`Self::try_as_col`] that take Cast expressions into account when the+ /// expression is as on condition for joins.+ ///+ /// Called this method when you are sure that the expression is a `Column`+ /// or a `Cast` expression that wraps a `Column`.+ pub fn get_as_join_column(&self) -> Option<&Column> {+ match self {+ Expr::Column(c) => Some(c),+ Expr::Cast(Cast { expr, .. }) => match &**expr {+ Expr::Column(c) => Some(c),+ _ => None,+ },+ _ => None,+ }+ }+/// Return all referenced columns of this expression.#[deprecated(since = "40.0.0", note = "use Expr::column_refs instead")]pub fn to_columns(&self) -> Result<HashSet<Column>> {diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rsindex 48fa6270b2..d4fe233cac 100644--- a/datafusion/expr/src/logical_plan/plan.rs+++ b/datafusion/expr/src/logical_plan/plan.rs@@ -496,18 +496,18 @@ impl LogicalPlan {// The join keys in using-join must be columns.let columns =on.iter().try_fold(HashSet::new(), |mut accumu, (l, r)| {- let Some(l) = l.try_as_col().cloned() else {+ let Some(l) = l.get_as_join_column() else {return internal_err!("Invalid join key. Expected column, found {l:?}");};- let Some(r) = r.try_as_col().cloned() else {+ let Some(r) = r.get_as_join_column() else {return internal_err!("Invalid join key. Expected column, found {r:?}");};- accumu.insert(l);- accumu.insert(r);+ accumu.insert(l.to_owned());+ accumu.insert(r.to_owned());Result::<_, DataFusionError>::Ok(accumu)})?;using_columns.push(columns);diff --git a/datafusion/sqllogictest/test_files/join.slt b/datafusion/sqllogictest/test_files/join.sltindex efebba1779..4f01d2b2c7 100644--- a/datafusion/sqllogictest/test_files/join.slt+++ b/datafusion/sqllogictest/test_files/join.slt@@ -1054,3 +1054,45 @@ DROP TABLE t1;statement okDROP TABLE t2;++# Join Using Issue with Cast Expr+# Found issue: https://github.com/apache/datafusion/issues/11412++statement ok+/*DML*/CREATE TABLE t60(v0 BIGINT, v1 BIGINT, v2 BOOLEAN, v3 BOOLEAN);++statement ok+/*DML*/CREATE TABLE t0(v0 DOUBLE, v1 BIGINT);++statement ok+/*DML*/CREATE TABLE t1(v0 DOUBLE);++query I+SELECT COUNT(*)+FROM t1+NATURAL JOIN t60+INNER JOIN t0+ON t60.v1 = t0.v0+AND t0.v1 > t60.v1;+----+0++query I+SELECT COUNT(*)+FROM t1+JOIN t60+USING (v0)+INNER JOIN t0+ON t60.v1 = t0.v0+AND t0.v1 > t60.v1;+----+0++statement ok+DROP TABLE t60;++statement ok+DROP TABLE t0;++statement ok+DROP TABLE t1;\ No newline at end of file