Omega359 commented on code in PR #9388:
URL: https://github.com/apache/arrow-datafusion/pull/9388#discussion_r1506948609
##########
datafusion/core/tests/simplification.rs:
##########
@@ -108,3 +224,191 @@ fn fold_and_simplify() {
let simplified = simplifier.simplify(expr).unwrap();
assert_eq!(simplified, lit(true))
}
+
+#[test]
+/// Ensure that timestamp expressions are folded so they aren't invoked on
each row
+fn to_timestamp_expr_folded() -> Result<()> {
+ let table_scan = test_table_scan();
+ let proj = vec![to_timestamp_expr("2020-09-08T12:00:00+00:00")];
+
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .project(proj)?
+ .build()?;
+
+ let expected = "Projection: TimestampNanosecond(1599566400000000000, None)
AS to_timestamp(Utf8(\"2020-09-08T12:00:00+00:00\"))\
+ \n TableScan: test"
+ .to_string();
+ let actual = get_optimized_plan_formatted(&plan, &Utc::now());
+ assert_eq!(expected, actual);
+ Ok(())
+}
+
+#[test]
+fn now_less_than_timestamp() -> Result<()> {
+ let table_scan = test_table_scan();
+
+ let ts_string = "2020-09-08T12:05:00+00:00";
+ let time = Utc.timestamp_nanos(1599566400000000000i64);
+
+ // cast(now() as int) < cast(to_timestamp(...) as int) + 50000_i64
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .filter(
+ cast_to_int64_expr(now_expr())
+ .lt(cast_to_int64_expr(to_timestamp_expr(ts_string)) +
lit(50000_i64)),
+ )?
+ .build()?;
+
+ // Note that constant folder runs and folds the entire
+ // expression down to a single constant (true)
+ let expected = "Filter: Boolean(true)\
+ \n TableScan: test";
+ let actual = get_optimized_plan_formatted(&plan, &time);
+
+ assert_eq!(expected, actual);
+ Ok(())
+}
+
+#[test]
+fn select_date_plus_interval() -> Result<()> {
+ let table_scan = test_table_scan();
+
+ let ts_string = "2020-09-08T12:05:00+00:00";
+ let time = Utc.timestamp_nanos(1599566400000000000i64);
+
+ // now() < cast(to_timestamp(...) as int) + 5000000000
+ let schema = table_scan.schema();
+
+ let date_plus_interval_expr = to_timestamp_expr(ts_string)
+ .cast_to(&DataType::Date32, schema)?
+ + Expr::Literal(ScalarValue::IntervalDayTime(Some(123i64 << 32)));
+
+ let plan = LogicalPlanBuilder::from(table_scan.clone())
+ .project(vec![date_plus_interval_expr])?
+ .build()?;
+
+ // Note that constant folder runs and folds the entire
+ // expression down to a single constant (true)
+ let expected = r#"Projection: Date32("18636") AS
to_timestamp(Utf8("2020-09-08T12:05:00+00:00")) +
IntervalDayTime("528280977408")
+ TableScan: test"#;
+ let actual = get_optimized_plan_formatted(&plan, &time);
+
+ assert_eq!(expected, actual);
+ Ok(())
+}
+
+#[test]
+fn test_const_evaluator() {
+ // true --> true
+ test_evaluate(lit(true), lit(true));
+ // true or true --> true
+ test_evaluate(lit(true).or(lit(true)), lit(true));
+ // true or false --> true
+ test_evaluate(lit(true).or(lit(false)), lit(true));
+
+ // "foo" == "foo" --> true
+ test_evaluate(lit("foo").eq(lit("foo")), lit(true));
+ // "foo" != "foo" --> false
+ test_evaluate(lit("foo").not_eq(lit("foo")), lit(false));
+
+ // c = 1 --> c = 1
+ test_evaluate(col("c").eq(lit(1)), col("c").eq(lit(1)));
+ // c = 1 + 2 --> c + 3
+ test_evaluate(col("c").eq(lit(1) + lit(2)), col("c").eq(lit(3)));
+ // (foo != foo) OR (c = 1) --> false OR (c = 1)
+ test_evaluate(
+ (lit("foo").not_eq(lit("foo"))).or(col("c").eq(lit(1))),
+ // lit(false).or(col("c").eq(lit(1))),
+ col("c").eq(lit(1)),
Review Comment:
The comment was the version that was in expr_simplifier but that no longer
passed the test once the test_evaluate_with_start_time function was changed to
use ExprSimplifier from ConstEvaluator. I am unsure if this is in fact correct
or not @alamb
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]