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

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


The following commit(s) were added to refs/heads/main by this push:
     new b231cf15ef fix(parquet): cap byte-budget level windows by value count 
(#11996)
b231cf15ef is described below

commit b231cf15efa5dbbbe4ae8503849aa6ab3cfc4e0c
Author: Adrian Garcia Badaracco <[email protected]>
AuthorDate: Tue Nov 3 02:20:48 2026 +0000

    fix(parquet): cap byte-budget level windows by value count (#11996)
    
    # Which issue does this PR close?
    
    - Closes #11994.
    
    # Rationale for this change
    
    `SubBatchStrategy::Levels` turns a value budget into a level count using
    the chunk's *average* level:value ratio. That ratio says nothing about
    how many values a single window holds. If 28 large values come before
    996 nulls, the first 37-level window contains all 28 values, so the
    dictionary page reaches about 2.2 GB before the spill check runs. That
    breaks the "at most two values per page" bound described in #10554.
    
    # What changes are included in this PR?
    
    - `SubBatchStrategy::Levels` now carries a `max_values` cap (twice the
    value budget) alongside the level count.
    - `write_granular_chunk` ends each level window at whichever comes
    first: the level count, or the point where `max_values` values have been
    seen. It reuses `window_end_for_values` to find that point.
    - When nulls are evenly spread the cap is never reached, so the number
    of mini-batches is unchanged. The throughput tradeoff from #10554 still
    holds. The added cost is one walk over the window's def levels, and only
    for `Materialized` def levels on the granular (large-value) path.
    
    # Are these changes tested?
    
    Yes. `test_column_writer_caps_dictionary_page_size_with_clustered_nulls`
    is a scaled-down version of the issue's reproducer: 28 distinct 64 KiB
    values, then 996 nulls, with a 64 KiB dictionary page limit. It fails
    without the cap (the dictionary page holds all 28 values) and passes
    with it.
    
    # Are there any user-facing changes?
    
    No API changes. Writers with clustered nulls and large values now
    produce bounded pages instead of failing with `Page uncompressed size
    overflow`.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 parquet/src/column/writer/byte_budget_chunker.rs | 22 +++++---
 parquet/src/column/writer/mod.rs                 | 65 ++++++++++++++++++++++--
 2 files changed, 77 insertions(+), 10 deletions(-)

diff --git a/parquet/src/column/writer/byte_budget_chunker.rs 
b/parquet/src/column/writer/byte_budget_chunker.rs
index 6e5917e11a..13acdecbe3 100644
--- a/parquet/src/column/writer/byte_budget_chunker.rs
+++ b/parquet/src/column/writer/byte_budget_chunker.rs
@@ -53,15 +53,20 @@ pub(crate) enum SubBatchStrategy {
     /// wherever they land — while value-exact windows cost throughput
     /// ([#10554]).
     ///
-    /// The round-up is bounded, which is what makes it an acceptable price. A
-    /// window spans `ceil(values * levels / values_in_chunk)` levels, so where
-    /// one value already fills the budget it covers at most two values,
-    /// whatever the null density. The page bound [#9972] added therefore still
-    /// holds; it is two values per page rather than one.
+    /// The round-up must stay bounded for that price to be acceptable. The
+    /// level count alone does not bound it: the ratio is a chunk *average*,
+    /// so a window over a run of clustered values can hold far more than the
+    /// budget (28 values of 76 MiB followed by 996 nulls all land in a
+    /// 37-level window, [#11994]). Each window is therefore also capped at
+    /// `max_values` values, twice the value budget, so the page bound [#9972]
+    /// added holds whatever the null layout: two values per page rather than
+    /// one. The cap walks the window's definition levels, but adds no
+    /// mini-batches where nulls are evenly spread.
     ///
     /// [#9972]: https://github.com/apache/arrow-rs/pull/9972
     /// [#10554]: https://github.com/apache/arrow-rs/pull/10554
-    Levels(usize),
+    /// [#11994]: https://github.com/apache/arrow-rs/issues/11994
+    Levels { levels: usize, max_values: usize },
 }
 
 /// Picks byte-budget-aware mini-batch sizes for one column.
@@ -257,7 +262,10 @@ impl ByteBudgetChunker {
                             .div_ceil(vals_in_chunk)
                             .max(1)
                     };
-                    
SubBatchStrategy::Levels(chunk_size.min(levels_per_subbatch))
+                    SubBatchStrategy::Levels {
+                        levels: chunk_size.min(levels_per_subbatch),
+                        max_values: values_per_subbatch.saturating_mul(2),
+                    }
                 })
             }
         }
