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

alamb pushed a commit to branch 59_maintenance
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/59_maintenance by this push:
     new dc6cf4778a [59_maintenance] Parquet: list the dictionary page first in 
page_encoding_stats (#11699)
dc6cf4778a is described below

commit dc6cf4778acf6c6d210cf063023c0ecc702e3c8a
Author: Jeffrey Vo <[email protected]>
AuthorDate: Tue Oct 20 04:01:21 2026 +0900

    [59_maintenance] Parquet: list the dictionary page first in 
page_encoding_stats (#11699)
    
    - Backport https://github.com/apache/arrow-rs/pull/11682
    - For inclusion in https://github.com/apache/arrow-rs/issues/11664
    
    Cherry-pick of
    
https://github.com/apache/arrow-rs/commit/33d338ddf661b6e85364a9b0bd4d367c3468257d
    
    Co-authored-by: Adrian Garcia Badaracco 
<[email protected]>
    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 8b9aea2e12..7bce3ce7ab 100644
--- a/parquet/src/arrow/arrow_writer/mod.rs
+++ b/parquet/src/arrow/arrow_writer/mod.rs
@@ -2210,6 +2210,60 @@ mod tests {
         roundtrip(batch, Some(SMALL_SIZE / 2));
     }
 
+    /// 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 b75dbc2e99..eb1c96683b 100644
--- a/parquet/src/column/writer/mod.rs
+++ b/parquet/src/column/writer/mod.rs
@@ -1631,11 +1631,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