Skip to content

GH-50333: [C++][Parquet] Add dense decode path for FIXED_LEN_BYTE_ARRAY - #50335

Merged
pitrou merged 5 commits into
apache:mainfrom
marcin-krystianc:dev-20260702-flba-decoder
Sep 9, 2026
Merged

GH-50333: [C++][Parquet] Add dense decode path for FIXED_LEN_BYTE_ARRAY#50335
pitrou merged 5 commits into
apache:mainfrom
marcin-krystianc:dev-20260702-flba-decoder

Conversation

@marcin-krystianc

@marcin-krystianc marcin-krystianc commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Rationale for this change

For performance reasons, we would like to decode float16 (FLBA) values directly into the caller-provided buffer.

What changes are included in this PR?

  • Adds a virtual int Decode(uint8_t* buffer, int max_values) to FLBADecoder in encoding.h. It writes decoded values contiguously into a caller-owned buffer.
  • Implements the new overload for all supported decoders. (The base implementation throws ParquetException, so unimplemented encodings fail with a clear message.)
  • Replaces the old PARQUET-1508 TODO comment.

Are these changes tested?

Yes

Are there any user-facing changes?

No

PS:

Closes #50333

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@rok rok left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good @marcin-krystianc !
My only suggestion would be to test the non-happy-path as well to make sure we throw, see diff below.

quick suggestion
diff --git i/cpp/src/parquet/encoding.h w/cpp/src/parquet/encoding.h
index 27b90a5fe9..fc6a42d897 100644
--- i/cpp/src/parquet/encoding.h
+++ w/cpp/src/parquet/encoding.h
@@ -426,7 +426,7 @@ class FLBADecoder : virtual public TypedDecoder<FLBAType> {
   /// except at the end of the current data page.
   ///
   /// \note API EXPERIMENTAL
-  virtual int Decode(uint8_t* buffer, int max_values);
+  virtual int Decode(uint8_t* buffer, int max_values);
 };

 PARQUET_EXPORT
diff --git i/cpp/src/parquet/encoding_test.cc w/cpp/src/parquet/encoding_test.cc
index d161b61af5..6e2b21fa62 100644
--- i/cpp/src/parquet/encoding_test.cc
+++ w/cpp/src/parquet/encoding_test.cc
@@ -2678,11 +2678,24 @@ class TestFLBADenseDecode : public ::testing::Test {
   }

   // Decode densely and compare each value against the original draw.
