laskoviymishka commented on code in PR #3574:
URL: https://github.com/apache/iceberg-rust/pull/3574#discussion_r4763894552
##########
crates/iceberg/src/spec/values/datum.rs:
##########
@@ -1312,3 +1305,78 @@ fn human_float<T: Float + LowerExp + FromStr>(value: T)
-> String {
format!("{sign}{}.{fraction}E{exponent}", &digits[..1])
}
}
+
+const MICROS_PER_DAY: i64 = 24 * 60 * 60 * 1_000_000;
+const NANOS_PER_MICRO: i64 = 1000;
+const NANOS_PER_DAY: i64 = NANOS_PER_MICRO * MICROS_PER_DAY;
+
+/// Converts an int literal between two [PrimitiveType]s.
+fn convert_int(
+ val: i32,
+ source_type: &PrimitiveType,
+ target_type: &PrimitiveType,
+) -> Result<Datum> {
+ let datum = match (source_type, target_type) {
+ (_, PrimitiveType::Int) => Datum::int(val),
+ (_, PrimitiveType::Date) => Datum::date(val),
+ (_, PrimitiveType::Long) => Datum::long(val),
+ (PrimitiveType::Date, PrimitiveType::Timestamp) =>
Datum::timestamp_micros(
+ (val as i64)
+ .checked_mul(MICROS_PER_DAY)
+ .ok_or_else(overflow)?,
+ ),
+ (PrimitiveType::Date, PrimitiveType::TimestampNs) =>
Datum::timestamp_nanos(
+ (val as i64)
+ .checked_mul(NANOS_PER_DAY)
+ .ok_or_else(overflow)?,
+ ),
+ _ => {
+ return Err(invalid_data!(
+ "Can't convert datum from {source_type} type to {target_type}
type."
+ ));
+ }
+ };
+
+ Ok(datum)
+}
+
+/// Converts a long literal between two [PrimitiveType]s.
+fn convert_long(
+ val: i64,
+ source_type: &PrimitiveType,
+ target_type: &PrimitiveType,
+) -> Result<Datum> {
+ let datum = match (source_type, target_type) {
+ (_, PrimitiveType::Int) => Datum::i64_to_i32(val),
+ (PrimitiveType::Timestamp | PrimitiveType::Timestamptz,
PrimitiveType::Timestamp) => {
+ Datum::timestamp_micros(val)
+ }
+ (PrimitiveType::Timestamp | PrimitiveType::Timestamptz,
PrimitiveType::Timestamptz) => {
Review Comment:
Is there a commit still to push? The recap says the arms that add a zone
were dropped, but on 1ff98840 (still the only commit) `Timestamp` →
`Timestamptz`, `TimestampNs` → `TimestamptzNs` and `Timestamp` →
`TimestamptzNs` are all here
([datum.rs#L1354-L1369](https://github.com/apache/iceberg-rust/blob/1ff988401a654aa709e59e63d70f40c8267e4dd8/crates/iceberg/src/spec/values/datum.rs#L1354-L1369)),
and the tests assert `Ok` for them
([tests.rs#L1644-L1664](https://github.com/apache/iceberg-rust/blob/1ff988401a654aa709e59e63d70f40c8267e4dd8/crates/iceberg/src/spec/values/tests.rs#L1644-L1664)).
On `main` the two nanos pairs return an error, so they're new here.
##########
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");
-
- let result = datum.to(&Primitive(PrimitiveType::Timestamp)).unwrap();
-
- let expected = Datum::timestamp_micros(-1407990900000000);
-
- assert_eq!(result, expected);
-}
-
-#[test]
-fn test_datum_string_convert_to_timestamptz() {
- let datum = Datum::string("1925-05-20T19:25:00.000 UTC");
-
- let result = datum.to(&Primitive(PrimitiveType::Timestamptz)).unwrap();
-
- let expected = Datum::timestamptz_micros(-1407990900000000);
+ let result = source.to(&Primitive(target_type)).unwrap();
Review Comment:
Could we add an error variant of this macro with a couple of the conversions
this PR now rejects, say `Datum::long(12345)` → `Timestamp` and
`Datum::timestamp_nanos(1500)` → `Timestamp`? On `main` the first returns `Ok`,
and `test_datum_long_convert_to_timestamp` checked that. The macro can only
`unwrap()`
([tests.rs#L1515-L1529](https://github.com/apache/iceberg-rust/blob/1ff988401a654aa709e59e63d70f40c8267e4dd8/crates/iceberg/src/spec/values/tests.rs#L1515-L1529)),
so putting either arm back wouldn't fail any test.
--
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]