From 6b0570bb8f48b0ccd605d0b5156cc98749c9c913 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Sun, 17 Nov 2024 12:37:09 +0100 Subject: [PATCH] Fix invalid swap for LeftMark nested loops join (#13426) --- .../src/physical_optimizer/join_selection.rs | 4 +- datafusion/sqllogictest/test_files/join.slt | 86 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/datafusion/core/src/physical_optimizer/join_selection.rs b/datafusion/core/src/physical_optimizer/join_selection.rs index fdb2920300dd..511aaacf3ef1 100644 --- a/datafusion/core/src/physical_optimizer/join_selection.rs +++ b/datafusion/core/src/physical_optimizer/join_selection.rs @@ -494,7 +494,9 @@ fn statistical_join_selection_subrule( } else if let Some(nl_join) = plan.as_any().downcast_ref::() { let left = nl_join.left(); let right = nl_join.right(); - if should_swap_join_order(&**left, &**right)? { + if supports_swap(*nl_join.join_type()) + && should_swap_join_order(&**left, &**right)? + { swap_nl_join(nl_join).map(Some)? } else { None diff --git a/datafusion/sqllogictest/test_files/join.slt b/datafusion/sqllogictest/test_files/join.slt index 39f903a58714..1feacc5ebe53 100644 --- a/datafusion/sqllogictest/test_files/join.slt +++ b/datafusion/sqllogictest/test_files/join.slt @@ -1226,3 +1226,89 @@ select t1.v1 from t1 join t1 using(v1) cross join (select struct('foo' as v1) as statement ok drop table t1; + + +statement ok +CREATE TABLE t1(a INTEGER, b INTEGER, c INTEGER, d INTEGER, e INTEGER); + +statement ok +INSERT INTO t1(e,c,b,d,a) VALUES(103,102,100,101,104); + +statement ok +INSERT INTO t1(a,c,d,e,b) VALUES(107,106,108,109,105); + +statement ok +INSERT INTO t1(d,c,e,a,b) VALUES(116,119,117,115,118); + +statement ok +INSERT INTO t1(c,d,b,e,a) VALUES(123,122,124,120,121); + +statement ok +INSERT INTO t1(b,a,e,d,c) VALUES(145,149,146,148,147); + +statement ok +INSERT INTO t1(b,c,a,d,e) VALUES(151,150,153,154,152); + +statement ok +INSERT INTO t1(c,b,a,d,e) VALUES(161,160,163,164,162); + +statement ok +INSERT INTO t1(b,d,a,e,c) VALUES(167,169,168,165,166); + +statement ok +INSERT INTO t1(d,b,c,e,a) VALUES(171,170,172,173,174); + +statement ok +INSERT INTO t1(e,c,a,d,b) VALUES(177,176,179,178,175); + +statement ok +INSERT INTO t1(b,e,a,d,c) VALUES(181,180,182,183,184); + +statement ok +INSERT INTO t1(c,e,a,b,d) VALUES(208,209,205,206,207); + +statement ok +INSERT INTO t1(c,e,a,d,b) VALUES(214,210,213,212,211); + +statement ok +INSERT INTO t1(b,c,a,d,e) VALUES(218,215,216,217,219); + +statement ok +INSERT INTO t1(e,c,b,a,d) VALUES(242,244,240,243,241); + +statement ok +INSERT INTO t1(e,d,c,b,a) VALUES(246,248,247,249,245); + +# Regression test for https://github.com/apache/datafusion/issues/13425 +query IIIIII +SELECT a+b*2, + a+b*2+c*3+d*4, + CASE WHEN ac OR e