Skip to content

[C++][Parquet] Let the writer choose the schema after buffering rows (needed for Variant shredding) #55902

Backlog

Description

@girotomas

Describe the enhancement requested

Writing shredded Variant columns (VariantShredding.md) usually means choosing the typed_value columns from the data: look at the first rows of a file, pick the fields that are consistently typed, then write the file with that shredded schema. Iceberg Java (BufferedFileAppender) and Spark (ParquetOutputWriterWithVariantShredding) both work this way.

The C++ writer needs the full schema before the first value is written:

  • ParquetFileWriter::Open(sink, schema, ...) fixes the columns; num_columns() is "fixed during the lifetime of the writer".
  • AppendBufferedRowGroup() creates one typed ColumnWriter per schema column as soon as the row group opens (RowGroupSerializer::InitColumns in file_writer.cc), and values are encoded, and page by page compressed, as they are written.
  • A column writer cannot be added or replaced, and the values held by a buffered row group cannot be read back.

So a caller that infers the shredding schema has to keep its own copy of the first rows, then open the writer and replay them. For callers that pass Arrow record batches this is cheap, because a batch can be held by reference. For callers that write through the low-level ColumnWriter API one value at a time, it is an extra copy of every sampled value and a second in-memory buffer next to the writer's own buffered row group.

Nothing in the file depends on the schema until the end, though. StartFile() only writes the PAR1 magic, a buffered row group is only flushed when it closes, and the schema is written in the footer at Close(). While the first row group is still buffered, the schema could in principle still change.

Possible directions

  1. Deferred schema finalization (preferred for low-level callers). Allow adding columns, and possibly widening a physical type such as INT32 to INT64, until the first row group is flushed. Only the new or changed columns of that row group would need to be written or re-encoded; the other column writers keep their pages.
  2. A buffering helper in the writer. An option to buffer the first N rows or bytes, call back to finalize the schema, then replay, so callers do not each implement it. This would also be a natural home for writer-side shredding inference (GH-45937: [C++][Parquet] Variant shredding #45948).
  3. Append pre-encoded column chunks to a row group. Unaffected columns would be encoded once and kept, and the shredded columns added later. This is close to the rewriter work in [C++][Parquet] Provide a rewriter to rewrite parquet files without decoding all the row groups/pages #47628 / GH-47628: [C++][Parquet] Implement basic parquet file rewriter #47775 (RowGroupMetaDataBuilder::NextColumnChunk(cc_metadata, shift)).

Questions

Component(s)

C++, Parquet

cc @wgtmac @pitrou @HuaHuaY @mapleFU @wecharyu for advice on the writer design, and @qzyu999 since you took #45948 and may hit the same limit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions