|
17 | 17 |
|
18 | 18 | use std::vec; |
19 | 19 |
|
| 20 | +use datafusion::functions::datetime::to_timestamp::ToTimestampFunc; |
20 | 21 | use datafusion::logical_expr::{Expr, Operator}; |
21 | 22 | use datafusion::scalar::ScalarValue; |
22 | 23 | use iceberg::expr::{BinaryExpression, Predicate, PredicateOperator, Reference, UnaryExpression}; |
@@ -120,6 +121,21 @@ fn to_iceberg_predicate(expr: &Expr) -> TransformedResult { |
120 | 121 | } |
121 | 122 | } |
122 | 123 | Expr::Cast(c) => to_iceberg_predicate(&c.expr), |
| 124 | + Expr::ScalarFunction(func) => { |
| 125 | + if func |
| 126 | + .func |
| 127 | + .inner() |
| 128 | + .as_any() |
| 129 | + .downcast_ref::<ToTimestampFunc>() |
| 130 | + .is_some() |
| 131 | + && func.args.len() == 1 |
| 132 | + // More than 1 argument means it's a custom format - not |
| 133 | + // supported for now |
| 134 | + { |
| 135 | + return to_iceberg_predicate(&func.args[0]); |
| 136 | + } |
| 137 | + TransformedResult::NotTransformed |
| 138 | + } |
123 | 139 | _ => TransformedResult::NotTransformed, |
124 | 140 | } |
125 | 141 | } |
@@ -403,4 +419,39 @@ mod tests { |
403 | 419 | Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00")); |
404 | 420 | assert_eq!(predicate, expected_predicate); |
405 | 421 | } |
| 422 | + |
| 423 | + #[test] |
| 424 | + fn test_to_timestamp_comparison_creates_predicate() { |
| 425 | + let sql = "TO_TIMESTAMP(ts) >= timestamp '2023-01-05T00:00:00'"; |
| 426 | + let predicate = convert_to_iceberg_predicate(sql).unwrap(); |
| 427 | + let expected_predicate = |
| 428 | + Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00")); |
| 429 | + assert_eq!(predicate, expected_predicate); |
| 430 | + } |
| 431 | + |
| 432 | + #[test] |
| 433 | + fn test_to_timestamp_comparison_to_cast_creates_predicate() { |
| 434 | + let sql = "TO_TIMESTAMP(ts) >= CAST('2023-01-05T00:00:00' AS TIMESTAMP)"; |
| 435 | + let predicate = convert_to_iceberg_predicate(sql).unwrap(); |
| 436 | + let expected_predicate = |
| 437 | + Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00")); |
| 438 | + assert_eq!(predicate, expected_predicate); |
| 439 | + } |
| 440 | + |
| 441 | + #[test] |
| 442 | + fn test_to_timestamp_with_custom_format_does_not_create_predicate() { |
| 443 | + let sql = |
| 444 | + "TO_TIMESTAMP(ts, 'YYYY-DD-MMTmm:HH:SS') >= CAST('2023-01-05T00:00:00' AS TIMESTAMP)"; |
| 445 | + let predicate = convert_to_iceberg_predicate(sql); |
| 446 | + assert_eq!(predicate, None); |
| 447 | + } |
| 448 | + |
| 449 | + //#[test] |
| 450 | + //fn test_to_date_comparison_creates_predicate() { |
| 451 | + // let sql = "TO_DATE(ts) >= CAST('2023-01-05T00:00:00' AS DATE)"; |
| 452 | + // let predicate = convert_to_iceberg_predicate(sql).unwrap(); |
| 453 | + // let expected_predicate = |
| 454 | + // Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05")); |
| 455 | + // assert_eq!(predicate, expected_predicate); |
| 456 | + //} |
406 | 457 | } |
0 commit comments