Skip to content

Parquet LevelEncoder is much too eager to write short rle runs #7739

Open

Description

@jhorstmann

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

While looking into parquet loading performance, I noticed that the definition levels for simple nullable columns often contain very short rle runs. These seem to mess with branch prediction when decoding and decrease performance.

I managed to reproduce this in a benchmark by creating a custom RleEncoder for a bitwidth of 1, which only writes bitpacked runs. With 90% of bits being set, these are the results:

rle_decoder/decode_bitpacked
                        time:   [121.83 µs 121.98 µs 122.13 µs]
                        thrpt:  [8.5857 Gelem/s 8.5966 Gelem/s 8.6068 Gelem/s]
rle_decoder/decode_hybrid
                        time:   [661.33 µs 662.31 µs 663.56 µs]
                        thrpt:  [1.5802 Gelem/s 1.5832 Gelem/s 1.5856 Gelem/s]

Note the decoder in this benchmark is the same for both cases, the only difference is how the levels are serialized.

I also hacked some statistics into the decoder. For the above benchmark these are the distributions of rle vs bitpacked encoded runs. On average, the rle runs are only 16 elements long, and the bitpacked only encoding even seems to be shorter:

[parquet/benches/rle.rs:292:9] bitpacked_data.len() = 135169
[parquet/benches/rle.rs:292:9] &bitpacked_stats = HybridRleStats {
    num_values: 1048576,
    num_rle_values: 0,
    num_rle_runs: 0,
    num_bitpacked_values: 1048576,
    num_bitpacked_runs: 2048,
}
[parquet/benches/rle.rs:296:9] hybrid_data.len() = 157050
[parquet/benches/rle.rs:296:9] &hybrid_stats = HybridRleStats {
    num_values: 1048576,
    num_rle_values: 501915,
    num_rle_runs: 29541,
    num_bitpacked_values: 546661,
    num_bitpacked_runs: 29541,
}

Describe the solution you'd like

I'd like the RleEncoder to be smarter about switching between bitpacked and rle runs. Maybe this is a one-line change in encodings/rle.rs, but I find the logic there a bit hard to follow. Alternatives could be a specialized implementation for single-bit levels, or making this configurable for the user.

Activity

  1. added
    enhancement
    on Jun 22, 2025
  2. jhorstmann commented on Jun 24, 2025

    @jhorstmann
    ContributorAuthor

    This idea is related to the parquet tuning topic, so @alamb, @XiangpengHao and @JigaoLuo might be interested

  3. alamb commented on Jun 24, 2025

    @alamb
    Contributor

    This sounds pretty amazing to me @jhorstmann

    I don't know of any changes to that RLE/bit packing logic -- and in fact git blame shows it came in via the initial implementation from @sunchao 7+ years ago: https://github.com/apache/arrow-rs/blame/df702cfc714dd25060f6a493570221df18b2c598/parquet/src/encodings/rle.rs#L258

    Adjusting the heuristics / optimizing it sounds like a great idea

  4. XiangpengHao commented on Jun 24, 2025

    @XiangpengHao
    Contributor

    This is super cool @jhorstmann

    As a parallel work, I'm optimizing decoding bit-packed data, and implemented the algorithm in this paper: https://www.microsoft.com/en-us/research/publication/selection-pushdown-in-column-stores-using-bit-manipulation-instructions/

    here: https://github.com/XiangpengHao/bmi-select

    I'll think about whether/how to integrate it into our decoder

  5. XiangpengHao commented on Jun 24, 2025

    @XiangpengHao
    Contributor

    According to this paper by @XinyuZeng :

    For integer columns, Parquet first dictionary encodes and then
    applies a hybrid of RLE and Bitpacking to the dictionary codes.
    If the same value repeats ≥ 8 times consecutively, it uses RLE;
    otherwise, it uses bitpacking. Interestingly, we found that the RLE-
    threshold 8 is a non-configurable parameter hard-coded in every
    implementation of Parquet. Although it saves Parquet a tuning
    knob, such inflexibility could lead to suboptimal compression ratios
    for specific data sets (e.g., when the common repetition length is 7).

    so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration

  6. alamb commented on Jun 24, 2025

    @alamb
    Contributor

    so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration

    When would it be bad to use the maximum RLE value rather than bit packing?

    Like for example, if we could encode 1000 values into a single RLE tuple, wouldn't that always be better than trying to bit pack the 1000 values 🤔

  7. XiangpengHao commented on Jun 25, 2025

    @XiangpengHao
    Contributor

    When would it be bad to use the maximum RLE value rather than bit packing?

    by default it will use the maximum RLE value.

    The choice here is, for example, the encoder see only 7 consecutive same value, should it use RLE or bit packing?

    The back of envelope calculation goes:

    1. RLE needs 1 byte header and 1 byte actual value, so every RLE needs 2 bytes.
    2. Bit packing needs 1 byte header and n * bit_width/8 bytes.

    If bit_width=1, then any sequence longer than 8 should use RLE.
    if bit_width=2, then anything longer than 4 should use RLE.

    But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.

  8. JigaoLuo commented on Jun 25, 2025

    @JigaoLuo
    Contributor

    Thanks for having me, and I really appreciate this insight from @XiangpengHao .

    But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.

    This insight point basically highlights the core dilemma we face with CPU-parquet-reader: the most highly compressed Parquet files are often not the fastest to read (to decode and decompress). We’ve even seen cases where this tradeoff makes uncompressed Parquet -- despite its larger size -- faster for end-to-end reading.

    I am always wishing for writing Parquet files optimized for read performance. However, identifying such low-level pitfalls is challenging, so thank you for shedding light on this!

  9. alamb commented on Jun 25, 2025

    @alamb
    Contributor

    But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.

    BTW I wonder if this calculation will change if we integrate native support for reading RLE encoded Arrays (e.g. an arrow RunEndArray 🤔 )

  10. jhorstmann commented on Jun 25, 2025

    @jhorstmann
    ContributorAuthor

    Slight addition to that calculation, if there is already a bitpacked run that can be continued, then adding 8 bits consumes only a single byte as bitpacked, but 2 bytes as rle. Adding 16 bits would consume 2 bytes independent of encoding. So for a bit width of 1, I think increasing the minimum rle length to 16 could be beneficial. Unfortunately I was not yet able to put that logic into code without breaking any tests 😅

  11. etseidl commented on Apr 2, 2026

    @etseidl
    Contributor

    so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration

    Coming late to this, but the reason for 8 does come from the spec. Specifically

    bit-packed-run := <bit-packed-header> <bit-packed-values>
    bit-packed-header := varint-encode(<bit-pack-scaled-run-len> << 1 | 1)
    // we always bit-pack a multiple of 8 values at a time, so we only store the number of values / 8
    bit-pack-scaled-run-len := (bit-packed-run-len) / 8
    

    This means you cannot have a bit-packed run of fewer than 8 elements.

    Edit:
    NVM...still not enough caffeine in me...the quoted comment relates to RLE runs, not bit-packed 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions