From 70080a20fe174cd826c0164983c906f7755eb82c Mon Sep 17 00:00:00 2001 From: Ryan Vickramasinghe Date: Tue, 10 Nov 2026 22:50:38 +0000 Subject: [PATCH 1/2] GH-55487: [C++][Parquet] Fix DeltaLengthByteArrayEncoder memory pool tracking and Arrow string batching Closes #55487 Generated-by: Gemini (reviewed by Ryan Vickramasinghe) --- cpp/src/parquet/encoder.cc | 95 ++++++++++++-- cpp/src/parquet/encoding_test.cc | 210 +++++++++++++++++++++++++++++++ 2 files changed, 295 insertions(+), 10 deletions(-) diff --git a/cpp/src/parquet/encoder.cc b/cpp/src/parquet/encoder.cc index 30341bbe25e1..89966b7b319a 100644 --- a/cpp/src/parquet/encoder.cc +++ b/cpp/src/parquet/encoder.cc @@ -18,6 +18,7 @@ #include "parquet/encoding.h" #include +#include #include #include #include @@ -1384,9 +1385,9 @@ void DeltaBitPackEncoder::PutSpaced(const T* src, int num_values, class DeltaLengthByteArrayEncoder : public EncoderImpl, virtual public TypedEncoder { public: - explicit DeltaLengthByteArrayEncoder(const ColumnDescriptor* descr, MemoryPool* pool) - : EncoderImpl(descr, Encoding::DELTA_LENGTH_BYTE_ARRAY, - pool = ::arrow::default_memory_pool()), + explicit DeltaLengthByteArrayEncoder(const ColumnDescriptor* descr, + MemoryPool* pool = ::arrow::default_memory_pool()) + : EncoderImpl(descr, Encoding::DELTA_LENGTH_BYTE_ARRAY, pool), sink_(pool), length_encoder_(nullptr, pool) {} @@ -1408,6 +1409,67 @@ class DeltaLengthByteArrayEncoder : public EncoderImpl, protected: template void PutBinaryArray(const ArrayType& array) { + if (array.length() == 0 || array.null_count() == array.length()) { + return; + } + + constexpr int kBatchSize = 256; + std::array lengths; + + if constexpr (!std::is_same_v) { + if (array.null_count() == 0) { + const int64_t num_values = array.length(); + const auto* offsets = array.raw_value_offsets(); + const int64_t start_offset = offsets[0]; + const int64_t total_bytes = + static_cast(offsets[num_values]) - start_offset; + if constexpr (sizeof(typename ArrayType::offset_type) > sizeof(int32_t)) { + for (int64_t i = 0; i < num_values; ++i) { + const int64_t len = offsets[i + 1] - offsets[i]; + if (ARROW_PREDICT_FALSE(len < 0 || + static_cast(len) > kMaxByteArraySize)) { + PARQUET_THROW_NOT_OK(Status::Invalid( + "Parquet cannot store strings with size 2GB or more, got: ", len)); + } + } + } + if (ARROW_PREDICT_FALSE(total_bytes < 0 || + total_bytes > std::numeric_limits::max() - + sink_.length())) { + PARQUET_THROW_NOT_OK( + Status::Invalid("excess expansion in DELTA_LENGTH_BYTE_ARRAY")); + } + if (total_bytes > 0) { + PARQUET_THROW_NOT_OK(sink_.Reserve(total_bytes)); + } + for (int64_t idx = 0; idx < num_values; idx += kBatchSize) { + const int batch_size = + static_cast(std::min(kBatchSize, num_values - idx)); + for (int j = 0; j < batch_size; ++j) { + const int64_t len = + static_cast(offsets[idx + j + 1]) - offsets[idx + j]; + if (ARROW_PREDICT_FALSE(len < 0)) { + PARQUET_THROW_NOT_OK( + Status::Invalid("Negative string length in BinaryArray: ", len)); + } + lengths[j] = static_cast(len); + } + length_encoder_.Put(lengths.data(), batch_size); + } + if (total_bytes > 0) { + sink_.UnsafeAppend(array.raw_data() + start_offset, total_bytes); + unencoded_byte_array_data_bytes_ += total_bytes; + } + return; + } + } + + const int64_t estimated_bytes = GetBinaryDataSize(array); + if (estimated_bytes > 0 && + estimated_bytes <= std::numeric_limits::max() - sink_.length()) { + PARQUET_THROW_NOT_OK(sink_.Reserve(estimated_bytes)); + } + int batch_len = 0; PARQUET_THROW_NOT_OK(::arrow::VisitArraySpanInline( *array.data(), [&](::std::string_view view) { @@ -1420,12 +1482,21 @@ class DeltaLengthByteArrayEncoder : public EncoderImpl, static_cast(std::numeric_limits::max()))) { return Status::Invalid("excess expansion in DELTA_LENGTH_BYTE_ARRAY"); } - length_encoder_.Put({static_cast(view.length())}, 1); - PARQUET_THROW_NOT_OK(sink_.Append(view.data(), view.length())); - unencoded_byte_array_data_bytes_ += view.size(); + lengths[batch_len++] = static_cast(view.length()); + if (batch_len == kBatchSize) { + length_encoder_.Put(lengths.data(), kBatchSize); + batch_len = 0; + } + if (!view.empty()) { + RETURN_NOT_OK(sink_.Append(view.data(), static_cast(view.length()))); + unencoded_byte_array_data_bytes_ += view.size(); + } return Status::OK(); }, []() { return Status::OK(); })); + if (batch_len > 0) { + length_encoder_.Put(lengths.data(), batch_len); + } } ::arrow::BufferBuilder sink_; @@ -1469,11 +1540,15 @@ void DeltaLengthByteArrayEncoder::Put(const T* src, int num_values) { if (sink_.length() + total_increment_size > std::numeric_limits::max()) { throw ParquetException("excess expansion in DELTA_LENGTH_BYTE_ARRAY"); } - PARQUET_THROW_NOT_OK(sink_.Reserve(total_increment_size)); - for (int idx = 0; idx < num_values; idx++) { - sink_.UnsafeAppend(src[idx].ptr, src[idx].len); + if (total_increment_size > 0) { + PARQUET_THROW_NOT_OK(sink_.Reserve(total_increment_size)); + for (int idx = 0; idx < num_values; idx++) { + if (src[idx].len > 0) { + sink_.UnsafeAppend(src[idx].ptr, src[idx].len); + } + } + unencoded_byte_array_data_bytes_ += total_increment_size; } - unencoded_byte_array_data_bytes_ += total_increment_size; } void DeltaLengthByteArrayEncoder::PutSpaced(const T* src, int num_values, diff --git a/cpp/src/parquet/encoding_test.cc b/cpp/src/parquet/encoding_test.cc index 672e55a900dc..8749314a8ca9 100644 --- a/cpp/src/parquet/encoding_test.cc +++ b/cpp/src/parquet/encoding_test.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -32,6 +33,7 @@ #include "arrow/array/builder_dict.h" #include "arrow/array/concatenate.h" #include "arrow/compute/cast.h" +#include "arrow/memory_pool.h" #include "arrow/testing/gtest_util.h" #include "arrow/testing/random.h" #include "arrow/testing/util.h" @@ -2391,6 +2393,214 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, ArrowDirectPut) { } } +TEST(DeltaLengthByteArrayEncodingAdHoc, UsesProvidedMemoryPool) { + constexpr int kCount = 320; + ::arrow::StringBuilder dense_builder; + ::arrow::StringBuilder nullable_builder; + ::arrow::LargeStringBuilder large_builder; + ::arrow::StringViewBuilder view_builder; + + int64_t expected_payload_bytes = 0; + std::vector raw_strings; + raw_strings.reserve(kCount); + for (int i = 0; i < kCount; ++i) { + std::string val = (i % 2 == 0) ? ("short_" + std::to_string(i)) + : ("long_out_of_line_payload_" + std::to_string(i)); + raw_strings.push_back(val); + ASSERT_OK(dense_builder.Append(val)); + expected_payload_bytes += static_cast(val.size()); + + if (i % 7 == 0) { + ASSERT_OK(nullable_builder.AppendNull()); + } else { + ASSERT_OK(nullable_builder.Append(val)); + expected_payload_bytes += static_cast(val.size()); + } + ASSERT_OK(large_builder.Append(val)); + ASSERT_OK(view_builder.Append(val)); + expected_payload_bytes += static_cast(val.size()); + } + ASSERT_OK_AND_ASSIGN(auto dense_arr, dense_builder.Finish()); + ASSERT_OK_AND_ASSIGN(auto nullable_arr, nullable_builder.Finish()); + ASSERT_OK_AND_ASSIGN(auto large_arr, large_builder.Finish()); + auto sliced_large = large_arr->Slice(5, kCount - 10); + for (int i = 5; i < kCount - 5; ++i) { + expected_payload_bytes += static_cast(raw_strings[i].size()); + } + ASSERT_OK_AND_ASSIGN(auto view_arr, view_builder.Finish()); + + std::vector raw_ba; + raw_ba.reserve(raw_strings.size()); + for (const auto& s : raw_strings) { + raw_ba.emplace_back(static_cast(s.size()), + reinterpret_cast(s.data())); + expected_payload_bytes += static_cast(s.size()); + } + + ::arrow::ProxyMemoryPool tracking_pool(default_memory_pool()); + { + auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, + /*use_dictionary=*/false, + /*descr=*/nullptr, &tracking_pool); + EXPECT_EQ(&tracking_pool, encoder->memory_pool()); + + for (const auto& arr : {dense_arr, nullable_arr, sliced_large, view_arr}) { + encoder->Put(*arr); + auto flushed = encoder->FlushValues(); + ASSERT_GT(flushed->size(), 0); + } + encoder->Put(raw_ba.data(), static_cast(raw_ba.size())); + auto flushed = encoder->FlushValues(); + ASSERT_GT(flushed->size(), 0); + + auto dba_encoder = MakeTypedEncoder(Encoding::DELTA_BYTE_ARRAY, + /*use_dictionary=*/false, + /*descr=*/nullptr, &tracking_pool); + dba_encoder->Put(*dense_arr); + ASSERT_GT(dba_encoder->FlushValues()->size(), 0); + } + + EXPECT_GT(tracking_pool.num_allocations(), 0); + EXPECT_GE(tracking_pool.total_bytes_allocated(), expected_payload_bytes); + EXPECT_EQ(0, tracking_pool.bytes_allocated()); +} + +TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) { + auto descr = ExampleDescr(); + auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, + /*use_dictionary=*/false, descr.get()); + auto decoder = + MakeTypedDecoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, descr.get()); + + auto VerifyArrayRoundTrip = [&](const std::shared_ptr<::arrow::Array>& arr, + const std::vector& expected_valid_lengths, + const std::string& expected_payload) { + encoder->Put(*arr); + EXPECT_EQ(static_cast(expected_payload.size()), + encoder->ReportUnencodedDataBytes()); + auto buf = encoder->FlushValues(); + + auto expected_len_buf = DeltaEncode(expected_valid_lengths); + ASSERT_EQ(expected_len_buf->size() + static_cast(expected_payload.size()), + buf->size()); + EXPECT_TRUE(SliceBuffer(buf, 0, expected_len_buf->size())->Equals(*expected_len_buf)); + EXPECT_TRUE( + SliceBuffer(buf, expected_len_buf->size())->Equals(Buffer(expected_payload))); + + const int num_valid = static_cast(expected_valid_lengths.size()); + decoder->SetData(num_valid, buf->data(), static_cast(buf->size())); + typename EncodingTraits::Accumulator acc; + ASSERT_OK_AND_ASSIGN(acc.builder, ::arrow::MakeBuilder(arr->type())); + ASSERT_EQ(num_valid, + decoder->DecodeArrow(static_cast(arr->length()), + static_cast(arr->null_count()), + arr->null_bitmap_data(), arr->offset(), &acc)); + std::shared_ptr<::arrow::Array> decoded; + ASSERT_OK(acc.builder->Finish(&decoded)); + ::arrow::AssertArraysEqual(*arr, *decoded, /*verbose=*/true); + }; + + for (int count : {0, 1, 255, 256, 257, 512, 600}) { + ARROW_SCOPED_TRACE("count = ", count); + ::arrow::StringBuilder dense_b; + ::arrow::StringBuilder nullable_b; + ::arrow::StringBuilder empty_b; + ::arrow::StringBuilder nulls_b; + std::vector dense_lens, nullable_lens, empty_lens(count, 0); + std::string dense_payload, nullable_payload; + + for (int i = 0; i < count; ++i) { + std::string s = (i % 11 == 0) ? "" : ("v_" + std::to_string(i)); + ASSERT_OK(dense_b.Append(s)); + dense_lens.push_back(static_cast(s.size())); + dense_payload.append(s); + + if (i % 5 == 0) { + ASSERT_OK(nullable_b.AppendNull()); + } else { + ASSERT_OK(nullable_b.Append(s)); + nullable_lens.push_back(static_cast(s.size())); + nullable_payload.append(s); + } + ASSERT_OK(empty_b.Append("")); + ASSERT_OK(nulls_b.AppendNull()); + } + ASSERT_OK_AND_ASSIGN(auto dense_arr, dense_b.Finish()); + ASSERT_OK_AND_ASSIGN(auto nullable_arr, nullable_b.Finish()); + ASSERT_OK_AND_ASSIGN(auto empty_arr, empty_b.Finish()); + ASSERT_OK_AND_ASSIGN(auto nulls_arr, nulls_b.Finish()); + + VerifyArrayRoundTrip(dense_arr, dense_lens, dense_payload); + VerifyArrayRoundTrip(nullable_arr, nullable_lens, nullable_payload); + VerifyArrayRoundTrip(empty_arr, empty_lens, ""); + VerifyArrayRoundTrip(nulls_arr, {}, ""); + } + + { + ::arrow::LargeStringBuilder large_b; + std::vector vals; + for (int i = 0; i < 300; ++i) { + vals.push_back((i % 13 == 0) ? "" : ("slice_item_" + std::to_string(i))); + ASSERT_OK(large_b.Append(vals.back())); + } + ASSERT_OK_AND_ASSIGN(auto large_arr, large_b.Finish()); + auto sliced = large_arr->Slice(17, 260); + std::vector lens; + std::string payload; + for (int i = 17; i < 17 + 260; ++i) { + lens.push_back(static_cast(vals[i].size())); + payload.append(vals[i]); + } + VerifyArrayRoundTrip(sliced, lens, payload); + } + + { + ::arrow::StringViewBuilder view_b; + std::vector lens; + std::string payload; + for (int i = 0; i < 270; ++i) { + if (i % 9 == 0) { + ASSERT_OK(view_b.AppendNull()); + continue; + } + std::string s = (i % 3 == 0) ? "" + : (i % 3 == 1) ? ("in_" + std::to_string(i)) + : ("out_of_line_string_view_" + std::to_string(i)); + ASSERT_OK(view_b.Append(s)); + lens.push_back(static_cast(s.size())); + payload.append(s); + } + ASSERT_OK_AND_ASSIGN(auto view_arr, view_b.Finish()); + VerifyArrayRoundTrip(view_arr, lens, payload); + } + + { + const std::string abc = "abc"; + std::vector mixed = { + ByteArray(0, nullptr), + ByteArray(3, reinterpret_cast(abc.data())), + ByteArray(0, nullptr), + }; + encoder->Put(mixed.data(), static_cast(mixed.size())); + EXPECT_EQ(3, encoder->ReportUnencodedDataBytes()); + auto buf = encoder->FlushValues(); + EXPECT_EQ(DeltaEncode({0, 3, 0})->ToString() + "abc", buf->ToString()); + + std::vector all_zero_null(4, ByteArray(0, nullptr)); + encoder->Put(all_zero_null.data(), static_cast(all_zero_null.size())); + EXPECT_EQ(0, encoder->ReportUnencodedDataBytes()); + auto zero_buf = encoder->FlushValues(); + EXPECT_EQ(DeltaEncode({0, 0, 0, 0})->ToString(), zero_buf->ToString()); + + const uint8_t valid_bits = 0b00001010; + encoder->PutSpaced(all_zero_null.data(), 4, &valid_bits, /*valid_bits_offset=*/0); + EXPECT_EQ(0, encoder->ReportUnencodedDataBytes()); + auto spaced_buf = encoder->FlushValues(); + EXPECT_EQ(DeltaEncode(std::vector{0, 0})->ToString(), + spaced_buf->ToString()); + } +} + // ---------------------------------------------------------------------- // DELTA_BYTE_ARRAY encode/decode tests. From 9d41a76606d35c649e4d4979dcdc3d21d73f6297 Mon Sep 17 00:00:00 2001 From: Ryan Vickramasinghe Date: Wed, 11 Nov 2026 22:52:04 +0000 Subject: [PATCH 2/2] GH-55487: [C++][Parquet] Refine DeltaLengthByteArrayEncoder offset checks and portable pool tests - Keep MemoryPool* mandatory in DeltaLengthByteArrayEncoder's constructor. - Eliminate the extra pre-scan on LargeBinary/LargeString arrays while guarding negative start offsets, non-monotonic steps, signed subtraction overflow, and >= 2GB elements in one pass. - Use Arrow's portable MemoryPool::CreateDefault() in UsesProvidedMemoryPool across DELTA_LENGTH_BYTE_ARRAY, DELTA_BYTE_ARRAY, and PutSpaced, and expand offset rejection, multi-Put accumulation, and batch-boundary test cases. Generated-by: Gemini (reviewed by Ryan Vickramasinghe) --- cpp/src/parquet/encoder.cc | 72 +++++++--- cpp/src/parquet/encoding_test.cc | 230 +++++++++++++++++++++++++------ 2 files changed, 239 insertions(+), 63 deletions(-) diff --git a/cpp/src/parquet/encoder.cc b/cpp/src/parquet/encoder.cc index 89966b7b319a..3a4e5ec5e00e 100644 --- a/cpp/src/parquet/encoder.cc +++ b/cpp/src/parquet/encoder.cc @@ -1385,8 +1385,7 @@ void DeltaBitPackEncoder::PutSpaced(const T* src, int num_values, class DeltaLengthByteArrayEncoder : public EncoderImpl, virtual public TypedEncoder { public: - explicit DeltaLengthByteArrayEncoder(const ColumnDescriptor* descr, - MemoryPool* pool = ::arrow::default_memory_pool()) + explicit DeltaLengthByteArrayEncoder(const ColumnDescriptor* descr, MemoryPool* pool) : EncoderImpl(descr, Encoding::DELTA_LENGTH_BYTE_ARRAY, pool), sink_(pool), length_encoder_(nullptr, pool) {} @@ -1416,46 +1415,76 @@ class DeltaLengthByteArrayEncoder : public EncoderImpl, constexpr int kBatchSize = 256; std::array lengths; + // Fast path for non-null offset-based arrays (Binary/String/Large*): + // raw_value_offsets() is slice-adjusted while raw_data() is unshifted, so all + // payloads sit contiguously in [offsets[0], offsets[num_values]). Pre-validate + // sizes, batch-encode lengths, and bulk-copy bytes in one shot. if constexpr (!std::is_same_v) { if (array.null_count() == 0) { const int64_t num_values = array.length(); const auto* offsets = array.raw_value_offsets(); const int64_t start_offset = offsets[0]; - const int64_t total_bytes = - static_cast(offsets[num_values]) - start_offset; - if constexpr (sizeof(typename ArrayType::offset_type) > sizeof(int32_t)) { - for (int64_t i = 0; i < num_values; ++i) { - const int64_t len = offsets[i + 1] - offsets[i]; - if (ARROW_PREDICT_FALSE(len < 0 || - static_cast(len) > kMaxByteArraySize)) { - PARQUET_THROW_NOT_OK(Status::Invalid( - "Parquet cannot store strings with size 2GB or more, got: ", len)); + const int64_t end_offset = offsets[num_values]; + if (ARROW_PREDICT_FALSE(start_offset < 0)) { + PARQUET_THROW_NOT_OK( + Status::Invalid("Negative offset in BinaryArray: ", start_offset)); + } + if (ARROW_PREDICT_FALSE(end_offset < start_offset || + end_offset - start_offset > + std::numeric_limits::max() - + sink_.length())) { + // For Large* arrays, disambiguate a >= 2GB element (or negative step) + // from cumulative excess expansion without an extra hot-path scan. + if constexpr (sizeof(typename ArrayType::offset_type) > sizeof(int32_t)) { + int64_t prev = start_offset; + for (int64_t i = 0; i < num_values; ++i) { + const int64_t next = offsets[i + 1]; + if (next < prev) { + PARQUET_THROW_NOT_OK( + Status::Invalid("Negative string length in BinaryArray")); + } + const int64_t len = next - prev; + if (len > static_cast(kMaxByteArraySize)) { + PARQUET_THROW_NOT_OK(Status::Invalid( + "Parquet cannot store strings with size 2GB or more, got: ", len)); + } + prev = next; } } - } - if (ARROW_PREDICT_FALSE(total_bytes < 0 || - total_bytes > std::numeric_limits::max() - - sink_.length())) { + if (end_offset < start_offset) { + PARQUET_THROW_NOT_OK( + Status::Invalid("Negative string length in BinaryArray")); + } PARQUET_THROW_NOT_OK( Status::Invalid("excess expansion in DELTA_LENGTH_BYTE_ARRAY")); } + const int64_t total_bytes = end_offset - start_offset; if (total_bytes > 0) { PARQUET_THROW_NOT_OK(sink_.Reserve(total_bytes)); } + int64_t prev_offset = start_offset; for (int64_t idx = 0; idx < num_values; idx += kBatchSize) { const int batch_size = static_cast(std::min(kBatchSize, num_values - idx)); for (int j = 0; j < batch_size; ++j) { - const int64_t len = - static_cast(offsets[idx + j + 1]) - offsets[idx + j]; - if (ARROW_PREDICT_FALSE(len < 0)) { + const int64_t next_offset = offsets[idx + j + 1]; + if (ARROW_PREDICT_FALSE(next_offset < prev_offset)) { PARQUET_THROW_NOT_OK( - Status::Invalid("Negative string length in BinaryArray: ", len)); + Status::Invalid("Negative string length in BinaryArray")); + } + const int64_t len = next_offset - prev_offset; + if constexpr (sizeof(typename ArrayType::offset_type) > sizeof(int32_t)) { + if (ARROW_PREDICT_FALSE(len > static_cast(kMaxByteArraySize))) { + PARQUET_THROW_NOT_OK(Status::Invalid( + "Parquet cannot store strings with size 2GB or more, got: ", len)); + } } lengths[j] = static_cast(len); + prev_offset = next_offset; } length_encoder_.Put(lengths.data(), batch_size); } + // Safe: Reserve(total_bytes) allocated capacity above; > 0 avoids null memcpy. if (total_bytes > 0) { sink_.UnsafeAppend(array.raw_data() + start_offset, total_bytes); unencoded_byte_array_data_bytes_ += total_bytes; @@ -1464,6 +1493,9 @@ class DeltaLengthByteArrayEncoder : public EncoderImpl, } } + // General path for BinaryViewArray or arrays with nulls: pre-reserve an + // estimated payload size, skip null slots, batch non-null lengths, and skip + // the no-op Append for empty values (their length 0 is still encoded). const int64_t estimated_bytes = GetBinaryDataSize(array); if (estimated_bytes > 0 && estimated_bytes <= std::numeric_limits::max() - sink_.length()) { @@ -1525,6 +1557,7 @@ void DeltaLengthByteArrayEncoder::Put(const T* src, int num_values) { constexpr int kBatchSize = 256; std::array lengths; uint32_t total_increment_size = 0; + // Pass 1: batch-encode lengths and accumulate total payload bytes with overflow checks. for (int idx = 0; idx < num_values; idx += kBatchSize) { const int batch_size = std::min(kBatchSize, num_values - idx); for (int j = 0; j < batch_size; ++j) { @@ -1540,6 +1573,7 @@ void DeltaLengthByteArrayEncoder::Put(const T* src, int num_values) { if (sink_.length() + total_increment_size > std::numeric_limits::max()) { throw ParquetException("excess expansion in DELTA_LENGTH_BYTE_ARRAY"); } + // Pass 2: Reserve once so UnsafeAppend skips per-value checks; skip len == 0 for UB. if (total_increment_size > 0) { PARQUET_THROW_NOT_OK(sink_.Reserve(total_increment_size)); for (int idx = 0; idx < num_values; idx++) { diff --git a/cpp/src/parquet/encoding_test.cc b/cpp/src/parquet/encoding_test.cc index 8749314a8ca9..f0bbe85d1e4c 100644 --- a/cpp/src/parquet/encoding_test.cc +++ b/cpp/src/parquet/encoding_test.cc @@ -23,8 +23,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -48,6 +50,7 @@ #include "arrow/util/endian.h" #include "arrow/util/string.h" #include "parquet/encoding.h" +#include "parquet/exception.h" #include "parquet/platform.h" #include "parquet/schema.h" #include "parquet/test_util.h" @@ -2400,33 +2403,35 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, UsesProvidedMemoryPool) { ::arrow::LargeStringBuilder large_builder; ::arrow::StringViewBuilder view_builder; - int64_t expected_payload_bytes = 0; + int64_t full_bytes = 0; + int64_t nullable_bytes = 0; + int64_t sliced_bytes = 0; std::vector raw_strings; raw_strings.reserve(kCount); for (int i = 0; i < kCount; ++i) { std::string val = (i % 2 == 0) ? ("short_" + std::to_string(i)) : ("long_out_of_line_payload_" + std::to_string(i)); + const auto val_bytes = static_cast(val.size()); raw_strings.push_back(val); - ASSERT_OK(dense_builder.Append(val)); - expected_payload_bytes += static_cast(val.size()); + full_bytes += val_bytes; + if (i >= 5 && i < kCount - 5) { + sliced_bytes += val_bytes; + } + ASSERT_OK(dense_builder.Append(val)); if (i % 7 == 0) { ASSERT_OK(nullable_builder.AppendNull()); } else { ASSERT_OK(nullable_builder.Append(val)); - expected_payload_bytes += static_cast(val.size()); + nullable_bytes += val_bytes; } ASSERT_OK(large_builder.Append(val)); ASSERT_OK(view_builder.Append(val)); - expected_payload_bytes += static_cast(val.size()); } ASSERT_OK_AND_ASSIGN(auto dense_arr, dense_builder.Finish()); ASSERT_OK_AND_ASSIGN(auto nullable_arr, nullable_builder.Finish()); ASSERT_OK_AND_ASSIGN(auto large_arr, large_builder.Finish()); auto sliced_large = large_arr->Slice(5, kCount - 10); - for (int i = 5; i < kCount - 5; ++i) { - expected_payload_bytes += static_cast(raw_strings[i].size()); - } ASSERT_OK_AND_ASSIGN(auto view_arr, view_builder.Finish()); std::vector raw_ba; @@ -2434,35 +2439,168 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, UsesProvidedMemoryPool) { for (const auto& s : raw_strings) { raw_ba.emplace_back(static_cast(s.size()), reinterpret_cast(s.data())); - expected_payload_bytes += static_cast(s.size()); } - ::arrow::ProxyMemoryPool tracking_pool(default_memory_pool()); - { - auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, - /*use_dictionary=*/false, - /*descr=*/nullptr, &tracking_pool); - EXPECT_EQ(&tracking_pool, encoder->memory_pool()); + constexpr int kSpacedCount = 8; + constexpr uint8_t kSpacedValidBits = 0b10110101; + int64_t spaced_bytes = 0; + for (int i = 0; i < kSpacedCount; ++i) { + if (bit_util::GetBit(&kSpacedValidBits, i)) { + spaced_bytes += raw_ba[i].len; + } + } + const int64_t expected_payload_bytes = + 3 * full_bytes + nullable_bytes + sliced_bytes + spaced_bytes; + auto descr = ExampleDescr(); - for (const auto& arr : {dense_arr, nullable_arr, sliced_large, view_arr}) { - encoder->Put(*arr); - auto flushed = encoder->FlushValues(); - ASSERT_GT(flushed->size(), 0); + for (auto encoding : {Encoding::DELTA_LENGTH_BYTE_ARRAY, Encoding::DELTA_BYTE_ARRAY}) { + ARROW_SCOPED_TRACE("encoding = ", EncodingToString(encoding)); + std::unique_ptr tracking_pool = MemoryPool::CreateDefault(); + int64_t flushed_bytes = 0; + + const int64_t default_before = default_memory_pool()->total_bytes_allocated(); + { + auto encoder = MakeTypedEncoder(encoding, /*use_dictionary=*/false, + descr.get(), tracking_pool.get()); + EXPECT_EQ(tracking_pool.get(), encoder->memory_pool()); + + for (const auto& arr : {dense_arr, nullable_arr, sliced_large, view_arr}) { + encoder->Put(*arr); + flushed_bytes += encoder->FlushValues()->size(); + } + encoder->Put(raw_ba.data(), static_cast(raw_ba.size())); + flushed_bytes += encoder->FlushValues()->size(); + + encoder->PutSpaced(raw_ba.data(), kSpacedCount, &kSpacedValidBits, + /*valid_bits_offset=*/0); + flushed_bytes += encoder->FlushValues()->size(); } - encoder->Put(raw_ba.data(), static_cast(raw_ba.size())); - auto flushed = encoder->FlushValues(); - ASSERT_GT(flushed->size(), 0); + const int64_t default_after = default_memory_pool()->total_bytes_allocated(); - auto dba_encoder = MakeTypedEncoder(Encoding::DELTA_BYTE_ARRAY, - /*use_dictionary=*/false, - /*descr=*/nullptr, &tracking_pool); - dba_encoder->Put(*dense_arr); - ASSERT_GT(dba_encoder->FlushValues()->size(), 0); + EXPECT_EQ(default_before, default_after); + EXPECT_GT(flushed_bytes, 0); + EXPECT_GT(tracking_pool->num_allocations(), 0); + EXPECT_GE(tracking_pool->total_bytes_allocated(), flushed_bytes); + if (encoding == Encoding::DELTA_LENGTH_BYTE_ARRAY) { + EXPECT_GT(flushed_bytes, expected_payload_bytes); + EXPECT_GE(tracking_pool->total_bytes_allocated(), expected_payload_bytes); + } + EXPECT_EQ(0, tracking_pool->bytes_allocated()); } +} - EXPECT_GT(tracking_pool.num_allocations(), 0); - EXPECT_GE(tracking_pool.total_bytes_allocated(), expected_payload_bytes); - EXPECT_EQ(0, tracking_pool.bytes_allocated()); +TEST(DeltaLengthByteArrayEncodingAdHoc, RejectsInvalidAndOverflowingOffsets) { + auto descr = ExampleDescr(); + const std::shared_ptr one_byte = ::arrow::Buffer::FromString("x"); + + auto MakeOffsetsBuffer = [](const auto& offsets) { + using OffsetType = typename std::decay_t::value_type; + const auto nbytes = static_cast(offsets.size() * sizeof(OffsetType)); + std::shared_ptr buf = AllocateBuffer(default_memory_pool(), nbytes); + std::memcpy(buf->mutable_data(), offsets.data(), static_cast(nbytes)); + return buf; + }; + auto MakeBinary = [&](const std::vector& offsets) { + return ::arrow::BinaryArray(static_cast(offsets.size() - 1), + MakeOffsetsBuffer(offsets), one_byte, + /*null_bitmap=*/nullptr, /*null_count=*/0); + }; + auto MakeLargeBinary = [&](const std::vector& offsets) { + return ::arrow::LargeBinaryArray(static_cast(offsets.size() - 1), + MakeOffsetsBuffer(offsets), one_byte, + /*null_bitmap=*/nullptr, /*null_count=*/0); + }; + auto ExpectPutThrows = [](TypedEncoder* enc, const ::arrow::Array& arr, + std::string_view expected_substr) { + try { + enc->Put(arr); + FAIL() << "Expected ParquetException containing: " << expected_substr; + } catch (const ParquetException& e) { + EXPECT_NE(std::string_view(e.what()).find(expected_substr), std::string_view::npos) + << "Actual message: " << e.what(); + } + }; + auto ExpectFreshPutThrows = [&](const ::arrow::Array& arr, + std::string_view expected_substr) { + auto enc = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, + /*use_dictionary=*/false, descr.get()); + ExpectPutThrows(enc.get(), arr, expected_substr); + }; + + // (1) sink_.length()-aware excess expansion rejects without mutating encoder state. + { + auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, + /*use_dictionary=*/false, descr.get()); + encoder->Put(*::arrow::ArrayFromJSON(::arrow::utf8(), R"(["a"])")); + ExpectPutThrows(encoder.get(), MakeBinary({0, std::numeric_limits::max()}), + "excess expansion"); + ExpectPutThrows(encoder.get(), + MakeLargeBinary({0, int64_t{1} << 30, (int64_t{1} << 31) - 1}), + "excess expansion"); + EXPECT_EQ(1, encoder->ReportUnencodedDataBytes()); + EXPECT_EQ(DeltaEncode(std::vector{1})->ToString() + "a", + encoder->FlushValues()->ToString()); + } + + // (2) LargeBinary >= 2GB element rejection (both monotonic and non-monotonic offsets). + ExpectFreshPutThrows(MakeLargeBinary({0, int64_t{1} << 31}), "size 2GB or more"); + ExpectFreshPutThrows(MakeLargeBinary({0, std::numeric_limits::max()}), + "size 2GB or more"); + ExpectFreshPutThrows(MakeLargeBinary({0, int64_t{1} << 31, 1}), "size 2GB or more"); + ExpectFreshPutThrows(MakeLargeBinary({0, int64_t{3} << 30, 10}), "size 2GB or more"); + + // (3) Negative start offsets and negative element steps (including INT64_MIN bounds). + ExpectFreshPutThrows(MakeBinary({-5, 0}), "Negative offset"); + ExpectFreshPutThrows(MakeBinary({5, 2}), "Negative string length"); + ExpectFreshPutThrows(MakeBinary({0, 5, 3}), "Negative string length"); + ExpectFreshPutThrows(MakeLargeBinary({-1, 0}), "Negative offset"); + ExpectFreshPutThrows(MakeLargeBinary({0, -5}), "Negative string length"); + ExpectFreshPutThrows(MakeLargeBinary({5, std::numeric_limits::min()}), + "Negative string length"); + ExpectFreshPutThrows(MakeLargeBinary({0, 5, std::numeric_limits::min(), 6}), + "Negative string length"); +} + +TEST(DeltaLengthByteArrayEncodingAdHoc, + PutAccumulatesAcrossFastAndGeneralPathsBeforeFlush) { + auto descr = ExampleDescr(); + auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, + /*use_dictionary=*/false, descr.get()); + auto nullable = ::arrow::ArrayFromJSON(::arrow::utf8(), R"(["ab", null, "", "cde"])"); + auto dense_slice = + ::arrow::ArrayFromJSON(::arrow::utf8(), R"(["x", "yz", "w"])")->Slice(1); + auto all_null = ::arrow::ArrayFromJSON(::arrow::utf8(), R"([null, null])"); + auto empty_arr = ::arrow::ArrayFromJSON(::arrow::utf8(), R"([])"); + auto view = ::arrow::ArrayFromJSON(::arrow::utf8_view(), R"(["fgh", ""])"); + const std::string tail = "ij"; + const ByteArray raw[] = {ByteArray(2, reinterpret_cast(tail.data()))}; + + encoder->Put(*nullable); // general path (nullable) + encoder->Put(*dense_slice); // fast path, start offset 1 + encoder->Put(*all_null); // no-op early return + encoder->Put(*empty_arr); // no-op early return + encoder->Put(*view); // general path (StringView) + encoder->Put(raw, 1); // raw ByteArray path + + EXPECT_EQ(13, encoder->ReportUnencodedDataBytes()); + EXPECT_EQ(0, encoder->ReportUnencodedDataBytes()); + std::shared_ptr buf = encoder->FlushValues(); + + const std::vector expected_lengths = {2, 0, 3, 2, 1, 3, 0, 2}; + ASSERT_EQ(DeltaEncode(expected_lengths)->ToString() + "abcdeyzwfghij", buf->ToString()); + + auto decoder = + MakeTypedDecoder(Encoding::DELTA_LENGTH_BYTE_ARRAY, descr.get()); + const int num_values = static_cast(expected_lengths.size()); + decoder->SetData(num_values, buf->data(), static_cast(buf->size())); + typename EncodingTraits::Accumulator acc; + ASSERT_OK_AND_ASSIGN(acc.builder, ::arrow::MakeBuilder(::arrow::utf8())); + ASSERT_EQ(num_values, decoder->DecodeArrowNonNull(num_values, &acc)); + std::shared_ptr<::arrow::Array> decoded; + ASSERT_OK(acc.builder->Finish(&decoded)); + auto expected = ::arrow::ArrayFromJSON( + ::arrow::utf8(), R"(["ab", "", "cde", "yz", "w", "fgh", "", "ij"])"); + ::arrow::AssertArraysEqual(*expected, *decoded, /*verbose=*/true); } TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) { @@ -2481,11 +2619,7 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) auto buf = encoder->FlushValues(); auto expected_len_buf = DeltaEncode(expected_valid_lengths); - ASSERT_EQ(expected_len_buf->size() + static_cast(expected_payload.size()), - buf->size()); - EXPECT_TRUE(SliceBuffer(buf, 0, expected_len_buf->size())->Equals(*expected_len_buf)); - EXPECT_TRUE( - SliceBuffer(buf, expected_len_buf->size())->Equals(Buffer(expected_payload))); + ASSERT_EQ(expected_len_buf->ToString() + expected_payload, buf->ToString()); const int num_valid = static_cast(expected_valid_lengths.size()); decoder->SetData(num_valid, buf->data(), static_cast(buf->size())); @@ -2500,7 +2634,10 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) ::arrow::AssertArraysEqual(*arr, *decoded, /*verbose=*/true); }; - for (int count : {0, 1, 255, 256, 257, 512, 600}) { + // Exercise kBatchSize = 256 boundaries on both dense and nullable arrays, plus 0-length + // empty and all-null arrays. Nullable arrays batch only non-null values (every 5th row + // is null), so 319/320/322 rows give 255/256/257 non-null values. + for (int count : {0, 1, 255, 256, 257, 319, 320, 322, 512, 600}) { ARROW_SCOPED_TRACE("count = ", count); ::arrow::StringBuilder dense_b; ::arrow::StringBuilder nullable_b; @@ -2530,12 +2667,14 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) ASSERT_OK_AND_ASSIGN(auto empty_arr, empty_b.Finish()); ASSERT_OK_AND_ASSIGN(auto nulls_arr, nulls_b.Finish()); - VerifyArrayRoundTrip(dense_arr, dense_lens, dense_payload); - VerifyArrayRoundTrip(nullable_arr, nullable_lens, nullable_payload); - VerifyArrayRoundTrip(empty_arr, empty_lens, ""); - VerifyArrayRoundTrip(nulls_arr, {}, ""); + ASSERT_NO_FATAL_FAILURE(VerifyArrayRoundTrip(dense_arr, dense_lens, dense_payload)); + ASSERT_NO_FATAL_FAILURE( + VerifyArrayRoundTrip(nullable_arr, nullable_lens, nullable_payload)); + ASSERT_NO_FATAL_FAILURE(VerifyArrayRoundTrip(empty_arr, empty_lens, "")); + ASSERT_NO_FATAL_FAILURE(VerifyArrayRoundTrip(nulls_arr, {}, "")); } + // Sliced non-null LargeStringArray with non-zero start offset across batch boundary. { ::arrow::LargeStringBuilder large_b; std::vector vals; @@ -2551,14 +2690,16 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) lens.push_back(static_cast(vals[i].size())); payload.append(vals[i]); } - VerifyArrayRoundTrip(sliced, lens, payload); + ASSERT_NO_FATAL_FAILURE(VerifyArrayRoundTrip(sliced, lens, payload)); } + // StringViewArray mixing inline (<=12B), out-of-line (>12B), empty, and nulls + // (288 rows with every 9th null gives exactly 256 non-nulls = kBatchSize). { ::arrow::StringViewBuilder view_b; std::vector lens; std::string payload; - for (int i = 0; i < 270; ++i) { + for (int i = 0; i < 288; ++i) { if (i % 9 == 0) { ASSERT_OK(view_b.AppendNull()); continue; @@ -2571,9 +2712,10 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) payload.append(s); } ASSERT_OK_AND_ASSIGN(auto view_arr, view_b.Finish()); - VerifyArrayRoundTrip(view_arr, lens, payload); + ASSERT_NO_FATAL_FAILURE(VerifyArrayRoundTrip(view_arr, lens, payload)); } + // Direct Put / PutSpaced with zero-length nullptr ByteArrays (UBSan safety). { const std::string abc = "abc"; std::vector mixed = { @@ -2592,7 +2734,7 @@ TEST(DeltaLengthByteArrayEncodingAdHoc, BatchedLengthsSlicesAndZeroLengthSafety) auto zero_buf = encoder->FlushValues(); EXPECT_EQ(DeltaEncode({0, 0, 0, 0})->ToString(), zero_buf->ToString()); - const uint8_t valid_bits = 0b00001010; + const uint8_t valid_bits = 0b00001010; // indices 1 and 3 valid (both 0-len nullptr) encoder->PutSpaced(all_zero_null.data(), 4, &valid_bits, /*valid_bits_offset=*/0); EXPECT_EQ(0, encoder->ReportUnencodedDataBytes()); auto spaced_buf = encoder->FlushValues();