Skip to content

Commit

Permalink
Minor: use lit(true) and lit(false) more (#11904)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Aug 10, 2024
1 parent 1e93f7f commit 12aa82c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 46 deletions.
6 changes: 2 additions & 4 deletions datafusion/optimizer/src/decorrelate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use datafusion_common::{plan_err, Column, DFSchemaRef, Result, ScalarValue};
use datafusion_expr::expr::Alias;
use datafusion_expr::simplify::SimplifyContext;
use datafusion_expr::utils::{conjunction, find_join_exprs, split_conjunction};
use datafusion_expr::{expr, EmptyRelation, Expr, LogicalPlan, LogicalPlanBuilder};
use datafusion_expr::{expr, lit, EmptyRelation, Expr, LogicalPlan, LogicalPlanBuilder};
use datafusion_physical_expr::execution_props::ExecutionProps;

/// This struct rewrite the sub query plan by pull up the correlated
Expand Down Expand Up @@ -282,9 +282,7 @@ impl TreeNodeRewriter for PullUpCorrelatedExpr {
)?;
if !expr_result_map_for_count_bug.is_empty() {
// has count bug
let un_matched_row =
Expr::Literal(ScalarValue::Boolean(Some(true)))
.alias(UN_MATCHED_ROW_INDICATOR);
let un_matched_row = lit(true).alias(UN_MATCHED_ROW_INDICATOR);
// add the unmatched rows indicator to the Aggregation's group expressions
missing_exprs.push(un_matched_row);
}
Expand Down
8 changes: 4 additions & 4 deletions datafusion/optimizer/src/eliminate_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod tests {

#[test]
fn filter_false() -> Result<()> {
let filter_expr = Expr::Literal(ScalarValue::Boolean(Some(false)));
let filter_expr = lit(false);

let table_scan = test_table_scan().unwrap();
let plan = LogicalPlanBuilder::from(table_scan)
Expand Down Expand Up @@ -127,7 +127,7 @@ mod tests {

#[test]
fn filter_false_nested() -> Result<()> {
let filter_expr = Expr::Literal(ScalarValue::Boolean(Some(false)));
let filter_expr = lit(false);

let table_scan = test_table_scan()?;
let plan1 = LogicalPlanBuilder::from(table_scan.clone())
Expand All @@ -149,7 +149,7 @@ mod tests {

#[test]
fn filter_true() -> Result<()> {
let filter_expr = Expr::Literal(ScalarValue::Boolean(Some(true)));
let filter_expr = lit(true);

let table_scan = test_table_scan()?;
let plan = LogicalPlanBuilder::from(table_scan)
Expand All @@ -164,7 +164,7 @@ mod tests {

#[test]
fn filter_true_nested() -> Result<()> {
let filter_expr = Expr::Literal(ScalarValue::Boolean(Some(true)));
let filter_expr = lit(true);

let table_scan = test_table_scan()?;
let plan1 = LogicalPlanBuilder::from(table_scan.clone())
Expand Down
8 changes: 4 additions & 4 deletions datafusion/optimizer/src/eliminate_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ impl OptimizerRule for EliminateJoin {
mod tests {
use crate::eliminate_join::EliminateJoin;
use crate::test::*;
use datafusion_common::{Result, ScalarValue};
use datafusion_common::Result;
use datafusion_expr::JoinType::Inner;
use datafusion_expr::{logical_plan::builder::LogicalPlanBuilder, Expr, LogicalPlan};
use datafusion_expr::{lit, logical_plan::builder::LogicalPlanBuilder, LogicalPlan};
use std::sync::Arc;

fn assert_optimized_plan_equal(plan: LogicalPlan, expected: &str) -> Result<()> {
Expand All @@ -98,7 +98,7 @@ mod tests {
.join_on(
LogicalPlanBuilder::empty(false).build()?,
Inner,
Some(Expr::Literal(ScalarValue::Boolean(Some(false)))),
Some(lit(false)),
)?
.build()?;

Expand All @@ -112,7 +112,7 @@ mod tests {
.join_on(
LogicalPlanBuilder::empty(false).build()?,
Inner,
Some(Expr::Literal(ScalarValue::Boolean(Some(true)))),
Some(lit(true)),
)?
.build()?;

Expand Down
34 changes: 17 additions & 17 deletions datafusion/optimizer/src/propagate_empty_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ mod tests {

use arrow::datatypes::{DataType, Field, Schema};

use datafusion_common::{Column, DFSchema, JoinType, ScalarValue};
use datafusion_common::{Column, DFSchema, JoinType};
use datafusion_expr::logical_plan::table_scan;
use datafusion_expr::{
binary_expr, col, lit, logical_plan::builder::LogicalPlanBuilder, Expr, Operator,
binary_expr, col, lit, logical_plan::builder::LogicalPlanBuilder, Operator,
};

use crate::eliminate_filter::EliminateFilter;
Expand Down Expand Up @@ -289,7 +289,7 @@ mod tests {
#[test]
fn propagate_empty() -> Result<()> {
let plan = LogicalPlanBuilder::empty(false)
.filter(Expr::Literal(ScalarValue::Boolean(Some(true))))?
.filter(lit(true))?
.limit(10, None)?
.project(vec![binary_expr(lit(1), Operator::Plus, lit(1))])?
.build()?;
Expand All @@ -305,7 +305,7 @@ mod tests {
let right_table_scan = test_table_scan_with_name("test2")?;
let right = LogicalPlanBuilder::from(right_table_scan)
.project(vec![col("a")])?
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;

let plan = LogicalPlanBuilder::from(left)
Expand All @@ -325,7 +325,7 @@ mod tests {
fn propagate_union_empty() -> Result<()> {
let left = LogicalPlanBuilder::from(test_table_scan()?).build()?;
let right = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;

let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
Expand All @@ -339,10 +339,10 @@ mod tests {
let one =
LogicalPlanBuilder::from(test_table_scan_with_name("test1")?).build()?;
let two = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let three = LogicalPlanBuilder::from(test_table_scan_with_name("test3")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let four =
LogicalPlanBuilder::from(test_table_scan_with_name("test4")?).build()?;
Expand All @@ -362,16 +362,16 @@ mod tests {
#[test]
fn propagate_union_all_empty() -> Result<()> {
let one = LogicalPlanBuilder::from(test_table_scan_with_name("test1")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let two = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let three = LogicalPlanBuilder::from(test_table_scan_with_name("test3")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let four = LogicalPlanBuilder::from(test_table_scan_with_name("test4")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;

let plan = LogicalPlanBuilder::from(one)
Expand All @@ -389,7 +389,7 @@ mod tests {
let one_schema = Schema::new(vec![Field::new("t1a", DataType::UInt32, false)]);
let t1_scan = table_scan(Some("test1"), &one_schema, None)?.build()?;
let one = LogicalPlanBuilder::from(t1_scan)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;

let two_schema = Schema::new(vec![Field::new("t2a", DataType::UInt32, false)]);
Expand All @@ -415,7 +415,7 @@ mod tests {
fn propagate_union_alias() -> Result<()> {
let left = LogicalPlanBuilder::from(test_table_scan()?).build()?;
let right = LogicalPlanBuilder::from(test_table_scan_with_name("test2")?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;

let plan = LogicalPlanBuilder::from(left).union(right)?.build()?;
Expand Down Expand Up @@ -449,7 +449,7 @@ mod tests {
let left_table_scan = test_table_scan()?;

LogicalPlanBuilder::from(left_table_scan)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()
} else {
let scan = test_table_scan_with_name("left").unwrap();
Expand All @@ -460,7 +460,7 @@ mod tests {
let right_table_scan = test_table_scan_with_name("right")?;

LogicalPlanBuilder::from(right_table_scan)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()
} else {
let scan = test_table_scan_with_name("right").unwrap();
Expand All @@ -487,14 +487,14 @@ mod tests {
let (left, right, join_type, expected) = if anti_left_join {
let left = test_table_scan()?;
let right = LogicalPlanBuilder::from(test_table_scan()?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let expected = left.display_indent().to_string();
(left, right, JoinType::LeftAnti, expected)
} else {
let right = test_table_scan()?;
let left = LogicalPlanBuilder::from(test_table_scan()?)
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))?
.filter(lit(false))?
.build()?;
let expected = right.display_indent().to_string();
(left, right, JoinType::RightAnti, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
op: Or,
right,
}) if is_not_of(&right, &left) && !info.nullable(&left)? => {
Transformed::yes(Expr::Literal(ScalarValue::Boolean(Some(true))))
Transformed::yes(lit(true))
}
// !A OR A ---> true (if A not nullable)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
}) if is_not_of(&left, &right) && !info.nullable(&right)? => {
Transformed::yes(Expr::Literal(ScalarValue::Boolean(Some(true))))
Transformed::yes(lit(true))
}
// (..A..) OR A --> (..A..)
Expr::BinaryExpr(BinaryExpr {
Expand Down Expand Up @@ -890,15 +890,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
op: And,
right,
}) if is_not_of(&right, &left) && !info.nullable(&left)? => {
Transformed::yes(Expr::Literal(ScalarValue::Boolean(Some(false))))
Transformed::yes(lit(false))
}
// !A AND A ---> false (if A not nullable)
Expr::BinaryExpr(BinaryExpr {
left,
op: And,
right,
}) if is_not_of(&left, &right) && !info.nullable(&right)? => {
Transformed::yes(Expr::Literal(ScalarValue::Boolean(Some(false))))
Transformed::yes(lit(false))
}
// (..A..) AND A --> (..A..)
Expr::BinaryExpr(BinaryExpr {
Expand Down
22 changes: 9 additions & 13 deletions datafusion/physical-expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ use datafusion_expr::execution_props::ExecutionProps;
use datafusion_expr::expr::{Alias, Cast, InList, ScalarFunction};
use datafusion_expr::var_provider::is_system_variables;
use datafusion_expr::var_provider::VarType;
use datafusion_expr::{binary_expr, Between, BinaryExpr, Expr, Like, Operator, TryCast};
use datafusion_expr::{
binary_expr, lit, Between, BinaryExpr, Expr, Like, Operator, TryCast,
};

/// [PhysicalExpr] evaluate DataFusion expressions such as `A + 1`, or `CAST(c1
/// AS int)`.
Expand Down Expand Up @@ -140,32 +142,26 @@ pub fn create_physical_expr(
let binary_op = binary_expr(
expr.as_ref().clone(),
Operator::IsNotDistinctFrom,
Expr::Literal(ScalarValue::Boolean(Some(true))),
lit(true),
);
create_physical_expr(&binary_op, input_dfschema, execution_props)
}
Expr::IsNotTrue(expr) => {
let binary_op = binary_expr(
expr.as_ref().clone(),
Operator::IsDistinctFrom,
Expr::Literal(ScalarValue::Boolean(Some(true))),
);
let binary_op =
binary_expr(expr.as_ref().clone(), Operator::IsDistinctFrom, lit(true));
create_physical_expr(&binary_op, input_dfschema, execution_props)
}
Expr::IsFalse(expr) => {
let binary_op = binary_expr(
expr.as_ref().clone(),
Operator::IsNotDistinctFrom,
Expr::Literal(ScalarValue::Boolean(Some(false))),
lit(false),
);
create_physical_expr(&binary_op, input_dfschema, execution_props)
}
Expr::IsNotFalse(expr) => {
let binary_op = binary_expr(
expr.as_ref().clone(),
Operator::IsDistinctFrom,
Expr::Literal(ScalarValue::Boolean(Some(false))),
);
let binary_op =
binary_expr(expr.as_ref().clone(), Operator::IsDistinctFrom, lit(false));
create_physical_expr(&binary_op, input_dfschema, execution_props)
}
Expr::IsUnknown(expr) => {
Expand Down

0 comments on commit 12aa82c

Please sign in to comment.