mbutrovich commented on code in PR #3574:
URL: https://github.com/apache/iceberg-rust/pull/3574#discussion_r4787254161


##########
crates/iceberg/src/spec/values/datum.rs:
##########
@@ -1312,3 +1306,104 @@ 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::Timestamptz) => 
Datum::timestamptz_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)?,
+        ),
+        (PrimitiveType::Date, PrimitiveType::TimestamptzNs) => 
Datum::timestamptz_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::Long) => Datum::long(val),
+        (
+            PrimitiveType::Long | PrimitiveType::Timestamp | 
PrimitiveType::Timestamptz,
+            PrimitiveType::Timestamp,
+        ) => Datum::timestamp_micros(val),
+        (
+            PrimitiveType::Long | PrimitiveType::Timestamp | 
PrimitiveType::Timestamptz,
+            PrimitiveType::Timestamptz,
+        ) => Datum::timestamptz_micros(val),
+        (
+            PrimitiveType::Long | PrimitiveType::TimestampNs | 
PrimitiveType::TimestamptzNs,
+            PrimitiveType::TimestampNs,
+        ) => Datum::timestamp_nanos(val),
+        (
+            PrimitiveType::Long | PrimitiveType::TimestampNs | 
PrimitiveType::TimestamptzNs,
+            PrimitiveType::TimestamptzNs,
+        ) => Datum::timestamptz_nanos(val),
+        (PrimitiveType::TimestampNs | PrimitiveType::TimestamptzNs, 
PrimitiveType::Timestamp) => {
+            Datum::timestamp_micros(val.div_euclid(NANOS_PER_MICRO))
+        }
+        (PrimitiveType::TimestampNs | PrimitiveType::TimestamptzNs, 
PrimitiveType::Timestamptz) => {
+            Datum::timestamptz_micros(val.div_euclid(NANOS_PER_MICRO))
+        }
+        (PrimitiveType::Timestamp | PrimitiveType::Timestamptz, 
PrimitiveType::TimestampNs) => {
+            
Datum::timestamp_nanos(val.checked_mul(NANOS_PER_MICRO).ok_or_else(overflow)?)
+        }
+        (PrimitiveType::Timestamp | PrimitiveType::Timestamptz, 
PrimitiveType::TimestamptzNs) => {
+            
Datum::timestamptz_nanos(val.checked_mul(NANOS_PER_MICRO).ok_or_else(overflow)?)
+        }
+        (PrimitiveType::Timestamp | PrimitiveType::Timestamptz, 
PrimitiveType::Date) => {
+            Datum::date(val.div_euclid(MICROS_PER_DAY) as i32)
+        }
+        (PrimitiveType::TimestampNs | PrimitiveType::TimestamptzNs, 
PrimitiveType::Date) => {
+            Datum::date(val.div_euclid(NANOS_PER_DAY) as i32)
+        }

Review Comment:
   > I kept the conversions that drop a timezone (eg `timestamptz` -> 
`timestamp`), since that seems ok per the spec?
   
   As I read it, no. Dropping the zone needs a zone for the same reason adding 
one does ([earlier 
comment](https://github.com/apache/iceberg-rust/pull/3574#discussion_r4428430700)),
 and these arms assume UTC. The [promotion 
table](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/format/spec.md?plain=1#L408-L416)
 doesn't list it either. Could `timestamptz` to `timestamp`, `timestamptz_ns` 
to `timestamp_ns`, and `timestamptz` to `timestamp_ns` in 
[`convert_long`](https://github.com/apache/iceberg-rust/blob/1ff988401a654aa709e59e63d70f40c8267e4dd8/crates/iceberg/src/spec/values/datum.rs#L1351-L1366)
 return an error too, along with the arms that add a zone?



-- 
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]

Reply via email to