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


##########
crates/iceberg/src/spec/values/datum.rs:
##########
@@ -1355,19 +1306,64 @@ fn human_float<T: Float + LowerExp + FromStr>(value: T) 
-> String {
     }
 }
 
-/// Converts `days` since the epoch to a `timestamp` (microseconds) or 
`timestamp_ns`
-/// (nanoseconds) literal at midnight of that day.
-///
-/// # Errors
-/// Returns `DataInvalid` if the result overflows `target`, or if `target` is 
not
-/// `timestamp` / `timestamp_ns`.
-fn date_to_timestamp(days: i32, target: &PrimitiveType) -> 
Result<PrimitiveLiteral> {
-    let value = match target {
-        PrimitiveType::Timestamp => date::days_to_microseconds(days),
-        PrimitiveType::TimestampNs => date::days_to_nanoseconds(days),
-        _ => return Err(invalid_data!("Cannot convert a date to {target}")),
+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)?,
+        ),

Review Comment:
   Since this branched, #3522 added `date_to_timestamp` to this file 
([datum.rs#L1358-L1373](https://github.com/apache/iceberg-rust/blob/6cca8cdec6881d057581bdf9cc21c3ad426c0283/crates/iceberg/src/spec/values/datum.rs#L1358-L1373)).
 It does the same conversion with the same overflow check, built on 
`date::days_to_microseconds` and `date::days_to_nanoseconds` 
([temporal.rs#L50-L63](https://github.com/apache/iceberg-rust/blob/6cca8cdec6881d057581bdf9cc21c3ad426c0283/crates/iceberg/src/spec/values/temporal.rs#L50-L63)),
 and its error names the date and the target type. Could these two arms call it 
on current `main`?
   
   ```rust
   (PrimitiveType::Date, PrimitiveType::Timestamp | PrimitiveType::TimestampNs) 
=> {
       Datum::new(target_type.clone(), date_to_timestamp(val, target_type)?)
   }
   ```
   
   `MICROS_PER_DAY` and `NANOS_PER_DAY` (lines 1309 and 1311) can then go, 
because `temporal.rs` already defines both.



##########
crates/iceberg/src/spec/values/datum.rs:
##########
@@ -1355,19 +1306,64 @@ fn human_float<T: Float + LowerExp + FromStr>(value: T) 
-> String {
     }
 }
 
-/// Converts `days` since the epoch to a `timestamp` (microseconds) or 
`timestamp_ns`
-/// (nanoseconds) literal at midnight of that day.
-///
-/// # Errors
-/// Returns `DataInvalid` if the result overflows `target`, or if `target` is 
not
-/// `timestamp` / `timestamp_ns`.
-fn date_to_timestamp(days: i32, target: &PrimitiveType) -> 
Result<PrimitiveLiteral> {
-    let value = match target {
-        PrimitiveType::Timestamp => date::days_to_microseconds(days),
-        PrimitiveType::TimestampNs => date::days_to_nanoseconds(days),
-        _ => return Err(invalid_data!("Cannot convert a date to {target}")),
+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::TimestampNs) => {
+            
Datum::timestamp_nanos(val.checked_mul(NANOS_PER_MICRO).ok_or_else(overflow)?)
+        }
+        (PrimitiveType::Timestamptz, PrimitiveType::TimestamptzNs) => {
+            
Datum::timestamptz_nanos(val.checked_mul(NANOS_PER_MICRO).ok_or_else(overflow)?)
+        }
+        _ => {
+            return Err(invalid_data!(
+                "Can't convert datum from {source_type} type to {target_type} 
type."
+            ));
+        }
     };
-    value
-        .map(PrimitiveLiteral::Long)
-        .ok_or_else(|| invalid_data!("Date {days} (days from epoch) is out of 
range for {target}"))
+
+    Ok(datum)
+}
+
+fn overflow() -> Error {
+    invalid_data!("integer overflow")
 }

Review Comment:
   Could the micros-to-nanos arms report the value and type the way 
`date_to_timestamp` does? Binding a `timestamp` literal after 2262-04-11 to a 
`timestamp_ns` column, such as 
`Datum::timestamp_micros(9_223_372_036_854_776)`, fails with only "integer 
overflow", which doesn't tell the caller which literal or column caused it.
   
   ```suggestion
   fn micros_to_nanos(micros: i64, target_type: &PrimitiveType) -> Result<i64> {
       micros.checked_mul(NANOS_PER_MICRO).ok_or_else(|| {
           invalid_data!("Timestamp {micros} (microseconds from epoch) is out 
of range for {target_type}")
       })
   }
   ```
   
   The arms at lines 1351-1356 would then call `micros_to_nanos(val, 
target_type)?`, and the two overflow tests in `tests.rs` (lines 1740 and 1767) 
would check for "out of 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]

Reply via email to