adamreeve commented on code in PR #55658:
URL: https://github.com/apache/arrow/pull/55658#discussion_r4794044899


##########
cpp/src/arrow/dataset/dataset_writer.h:
##########
@@ -46,14 +43,13 @@ class ARROW_DS_EXPORT DatasetWriter {
   /// Will fail if basename_template is invalid or if there is existing data 
and
   /// existing_data_behavior is kError
   ///
-  /// \param write_options options to control how the data should be written
-  /// \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

Review Comment:
   ```suggestion
     ///                      max_rows_queued, the maximum number of rows 
allowed to be queued before
   ```



##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -201,35 +206,42 @@ class DatasetWriterFileQueue
         "DatasetWriter::WriteBatch"sv);
   }
 
-  Result<int64_t> PopAndDeliverStagedBatch() {
+  Status PopAndDeliverStagedBatch() {
     ARROW_ASSIGN_OR_RAISE(std::shared_ptr<RecordBatch> next_batch, 
PopStagedBatch());
-    int64_t rows_popped = next_batch->num_rows();
-    rows_currently_staged_ -= next_batch->num_rows();
+    uint64_t rows_popped = static_cast<uint64_t>(next_batch->num_rows());
+    rows_currently_staged_ -= rows_popped;
+    writer_state_->staged_rows_count -= rows_popped;
     ScheduleBatch(std::move(next_batch));
-    return rows_popped;
+    return Status::OK();
   }
 
   // Stage batches, popping and delivering batches if enough data has arrived
   Status Push(std::shared_ptr<RecordBatch> batch) {
-    uint64_t delta_staged = batch->num_rows();
-    rows_currently_staged_ += delta_staged;
+    uint64_t rows_pushed = static_cast<uint64_t>(batch->num_rows());
+    rows_currently_staged_ += rows_pushed;
+    writer_state_->staged_rows_count += rows_pushed;
     staged_batches_.push_back(std::move(batch));
     while (!staged_batches_.empty() &&
-           (writer_state_->StagingFull() ||
-            rows_currently_staged_ >= options_.min_rows_per_group)) {
-      ARROW_ASSIGN_OR_RAISE(int64_t rows_popped, PopAndDeliverStagedBatch());
-      delta_staged -= rows_popped;
+           rows_currently_staged_ >= options_.min_rows_per_group) {
+      RETURN_NOT_OK(PopAndDeliverStagedBatch());
+    }
+    return Status::OK();
+  }
+
+  uint64_t rows_staged() const { return rows_currently_staged_; }
+
+  // Deliver all staged batches even though there are fewer than 
min_rows_per_group

Review Comment:
   ```suggestion
     // Deliver all staged batches even if there are fewer than 
min_rows_per_group
   ```



##########
python/pyarrow/dataset.py:
##########
@@ -913,6 +914,15 @@ def write_dataset(data, base_dir, *, 
basename_template=None, format=None,
         Minimum number of rows per group. When the value is greater than 0,
         the dataset writer will batch incoming data and only write the row
         groups to the disk when sufficient rows have accumulated.
+        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.

Review Comment:
   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``).
   ```
   ?



##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -466,6 +467,85 @@ TEST_F(DatasetWriterTestFixture, MinRowGroupBackpressure) {
   AssertCreatedData(expected_files);
 }
 
+TEST_F(DatasetWriterTestFixture, MinRowGroupLargerThanStagingLimit) {
+  // GH-55642: a single file should reach min_rows_per_group even when that is 
more
+  // rows than the dataset writer would otherwise be willing to stage
+  write_options_.min_rows_per_group = 60;
+  auto dataset_writer = MakeDatasetWriter(/*max_rows=*/100);
+  for (int i = 0; i < 12; i++) {
+    dataset_writer->WriteRecordBatch(MakeBatch(10), "");
+  }
+  EndWriterChecked(dataset_writer.get());
+  AssertCreatedData({{"testdir/chunk-0.arrow", 0, 120, 2}});
+}
+
+TEST_F(DatasetWriterTestFixture, MinRowGroupEvictsLargestStaged) {
+  // GH-55642: when too many rows are staged the file with the most staged 
rows should
+  // be written out, not the file that happened to receive the latest batch
+  write_options_.min_rows_per_group = 50;
+  auto dataset_writer = MakeDatasetWriter(/*max_rows=*/100);
+  dataset_writer->WriteRecordBatch(MakeBatch(30), "a");
+  for (int i = 0; i < 4; i++) {
+    dataset_writer->WriteRecordBatch(MakeBatch(5), "b");
+  }
+  EndWriterChecked(dataset_writer.get());
+  AssertCreatedData(
+      {{"testdir/a/chunk-0.arrow", 0, 30, 1}, {"testdir/b/chunk-0.arrow", 30, 
20, 1}});
+}
+
+TEST_F(DatasetWriterTestFixture, MinRowGroupManyFilesWithRoomToStage) {

Review Comment:
   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.



##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -111,12 +114,14 @@ struct DatasetWriterState {
   // the dataset writer will pause and it will also close the largest open 
file.
   Throttle open_files_throttle;
   // 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)

Review Comment:
   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).
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to