Skip to content

Apache DataFusion SQL Query Engine

fix: make `UnKnownColumn`s not equal to others physical exprs (#11536)

Jonah Gaoebe61bae2aed
diff --git a/datafusion/physical-expr/src/expressions/column.rs b/datafusion/physical-expr/src/expressions/column.rs
index 38779c5460..ab43201ceb 100644
--- a/datafusion/physical-expr/src/expressions/column.rs
+++ b/datafusion/physical-expr/src/expressions/column.rs
@@ -21,7 +21,6 @@ use std::any::Any;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
-use crate::physical_expr::down_cast_any_ref;
use crate::PhysicalExpr;
use arrow::{
@@ -95,11 +94,10 @@ impl PhysicalExpr for UnKnownColumn {
}
impl PartialEq<dyn Any> for UnKnownColumn {
- fn eq(&self, other: &dyn Any) -> bool {
- down_cast_any_ref(other)
- .downcast_ref::<Self>()
- .map(|x| self == x)
- .unwrap_or(false)
+ fn eq(&self, _other: &dyn Any) -> bool {
+ // UnknownColumn is not a valid expression, so it should not be equal to any other expression.
+ // See https://github.com/apache/datafusion/pull/11536
+ false
}
}
diff --git a/datafusion/physical-plan/src/union.rs b/datafusion/physical-plan/src/union.rs
index b39c6aee82..24c80048ab 100644
--- a/datafusion/physical-plan/src/union.rs
+++ b/datafusion/physical-plan/src/union.rs
@@ -431,6 +431,12 @@ impl ExecutionPlan for InterleaveExec {
self: Arc<Self>,
children: Vec<Arc<dyn ExecutionPlan>>,
) -> Result<Arc<dyn ExecutionPlan>> {
+ // New children are no longer interleavable, which might be a bug of optimization rewrite.
+ if !can_interleave(children.iter()) {
+ return internal_err!(
+ "Can not create InterleaveExec: new children can not be interleaved"
+ );
+ }
Ok(Arc::new(InterleaveExec::try_new(children)?))
}
diff --git a/datafusion/sqllogictest/test_files/union.slt b/datafusion/sqllogictest/test_files/union.slt
index 31b16f975e..2dc8385bf1 100644
--- a/datafusion/sqllogictest/test_files/union.slt
+++ b/datafusion/sqllogictest/test_files/union.slt
@@ -602,3 +602,48 @@ physical_plan
09)--ProjectionExec: expr=[1 as count, MAX(Int64(10))@0 as n]
10)----AggregateExec: mode=Single, gby=[], aggr=[MAX(Int64(10))]
11)------PlaceholderRowExec
+
+
+# Test issue: https://github.com/apache/datafusion/issues/11409
+statement ok
+CREATE TABLE t1(v0 BIGINT, v1 BIGINT, v2 BIGINT, v3 BOOLEAN);
+
+statement ok
+CREATE TABLE t2(v0 DOUBLE);
+
+query I
+INSERT INTO t1(v0, v2, v1) VALUES (-1229445667, -342312412, -1507138076);
+----
+1
+
+query I
+INSERT INTO t1(v0, v1) VALUES (1541512604, -1229445667);
+----
+1
+
+query I
+INSERT INTO t1(v1, v3, v0, v2) VALUES (-1020641465, false, -1493773377, 1751276473);
+----
+1
+
+query I
+INSERT INTO t1(v3) VALUES (true), (true), (false);
+----
+3
+
+query I
+INSERT INTO t2(v0) VALUES (0.28014577292925047);
+----
+1
+
+query II
+SELECT t1.v2, t1.v0 FROM t2 NATURAL JOIN t1
+ UNION ALL
+SELECT t1.v2, t1.v0 FROM t2 NATURAL JOIN t1 WHERE (t1.v2 IS NULL);
+----
+
+statement ok
+DROP TABLE t1;
+
+statement ok
+DROP TABLE t2;