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


##########
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:
   Done, I used your wording for both comments.



##########
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:
   Done.



##########
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:
   Done. I wrapped the line differently from the suggestion to stay within the 
90 column limit.



##########
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:
   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. The note in the Python user guide still has the longer explanation, 
since it has no separate entry for `max_rows_queued`; I can shorten that too if 
you prefer.



##########
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:
   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. The test also checks the size 
of each row group now (30 then 10 in "a", 30 in "b").



-- 
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