linyiru opened a new issue, #12541:
URL: https://github.com/apache/arrow-rs/issues/12541

   ### Describe the bug
   
   With the default `WriterProperties`, the parquet writer truncates the min 
and max statistics of a `FIXED_LEN_BYTE_ARRAY` column to 64 bytes. For a column 
of `type_length` greater than 64 it therefore writes bounds that are shorter 
than `type_length`, which are not values of the column's type.
   
   Parquet C++ does not accept them: with pyarrow 26.0.1, reading the 
statistics of such a column chunk terminates the process 
(`parquet::ParquetException: Unexpected end of stream`, uncaught), and a 
`read_table` with a filter on the column never returns. Without a filter the 
table reads fine, and DuckDB 2.0.1 reads the file and filters it correctly.
   
   `can_truncate_value` allows truncation for every `FIXED_LEN_BYTE_ARRAY` 
column that is neither a decimal nor `Float16`:
   
   
https://github.com/apache/arrow-rs/blob/2d2919c18b3856a11d8a61560b2b3781bddc87da/parquet/src/column/writer/mod.rs#L1361-L1377
   
   and both limits are 64 by default (`DEFAULT_STATISTICS_TRUNCATE_LENGTH`, 
`DEFAULT_COLUMN_INDEX_TRUNCATE_LENGTH`), so this is what a writer gets without 
asking for anything.
   
   The format says of `Statistics.min_value` / `max_value` (and in the same 
words of the `ColumnIndex` bounds):
   
   > These may be the actual minimum and maximum values found on a page or 
column chunk, but can also be (more compact) values that do not exist on a page 
or column chunk. [...] Such more compact values must still be valid values 
within the column's logical type.
   >
   > Values are encoded using PLAIN encoding, except that variable-length byte 
arrays do not include a length prefix.
   
   A PLAIN-encoded `FIXED_LEN_BYTE_ARRAY(100)` value is 100 bytes, so a 64-byte 
bound is not a valid value of the column, however it is flagged 
(`is_min_value_exact` / `is_max_value_exact` are written as `false`).
   
   ### To Reproduce
   
   `parquet` 60.2.0, and the same on `main` at 2d2919c18.
   
   ```rust
   use std::{fs::File, sync::Arc};
   
   use parquet::{
       data_type::{ByteArray, FixedLenByteArray, FixedLenByteArrayType},
       file::{
           properties::WriterProperties,
           reader::{FileReader, SerializedFileReader},
           writer::SerializedFileWriter,
       },
       schema::parser::parse_message_type,
   };
   
   fn main() -> Result<(), Box<dyn std::error::Error>> {
       let path = std::env::args().nth(1).expect("a path to write");
       let schema = Arc::new(parse_message_type(
           "message m { required fixed_len_byte_array(100) v; }",
       )?);
       // A second argument turns the truncation off, for a file to compare 
with.
       let builder = WriterProperties::builder();
       let builder = if std::env::args().nth(2).is_some() {
           builder.set_statistics_truncate_length(None)
       } else {
           builder
       };
       let mut writer = SerializedFileWriter::new(File::create(&path)?, schema, 
Arc::new(builder.build()))?;
       let mut row_group = writer.next_row_group()?;
       let mut column = row_group.next_column()?.expect("one column");
       let values: Vec<FixedLenByteArray> = (0u8..4)
           .map(|i| FixedLenByteArray::from(ByteArray::from(vec![b'a' + i; 
100])))
           .collect();
       column.typed::<FixedLenByteArrayType>().write_batch(&values, None, 
None)?;
       column.close()?;
       row_group.close()?;
       writer.close()?;
   
       let reader = SerializedFileReader::new(File::open(&path)?)?;
       let chunk = reader.metadata().row_group(0).column(0);
       let statistics = chunk.statistics().expect("statistics");
       println!(
           "type_length {}, min {} bytes, max {} bytes, min exact {:?}, max 
exact {:?}",
           chunk.column_descr().type_length(),
           statistics.min_bytes_opt().map_or(0, <[u8]>::len),
           statistics.max_bytes_opt().map_or(0, <[u8]>::len),
           statistics.min_is_exact(),
           statistics.max_is_exact(),
       );
       Ok(())
   }
   ```
   
   ```
   $ cargo run --release -- default.parquet
   type_length 100, min 64 bytes, max 64 bytes, min exact false, max exact false
   $ cargo run --release -- untruncated.parquet none
   type_length 100, min 100 bytes, max 100 bytes, min exact true, max exact true
   ```
   
   Reading both with pyarrow 26.0.1 (Python 3.12, macOS arm64):
   
   ```python
   import pyarrow.parquet as pq
   
   pq.read_table(path)                                        # (1)
   pq.read_metadata(path).row_group(0).column(0).statistics   # (2)
   pq.read_table(path, filters=[("v", ">=", b"b" * 100)])     # (3)
   ```
   
   | | `untruncated.parquet` | `default.parquet` |
   |---|---|---|
   | (1) | 4 rows, `fixed_size_binary[100]` | 4 rows, `fixed_size_binary[100]` |
   | (2) | min and max of 100 bytes | the process ends: `libc++abi: terminating 
due to uncaught exception of type parquet::ParquetException: Unexpected end of 
stream` (exit status 134) |
   | (3) | 3 rows | does not return: still running, at 0% CPU, after ten 
minutes |
   
   DuckDB 2.0.1 reads both files, reports bounds of 64 and of 100 bytes in 
`parquet_metadata`, and returns 3 rows for the same filter on either.
   
   ### Expected behavior
   
   Bounds of a `FIXED_LEN_BYTE_ARRAY` column that are either `type_length` 
bytes long or not written. Since a bound of the full length is no more compact 
than the value, I would expect `can_truncate_value` to be `false` for 
`FIXED_LEN_BYTE_ARRAY` whatever its logical type, in the chunk and page 
statistics and in the column index alike; leaving the bounds of such a column 
out when they are longer than the limit would be the other reading of the 
setting.
   
   ### Additional context
   
   - The way around it today is `set_statistics_truncate_length(None)` (and 
`set_column_index_truncate_length(None)`), which also stops the truncation of 
every `BYTE_ARRAY` column of the file, since the limits are not per column 
(#8094), or turning statistics off for the column.
   - That Parquet C++ ends the process, or hangs, on a bound of the wrong 
length rather than reporting an error is a problem of its own on that side; I 
have not looked for a report of it there.
   - Only the chunk statistics were inspected above. The column index goes 
through the same `can_truncate_value` check with its own limit of 64; I did not 
read it back.
   


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

Reply via email to