Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions cpp/src/parquet/column_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1066,16 +1066,18 @@ void ColumnWriterImpl::BuildDataPageV2(int64_t definition_levels_rle_size,
page_is_compressed =
(1.0 - static_cast<double>(compressed) / values->size()) >= minimum;
}
std::shared_ptr<Buffer> compressed_values =
(page_is_compressed ? compressor_temp_buffer_ : values);

// Concatenate uncompressed levels and the possibly compressed values
int64_t combined_size =
definition_levels_rle_size + repetition_levels_rle_size + compressed_values->size();
std::shared_ptr<ResizableBuffer> combined = AllocateBuffer(allocator_, combined_size);

ConcatenateBuffers(definition_levels_rle_size, repetition_levels_rle_size,
compressed_values, combined->mutable_data());
std::shared_ptr<Buffer> page_buffer =
page_is_compressed ? compressor_temp_buffer_ : values;

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.

There is an issue here, though I am not sure if it should block this PR.

PageWriter is a pure virtual class with the method signature virtual int64_t WriteDataPage(const DataPage& page) = 0. If a custom class inherits from PageWriter and invokes std::shared_ptr<Buffer> Page::buffer() const { return buffer_; }, it can retrieve a shared_ptr from the const DataPage& page argument.

However, with this change, compressor_temp_buffer_ is written out directly; consequently, downstream components can no longer rely on this shared_ptr remaining valid during the next call to BuildDataPageV2. That said, the fact that const DataPage& page is passed as an argument arguably carries the implicit semantic that the page object might become invalid after the call returns.

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.

I discovered that BuildDataPageV1 already contained similar code six years ago.

// Write the page to OutputStream eagerly if there is no dictionary or
// if dictionary encoding has fallen back to PLAIN.
const bool buffer_page = has_dictionary_ && !fallback_;
const int64_t levels_size = definition_levels_rle_size + repetition_levels_rle_size;
if (levels_size > 0 || buffer_page) {
auto combined = AllocateBuffer(allocator_, levels_size + page_buffer->size());
ConcatenateBuffers(definition_levels_rle_size, repetition_levels_rle_size,
page_buffer, combined->mutable_data());
page_buffer = std::move(combined);
}

auto [page_stats, page_size_stats] = GetPageStatistics();
page_stats.ApplyStatSizeLimits(properties_->max_statistics_size(descr_->path()));
Expand All @@ -1093,17 +1095,15 @@ void ColumnWriterImpl::BuildDataPageV2(int64_t definition_levels_rle_size,
// here for safety check.
DCHECK(!page_stats.has_null_count || page_stats.null_count == null_count);

// Write the page to OutputStream eagerly if there is no dictionary or
// if dictionary encoding has fallen back to PLAIN
if (has_dictionary_ && !fallback_) { // Save pages until end of dictionary encoding
if (buffer_page) {
std::unique_ptr<DataPage> page_ptr = std::make_unique<DataPageV2>(
combined, num_values, null_count, num_rows, encoding_, def_levels_byte_length,
page_buffer, num_values, null_count, num_rows, encoding_, def_levels_byte_length,
rep_levels_byte_length, uncompressed_size, page_is_compressed,
std::move(page_stats), first_row_index, std::move(page_size_stats));
total_compressed_bytes_ += page_ptr->size() + sizeof(format::PageHeader);
data_pages_.push_back(std::move(page_ptr));
} else {
DataPageV2 page(combined, num_values, null_count, num_rows, encoding_,
DataPageV2 page(page_buffer, num_values, null_count, num_rows, encoding_,
def_levels_byte_length, rep_levels_byte_length, uncompressed_size,
page_is_compressed, std::move(page_stats), first_row_index,
std::move(page_size_stats));
Comment on lines +1106 to 1109

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.

This comment was addressed earlier.

Expand Down
23 changes: 17 additions & 6 deletions cpp/src/parquet/file_serialize_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,10 @@ TEST(TestBufferedRowGroupWriter, DisabledDictionary) {
ASSERT_FALSE(rg_reader->metadata()->ColumnChunk(0)->has_dictionary_page());
}

TEST(TestBufferedRowGroupWriter, MultiPageDisabledDictionary) {
Comment thread
wgtmac marked this conversation as resolved.
Resolved
static void CheckMultiPageRoundTrip(
const std::shared_ptr<WriterProperties>& writer_props) {
constexpr int kValueCount = 10000;
constexpr int kPageSize = 16384;
auto sink = CreateOutputStream();
auto writer_props = parquet::WriterProperties::Builder()
.disable_dictionary()
->data_pagesize(kPageSize)
->build();
schema::NodeVector fields;
fields.push_back(
PrimitiveNode::Make("col", parquet::Repetition::REQUIRED, parquet::Type::INT32));
Expand Down Expand Up @@ -447,6 +443,21 @@ TEST(TestBufferedRowGroupWriter, MultiPageDisabledDictionary) {
}
}

TEST(TestBufferedRowGroupWriter, MultiPage) {
WriterProperties::Builder builder;
builder.disable_dictionary()->data_pagesize(16384);
ASSERT_NO_FATAL_FAILURE(CheckMultiPageRoundTrip(builder.build()));
#ifdef ARROW_WITH_ZSTD
// Eight equal-sized pages start alternately at 1 and 51 in the input sequence.
builder.data_page_version(ParquetDataPageVersion::V2)
->compression(Compression::ZSTD)
->max_rows_per_page(1250);
ASSERT_NO_FATAL_FAILURE(CheckMultiPageRoundTrip(builder.build()));
builder.enable_dictionary();
ASSERT_NO_FATAL_FAILURE(CheckMultiPageRoundTrip(builder.build()));
#endif
}

TEST(ParquetRoundtrip, AllNulls) {
auto primitive_node =
PrimitiveNode::Make("nulls", Repetition::OPTIONAL, nullptr, Type::INT32);
Expand Down
Loading