Skip to content

GH-55642: [C++][Python] Make the dataset writer honor min_rows_per_group and add max_rows_queued - #55658

Open
tolleybot wants to merge 11 commits into
apache:mainfrom
tolleybot:GH-55642-min-rows-per-group-docs
Open

tolleybot wants to merge 11 commits into
apache:mainfrom
tolleybot:GH-55642-min-rows-per-group-docs

Conversation

@tolleybot

@tolleybot tolleybot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

The dataset writer stops holding rows back for min_rows_per_group once 2,097,152 rows are staged in
total across all files being written. When that happens it writes out the staged rows of whichever file
received the latest batch, however few they are. As described in #55642 this means:

  • a single file never gets row groups larger than about 2.1 million rows, whatever min_rows_per_group
    is (also reported in Is there a max limit to PyArrow write_dataset row-group sizes #45018)
  • a partitioned write can produce row groups far below min_rows_per_group, down to a few thousand rows
    in the middle of a file
  • there is no option to change the limit

What changes are included in this PR?

Three changes to the dataset writer:

  1. When too many rows are staged, write out the file holding the most staged rows. Before, it was the
    file that received the latest batch. This is what the existing comment on staged_rows_count already
    described. The staged row count is now updated as rows are staged and delivered, which also removes a
    one batch lag in noticing the limit.
  2. The staging limit is never smaller than min_rows_per_group. A single file therefore always
    reaches min_rows_per_group. Staged rows count against max_rows_queued, so a max_rows_queued
    smaller than min_rows_per_group could stall the writer and is now rejected with Invalid.
  3. max_rows_queued becomes an option. It is added to FileSystemDatasetWriteOptions and to
    pyarrow.dataset.write_dataset. It replaces the max_rows_queued parameter of the internal
    DatasetWriter::Make, so there is one place to set it. The staging limit stays at
    max_rows_queued / 4 but no longer has a fixed ceiling of 8,388,608 rows, so raising
    max_rows_queued lets more partitions reach min_rows_per_group at the same time.
  4. max_rows_queued = 0 means no limit, on queued rows and on staged rows. The writer's Throttle
    returned a finished future from Acquire when unthrottled, which callers treat as backpressure, so a
    limit of 0 made the writer retry forever. It now returns std::nullopt.

The writer keeps an ordered record of the directories that have staged rows, so finding the one with the
most staged rows does not scan every directory.

The documentation for min_rows_per_group and max_rows_queued is updated in file_base.h, in the
write_dataset docstring and in the Python dataset user guide to describe the limit and how the two
options interact.

Not included: the R bindings do not expose max_rows_queued.

Are these changes tested?

Yes.

  • dataset_writer_test.cc has five new tests: a single file with min_rows_per_group above the staging
    limit, eviction of the file with the most staged rows, several files that all reach
    min_rows_per_group when max_rows_queued leaves room, the same with max_rows_queued = 0, and the
    Invalid status when max_rows_queued is smaller than min_rows_per_group. The first two fail
    without the change.
  • test_dataset.py has three new tests covering the single file case, a partitioned write with
    max_rows_queued raised, and the error.

With the reproducer from #55642 (20,000,000 rows, min_rows_per_group == max_rows_per_group):

case before after
not partitioned, 3,000,000 rows per group groups of 2,129,920 groups of 3,000,000
3 partitions, 1,000,000 rows per group, default max_rows_queued smallest group inside a file 3,734 smallest group inside a file 699,051
3 or 4 partitions, max_rows_queued = 4 * partitions * min_rows_per_group not possible every group 1,000,000, in 10 of 10 runs with and without threads
4 or 13 partitions, max_rows_queued = 0 not possible every group 1,000,000 except the last of each file, with and without threads

Known limitation: the staging limit is still shared by all files. With the default max_rows_queued, a
write with many partitions that each hold a few staged rows can keep the total near the limit, and the
partition receiving most of the rows is then written in groups smaller than min_rows_per_group. Raising
max_rows_queued is the way out.

Are there any user-facing changes?

  • New option max_rows_queued in FileSystemDatasetWriteOptions and pyarrow.dataset.write_dataset.
    The default is unchanged (8 * 1024 * 1024).
  • Row group sizes change for writes that reach the staging limit. A single file now reaches
    min_rows_per_group. A partitioned write with the default max_rows_queued still may not, but the
    undersized groups are the largest available instead of arbitrary.
  • A min_rows_per_group above 2,097,152 now uses more memory than before, because that many rows are
    held until the group is complete.
  • A min_rows_per_group larger than max_rows_queued is now an error. With the default
    max_rows_queued this affects min_rows_per_group above 8,388,608, which used to be accepted and
    silently produce row groups of about 2.1 million rows. The message says to raise max_rows_queued.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

…is best effort

The dataset writer stops staging rows once 2,097,152 rows are held back across
all files being written, after which row groups smaller than
min_rows_per_group are written. Describe this in the Python docstring, the
C++ option documentation and the Python user guide.
…file and evict the largest staged file

When more rows are staged than the dataset writer allows, write out the staged
rows of the file that holds the most of them instead of the file that received
the latest batch, which could produce very small row groups.

Never let the staging limit be smaller than min_rows_per_group, raising the
limit on queued rows when needed, so that a single file always reaches
min_rows_per_group.
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55642 has been automatically assigned in GitHub to PR creator.

…aset write options

Expose the limit on rows held in memory by the dataset writer as
FileSystemDatasetWriteOptions::max_rows_queued and as the max_rows_queued
argument of pyarrow.dataset.write_dataset. The limit on staged rows now grows
with it without a fixed ceiling, so a partitioned write can reach
min_rows_per_group in every partition when given enough room.
@tolleybot tolleybot changed the title GH-55642: [C++][Python][Docs] Document that min_rows_per_group is best effort GH-55642: [C++][Python] Make the dataset writer honor min_rows_per_group and add max_rows_queued Nov 17, 2026
…aging changes

Read max_rows_queued from the write options only, define its default once, and
reject a max_rows_queued smaller than min_rows_per_group instead of raising it.
Treat max_rows_queued = 0 as no limit on staged rows, which also needed the
throttle to report success when it is unthrottled. Track the directories with
staged rows in order so eviction does not scan every directory. Restore the
parquet mark on test_write_dataset_max_rows_per_group.
@tolleybot
tolleybot marked this pull request as ready for review November 24, 2026 04:46
@tolleybot

Copy link
Copy Markdown
Contributor Author

The one failing check, AMD64 Conda C++ AVX2, is not caused by this change. It fails in the Docker build step, and the same job is failing on main, for example in https://github.com/apache/arrow/actions/runs/45308364892/job/138157637678.

All other checks pass.

@adamreeve
adamreeve self-requested a review December 4, 2026 01:04

@adamreeve adamreeve 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.

Thanks for this @tolleybot, this looks like a worthwhile improvement to me. I have left a few suggestions.

Comment thread cpp/src/arrow/dataset/dataset_writer.cc Outdated
Comment on lines +116 to +119
// Control for how many rows the dataset writer will allow to be staged. A row is
// staged if it is waiting for more rows to reach minimum_batch_size. If this is
// exceeded then the largest staged batch is unstaged (no backpressure is applied)
// staged if it is waiting for more rows to reach min_rows_per_group. If this is
// exceeded then the file with the most staged rows has its staged rows written out
// (no backpressure is applied)

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.

This comment on staged_rows_count seems to be more about the limit than the count. How about simplifying it to something like:

The total number of rows currently staged across all open files.
Rows are staged for each open file until min_rows_per_group is reached.

And then on max_rows_staged, combine what you have here with the other comment below and clarify some things:

Control for how many rows the dataset writer will allow to be staged.
If too many rows get staged we will end up with poor performance and, if more rows
are staged than max_rows_queued we will end up with deadlock. To avoid this,
once staged_rows_count reaches max_rows_staged the file with the most staged rows has
its staged rows written out, even if this results in a row group with fewer rows
than min_rows_per_group (no backpressure is applied).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, I used your wording for both comments.

Comment thread cpp/src/arrow/dataset/dataset_writer.cc Outdated

uint64_t rows_staged() const { return rows_currently_staged_; }

// Deliver all staged batches even though there are fewer than min_rows_per_group

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.

Suggested change
// Deliver all staged batches even though there are fewer than min_rows_per_group
// Deliver all staged batches even if there are fewer than min_rows_per_group

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread cpp/src/arrow/dataset/dataset_writer.h Outdated
/// \param max_rows_queued max # of rows allowed to be queued before the dataset_writer
/// will ask for backpressure
/// \param write_options options to control how the data should be written, including
/// max_rows_queued, the max # of rows allowed to be queued before

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.

Suggested change
/// max_rows_queued, the max # of rows allowed to be queued before
/// max_rows_queued, the maximum number of rows allowed to be queued before

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. I wrapped the line differently from the suggestion to stay within the 90 column limit.

Comment thread python/pyarrow/dataset.py Outdated
Comment on lines +917 to +925
Rows waiting for a row group to fill are held in memory, and the
writer limits how many rows it holds this way, across all files
being written, to the larger of ``max_rows_queued / 4`` and this
value. When the limit is reached, the file holding the most waiting
rows has them written out as a smaller row group. A single file
therefore always reaches this value, but a partitioned write may
not unless ``max_rows_queued`` is 0 or at least 4 times this value
multiplied by the number of partitions being written at the same
time.

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.

This seems like a lot of detail on the internal implementation for a public parameter doc. Could this be simplified to something like:

Partitioned writes may result in row groups with fewer rows than this if rows need to be flushed
to avoid hitting internal limits on the number of staged rows (see ``max_rows_queued``).

?

@tolleybot tolleybot Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, I used your wording. The detail about how the limit on staged rows is derived now sits under max_rows_queued. I made the same change to the doc comments on min_rows_per_group and max_rows_queued in file_base.h so the two match.

{{"testdir/a/chunk-0.arrow", 0, 30, 1}, {"testdir/b/chunk-0.arrow", 30, 20, 1}});
}