-  void CheckDenseDecode(FLBADecoder* decoder) {
+  void CheckDenseDecode(FLBADecoder* decoder, const std::vector<int>& decode_sizes) {
     ASSERT_NE(nullptr, decoder);
     std::vector<uint8_t> dense(static_cast<size_t>(type_length_) * kNumValues);
-    int values_decoded = decoder->Decode(dense.data(), kNumValues);
+
+    int values_decoded = 0;
+    for (int decode_size : decode_sizes) {
+      const int expected_decoded = std::min(decode_size, kNumValues - values_decoded);
+      ASSERT_EQ(expected_decoded,
+                decoder->Decode(dense.data() +
+                                    static_cast<int64_t>(values_decoded) * type_length_,
+                                decode_size));
+      values_decoded += expected_decoded;
+      ASSERT_EQ(kNumValues - values_decoded, decoder->values_left());
+    }
     ASSERT_EQ(kNumValues, values_decoded);
+    ASSERT_EQ(0, decoder->Decode(dense.data(), /*max_values=*/1));
+    ASSERT_EQ(0, decoder->values_left());
+
     for (int i = 0; i < kNumValues; ++i) {
       ASSERT_EQ(0, memcmp(dense.data() + static_cast<int64_t>(i) * type_length_,
                           draws_[i].ptr, type_length_))
@@ -2690,6 +2703,10 @@ class TestFLBADenseDecode : public ::testing::Test {
     }
   }

+  void CheckDenseDecode(FLBADecoder* decoder) {
+    CheckDenseDecode(decoder, {1, 17, kNumValues / 2, kNumValues});
+  }
+
  protected:
   static constexpr int kNumValues = 1000;
   int type_length_;
@@ -2754,4 +2771,49 @@ TEST_F(TestFLBADenseDecode, ByteStreamSplit) {
   ASSERT_NO_FATAL_FAILURE(CheckDenseDecode(dynamic_cast<FLBADecoder*>(decoder.get())));
 }

+TEST_F(TestFLBADenseDecode, PlainRejectsTruncatedDenseBuffer) {
+  auto encoder =
+      MakeTypedEncoder<FLBAType>(Encoding::PLAIN, /*use_dictionary=*/false, descr_.get());
+  encoder->Put(draws_.data(), kNumValues);
+  auto buffer = encoder->FlushValues();
+
+  auto decoder = MakeTypedDecoder<FLBAType>(Encoding::PLAIN, descr_.get());
+  decoder->SetData(kNumValues, buffer->data(), static_cast<int>(buffer->size() - 1));
+
+  std::vector<uint8_t> dense(static_cast<size_t>(type_length_) * kNumValues);
+  ASSERT_THROW(decoder->Decode(dense.data(), kNumValues), ParquetException);
+}
+
+TEST_F(TestFLBADenseDecode, DictionaryRejectsOutOfBoundsDenseIndex) {
+  auto dict_decoder = MakeTypedDecoder<FLBAType>(Encoding::PLAIN, descr_.get());
+  dict_decoder->SetData(/*num_values=*/1, draws_[0].ptr, type_length_);
+
+  auto decoder = MakeDictDecoder<FLBAType>(descr_.get());
+  decoder->SetDict(dict_decoder.get());
+
+  // RLE_DICTIONARY data: bit width 1, followed by an RLE run of one index value
+  // 1. The dictionary has only one entry, so the index is out of bounds.
+  const std::vector<uint8_t> indices = {1, 2, 1};
+  decoder->SetData(/*num_values=*/1, indices.data(), static_cast<int>(indices.size()));
+
+  auto flba_decoder = dynamic_cast<FLBADecoder*>(decoder.get());
+  ASSERT_NE(nullptr, flba_decoder);
+  std::vector<uint8_t> dense(static_cast<size_t>(type_length_));
+  ASSERT_THROW(flba_decoder->Decode(dense.data(), /*max_values=*/1), ParquetException);
+}
+
+TEST_F(TestFLBADenseDecode, DeltaByteArrayRejectsWrongFLBALengthDense) {
+  std::string suffix(static_cast<size_t>(type_length_ - 1), 'x');
+  auto buffer = ::arrow::ConcatenateBuffers({DeltaEncode({0}),
+                                             DeltaEncode({type_length_ - 1}),
+                                             std::make_shared<Buffer>(suffix)})
+                    .ValueOrDie();
+
+  auto decoder = MakeTypedDecoder<FLBAType>(Encoding::DELTA_BYTE_ARRAY, descr_.get());
+  decoder->SetData(/*num_values=*/1, buffer->data(), static_cast<int>(buffer->size()));
+
+  std::vector<uint8_t> dense(static_cast<size_t>(type_length_));
+  ASSERT_THROW(decoder->Decode(dense.data(), /*max_values=*/1), ParquetException);
+}
+
 }  // namespace parquet::test

</details>

For linting errors pre-commit is useful:
```bash
pre-commit run --show-diff-on-failure --color=always --all-files cpp

@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting review Awaiting review labels Jul 6, 2026
@github-actions github-actions Bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Jul 6, 2026
@marcin-krystianc
marcin-krystianc marked this pull request as ready for review July 6, 2026 21:04
Comment thread cpp/src/parquet/encoding.h Outdated
Comment thread cpp/src/parquet/decoder.cc Outdated
Comment thread cpp/src/parquet/decoder.cc Outdated
@marcin-krystianc
marcin-krystianc requested a review from pitrou July 28, 2026 06:35
}

// Same internal decode as above, but copy the bytes contiguously into the
// caller's buffer instead of materializing per-value pointers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The per-value pointers are still materialized in the temporary std::vector<ByteArray> below, do we want to do something about it? Or do we think it's good enough (DELTA_BYTE_ARRAY might not be used much with FLBA)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me think what it would take to implement the DELTA_BYTE_ARRAY decoder that avoids all that overhead.
And whether it fits the current PR or better should go as another one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me think what it would take to implement the DELTA_BYTE_ARRAY decoder that avoids all that overhead.

I think the key is to refactor GetInternal to be storage-agnostic, but that's not trivial since it also delegates to DeltaLengthByteArrayDecoder. Perhaps it's better for another PR.

(the good news is that it would probably also benefit DeltaByteArrayDecoderImpl::DecodeArrow)

@marcin-krystianc marcin-krystianc Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After an initial look, I can see that refactoring the GetInternal will introduce lots of extra changes. Thus, I reckon it is better to do it in a separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I've prepared a follow-up PR that ìmpreves the the DELTA_BYTE_ARRAYdecoder (#50823). So I think this PR is ready to go. Once it is merged, I'll mark the next one as ready for a review.

@marcin-krystianc

Copy link
Copy Markdown
Contributor Author

Hi Antoine, I would appreciate another review on this issue.
I think I've addressed all the remarks, and opened a deaf PR with follow-up changes.

@pitrou

pitrou commented Sep 9, 2026

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g cpp

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @marcin-krystianc , LGTM. I'm just running additional CI and then this can be merged.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Revision: 7bb950b

Submitted crossbow builds: ursacomputing/crossbow @ actions-c07d48abce

Task Status
example-cpp-minimal-build-static GitHub Actions
example-cpp-minimal-build-static-system-dependency GitHub Actions
example-cpp-tutorial GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-debian-13-cpp-amd64 GitHub Actions
test-debian-13-cpp-i386 GitHub Actions
test-debian-experimental-cpp-gcc-15 GitHub Actions
test-fedora-42-cpp GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-bundled GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-bundled-offline GitHub Actions
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions
test-ubuntu-24.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-24.04-cpp-thread-sanitizer GitHub Actions

@pitrou

pitrou commented Sep 9, 2026

Copy link
Copy Markdown
Member

CI failures look unrelated, I'll merge.

@pitrou pitrou changed the title GH-50333: [C++][Parquet] Add dense decode path for FIXED_LEN_BYTE_ARRAY GH-50333: [C++][Parquet] Add dense decode path for FIXED_LEN_BYTE_ARRAY Sep 9, 2026
@pitrou
pitrou merged commit a811bcd into apache:main Sep 9, 2026
58 checks passed
@pitrou pitrou removed the awaiting change review Awaiting change review label Sep 9, 2026
@marcin-krystianc
marcin-krystianc deleted the dev-20260702-flba-decoder branch September 10, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][Parquet] Add a dense decoder for FIXED_LEN_BYTE_ARRAY values

3 participants