m4ru1 opened a new issue, #19167:
URL: https://github.com/apache/iceberg/issues/19167

   ### Apache Iceberg version
   
   main (development)
   
   ### Query engine
   
   None
   
   ### Please describe the bug 🐞
   
   Parquet Bloom filters hash the raw bytes written to a column, so evaluating 
an `=` or `IN` predicate requires converting the query's `BigDecimal` literal 
to the file column's physical encoding and scale. `ParquetBloomRowGroupFilter` 
instead hashes decimals using the literal's own scale and the read schema's 
minimal byte length, causing `Parquet.read(...).filter(...)` to skip matching 
row groups or fail with `IllegalArgumentException`.
   
   For example, when a `DECIMAL(9, 2)` column with a Bloom filter (and 
dictionary disabled) contains `12.50`:
   
   ```java
   // Succeeds when literal scale matches the file scale (2)
   readFile(f1, intDec, equal("dec", new BigDecimal("12.50"))); // -> [12.50]
   
   // Silently skips the matching row group when literal scale differs (1)
   readFile(f1, intDec, equal("dec", new BigDecimal("12.5")));  // -> []
   ```
   
   The same mismatch affects three scenarios:
   - **Different literal scale** (`12.5` or `12.500` against `12.50`, since 
`Literals.DecimalLiteral` preserves input scale): `INT32`/`INT64` columns hash 
the unrescaled integer (`125` instead of `1250`) and skip matching row groups; 
`FIXED_LEN_BYTE_ARRAY` columns throw `IllegalArgumentException` (`wrong scale`).
   - **Non-minimal `FIXED_LEN_BYTE_ARRAY` or `BINARY` encodings** (e.g., 
`DECIMAL(20, 2)` stored in `fixed_len_byte_array(16)`, or `DECIMAL(10, 2)` in 
`binary`): the filter hashes 9 bytes instead of the column's 16-byte or 
variable-length encoding and skips matching row groups.
   - **Decimal precision widening** (`DECIMAL(20, 2)` -> `DECIMAL(30, 2)`): 
wider literals in `=` or `IN` throw `IllegalArgumentException` (`too large`) on 
`FIXED_LEN_BYTE_ARRAY`, or truncate high bits via `intValue()`/`longValue()` on 
`INT32`/`INT64`.
   
   Reproducer on `main` (`ce1ce41`):
   
   ```java
   Schema intDec = new Schema(optional(1, "dec", Types.DecimalType.of(9, 2)));
   InputFile f1 = writeFile(intDec, new BigDecimal("12.50"));
   System.out.println("1. same scale (12.50) -> " + readFile(f1, intDec, 
equal("dec", new BigDecimal("12.50"))));
   System.out.println("2. diff scale (12.5)  -> " + readFile(f1, intDec, 
equal("dec", new BigDecimal("12.5"))));
   
   MessageType fixed16Type = MessageTypeParser.parseMessageType(
       "message table { optional fixed_len_byte_array(16) dec (DECIMAL(20, 2)) 
= 1; }");
   InMemoryOutputFile fixed16Out = new InMemoryOutputFile();
   try (ParquetWriter<Group> writer = 
ExampleParquetWriter.builder(ParquetIO.file(fixed16Out))
       
.withType(fixed16Type).withDictionaryEncoding(false).withBloomFilterEnabled("dec",
 true).build()) {
     writer.write(new SimpleGroupFactory(fixed16Type).newGroup().append("dec",
         Binary.fromConstantByteArray(DecimalUtil.toReusedFixLengthBytes(20, 2, 
new BigDecimal("12.34"), new byte[16]))));
   }
   Schema dec20 = new Schema(optional(1, "dec", Types.DecimalType.of(20, 2)));
   System.out.println("3. FIXED(16) (12.34)  -> " + 
readFile(fixed16Out.toInputFile(), dec20, equal("dec", new 
BigDecimal("12.34"))));
   
   Schema dec30 = new Schema(optional(1, "dec", Types.DecimalType.of(30, 2)));
   InputFile f2 = writeFile(dec20, new BigDecimal("1.00"), new 
BigDecimal("50.00"), new BigDecimal("100.00"));
   try {
     System.out.println("4. widened IN         -> " + readFile(f2, dec30,
         in("dec", new BigDecimal("50.00"), new 
BigDecimal("1234567890123456789012.34"))));
   } catch (Exception e) {
     System.out.println("4. widened IN         -> " + 
e.getClass().getSimpleName() + ": " + e.getMessage());
   }
   ```
   
   Output:
   
   ```text
   1. same scale (12.50) -> [12.50]
   2. diff scale (12.5)  -> []
   3. FIXED(16) (12.34)  -> []
   4. widened IN         -> IllegalArgumentException: Cannot write value as 
decimal(20,2), too large: 1234567890123456789012.34
   ```
   
   **Expected behavior:** `ParquetBloomRowGroupFilter` should never skip row 
groups containing matching decimal rows across `INT32`, `INT64`, 
`FIXED_LEN_BYTE_ARRAY`, and `BINARY` encodings or differing literal scales, and 
should treat literals that cannot be represented in the file column's 
`(precision, scale)` as `ROWS_CANNOT_MATCH` without throwing or truncating.
   
   **Cause:** `ParquetBloomRowGroupFilter.shouldRead` hashes `INT32`/`INT64` 
decimals via `intValue()`/`longValue()` without rescaling or range checks, and 
hashes `FIXED_LEN_BYTE_ARRAY` and `BINARY` decimals using 
`TypeUtil.decimalRequiredBytes(readPrecision)` instead of the file column's 
`DecimalLogicalTypeAnnotation` and byte length.
   
   ### Willingness to contribute
   
   - [x] I can contribute a fix for this bug independently
   - [ ] I would be willing to contribute a fix for this bug with guidance from 
the Iceberg community
   - [ ] I cannot contribute a fix for this bug at this time
   


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