Skip to content
Merged
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
58 changes: 58 additions & 0 deletions datafusion/src/optimizer/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::optimizer::utils;
use crate::physical_plan::functions::BuiltinScalarFunction;
use crate::scalar::ScalarValue;
use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;
use arrow::compute::{kernels, DEFAULT_CAST_OPTIONS};

/// Optimizer that simplifies comparison expressions involving boolean literals.
///
Expand Down Expand Up @@ -247,6 +248,25 @@ impl<'a> ExprRewriter for ConstantRewriter<'a> {
}
}
}
Expr::Cast {
expr: inner,
data_type,
} => match inner.as_ref() {
Expr::Literal(val) => {
let scalar_array = val.to_array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea👍 I think converting to an array is the best we can do for now, without duplicating the code.

let cast_array = kernels::cast::cast_with_options(
&scalar_array,
&data_type,
&DEFAULT_CAST_OPTIONS,
)?;
let cast_scalar = ScalarValue::try_from_array(&cast_array, 0)?;
Expr::Literal(cast_scalar)
}
_ => Expr::Cast {
expr: inner,
data_type,
},
},
expr => {
// no rewrite possible
expr
Expand Down Expand Up @@ -724,6 +744,44 @@ mod tests {
assert_eq!(expected, actual);
}

#[test]
fn cast_expr() {
let table_scan = test_table_scan().unwrap();
let proj = vec![Expr::Cast {
expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some("0".to_string())))),
data_type: DataType::Int32,
}];
let plan = LogicalPlanBuilder::from(&table_scan)
.project(proj)
.unwrap()
.build()
.unwrap();

let expected = "Projection: Int32(0)\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is beautiful ❤️ -- thank you @msathis

\n TableScan: test projection=None";
let actual = get_optimized_plan_formatted(&plan, &chrono::Utc::now());
assert_eq!(expected, actual);
}

#[test]
fn cast_expr_wrong_arg() {
let table_scan = test_table_scan().unwrap();
let proj = vec![Expr::Cast {
expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some("".to_string())))),
data_type: DataType::Int32,
}];
let plan = LogicalPlanBuilder::from(&table_scan)
.project(proj)
.unwrap()
.build()
.unwrap();

let expected = "Projection: Int32(NULL)\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

\n TableScan: test projection=None";
let actual = get_optimized_plan_formatted(&plan, &chrono::Utc::now());
assert_eq!(expected, actual);
}

#[test]
fn single_now_expr() {
let table_scan = test_table_scan().unwrap();
Expand Down