TEST_F(DatasetWriterTestFixture, MinRowGroupManyFilesWithRoomToStage) {

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.

Can you also please add a test case where the staged rows limit is hit and a row group other than the last one needs to be written out with number of rows less than min_rows_per_group.

This might be achieved by just modifying MinRowGroupEvictsLargestStaged to send an extra batch to both "a" and "b" after the eviction.

@tolleybot tolleybot Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done as you suggested. MinRowGroupEvictsLargestStaged now sends one more batch to "a" and to "b" after the eviction, so the 30 rows evicted from "a" are followed by a second row group in the same file. I made the test also checks the size of each row group now (30 then 10 in "a", 30 in "b").

Copilot AI 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.

🟢 Approval recommended

There are no active review issues for the dataset staging and queue-limit changes.

No open issues 🎉

What changed in this PR

Addresses undersized dataset row groups reported in #55642 and #45018 by revising staging limits and exposing queue control in C++ and Python.

Changes:

  • Flushes the file with the most staged rows when the shared staging limit is reached.
  • Adds max_rows_queued, including unlimited mode and validation against min_rows_per_group.
  • Adds regression tests and documents memory and row-group sizing tradeoffs.
File Description
python/​pyarrow/​tests/​test_dataset.py Tests row-group sizing and queue-limit validation.
python/​pyarrow/​includes/​libarrow_dataset.pxd Declares the C++ queue-limit option for Cython.
python/​pyarrow/​dataset.py Exposes and documents max_rows_queued.
python/​pyarrow/​_dataset.pyx Passes the Python option to C++.
docs/​source/​python/​dataset.rst Explains staging limits and memory tradeoffs.
cpp/​src/​arrow/​dataset/​file_base.h Adds the public queue-limit option and documentation.
cpp/​src/​arrow/​dataset/​dataset_writer.h Moves queue configuration into write options.
cpp/​src/​arrow/​dataset/​dataset_writer.cc Updates staging, eviction, throttling, and validation.
cpp/​src/​arrow/​dataset/​dataset_writer_test.cc Adds regression coverage for staging and queue limits.

Review effort: Balanced

@adamreeve
adamreeve requested a review from zanmato1984 December 4, 2026 02:52
Reword the comments on the staged row count and limit, simplify the
min_rows_per_group documentation and move the detail to max_rows_queued, and
extend MinRowGroupEvictsLargestStaged so that the evicted row group is not the
last one in its file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][Python] write_dataset writes row groups smaller than min_rows_per_group when partitioned

3 participants