mbutrovich commented on code in PR #3574:
URL: https://github.com/apache/iceberg-rust/pull/3574#discussion_r4428433178
##########
crates/iceberg/src/spec/values/datum.rs:
##########
@@ -1132,25 +1132,19 @@ impl Datum {
match target_type {
Type::Primitive(target_primitive_type) => {
match (&self.literal, &self.r#type, target_primitive_type) {
- (PrimitiveLiteral::Int(val), _, PrimitiveType::Int) =>
Ok(Datum::int(*val)),
- (PrimitiveLiteral::Int(val), _, PrimitiveType::Date) =>
Ok(Datum::date(*val)),
- (PrimitiveLiteral::Int(val), _, PrimitiveType::Long) =>
Ok(Datum::long(*val)),
- (PrimitiveLiteral::Long(val), _, PrimitiveType::Int) => {
- Ok(Datum::i64_to_i32(*val))
+ (_, self_type, target_type) if self_type == target_type =>
Ok(self),
+ (PrimitiveLiteral::Int(val), source_type, target_type) => {
+ convert_int(*val, source_type, target_type)
+ }
+ (PrimitiveLiteral::Long(val), source_type, target_type) =>
{
+ convert_long(*val, source_type, target_type)
}
(PrimitiveLiteral::Double(val), _, PrimitiveType::Float)
=> {
Ok(Datum::f64_to_f32(val.0))
}
(PrimitiveLiteral::Float(val), _, PrimitiveType::Double)
=> {
Ok(Datum::double(val.0 as f64))
}
- (PrimitiveLiteral::Long(val), _, PrimitiveType::Timestamp)
=> {
- Ok(Datum::timestamp_micros(*val))
- }
- (PrimitiveLiteral::Long(val), _,
PrimitiveType::Timestamptz) => {
- Ok(Datum::timestamptz_micros(*val))
- }
Review Comment:
Could the PR description mention this removal?
`Datum::long(12345).to(&Primitive(PrimitiveType::Timestamp))` returns
`timestamp_micros(12345)` on `main`, and `test_datum_long_convert_to_timestamp`
checked that. On the head commit it returns "Can't convert datum from long type
to timestamp type." The same applies to `timestamptz`. That matches my reading
of the spec, but a caller binding a `long` literal to a timestamp column now
gets an error, and the description only mentions what the PR adds.
##########
crates/iceberg/src/spec/values/tests.rs:
##########
@@ -1511,213 +1512,195 @@ fn test_datum_ser_deser() {
test_fn(datum);
}
-#[test]
-fn test_datum_date_convert_to_int() {
- let datum_date = Datum::date(12345);
-
- let result = datum_date.to(&Primitive(PrimitiveType::Int)).unwrap();
-
- let expected = Datum::int(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_int_convert_to_date() {
- let datum_int = Datum::int(12345);
-
- let result = datum_int.to(&Primitive(PrimitiveType::Date)).unwrap();
-
- let expected = Datum::date(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_long_convert_to_int() {
- let datum = Datum::long(12345);
-
- let result = datum.to(&Primitive(PrimitiveType::Int)).unwrap();
-
- let expected = Datum::int(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_long_convert_to_int_above_max() {
- let datum = Datum::long(INT_MAX as i64 + 1);
-
- let result = datum.to(&Primitive(PrimitiveType::Int)).unwrap();
-
- let expected = Datum::new(PrimitiveType::Int, PrimitiveLiteral::AboveMax);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_long_convert_to_int_below_min() {
- let datum = Datum::long(INT_MIN as i64 - 1);
-
- let result = datum.to(&Primitive(PrimitiveType::Int)).unwrap();
-
- let expected = Datum::new(PrimitiveType::Int, PrimitiveLiteral::BelowMin);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_double_convert_to_float() {
- let datum = Datum::double(2.5);
-
- let result = datum.to(&Primitive(PrimitiveType::Float)).unwrap();
-
- let expected = Datum::float(2.5_f32);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_double_convert_to_float_above_max() {
- let datum = Datum::double(1e39);
-
- let result = datum.to(&Primitive(PrimitiveType::Float)).unwrap();
-
- let expected = Datum::new(PrimitiveType::Float,
PrimitiveLiteral::AboveMax);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_double_convert_to_float_below_min() {
- let datum = Datum::double(-1e39);
-
- let result = datum.to(&Primitive(PrimitiveType::Float)).unwrap();
-
- let expected = Datum::new(PrimitiveType::Float,
PrimitiveLiteral::BelowMin);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_float_convert_to_double() {
- let datum = Datum::float(2.5_f32);
-
- let result = datum.to(&Primitive(PrimitiveType::Double)).unwrap();
-
- let expected = Datum::double(2.5);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_long_convert_to_timestamp() {
- let datum = Datum::long(12345);
-
- let result = datum.to(&Primitive(PrimitiveType::Timestamp)).unwrap();
-
- let expected = Datum::timestamp_micros(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_long_convert_to_timestamptz() {
- let datum = Datum::long(12345);
+macro_rules! datum_convert_tests {
+ ($($name:ident: ($source:expr => $target_type:ident,
$expected:expr),)*) => {
+ $(
+ #[test]
+ fn $name() {
+ let source = $source;
+ let target_type = PrimitiveType::$target_type;
+ let expected = $expected;
- let result = datum.to(&Primitive(PrimitiveType::Timestamptz)).unwrap();
-
- let expected = Datum::timestamptz_micros(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_decimal_convert_to_long() {
- let datum = Datum::decimal(decimal_new(12345, 0)).unwrap();
-
- let result = datum.to(&Primitive(PrimitiveType::Long)).unwrap();
-
- let expected = Datum::long(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_decimal_convert_to_long_above_max() {
- let datum = Datum::decimal(decimal_from_i128_with_scale(LONG_MAX as i128 +
1, 0)).unwrap();
-
- let result = datum.to(&Primitive(PrimitiveType::Long)).unwrap();
-
- let expected = Datum::new(PrimitiveType::Long, PrimitiveLiteral::AboveMax);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_decimal_convert_to_long_below_min() {
- let datum = Datum::decimal(decimal_from_i128_with_scale(LONG_MIN as i128 -
1, 0)).unwrap();
-
- let result = datum.to(&Primitive(PrimitiveType::Long)).unwrap();
-
- let expected = Datum::new(PrimitiveType::Long, PrimitiveLiteral::BelowMin);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_string_convert_to_boolean() {
- let datum = Datum::string("true");
-
- let result = datum.to(&Primitive(PrimitiveType::Boolean)).unwrap();
-
- let expected = Datum::bool(true);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_string_convert_to_int() {
- let datum = Datum::string("12345");
-
- let result = datum.to(&Primitive(PrimitiveType::Int)).unwrap();
-
- let expected = Datum::int(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_string_convert_to_long() {
- let datum = Datum::string("12345");
-
- let result = datum.to(&Primitive(PrimitiveType::Long)).unwrap();
-
- let expected = Datum::long(12345);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_string_convert_to_timestamp() {
- let datum = Datum::string("1925-05-20T19:25:00.000");
-
Review Comment:
Could these tests check the values at and just past each boundary? `i32::MAX
/ 2` and `i64::MAX / 10` are far past it, so an off-by-one in the range check
would still pass. On the head commit, `Datum::date(106751)` and
`Datum::date(-106751)` convert to `timestamp_ns`, and `Datum::date(106752)` and
`Datum::date(-106752)` return the overflow error. For micros to nanos,
`i64::MAX / 1000` converts and `i64::MAX / 1000 + 1` overflows, and the
negative side has no test yet. The [promotion
table](https://github.com/apache/iceberg/blob/dfd7ef3b711cd1977b93f4e6f86cb4148712acd3/format/spec.md?plain=1#L402)
requires the failure for values outside the promoted type's range.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]