Skip to content

Let ParquetSink opt out of retaining written files' ParquetMetaData, and do so for COPY / INSERT - #27027

Open
xudong963 wants to merge 2 commits into
apache:mainfrom
xudong963:xudong963/parquet-sink-no-metadata-retention
Open

xudong963 wants to merge 2 commits into
apache:mainfrom
xudong963:xudong963/parquet-sink-no-metadata-retention

Conversation

@xudong963

@xudong963 xudong963 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

A COPY ... TO ... STORED AS PARQUET or INSERT INTO that writes many files keeps growing in memory until the statement finishes, outside the memory pool. ParquetSink kept every written file's ParquetMetaData (all column chunk statistics plus page indexes) until the sink was dropped, in two places: the writer tasks returned (Path, ParquetMetaData) through a JoinSet that is only drained after the demux loop has announced the last file, and the join loop then moved each one into written: HashMap<Path, ParquetMetaData>.

Nothing on the COPY / INSERT path 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 (builds DataFiles 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), default true. With false, written() returns an empty map and no file metadata is kept.
  • ParquetFormat::create_writer_physical_plan turns it off for the sinks it builds for COPY and INSERT, since nothing on that path reads the metadata.
  • The writer tasks now return only their file's row and compressed byte counts. record_written_file runs inside the task: it stores the ParquetMetaData in the map when retention is on, or drops it right there when it is off. Either way nothing is buffered in the JoinSet until 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.
  • A test for the switch (parquet_sink_write_without_retained_metadata), a test for the sink that COPY plans (parquet_sink_planned_by_copy_does_not_retain_metadata), and an upgrade guide entry (57.0.0) for the ParquetFormat behavior 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::new and retains metadata. This is documented on with_retain_written_metadata and in the upgrade guide. A proto field could be added if we want COPY on remote executors to benefit too.

What is the testing strategy for this PR?

  • parquet_sink_write_without_retained_metadata checks that a sink with retention off still writes and counts the rows and that written() is empty.
  • parquet_sink_planned_by_copy_does_not_retain_metadata plans a partitioned COPY ... STORED AS PARQUET, takes the ParquetSink out of the plan, runs it, and checks the row count, the three output files, the rows read back, and that written() is empty. It fails if ParquetFormat stops turning retention off.
  • The existing parquet_sink_* tests (default retention) cover written() for single, partitioned and parallel writes and are unchanged. cargo test -p datafusion --lib -- datasource::file_format::parquet::tests::parquet_sink passes (10 tests), as do the datafusion-proto sink round-trip tests.
  • cargo clippy --all-targets --all-features -- -D warnings on the workspace is clean.

Are there any user-facing changes?

New builder method ParquetSink::with_retain_written_metadata. Sinks created by ParquetFormat no longer retain written file metadata; anyone downcasting the sink of such a plan to call written() afterwards would now get an empty map and should construct the ParquetSink themselves (documented in the upgrade guide). No existing API is removed.

@xudong963 xudong963 added the api change Changes the API exposed to users of the crate label Nov 3, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation core Core DataFusion crate datasource Changes to the datasource crate labels Nov 3, 2026
@xudong963
xudong963 marked this pull request as ready for review November 3, 2026 01:51
@matthewmturner

Copy link
Copy Markdown
Contributor

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?

@xudong963

Copy link
Copy Markdown
Member Author

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 written() after all: InfluxDB 3 (parquet_file/src/serialize.rs), datafusion_iceberg (builds DataFiles from it) and LakeSoul (file_sink_writer.rs). All construct ParquetSink themselves and write one or a few files per sink, so the retention is harmless for them.

I’ll switch this PR to keep written() as is and add an opt-out on the builder, turn it off in ParquetFormat::create_writer_physical_plan (nothing on the COPY/INSERT path reads it), and still fix the JoinSet buffering so that opting out retains nothing.

@xudong963
xudong963 force-pushed the xudong963/parquet-sink-no-metadata-retention branch from 247c0bb to 88929ae Compare November 4, 2026 01:51
@xudong963 xudong963 changed the title Stop ParquetSink from retaining every written file's ParquetMetaData Let ParquetSink opt out of retaining written files' ParquetMetaData, and do so for COPY / INSERT Nov 4, 2026
@github-actions
github-actions Bot marked this pull request as draft November 19, 2026 01:07
@github-actions

Copy link
Copy Markdown

Marked as draft after 14 days without activity: merge conflicts.

Please address these items and mark Ready for review when ready.

@xudong963
xudong963 marked this pull request as ready for review November 19, 2026 09:50

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please say metadata is dropped at file close instead. JoinSet still retains a task entry and two counts per completed file until drained.

kosiew

This comment was marked as duplicate.

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.
@xudong963
xudong963 force-pushed the xudong963/parquet-sink-no-metadata-retention branch from 88929ae to 4fcfaae Compare November 26, 2026 02:34
@xudong963

Copy link
Copy Markdown
Member Author

@kosiew thanks for your review, addressed your comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api change Changes the API exposed to users of the crate core Core DataFusion crate datasource Changes to the datasource crate documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ParquetSink keeps every written file's ParquetMetaData in memory until the sink finishes

3 participants