Skip to content

Commit

Permalink
apacheGH-39210: [C++][Parquet] Avoid WriteRecordBatch from produce ze…
Browse files Browse the repository at this point in the history
…ro-sized RowGroup (apache#39211)

### Rationale for this change

`WriteRecordBatch` might produce zero-sized row-group, which is mentioned in apache#39210 . This patch avoid WriteRecordBatch from produce zero-sized RowGroup.

### What changes are included in this PR?

adding a check for zero-sized row-group

### Are these changes tested?

Yes

### Are there any user-facing changes?

no

* Closes: apache#39210

Lead-authored-by: mwish <[email protected]>
Co-authored-by: Antoine Pitrou <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
mapleFU and pitrou authored Dec 13, 2023
1 parent b5a4657 commit 4142607
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
41 changes: 41 additions & 0 deletions cpp/src/parquet/arrow/arrow_reader_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5224,6 +5224,47 @@ TEST(TestArrowReadWrite, WriteAndReadRecordBatch) {
EXPECT_TRUE(record_batch->Equals(*read_record_batch));
}

TEST(TestArrowReadWrite, WriteRecordBatchNotProduceEmptyRowGroup) {
// GH-39211: WriteRecordBatch should prevent from writing a empty row group
// in the end of the file.
auto pool = ::arrow::default_memory_pool();
auto sink = CreateOutputStream();
// Limit the max number of rows in a row group to 2
auto writer_properties = WriterProperties::Builder().max_row_group_length(2)->build();
auto arrow_writer_properties = default_arrow_writer_properties();

// Prepare schema
auto schema = ::arrow::schema({::arrow::field("a", ::arrow::int64())});
std::shared_ptr<SchemaDescriptor> parquet_schema;
ASSERT_OK_NO_THROW(ToParquetSchema(schema.get(), *writer_properties,
*arrow_writer_properties, &parquet_schema));
auto schema_node = std::static_pointer_cast<GroupNode>(parquet_schema->schema_root());

auto gen = ::arrow::random::RandomArrayGenerator(/*seed=*/42);

// Create writer to write data via RecordBatch.
auto writer = ParquetFileWriter::Open(sink, schema_node, writer_properties);
std::unique_ptr<FileWriter> arrow_writer;
ASSERT_OK(FileWriter::Make(pool, std::move(writer), schema, arrow_writer_properties,
&arrow_writer));
// NewBufferedRowGroup() is not called explicitly and it will be called
// inside WriteRecordBatch().
// Write 20 rows for two times
for (int i = 0; i < 2; ++i) {
auto record_batch =
gen.BatchOf({::arrow::field("a", ::arrow::int64())}, /*length=*/20);
ASSERT_OK_NO_THROW(arrow_writer->WriteRecordBatch(*record_batch));
}
ASSERT_OK_NO_THROW(arrow_writer->Close());
ASSERT_OK_AND_ASSIGN(auto buffer, sink->Finish());

auto file_metadata = arrow_writer->metadata();
EXPECT_EQ(20, file_metadata->num_row_groups());
for (int i = 0; i < 20; ++i) {
EXPECT_EQ(2, file_metadata->RowGroup(i)->num_rows());
}
}

TEST(TestArrowReadWrite, MultithreadedWrite) {
const int num_columns = 20;
const int num_rows = 1000;
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/parquet/arrow/writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ class FileWriterImpl : public FileWriter {
// Max number of rows allowed in a row group.
const int64_t max_row_group_length = this->properties().max_row_group_length();

// Initialize a new buffered row group writer if necessary.
if (row_group_writer_ == nullptr || !row_group_writer_->buffered() ||
row_group_writer_->num_rows() >= max_row_group_length) {
RETURN_NOT_OK(NewBufferedRowGroup());
Expand Down Expand Up @@ -461,8 +462,9 @@ class FileWriterImpl : public FileWriter {
RETURN_NOT_OK(WriteBatch(offset, batch_size));
offset += batch_size;

// Flush current row group if it is full.
if (row_group_writer_->num_rows() >= max_row_group_length) {
// Flush current row group writer and create a new writer if it is full.
if (row_group_writer_->num_rows() >= max_row_group_length &&
offset < batch.num_rows()) {
RETURN_NOT_OK(NewBufferedRowGroup());
}
}
Expand Down

0 comments on commit 4142607

Please sign in to comment.