Repository navigation
Description
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 typedColumnWriterper schema column as soon as the row group opens (RowGroupSerializer::InitColumnsinfile_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
- 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.
- 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).
- 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
- Would the project accept option 1 or option 3, and which fits the writer's design better?
- For Variant shredding (GH-45937: [C++][Parquet] Variant shredding #45948, GH-45937: [C++][Parquet] support to read, write and validate variant #50252): is schema inference planned on the writer side, and at which level, the Arrow
FileWriteror the low-levelParquetFileWriter? - Do other users need to defer schema decisions, for example encodings chosen from the data?
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
Activity