diff --git a/cpp/src/parquet/column_writer.cc b/cpp/src/parquet/column_writer.cc index fbd34677b03..216c608b990 100644 --- a/cpp/src/parquet/column_writer.cc +++ b/cpp/src/parquet/column_writer.cc @@ -1364,8 +1364,13 @@ class TypedColumnWriterImpl : public ColumnWriterImpl, value_offset += values_to_write; // Dictionary size checked separately from data page size since we - // circumvent this check when writing ::arrow::DictionaryArray directly - CheckDictionarySizeLimit(); + // circumvent this check when writing ::arrow::DictionaryArray directly. + // check_page is true only when this chunk ends on a record boundary (always + // for non-repeated columns). Fallback flushes buffered pages, so only check + // here to avoid splitting a repeated record across pages. + if (check_page) { + CheckDictionarySizeLimit(); + } }; DoInBatches(def_levels, rep_levels, num_values, properties_->write_batch_size(), properties_->max_rows_per_page(), pages_change_on_record_boundaries(), @@ -1416,8 +1421,13 @@ class TypedColumnWriterImpl : public ColumnWriterImpl, value_offset += batch_num_spaced_values; // Dictionary size checked separately from data page size since we - // circumvent this check when writing ::arrow::DictionaryArray directly - CheckDictionarySizeLimit(); + // circumvent this check when writing ::arrow::DictionaryArray directly. + // check_page is true only when this chunk ends on a record boundary (always + // for non-repeated columns). Fallback flushes buffered pages, so only check + // here to avoid splitting a repeated record across pages. + if (check_page) { + CheckDictionarySizeLimit(); + } }; DoInBatches(def_levels, rep_levels, num_values, properties_->write_batch_size(), properties_->max_rows_per_page(), pages_change_on_record_boundaries(), @@ -2562,7 +2572,11 @@ Status TypedColumnWriterImpl::WriteArrowDense( CommitWriteAndCheckPageLimit(batch_size, batch_num_values, batch_size - non_null, check_page); - CheckDictionarySizeLimit(); + // Only check on record boundaries so fallback never splits a repeated record + // across pages (see WriteBatchInternal). + if (check_page) { + CheckDictionarySizeLimit(); + } value_offset += batch_num_spaced_values; }; diff --git a/cpp/src/parquet/column_writer_test.cc b/cpp/src/parquet/column_writer_test.cc index cd553ffe35d..9c5c6804319 100644 --- a/cpp/src/parquet/column_writer_test.cc +++ b/cpp/src/parquet/column_writer_test.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -2621,5 +2622,70 @@ TEST_F(TestDoubleValuesWriter, AlpAllNullDataPageV2) { this->TestAlpOptionalDataPageV2(/*num_nulls=*/SMALL_SIZE); } +TEST(TestColumnWriter, DictionaryFallbackWaitsForRecordBoundary) { + // A WriteBatch call may end mid-record. If the dictionary overflows in that trailing + // partial record, fallback must wait for the next record boundary instead of + // flushing a data page that splits the record (V2 pages must start at a record). + auto schema = std::static_pointer_cast(GroupNode::Make( + "schema", Repetition::REQUIRED, {schema::ByteArray("v", Repetition::REPEATED)})); + auto props = WriterProperties::Builder() + .enable_dictionary() + ->data_page_version(ParquetDataPageVersion::V2) + ->dictionary_pagesize_limit(32) + ->data_pagesize(1 << 20) + ->build(); + auto sink = CreateOutputStream(); + auto file_writer = ParquetFileWriter::Open(sink, schema, props); + auto* writer = + static_cast(file_writer->AppendRowGroup()->NextColumn()); + + // Call 1: record 0 = {"a", "b"} keeps the dictionary small; record 1 starts with three + // long values that overflow the dictionary and is left unfinished. + const std::string long0(24, 'x'), long1(24, 'y'), long2(24, 'z'); + const std::vector values1 = {ByteArray("a"), ByteArray("b"), + ByteArray(long0), ByteArray(long1), + ByteArray(long2)}; + const std::vector def1(values1.size(), 1); + const std::vector rep1 = {0, 1, 0, 1, 1}; + writer->WriteBatch(static_cast(values1.size()), def1.data(), rep1.data(), + values1.data()); + + // Call 2: finish record 1, then write records 2 and 3. + const std::vector values2 = {ByteArray("c"), ByteArray("d"), ByteArray("e")}; + const std::vector def2(values2.size(), 1); + const std::vector rep2 = {1, 0, 0}; + writer->WriteBatch(static_cast(values2.size()), def2.data(), rep2.data(), + values2.data()); + ASSERT_NO_THROW(file_writer->Close()); + ASSERT_OK_AND_ASSIGN(auto buffer, sink->Finish()); + + std::vector all_rep = rep1; + all_rep.insert(all_rep.end(), rep2.begin(), rep2.end()); + + auto file_reader = ParquetFileReader::Open( + std::make_shared<::arrow::io::BufferReader>(buffer), default_reader_properties()); + auto page_reader = file_reader->RowGroup(0)->GetColumnPageReader(0); + int64_t levels_seen = 0; + int64_t rows_seen = 0; + int num_data_pages = 0; + bool saw_fallback_page = false; + std::shared_ptr page; + while ((page = page_reader->NextPage()) != nullptr) { + if (page->type() != PageType::DATA_PAGE_V2) continue; + auto data_page = std::static_pointer_cast(page); + ASSERT_LT(levels_seen, static_cast(all_rep.size())); + EXPECT_EQ(0, all_rep[levels_seen]) << "data page " << num_data_pages + << " starts mid-record at level " << levels_seen; + saw_fallback_page |= data_page->encoding() == Encoding::PLAIN; + levels_seen += data_page->num_values(); + rows_seen += data_page->num_rows(); + ++num_data_pages; + } + EXPECT_EQ(static_cast(all_rep.size()), levels_seen); + EXPECT_EQ(4, rows_seen); + EXPECT_GE(num_data_pages, 2); + EXPECT_TRUE(saw_fallback_page); +} + } // namespace test } // namespace parquet