laskoviymishka commented on code in PR #3574:
URL: https://github.com/apache/iceberg-rust/pull/3574#discussion_r4429217420
##########
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:
+1 to failing on the narrowing arms. The spec is explicit ("must fail rather
than insert unsafe casts"), and an error is better than the wrong answer we
return on main today.
Since this means Rust deliberately diverges from Java's flooring in
Literals.java, could we raise it upstream so the clients don't drift apart?
Either a short dev-list thread or an issue/PR in apache/iceberg.
If Java is wrong here, it should be fixed there too. If not, we should
understand why.
--
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]