Apache DataFusion SQL Query Engine
fix: failed to execute sql with subquery (#5542)
LFC1a22f9fd436c
diff --git a/datafusion/core/tests/sqllogictests/test_files/subquery.slt b/datafusion/core/tests/sqllogictests/test_files/subquery.sltindex 70c3c3e13d..0c108e2c81 100644--- a/datafusion/core/tests/sqllogictests/test_files/subquery.slt+++ b/datafusion/core/tests/sqllogictests/test_files/subquery.slt@@ -60,3 +60,42 @@ where t1.t1_id + 12 not in ()----22 b 2++# in subquery with two parentheses, see #5529+query ITI rowsort+select t1.t1_id,+ t1.t1_name,+ t1.t1_int+from t1+where t1.t1_id in ((+ select t2.t2_id from t2+ ))+----+11 a 1+22 b 2+44 d 4++query ITI rowsort+select t1.t1_id,+ t1.t1_name,+ t1.t1_int+from t1+where t1.t1_id in ((+ select t2.t2_id from t2+ ))+and t1.t1_int < 3+----+11 a 1+22 b 2++query ITI rowsort+select t1.t1_id,+ t1.t1_name,+ t1.t1_int+from t1+where t1.t1_id not in ((+ select t2.t2_id from t2 where t2.t2_int = 3+ ))+----+22 b 2+33 c 3diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rsindex 033b554402..c2e96bf97c 100644--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs@@ -390,6 +390,22 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {lit(negated)}+ // expr IN ((subquery)) -> expr IN (subquery), see ##5529+ Expr::InList {+ expr,+ mut list,+ negated,+ } if list.len() == 1+ && matches!(list.first(), Some(Expr::ScalarSubquery { .. })) =>+ {+ let Expr::ScalarSubquery(subquery) = list.remove(0) else { unreachable!() };+ Expr::InSubquery {+ expr,+ subquery,+ negated,+ }+ }+// if expr is a single column reference:// expr IN (A, B, ...) --> (expr = A) OR (expr = B) OR (expr = C)Expr::InList {@@ -1110,6 +1126,7 @@ mod tests {};use super::*;+ use crate::test::test_table_scan_with_name;use arrow::{array::{ArrayRef, Int32Array},datatypes::{DataType, Field, Schema},@@ -2688,6 +2705,51 @@ mod tests {simplify(in_list(col("c1"), vec![lit(1), lit(2)], true)),col("c1").not_eq(lit(2)).and(col("c1").not_eq(lit(1))));++ let subquery = Arc::new(test_table_scan_with_name("test").unwrap());+ assert_eq!(+ simplify(in_list(+ col("c1"),+ vec![scalar_subquery(subquery.clone())],+ false+ )),+ in_subquery(col("c1"), subquery.clone())+ );+ assert_eq!(+ simplify(in_list(+ col("c1"),+ vec![scalar_subquery(subquery.clone())],+ true+ )),+ not_in_subquery(col("c1"), subquery)+ );++ let subquery1 =+ scalar_subquery(Arc::new(test_table_scan_with_name("test1").unwrap()));+ let subquery2 =+ scalar_subquery(Arc::new(test_table_scan_with_name("test2").unwrap()));++ // c1 NOT IN (<subquery1>, <subquery2>) -> c1 != <subquery2> AND c1 != <subquery1>+ assert_eq!(+ simplify(in_list(+ col("c1"),+ vec![subquery1.clone(), subquery2.clone()],+ true+ )),+ col("c1")+ .not_eq(subquery2.clone())+ .and(col("c1").not_eq(subquery1.clone()))+ );++ // c1 IN (<subquery1>, <subquery2>) -> c1 == <subquery2> OR c1 == <subquery1>+ assert_eq!(+ simplify(in_list(+ col("c1"),+ vec![subquery1.clone(), subquery2.clone()],+ false+ )),+ col("c1").eq(subquery2).or(col("c1").eq(subquery1))+ );}#[test]