yshcz opened a new issue, #12479: URL: https://github.com/apache/arrow-rs/issues/12479
### Describe the bug Hi, this is a bug where `ArrowWriter` writes a wrong `boundary_order` to the column index for certain inputs. According to [the spec](https://github.com/apache/parquet-format/blob/6f879257c76e61f05fb4d97da952fae452cb858a/src/main/thrift/parquet.thrift#L1342-L1348), `boundary_order` tells in which direction `min_values` and `max_values` of the column index are ordered: > Stores whether both min_values and max_values are ordered and if so, in which direction. This allows readers to perform binary searches in both lists. The current implementation writes the order of the actual min and max values of the pages, not the order of the values in the column index. The writer truncates the values in `min_values` and `max_values` to 64 bytes by default (`column_index_truncate_length`). A UTF-8 string is cut at a Unicode code point boundary, so it does not always keep 64 bytes: one value may keep 64 bytes and another 63. Because of this, the truncated values can be in a different order than the actual values. The writer, however, decides `boundary_order` from the actual values. The same can happen with binary values, for a different reason. When the writer truncates a max, it also increments the last byte, so that the result is still an upper bound. The increment can carry. For example, take two consecutive pages whose largest values are `01 FF FF .. FF` (65 bytes) and `02`: - `01 FF .. FF` sorts before `02`, so the actual max ascends. - For the first page the writer keeps the first 64 bytes and adds one. Every `FF` overflows, and the result is `02 00 .. 00` (64 bytes). - For the second page the max stays `02`. - `02 00 .. 00` sorts after `02`, so `max_values` descends. A reader that trusts `boundary_order` and binary searches `min_values` and `max_values` of the column index can then possibly skip pages that contain matching rows. ### To Reproduce With `parquet` 60.2.0 and the default writer properties, write one string column that holds only two distinct values: 30,000 rows of `first` followed by 30,000 rows of `second`. ``` first = "a" * 63 + "é" + "é" (63 + 2 + 2 = 67 bytes) second = "a" * 63 + "b" (63 + 1 = 64 bytes) ``` `first` is longer than 64 bytes, so it is truncated. The position after 64 bytes is not a code point boundary, because bytes 64 and 65 are the two-byte `é`. So `first` is cut after 63 bytes and keeps `"a" * 63`. `second` is exactly 64 bytes and is not truncated. `first` sorts after `second`, but the truncated `first` sorts before `second`. ```rust use std::sync::Arc; use arrow_array::{ArrayRef, RecordBatch, StringArray}; use bytes::Bytes; use parquet::arrow::ArrowWriter; use parquet::file::page_index::column_index::ColumnIndexMetaData; use parquet::file::reader::{FileReader, SerializedFileReader}; use parquet::file::serialized_reader::ReadOptionsBuilder; fn main() { let first = format!("{}éé", "a".repeat(63)); // 67 bytes let second = format!("{}b", "a".repeat(63)); // 64 bytes assert!(first > second); // 30,000 rows of `first`, then 30,000 rows of `second` let values = std::iter::repeat_n(first.as_str(), 30_000) .chain(std::iter::repeat_n(second.as_str(), 30_000)); let array = Arc::new(StringArray::from_iter_values(values)) as ArrayRef; let batch = RecordBatch::try_from_iter([("a", array)]).unwrap(); let mut buf = Vec::new(); let mut writer = ArrowWriter::try_new(&mut buf, batch.schema(), None).unwrap(); writer.write(&batch).unwrap(); writer.close().unwrap(); let options = ReadOptionsBuilder::new().with_page_index().build(); let reader = SerializedFileReader::new_with_options(Bytes::from(buf), options).unwrap(); let index = reader.metadata().page_index().unwrap().column_index(0, 0).unwrap(); let ColumnIndexMetaData::BYTE_ARRAY(pages) = index else { panic!("expected a byte array column index") }; for page in 0..pages.num_pages() as usize { let min = std::str::from_utf8(pages.min_value(page).unwrap()).unwrap(); let max = std::str::from_utf8(pages.max_value(page).unwrap()).unwrap(); // The first 60 bytes are all "a", so print the length and the rest. let show = |v: &str| format!("{} bytes, ..{:?}", v.len(), &v[60..]); println!("page {page}: min_values = {}; max_values = {}", show(min), show(max)); } println!("boundary_order = {:?}", index.get_boundary_order()); } ``` The program prints `min_values` and `max_values` of each page, and then `boundary_order`: ``` page 0: min_values = 63 bytes, .."aaa"; max_values = 63 bytes, .."aab" page 1: min_values = 64 bytes, .."aaab"; max_values = 63 bytes, .."aab" page 2: min_values = 64 bytes, .."aaab"; max_values = 64 bytes, .."aaab" boundary_order = Some(DESCENDING) ``` A data page holds about 20,000 rows by default (`data_page_row_count_limit`), so the rows fall into three pages: | page | values | actual min | actual max | `min_values` | `max_values` | |---|---|---|---|---|---| | 0 | `first` | `first` | `first` | `"a" * 63` | `"a" * 62 + "b"` | | 1 | `first` and `second` | `second` | `first` | `second` | `"a" * 62 + "b"` | | 2 | `second` | `second` | `second` | `second` | `second` | The actual min and max descend, so the writer records `DESCENDING`. But `min_values` ascends from page 0 to page 1. ### Expected behavior `boundary_order` describes the order of `min_values` and `max_values` in the column index. For the file above that is `UNORDERED`. ### Additional context _No response_ ### Willingness to contribute I plan to 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]
