Apache DataFusion SQL Query Engine
feat: makes Expr::not normalize more negations (#17848)
Thomas Tanonb81073ad4ca9
diff --git a/datafusion/expr/src/operation.rs b/datafusion/expr/src/operation.rsindex 6b79a8248b..3158a19dce 100644--- a/datafusion/expr/src/operation.rs+++ b/datafusion/expr/src/operation.rs@@ -17,8 +17,8 @@//! This module contains implementations of operations (unary, binary etc.) for DataFusion expressions.+use crate::expr::{Exists, Expr, InList, InSubquery, Like};use crate::expr_fn::binary_expr;-use crate::{Expr, Like};use datafusion_expr_common::operator::Operator;use std::ops::{self, Not};@@ -153,6 +153,19 @@ impl Not for Expr {escape_char,case_insensitive,)),+ Expr::InList(InList {+ expr,+ list,+ negated,+ }) => Expr::InList(InList::new(expr, list, !negated)),+ Expr::Exists(Exists { subquery, negated }) => {+ Expr::Exists(Exists::new(subquery, !negated))+ }+ Expr::InSubquery(InSubquery {+ expr,+ subquery,+ negated,+ }) => Expr::InSubquery(InSubquery::new(expr, subquery, !negated)),_ => Expr::Not(Box::new(self)),}}diff --git a/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs b/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rsindex b828888305..4faf9389cf 100644--- a/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs+++ b/datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs@@ -926,4 +926,152 @@ mod tests {"#)}++ #[test]+ fn simplify_not_in_list() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Utf8, false)]);+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;++ let plan = LogicalPlanBuilder::from(table_scan)+ .filter(col("a").in_list(vec![lit("a"), lit("b")], false).not())?+ .build()?;++ assert_optimized_plan_equal!(+ plan,+ @ r#"+ Filter: test.a != Utf8("a") AND test.a != Utf8("b")+ TableScan: test+ "#+ )+ }++ #[test]+ fn simplify_not_not_in_list() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Utf8, false)]);+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;++ let plan = LogicalPlanBuilder::from(table_scan)+ .filter(+ col("a")+ .in_list(vec![lit("a"), lit("b")], false)+ .not()+ .not(),+ )?+ .build()?;++ assert_optimized_plan_equal!(+ plan,+ @ r#"+ Filter: test.a = Utf8("a") OR test.a = Utf8("b")+ TableScan: test+ "#+ )+ }++ #[test]+ fn simplify_not_exists() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Utf8, false)]);+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;+ let table_scan2 =+ datafusion_expr::table_scan(Some("test2"), &schema, None)?.build()?;++ let plan = LogicalPlanBuilder::from(table_scan)+ .filter(+ exists(Arc::new(LogicalPlanBuilder::from(table_scan2).build()?)).not(),+ )?+ .build()?;++ assert_optimized_plan_equal!(+ plan,+ @ r"+ Filter: NOT EXISTS (<subquery>)+ Subquery:+ TableScan: test2+ TableScan: test+ "+ )+ }++ #[test]+ fn simplify_not_not_exists() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Utf8, false)]);+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;+ let table_scan2 =+ datafusion_expr::table_scan(Some("test2"), &schema, None)?.build()?;++ let plan = LogicalPlanBuilder::from(table_scan)+ .filter(+ exists(Arc::new(LogicalPlanBuilder::from(table_scan2).build()?))+ .not()+ .not(),+ )?+ .build()?;++ assert_optimized_plan_equal!(+ plan,+ @ r"+ Filter: EXISTS (<subquery>)+ Subquery:+ TableScan: test2+ TableScan: test+ "+ )+ }++ #[test]+ fn simplify_not_in_subquery() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Utf8, false)]);+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;+ let table_scan2 =+ datafusion_expr::table_scan(Some("test2"), &schema, None)?.build()?;++ let plan = LogicalPlanBuilder::from(table_scan)+ .filter(+ in_subquery(+ col("a"),+ Arc::new(LogicalPlanBuilder::from(table_scan2).build()?),+ )+ .not(),+ )?+ .build()?;++ assert_optimized_plan_equal!(+ plan,+ @ r"+ Filter: test.a NOT IN (<subquery>)+ Subquery:+ TableScan: test2+ TableScan: test+ "+ )+ }++ #[test]+ fn simplify_not_not_in_subquery() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Utf8, false)]);+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;+ let table_scan2 =+ datafusion_expr::table_scan(Some("test2"), &schema, None)?.build()?;++ let plan = LogicalPlanBuilder::from(table_scan)+ .filter(+ in_subquery(+ col("a"),+ Arc::new(LogicalPlanBuilder::from(table_scan2).build()?),+ )+ .not()+ .not(),+ )?+ .build()?;++ assert_optimized_plan_equal!(+ plan,+ @ r"+ Filter: test.a IN (<subquery>)+ Subquery:+ TableScan: test2+ TableScan: test+ "+ )+ }}