← Back to Pull requests

GH-55900: [C++][Parquet] Use validity bitmap runs when encoding and decoding nullable values #55901

Open
HuaHuaY opened this pull request with 801 lines diff across 6 files
View on GitHub
HuaHuaY HuaHuaY commented on Dec 02, 2026 12:38 UTC
Author

Rationale for this change

Reduce per-value validity checks and temporary copies in Parquet encoding and decoding.

What changes are included in this PR?

  • Process valid values in runs in the column writer and encoders.

  • Share batched index decoding for dictionary-encoded byte arrays and append nulls in runs.
  • Are these changes tested?

    Yes.

    Are there any user-facing changes?

    No.

    Was AI used for this PR?

    In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

    PR code and description written by:

  • Human

  • AI
  • Reviewed before submission by:

  • Human

  • AI

  • Not reviewed
  • GitHub Issue: Closes #55900
  • Diff: 801 lines (+326 / -475) across 6 files Reviewer: wgtmac Review duration: 4425m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.55) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 801 lines diff across 6 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.82 · 5 substantive comment(s)
    0.82
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    86%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    16 items
    HuaHuaY HuaHuaY commented on Dec 02, 2026 12:40 UTC
    Author

    @ursabot please benchmark lang=C++

    rok rok commented on Dec 02, 2026 12:40 UTC
    Contributor

    Benchmark runs are scheduled for commit 57e4f90ac5a25cee878fc36556a98fff5e9132b3. Watch https://buildkite.com/apache-arrow and https://conbench.arrow-dev.org for updates. A comment will be posted here when the runs are complete.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Dec 02, 2026 12:53 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🟢 Approval recommended

    No active review issues are reported for the nullable Parquet encoding and decoding optimizations.

    No open issues 🎉

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Optimizes nullable Parquet encoding and decoding by processing validity runs, reducing per-value checks and temporary copies.

    Changes:

  • Shares run-based encoding and column-writer conversion helpers.

  • Unifies byte-array dictionary decoding and appends nulls in batches.

  • Optimizes short bitmap appends and tests decoding across batch boundaries.
  • | File | Description |
    | ---- | ----------- |
    | cpp/​src/​parquet/​encoding_test.cc | Tests batched decoding with non-byte-aligned offsets. |
    | cpp/​src/​parquet/​encoder.cc | Shares helpers for valid runs and direct buffer compaction. |
    | cpp/​src/​parquet/​decoder.cc | Shares batched dictionary decoding and null-run appends. |
    | cpp/​src/​parquet/​column_writer.cc | Converts nullable values using validity runs. |
    | cpp/​src/​arrow/​buffer_builder.h | Copies and counts short bitmap segments in one pass. |
    </details>

    <sub>Review effort: Balanced</sub>

    HuaHuaY HuaHuaY commented on Dec 03, 2026 06:33 UTC
    Author

    @ursabot please benchmark lang=C++

    rok rok commented on Dec 03, 2026 06:33 UTC
    Contributor

    Benchmark runs are scheduled for commit 6bbb7de20c5ad3c36e95b158072ba3a9ebd0dc80. Watch https://buildkite.com/apache-arrow and https://conbench.arrow-dev.org for updates. A comment will be posted here when the runs are complete.

    wgtmac wgtmac commented on code on Dec 03, 2026 06:55 UTC
    Code Review
    cpp/src/parquet/column_writer.cc:1
    @@ -2363,29 +2376,36 @@ struct SerializeFunctor<Int64Type, ::arrow::TimestampType> {
         auto target_type = ::arrow::timestamp(target_unit);
         bool truncation_allowed = ctx->properties->truncated_timestamps_allowed();
     
    +    const auto* valid_bits = array.null_count() == 0 ? nullptr : array.null_bitmap_data();
    +
         auto DivideBy = [&](const int64_t factor) {
    -      for (int64_t i = 0; i < array.length(); i++) {
    -        if (!truncation_allowed && array.IsValid(i) && (values[i] % factor != 0)) {
    -          return Status::Invalid("Casting from ", source_type.ToString(), " to ",
    -                                 target_type->ToString(),
    -                                 " would lose data: ", values[i]);
    -        }
    -        out[i] = values[i] / factor;
    -      }
    -      return Status::OK();
    +      return ::arrow::internal::VisitSetBitRuns(
    +          valid_bits, array.offset(), array.length(), [&](int64_t start, int64_t length) {
    +            for (int64_t i = start; i < start + length; ++i) {
    +              if (!truncation_allowed && values[i] % factor != 0) {

    nit: move the if check out of the loop by setting violated = violated || (!truncation_allowed && values[i] % factor != 0) and then check violated in the end. Or simply add ARROW_PREDICT_FALSE just like below?

    wgtmac wgtmac commented on code on Dec 03, 2026 07:00 UTC
    Code Review
    cpp/src/parquet/decoder.cc:1
    @@ -102,8 +103,9 @@ struct ArrowBinaryHelper<ByteArrayType, ::arrow::BinaryType> {
     
       // If a new chunk is created and estimated_remaining_data_length is provided,
       // it will also reserve the estimated data length for this chunk.
    -  Status AppendValue(const uint8_t* data, int32_t length,
    -                     std::optional<int64_t> estimated_remaining_data_length = {}) {
    +  ARROW_FORCE_INLINE Status

    Why this change?

    wgtmac wgtmac commented on code on Dec 03, 2026 07:25 UTC
    Code Review
    cpp/src/parquet/decoder.cc:1
    @@ -1290,138 +1289,77 @@ class DictByteArrayDecoderImpl : public DictDecoderImpl<ByteArrayType> {
         int result = 0;
         if (null_count == 0) {
           PARQUET_THROW_NOT_OK(DecodeArrowNonNull(num_values, builder, &result));
    -    } else {
    -      PARQUET_THROW_NOT_OK(DecodeArrow(num_values, null_count, valid_bits,
    -                                       valid_bits_offset, builder, &result));
    +      return result;
         }
    +    PARQUET_THROW_NOT_OK(builder->Reserve(num_values));
    +    PARQUET_THROW_NOT_OK(DecodeArrowRuns(
    +        num_values, null_count, valid_bits, valid_bits_offset, &result,
    +        [&](const ByteArray& value) { return builder->Append(value.ptr, value.len); },
    +        [&](int64_t length) { return builder->AppendNulls(length); }));
         return result;
       }
     
       int DecodeArrow(int num_values, int null_count, const uint8_t* valid_bits,
                       int64_t valid_bits_offset,
                       typename EncodingTraits<ByteArrayType>::Accumulator* out) override {
         int result = 0;
    -    if (null_count == 0) {
    -      PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count,
    -                                            /*valid_bits=*/nullptr, valid_bits_offset,
    -                                            out, &result));
    -    } else {
    -      PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count, valid_bits,
    -                                            valid_bits_offset, out, &result));
    -    }
    +    // The encoded indices size doesn't tell us how many bytes the values will occupy.

    Does this comment relate to /estimated_data_length=/{} below?

    wgtmac wgtmac commented on code on Dec 03, 2026 07:28 UTC
    Code Review
    cpp/src/parquet/encoder.cc:1
    @@ -124,6 +130,105 @@ class EncoderImpl : virtual public Encoder {
       int64_t unencoded_byte_array_data_bytes_ = 0;
     };
     
    +// A valid run in an Arrow fixed-width values buffer is contiguous in bytes.
    +int64_t AppendFixedWidthArray(const ::arrow::Array& array, const uint8_t* values,
    +                              int64_t byte_width, ::arrow::BufferBuilder& sink) {
    +  const int64_t num_valid = array.length() - array.null_count();
    +  if (num_valid == 0 || byte_width == 0) {
    +    return num_valid;
    +  }
    +  if (num_valid == array.length()) {
    +    PARQUET_THROW_NOT_OK(sink.Append(values, num_valid * byte_width));
    +  } else {
    +    PARQUET_THROW_NOT_OK(sink.Reserve(num_valid * byte_width));
    +    VisitSetBitRunsVoid(array.null_bitmap_data(), array.offset(), array.length(),
    +                        [&](int64_t start, int64_t length) {
    +                          sink.UnsafeAppend(values + start * byte_width,
    +                                            length * byte_width);
    +                        });
    +  }
    +  return num_valid;
    +}
    +
    +template <typename DType>
    +int64_t EncoderImpl::AppendNumericArray(const ::arrow::Array& values,
    +                                        ::arrow::BufferBuilder& sink) const {
    +  using ArrowType = typename EncodingTraits<DType>::ArrowType;
    +  using ArrayType = typename ::arrow::TypeTraits<ArrowType>::ArrayType;
    +  if (values.type_id() != ArrowType::type_id) {
    +    if (encoding_ == Encoding::PLAIN) {
    +      throw ParquetException("direct put to ", ArrowType::type_name(), " from ",

    Why do we need to use two different error messages? Just to keep the original behavior? Is it better to use the same one?

    HuaHuaY HuaHuaY commented on code on Dec 03, 2026 07:58 UTC
    Code Review
    cpp/src/parquet/decoder.cc:1
    @@ -102,8 +103,9 @@ struct ArrowBinaryHelper<ByteArrayType, ::arrow::BinaryType> {
     
       // If a new chunk is created and estimated_remaining_data_length is provided,
       // it will also reserve the estimated data length for this chunk.
    -  Status AppendValue(const uint8_t* data, int32_t length,
    -                     std::optional<int64_t> estimated_remaining_data_length = {}) {
    +  ARROW_FORCE_INLINE Status

    Although I did not modify PlainDecoder, I discovered that my changes cause the inlining of the AppendValue call within PlainDecoder to be lost. This is a performance optimization.

    wgtmac wgtmac approved these changes on Dec 03, 2026 09:12 UTC
    Approved Reviewer

    Approved these changes.

    wgtmac wgtmac commented on Dec 03, 2026 09:14 UTC
    Contributor

    @pitrou Do you want to take a look?

    HuaHuaY HuaHuaY commented on code on Dec 03, 2026 10:06 UTC
    Code Review
    cpp/src/parquet/decoder.cc:1
    @@ -102,8 +103,9 @@ struct ArrowBinaryHelper<ByteArrayType, ::arrow::BinaryType> {
     
       // If a new chunk is created and estimated_remaining_data_length is provided,
       // it will also reserve the estimated data length for this chunk.
    -  Status AppendValue(const uint8_t* data, int32_t length,
    -                     std::optional<int64_t> estimated_remaining_data_length = {}) {
    +  ARROW_FORCE_INLINE Status

    This change should be reverted after #55940 is merged.

    HuaHuaY HuaHuaY commented on Dec 04, 2026 04:00 UTC
    Author

    @ursabot please benchmark lang=C++

    rok rok commented on Dec 04, 2026 04:01 UTC
    Contributor

    Benchmark runs are scheduled for commit 4690168cc40bc4d103161dd261599088378ab285. Watch https://buildkite.com/apache-arrow and https://conbench.arrow-dev.org for updates. A comment will be posted here when the runs are complete.

    Reviewers
    Assignees
    HuaHuaY HuaHuaY
    Labels
    risk: low confidence: 86.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 55.3% (MEDIUM) · Higher = larger blast radius
    55.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 86.9% · Lower = rushed or shallow review
    86.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.3% · Safe to merge
    7.3%
    Repository