Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion datafusion/expr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)),
}
}
Expand Down
148 changes: 148 additions & 0 deletions datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
)
}
}