Skip to content

Commit

Permalink
fix: make UnKnownColumns not equal to others physical exprs (#11536)
Browse files Browse the repository at this point in the history
* fix: fall back to `UnionExec` if can't interleave

* alternative fix

* check interleavable in with_new_children

* link to pr
  • Loading branch information
jonahgao committed Jul 19, 2024
1 parent 9189a1a commit ebe61ba
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
10 changes: 4 additions & 6 deletions datafusion/physical-expr/src/expressions/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
}
}

Expand Down
6 changes: 6 additions & 0 deletions datafusion/physical-plan/src/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?))
}

Expand Down
45 changes: 45 additions & 0 deletions datafusion/sqllogictest/test_files/union.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit ebe61ba

Please sign in to comment.