Repository navigation
Let ParquetSink opt out of retaining written files' ParquetMetaData, and do so for COPY / INSERT - #27027
Let ParquetSink opt out of retaining written files' ParquetMetaData, and do so for COPY / INSERT#27027xudong963 wants to merge 2 commits into
Conversation
|
I am wondering if users of datafusion may have used that returned parquet metadata for creating external row group indexes so while internally its not used it could potentially be disruptive to users. Maybe it would be safer to keep existing functionality as is and add the ability to opt out of returning metadata? |
Good call, thanks @matthewmturner! I let llm re-check all github repos, lol, and there are real users of I’ll switch this PR to keep |
247c0bb to
88929ae
Compare
|
Marked as draft after 14 days without activity: merge conflicts. Please address these items and mark Ready for review when ready. |
kosiew
left a comment
There was a problem hiding this comment.
@xudong963, thanks for addressing the metadata memory growth. Handling metadata inside writer tasks while preserving retention for direct consumers is appropriate.
Please add regression coverage for the actual COPY planning path. The current test does not verify that production sinks disable retention.
| // Nothing on this path reads `ParquetSink::written`, and keeping | ||
| // every file's metadata would grow memory with the number of | ||
| // files a statement writes. | ||
| .with_retain_written_metadata(false), |
There was a problem hiding this comment.
The current test explicitly disables retention, so it cannot catch a regression here. Please add a multi-file COPY test using the planned sink, verifying output files, row counts, and empty written() metadata.
|
|
||
| See [issue #18882](https://github.com/apache/datafusion/issues/18882) for details. | ||
|
|
||
| ### `ParquetSink`s created by `ParquetFormat` no longer retain written file metadata |
There was a problem hiding this comment.
Please move this note to upgrading/57.0.0.md when rebasing. Main already links the 57.0.0 guide.
| /// files pays for it in memory. Turn it off when nothing reads | ||
| /// [`Self::written`], as the sinks `ParquetFormat` creates for `COPY` and | ||
| /// `INSERT` do. | ||
| pub fn with_retain_written_metadata(mut self, retain: bool) -> Self { |
There was a problem hiding this comment.
Could we document that protobuf serialization does not preserve this opt-out? Decoding constructs a new ParquetSink with retention enabled, including on distributed executors. Please clarify this in the upgrade note.
| /// when the sink retains metadata. | ||
| /// | ||
| /// Runs inside the writer task so that, with retention off, the metadata is | ||
| /// dropped as soon as the file is closed and the sink holds no per-file state. |
There was a problem hiding this comment.
Nit: Please say metadata is dropped at file close instead. JoinSet still retains a task entry and two counts per completed file until drained.
ParquetSink kept each written file's ParquetMetaData until the sink was dropped: the writer tasks returned it through a JoinSet that is only drained once the last file has been announced, and the join loop then stored it in the `written` map. The metadata carries every column chunk's statistics and page indexes, so a COPY or INSERT that writes many files grew memory outside the pool until it finished, although nothing on that path reads the map. ParquetSink::written() does have users that construct the sink themselves (InfluxDB, datafusion_iceberg, LakeSoul), so the map stays and keeps its default. Add ParquetSink::with_retain_written_metadata(bool) and turn it off for the sinks ParquetFormat creates. Writer tasks now return only their file's row and byte counts; record_written_file stores or drops the metadata inside the task, so nothing is buffered in the JoinSet either. Closes apache#27026
- Add parquet_sink_planned_by_copy_does_not_retain_metadata, which plans a partitioned COPY, checks the output files and rows, and asserts that the ParquetSink built by ParquetFormat retains no metadata. - Document that the retention switch is not encoded by datafusion-proto, so a decoded sink retains metadata again (rustdoc, encode comments, upgrade guide). - Say the metadata is dropped when the file is closed, not that the sink holds no per-file state: the JoinSet keeps finished tasks until drained.
88929ae to
4fcfaae
Compare
|
@kosiew thanks for your review, addressed your comments! |
Which issue does this PR close?
ParquetSink).Rationale for this change
A
COPY ... TO ... STORED AS PARQUETorINSERT INTOthat writes many files keeps growing in memory until the statement finishes, outside the memory pool.ParquetSinkkept every written file'sParquetMetaData(all column chunk statistics plus page indexes) until the sink was dropped, in two places: the writer tasks returned(Path, ParquetMetaData)through aJoinSetthat is only drained after the demux loop has announced the last file, and the join loop then moved each one intowritten: HashMap<Path, ParquetMetaData>.Nothing on the
COPY/INSERTpath reads that map.ParquetSink::written()does have users who construct the sink themselves and read the metadata of the one or few files it wrote: InfluxDB 3 (parquet_file/src/serialize.rs),datafusion_iceberg(buildsDataFiles from it) and LakeSoul (file_sink_writer.rs). Their behavior is unchanged. #27026 has the production numbers: a ~700-file partition write grew RSS by roughly 1 GiB per 10 GB written until it was OOM-killed.What changes are included in this PR?
ParquetSink::with_retain_written_metadata(bool), defaulttrue. Withfalse,written()returns an empty map and no file metadata is kept.ParquetFormat::create_writer_physical_planturns it off for the sinks it builds forCOPYandINSERT, since nothing on that path reads the metadata.record_written_fileruns inside the task: it stores theParquetMetaDatain the map when retention is on, or drops it right there when it is off. Either way nothing is buffered in theJoinSetuntil the last file is announced. The duplicate path check moves with it; since it is done against the map, it only runs when retention is on. Metrics are unchanged.parquet_sink_write_without_retained_metadata), a test for the sink thatCOPYplans (parquet_sink_planned_by_copy_does_not_retain_metadata), and an upgrade guide entry (57.0.0) for theParquetFormatbehavior change and the protobuf caveat below.The retention switch is not part of the plan's protobuf encoding, like the map itself; a sink that went through a round-trip is created with
ParquetSink::newand retains metadata. This is documented onwith_retain_written_metadataand in the upgrade guide. A proto field could be added if we wantCOPYon remote executors to benefit too.What is the testing strategy for this PR?
parquet_sink_write_without_retained_metadatachecks that a sink with retention off still writes and counts the rows and thatwritten()is empty.parquet_sink_planned_by_copy_does_not_retain_metadataplans a partitionedCOPY ... STORED AS PARQUET, takes theParquetSinkout of the plan, runs it, and checks the row count, the three output files, the rows read back, and thatwritten()is empty. It fails ifParquetFormatstops turning retention off.parquet_sink_*tests (default retention) coverwritten()for single, partitioned and parallel writes and are unchanged.cargo test -p datafusion --lib -- datasource::file_format::parquet::tests::parquet_sinkpasses (10 tests), as do thedatafusion-protosink round-trip tests.cargo clippy --all-targets --all-features -- -D warningson the workspace is clean.Are there any user-facing changes?
New builder method
ParquetSink::with_retain_written_metadata. Sinks created byParquetFormatno longer retain written file metadata; anyone downcasting the sink of such a plan to callwritten()afterwards would now get an empty map and should construct theParquetSinkthemselves (documented in the upgrade guide). No existing API is removed.