Skip to content

Commit 3668476

Browse files
committed
Support TO_TIMESTAMP like a cast
1 parent 6f2ce71 commit 3668476

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

crates/integrations/datafusion/src/physical_plan/expr_to_predicate.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use std::vec;
1919

20+
use datafusion::functions::datetime::to_timestamp::ToTimestampFunc;
2021
use datafusion::logical_expr::{Expr, Operator};
2122
use datafusion::scalar::ScalarValue;
2223
use iceberg::expr::{BinaryExpression, Predicate, PredicateOperator, Reference, UnaryExpression};
@@ -120,6 +121,21 @@ fn to_iceberg_predicate(expr: &Expr) -> TransformedResult {
120121
}
121122
}
122123
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+
}
123139
_ => TransformedResult::NotTransformed,
124140
}
125141
}
@@ -403,4 +419,39 @@ mod tests {
403419
Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00"));
404420
assert_eq!(predicate, expected_predicate);
405421
}
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+
//}
406457
}

0 commit comments

Comments
 (0)