abnobdoss commented on code in PR #2508:
URL: https://github.com/apache/iceberg-rust/pull/2508#discussion_r3406779817
##########
crates/iceberg/src/spec/values/datum.rs:
##########
@@ -1130,6 +1137,21 @@ impl Datum {
(PrimitiveLiteral::Int128(val), _, PrimitiveType::Long) =>
{
Ok(Datum::i128_to_i64(*val))
}
+ (
+ PrimitiveLiteral::Int128(val),
+ PrimitiveType::Decimal {
+ scale: self_scale, ..
+ },
+ PrimitiveType::Decimal {
+ precision,
+ scale: target_scale,
+ },
+ ) if self_scale == target_scale =>
Datum::decimal_from_mantissa(
Review Comment:
Definitely! I've added the arm and the clarifying comment.
##########
crates/iceberg/src/spec/values/tests.rs:
##########
@@ -1424,3 +1424,115 @@ fn test_date_from_json_as_number() {
// Both formats should produce the same Literal value
}
+
+#[test]
+fn test_datum_to_decimal_narrows_precision_when_scale_matches() {
+ let target_type = Type::Primitive(PrimitiveType::Decimal {
+ precision: 9,
+ scale: 2,
+ });
+ let datum = Datum::decimal_from_str("123.45").unwrap();
+
+ let converted = datum.to(&target_type).unwrap();
+
+ assert_eq!(converted.data_type(), &PrimitiveType::Decimal {
+ precision: 9,
+ scale: 2,
+ });
+ assert_eq!(converted.literal(), &PrimitiveLiteral::Int128(12345));
+}
+
+#[test]
+fn test_datum_to_decimal_widens_precision_when_scale_matches() {
+ let target_type = Type::Primitive(PrimitiveType::Decimal {
+ precision: 38,
+ scale: 2,
+ });
+ let datum =
Datum::decimal_with_precision(decimal_from_i128_with_scale(12345, 2),
9).unwrap();
+
+ let converted = datum.to(&target_type).unwrap();
+
+ assert_eq!(converted.data_type(), &PrimitiveType::Decimal {
+ precision: 38,
+ scale: 2,
+ });
+ assert_eq!(converted.literal(), &PrimitiveLiteral::Int128(12345));
+}
+
+#[test]
+fn test_datum_to_decimal_rejects_precision_too_narrow() {
Review Comment:
I've made the fixture changes! I can raise a follow-up PR for enforcing the
decimal constant if we're open to that change and would like to enforce it?
--
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]