mbutrovich opened a new issue, #3815:
URL: https://github.com/apache/iceberg-rust/issues/3815

   ## Apache Iceberg Rust version
   
   `main` (`6cca8cdec6881d057581bdf9cc21c3ad426c0283`)
   
   ## Describe the bug
   
   `Datum::to` converts a decimal literal to `long` by taking its unscaled 
value and ignoring the scale 
([datum.rs#L1174-L1176](https://github.com/apache/iceberg-rust/blob/6cca8cdec6881d057581bdf9cc21c3ad426c0283/crates/iceberg/src/spec/values/datum.rs#L1174-L1176)).
 So `1.50` and `150` both become `150`. Predicate binding converts each literal 
to the column type with `Datum::to` 
([predicate.rs#L419](https://github.com/apache/iceberg-rust/blob/6cca8cdec6881d057581bdf9cc21c3ad426c0283/crates/iceberg/src/expr/predicate.rs#L419)),
 so `long_col < 1.50` binds as `long_col < 150`. The existing tests use only 
scale 0 
([tests.rs#L1945-L1975](https://github.com/apache/iceberg-rust/blob/6cca8cdec6881d057581bdf9cc21c3ad426c0283/crates/iceberg/src/spec/values/tests.rs#L1945-L1975)).
   
   `decimal` to `long` is not in the [promotion 
table](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/format/spec.md?plain=1#L408-L416),
 and the [expressions 
spec](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/format/expressions-spec.md?plain=1#L127)
 says:
   
   > If the types cannot be aligned according to type promotion rules (for 
instance, `"goats" > -Infinity`), the predicate cannot be evaluated and 
implementations must fail.
   
   Java's `DecimalLiteral.to` returns `null` for every target other than 
`decimal` 
([Literals.java#L497-L504](https://github.com/apache/iceberg/blob/06f1574471c4603443a67f78af72e4e1d0aeacea/api/src/main/java/org/apache/iceberg/expressions/Literals.java#L497-L504)).
   
   ## To Reproduce
   
   ```rust
   use std::sync::Arc;
   
   use iceberg::expr::{Bind, Reference};
   use iceberg::spec::{Datum, NestedField, PrimitiveType, Schema, Type};
   
   #[test]
   fn decimal_to_long_drops_scale() {
       let long = Type::Primitive(PrimitiveType::Long);
       println!("{:?}", Datum::decimal_from_str("1.50").unwrap().to(&long));
       println!("{:?}", Datum::decimal_from_str("150").unwrap().to(&long));
   
       let schema = Arc::new(
           Schema::builder()
               .with_fields(vec![NestedField::required(1, "long_col", 
long).into()])
               .build()
               .unwrap(),
       );
       let bound = Reference::new("long_col")
           .less_than(Datum::decimal_from_str("1.50").unwrap())
           .bind(schema, true)
           .unwrap();
       println!("{bound}");
   }
   ```
   
   Output:
   
   ```text
   Ok(Datum { type: Long, literal: Long(150) })
   Ok(Datum { type: Long, literal: Long(150) })
   long_col < 150
   ```
   
   ## Expected behavior
   
   Converting a decimal to `long` returns an error, the way 
`Datum::string("x").to(date)` does ("Can't convert datum from string type to 
date type.").
   
   Only three tests depend on the current behavior. Without the arm at 
datum.rs#L1174-L1176, `cargo test -p iceberg --lib` fails only 
`test_datum_decimal_convert_to_long`, 
`test_datum_decimal_convert_to_long_above_max`, and 
`test_datum_decimal_convert_to_long_below_min` (2354 passed, 3 failed).
   
   ## Willingness to contribute
   
   I can contribute a fix for this bug independently
   


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