This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-11682-3658fc01e8fed4179d476aa3efc34b2723f89dd3
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 33d338ddf661b6e85364a9b0bd4d367c3468257d
Author: Adrian Garcia Badaracco <[email protected]>
AuthorDate: Sun Oct 18 14:28:16 2026 +0000

    Parquet: list the dictionary page first in page_encoding_stats (#11682)
    
    # Which issue does this PR close?
    
    - Closes https://github.com/apache/arrow-rs/issues/11659
    
    # Rationale for this change
    
    Since 59.0.0, `ArrowWriter` writes the dictionary page's
    `encoding_stats` entry after the data page entries. The file layout did
    not change: the dictionary page is still written first. Arrow C++
    
([`IsColumnChunkFullyDictionaryEncoded`](https://github.com/apache/arrow/blob/9f0341314ca96eb58081d4132c77b460d880b2b0/cpp/src/parquet/file_reader.cc#L63-L88))
    requires `encoding_stats[0]` to be the dictionary page. With the 59.x
    order, Arrow C++ does not see any parquet-rs column chunk as fully
    dictionary encoded, so it cannot use its dictionary fast path.
    
    | | `encoding_stats` (file order) |
    |---|---|
    | before this PR | `DATA_PAGE RLE_DICTIONARY`, `DICTIONARY_PAGE PLAIN` |
    | after this PR (and 58.x) | `DICTIONARY_PAGE PLAIN`, `DATA_PAGE
    RLE_DICTIONARY` |
    
    The new order was not a design goal of
    https://github.com/apache/arrow-rs/pull/10020. It was a side effect: the
    deferred-ordering page writer gets the dictionary page last, and the
    stats were recorded in the order the pages were produced. Review
    accepted this because the spec defines the list as a set
    (https://github.com/apache/arrow-rs/pull/10051 was closed for that
    reason). But a known reader depends on the order, and restoring it
    changes only metadata, so the change has no memory or throughput cost.
    
    # What changes are included in this PR?
    
    `GenericColumnWriter::write_dictionary_page` now inserts the dictionary
    entry at the front of `encoding_stats` instead of appending it. The
    dictionary page is always laid out first, on both the deferred (Arrow)
    path and the `SerializedFileWriter` path, so the stats now follow file
    order. On the non-deferred path the list is still empty when the
    dictionary is written, so its output does not change. When the writer
    falls back from dictionary to plain encoding, the result is `DICTIONARY,
    RLE_DICTIONARY data, PLAIN data`, which also matches the file layout.
    
    # Are these changes tested?
    
    Yes. The new `dictionary_page_encoding_stats_lists_dictionary_first`
    test (adapted from https://github.com/apache/arrow-rs/pull/10051) checks
    that `stats[0]` is the dictionary page. It fails on `main` and passes
    with this change. The existing column writer and arrow writer tests
    pass.
    
    # Are there any user-facing changes?
    
    Only the order of `page_encoding_stats` in files written by
    `ArrowWriter`. It goes back to the order used before 59.0.0.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 parquet/src/arrow/arrow_writer/mod.rs | 54 +++++++++++++++++++++++++++++++++++
 parquet/src/column/writer/mod.rs      | 17 +++++++----
 2 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/parquet/src/arrow/arrow_writer/mod.rs 
b/parquet/src/arrow/arrow_writer/mod.rs
index 180d20625a..09b092b35f 100644
--- a/parquet/src/arrow/arrow_writer/mod.rs
+++ b/parquet/src/arrow/arrow_writer/mod.rs
@@ -2330,6 +2330,60 @@ mod tests {
         );
     }
 
+    /// The `page_encoding_stats` list of a dictionary-encoded column chunk 
must
+    /// list the dictionary page *before* the data pages, matching the on-disk
+    /// page layout (the dictionary page is always written first) and the order
+    /// produced by the column-at-a-time `SerializedFileWriter`.
+    #[test]
+    fn dictionary_page_encoding_stats_lists_dictionary_first() {
+        use crate::basic::PageType;
+        use crate::file::metadata::ParquetMetaDataOptions;
+
+        // A low-cardinality column so it stays dictionary-encoded, with enough
+        // rows to emit at least one data page in addition to the dictionary 
page.
+        let schema = Arc::new(Schema::new(vec![Field::new("k", 
DataType::Int32, true)]));
+        let values: Vec<Option<i32>> = (0..4096).map(|i| Some(i % 
4)).collect();
+        let array = Int32Array::from(values);
+        let batch = RecordBatch::try_new(schema.clone(), 
vec![Arc::new(array)]).unwrap();
+
+        let mut buffer = Vec::new();
+        let mut writer = ArrowWriter::try_new(&mut buffer, schema.clone(), 
None).unwrap();
+        writer.write(&batch).unwrap();
+        writer.close().unwrap();
+        let buffer = Bytes::from(buffer);
+
+        // Read the *full*, ordered page encoding stats. By default this crate
+        // collapses them to a bitmask, which would discard the ordering this
+        // test is about, so opt out of that here.
+        let options = 
ParquetMetaDataOptions::new().with_encoding_stats_as_mask(false);
+        let metadata = ParquetMetaDataReader::new()
+            .with_metadata_options(Some(options))
+            .parse_and_finish(&buffer)
+            .unwrap();
+
+        let col = metadata.row_group(0).column(0);
+        assert!(
+            col.dictionary_page_offset().is_some(),
+            "column should be dictionary encoded"
+        );
+        let stats = col
+            .page_encoding_stats()
+            .expect("full (non-mask) page encoding stats should be present");
+
+        // Arrow C++ (`IsColumnChunkFullyDictionaryEncoded`) requires the
+        // dictionary page entry to be the first one.
+        assert_eq!(
+            stats[0].page_type,
+            PageType::DICTIONARY_PAGE,
+            "the DICTIONARY_PAGE encoding stat must come first, got {stats:?}"
+        );
+        assert!(
+            stats[1..]
+                .iter()
+                .all(|s| s.page_type == PageType::DATA_PAGE)
+        );
+    }
+
     fn get_bytes_after_close(schema: SchemaRef, expected_batch: &RecordBatch) 
-> Vec<u8> {
         let mut buffer = vec![];
 
diff --git a/parquet/src/column/writer/mod.rs b/parquet/src/column/writer/mod.rs
index 4f8c04b4ac..e5a66e913f 100644
--- a/parquet/src/column/writer/mod.rs
+++ b/parquet/src/column/writer/mod.rs
@@ -1784,11 +1784,18 @@ impl<'a, E: ColumnValueEncoder> GenericColumnWriter<'a, 
E> {
         };
 
         self.encodings.insert(compressed_page.encoding());
-        self.encoding_stats.push(PageEncodingStats {
-            page_type: PageType::DICTIONARY_PAGE,
-            encoding: compressed_page.encoding(),
-            count: 1,
-        });
+        // The dictionary page is always laid out first in the column chunk,
+        // even when a page writer that defers dictionary ordering receives it
+        // after the data pages. Keep `encoding_stats` in file order, as some
+        // readers (e.g. Arrow C++) expect the dictionary page entry first.
+        self.encoding_stats.insert(
+            0,
+            PageEncodingStats {
+                page_type: PageType::DICTIONARY_PAGE,
+                encoding: compressed_page.encoding(),
+                count: 1,
+            },
+        );
         let page_spec = self.page_writer.write_page(compressed_page)?;
         self.update_metrics_for_page(page_spec);
         // For the directory page, don't need to update column/offset index.

Reply via email to