diff --git a/parquet/src/column/writer/mod.rs b/parquet/src/column/writer/mod.rs
index 9d896dce01..555452ac16 100644
--- a/parquet/src/column/writer/mod.rs
+++ b/parquet/src/column/writer/mod.rs
@@ -866,7 +866,8 @@ impl<'a, E: ColumnValueEncoder> GenericColumnWriter<'a, E> {
     ///
     /// [`SubBatchStrategy::Values`] windows are cut on value boundaries,
     /// walking definition levels to find where the value budget is used up;
-    /// [`SubBatchStrategy::Levels`] windows are a fixed level count. See
+    /// [`SubBatchStrategy::Levels`] windows are a fixed level count, capped
+    /// at a maximum number of values. See
     /// [`SubBatchStrategy`] for which budget gets which and why.
     ///
     /// For repeated/nested columns sub-batches then extend to the next
@@ -895,7 +896,11 @@ impl<'a, E: ColumnValueEncoder> GenericColumnWriter<'a, E> 
{
         // level, and a value-exact window spans at least one level, so each
         // iteration below makes progress (`sub_end > sub_start`).
         debug_assert!(
-            matches!(sub_batch, SubBatchStrategy::Values(n) | 
SubBatchStrategy::Levels(n) if n >= 1),
+            matches!(
+                sub_batch,
+                SubBatchStrategy::Values(n) | SubBatchStrategy::Levels { 
levels: n, max_values: _ }
+                    if n >= 1
+            ),
             "chunker must size at least one value or level"
         );
         let max_def_level = self.descr.max_def_level();
@@ -906,7 +911,18 @@ impl<'a, E: ColumnValueEncoder> GenericColumnWriter<'a, E> 
{
                 SubBatchStrategy::Values(n) => {
                     Self::window_end_for_values(chunk_def, chunk_size, 
max_def_level, sub_start, n)
                 }
-                SubBatchStrategy::Levels(n) => (sub_start + n).min(chunk_size),
+                SubBatchStrategy::Levels { levels, max_values } => {
+                    // Cap the ratio-sized window at `max_values` values so
+                    // clustered values cannot pile into one window.
+                    let level_end = (sub_start + levels).min(chunk_size);
+                    Self::window_end_for_values(
+                        chunk_def,
+                        level_end,
+                        max_def_level,
+                        sub_start,
+                        max_values,
+                    )
+                }
             };
             let sub_end = match chunk_rep {
                 LevelDataRef::Materialized(levels) => {
@@ -3733,6 +3749,49 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_column_writer_caps_dictionary_page_size_with_clustered_nulls() {
+        // Regression test for #11994. Large distinct values followed by a run
+        // of nulls: the chunk's average level:value ratio is ~36 levels per
+        // value, so a ratio-sized window over the leading values covers all
+        // of them. Each window must also be capped by value count, or every
+        // value lands in the dictionary page before its spill check runs.
+        let value_size = 64 * 1024;
+        let num_values = 28;
+        let num_nulls = 996;
+
+        let props = WriterProperties::builder()
+            .set_writer_version(WriterVersion::PARQUET_1_0)
+            .set_dictionary_enabled(true)
+            .set_dictionary_page_size_limit(value_size)
+            .build();
+
+        let data: Vec<_> = (0..num_values)
+            .map(|i| {
+                let mut v = vec![0u8; value_size];
+                v[..8].copy_from_slice(&(i as u64).to_le_bytes());
+                ByteArray::from(v)
+            })
+            .collect();
+        let mut def_levels = vec![1i16; num_values];
+        def_levels.extend(std::iter::repeat_n(0i16, num_nulls));
+
+        let pages =
+            write_and_collect_pages::<ByteArrayType>(props, 1, 0, &data, 
Some(&def_levels), None);
+        assert!(
+            pages.dict_page_size > 0,
+            "expected the column to dictionary-encode"
+        );
+        // At most two values per window. Before the fix the dictionary page
+        // held all 28 values (~1.8 MiB).
+        assert!(
+            pages.dict_page_size <= 3 * value_size,
+            "dictionary page {} exceeds three values ({}B)",
+            pages.dict_page_size,
+            3 * value_size,
+        );
+    }
+
     #[test]
     fn test_column_writer_caps_page_size_for_fixed_len_byte_array() {
         // Coverage for `ParquetValueType::byte_size` override on

Reply via email to