Skip to content

Commit cc73766

Browse files
authored
Datafusion: Support cast operations (#821)
Resolves #811 Depends on #820
1 parent 55cca03 commit cc73766

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

crates/iceberg/src/spec/values.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,9 @@ mod tests {
35693569
let value = Datum::timestamp_from_str("2021-08-01T01:09:00.0899").unwrap();
35703570
assert_eq!(&format!("{value}"), "2021-08-01 01:09:00.089900");
35713571

3572+
let value = Datum::timestamp_from_str("2023-01-06T00:00:00").unwrap();
3573+
assert_eq!(&format!("{value}"), "2023-01-06 00:00:00");
3574+
35723575
let value = Datum::timestamp_from_str("2021-08-01T01:09:00.0899+0800");
35733576
assert!(value.is_err(), "Parse timestamp with timezone should fail!");
35743577

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ fn to_iceberg_predicate(expr: &Expr) -> TransformedResult {
119119
_ => TransformedResult::NotTransformed,
120120
}
121121
}
122+
Expr::Cast(c) => to_iceberg_predicate(&c.expr),
122123
_ => TransformedResult::NotTransformed,
123124
}
124125
}
@@ -211,7 +212,7 @@ fn scalar_value_to_datum(value: &ScalarValue) -> Option<Datum> {
211212

212213
#[cfg(test)]
213214
mod tests {
214-
use datafusion::arrow::datatypes::{DataType, Field, Schema};
215+
use datafusion::arrow::datatypes::{DataType, Field, Schema, TimeUnit};
215216
use datafusion::common::DFSchema;
216217
use datafusion::logical_expr::utils::split_conjunction;
217218
use datafusion::prelude::{Expr, SessionContext};
@@ -224,6 +225,7 @@ mod tests {
224225
let arrow_schema = Schema::new(vec![
225226
Field::new("foo", DataType::Int32, true),
226227
Field::new("bar", DataType::Utf8, true),
228+
Field::new("ts", DataType::Timestamp(TimeUnit::Second, None), true),
227229
]);
228230
DFSchema::try_from_qualified_schema("my_table", &arrow_schema).unwrap()
229231
}
@@ -392,4 +394,13 @@ mod tests {
392394
let expected_predicate = Reference::new("foo").less_than(Datum::long(0));
393395
assert_eq!(predicate, expected_predicate);
394396
}
397+
398+
#[test]
399+
fn test_predicate_conversion_with_cast() {
400+
let sql = "ts >= timestamp '2023-01-05T00:00:00'";
401+
let predicate = convert_to_iceberg_predicate(sql).unwrap();
402+
let expected_predicate =
403+
Reference::new("ts").greater_than_or_equal_to(Datum::string("2023-01-05T00:00:00"));
404+
assert_eq!(predicate, expected_predicate);
405+
}
395406
}

0 commit comments

Comments
 (0)