From fe9f21ec2cb10fd4cc9255d393f0257fd30ba09f Mon Sep 17 00:00:00 2001 From: ygf11 Date: Tue, 10 Jan 2023 08:42:47 -0500 Subject: [PATCH 1/2] Skip EliminateCrossJoin rule when meet non-empty join filter --- .../tests/sqllogictests/test_files/join.slt | 44 +++++++++++++++++++ .../optimizer/src/eliminate_cross_join.rs | 6 +++ 2 files changed, 50 insertions(+) create mode 100644 datafusion/core/tests/sqllogictests/test_files/join.slt diff --git a/datafusion/core/tests/sqllogictests/test_files/join.slt b/datafusion/core/tests/sqllogictests/test_files/join.slt new file mode 100644 index 000000000000..8c120a91118b --- /dev/null +++ b/datafusion/core/tests/sqllogictests/test_files/join.slt @@ -0,0 +1,44 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +########## +## JOIN Tests +########## + +statement ok +CREATE TABLE students(name TEXT, mark INT) AS VALUES +('Stuart', 28), +('Amina', 89), +('Christen', 50), +('Salma', 77), +('Samantha', 21); + +statement ok +CREATE TABLE grades(grade INT, min INT, max INT) AS VALUES +(1, 0, 14), +(2, 15, 35), +(3, 36, 55), +(4, 56, 79), +(5, 80, 100); + +# Regression test: https://github.com/apache/arrow-datafusion/issues/4844 +query I +SELECT s.*, g.grade FROM students s join grades g on s.mark between g.min and g.max WHERE grade > 2 ORDER BY s.mark DESC +---- +Amina 89 5 +Salma 77 4 +Christen 50 3 \ No newline at end of file diff --git a/datafusion/optimizer/src/eliminate_cross_join.rs b/datafusion/optimizer/src/eliminate_cross_join.rs index 458eab95905a..756ec24ab014 100644 --- a/datafusion/optimizer/src/eliminate_cross_join.rs +++ b/datafusion/optimizer/src/eliminate_cross_join.rs @@ -62,6 +62,12 @@ impl OptimizerRule for EliminateCrossJoin { let mut all_inputs: Vec = vec![]; match &input { LogicalPlan::Join(join) if (join.join_type == JoinType::Inner) => { + // The filter of inner join will lost, skip this rule. + // issue: https://github.com/apache/arrow-datafusion/issues/4844 + if join.filter.is_some() { + return Ok(None); + } + flatten_join_inputs( &input, &mut possible_join_keys, From 706d362b1a5642c49936235a1536dfe4ae1aa57a Mon Sep 17 00:00:00 2001 From: ygf11 Date: Tue, 10 Jan 2023 20:57:35 -0500 Subject: [PATCH 2/2] fix cargo fmt --- datafusion/core/tests/sqllogictests/test_files/join.slt | 2 +- datafusion/optimizer/src/eliminate_cross_join.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/datafusion/core/tests/sqllogictests/test_files/join.slt b/datafusion/core/tests/sqllogictests/test_files/join.slt index 8c120a91118b..4366d99a9e67 100644 --- a/datafusion/core/tests/sqllogictests/test_files/join.slt +++ b/datafusion/core/tests/sqllogictests/test_files/join.slt @@ -41,4 +41,4 @@ SELECT s.*, g.grade FROM students s join grades g on s.mark between g.min and g. ---- Amina 89 5 Salma 77 4 -Christen 50 3 \ No newline at end of file +Christen 50 3 diff --git a/datafusion/optimizer/src/eliminate_cross_join.rs b/datafusion/optimizer/src/eliminate_cross_join.rs index 756ec24ab014..e0bb114307c3 100644 --- a/datafusion/optimizer/src/eliminate_cross_join.rs +++ b/datafusion/optimizer/src/eliminate_cross_join.rs @@ -67,7 +67,7 @@ impl OptimizerRule for EliminateCrossJoin { if join.filter.is_some() { return Ok(None); } - + flatten_join_inputs( &input, &mut possible_join_keys,