suhyeon729 commented on issue #19142:
URL: https://github.com/apache/iceberg/issues/19142#issuecomment-7328087383

   Update after measuring on Spark 4.1 and Flink 2.3. Two corrections to the 
description above.
   
   - The 2 × 0.6T loop only reproduces when each file has a single row group. 
With a 512 MB target and the default 128 MB row groups, main rewrites it once 
(into 0.25T + 0.95T) and stops.
   - With single-row-group files, the loop is wider than two files, and #19163 
does not stop it. #19163 only changes `enoughContent`, so groups selected 
through `enoughInputFiles`, `tooMuchContent` or the delete thresholds are still 
selected. So are groups whose estimate is below the file count.
   
   Setup: main at 2e12b22, rewrite options at their defaults (T = target, min = 
0.75T, max = 1.8T, min-input-files = 5), equal-size Parquet files, three 
consecutive rewrites. For single-row-group files at the 512 MB target, I set 
`write.parquet.row-group-size-bytes` above the file size. At the small targets, 
the files are smaller than the default row group. A = main without the guard, 
run as a planner subclass. C = main + #19163, run through the Spark action, and 
on Flink through the maintenance planner and runner operators.
   
   **Spark, target 512 MB, one row group per file**
   
   | Files | A | C |
   |---|---|---|
   | 2 × 0.60T | 2 → 2 on every run | not selected |
   | 3 × 0.55T | 3 → 3 on every run | 3 → 3 on every run |
   | 3 × 0.667T | 3 → 3 on every run | 3 → 3 on every run |
   | 5 × 0.52T | 5 → 5 on every run | 5 → 5 on every run |
   
   With 128 MB row groups (three per file), all of these are rewritten at most 
once. 3 × 0.667T becomes 0.167T + 0.92T + 0.92T, and 3 × 0.55T becomes 2 files.
   
   At small targets with single-row-group files, C repeated at these measured 
totals:
   - 3 files: 1.65T, 2.0T and 2.24T. At 1.50T the files reduced to 2.
   - 5 files: 2.51T to 3.10T. At 2.40T they reduced to 3.
   
   **Flink maintenance rewrite (v2.3), C only, 8 MB target.** Every case 
stopped after one rewrite, with one or three row groups per file:
   - 3 × 0.55T → 2 files
   - 5 × 0.52T → 3 files
   - 3 × 0.667T → 0.97T + 0.97T + 0.06T (0.04T with three row groups)
   
   So in these runs only the Spark bin-pack runner repeated. I did not run A on 
Flink, or sort/z-order.
   
   ## Cause
   `expectedOutputFiles` assumes the bytes can be divided freely. The planner 
is shared, but each runner decides how the group is read and written.
   
   - **Spark:** the estimate sets the read split size 
([SizeBasedFileRewritePlanner.java#L212-L219](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/core/src/main/java/org/apache/iceberg/actions/SizeBasedFileRewritePlanner.java#L212-L219),
 
[SparkBinPackFileRewriteRunner.java#L48-L49](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/SparkBinPackFileRewriteRunner.java#L48-L49)),
 and each read task writes one file. A file with split offsets is divided only 
at those offsets 
([BaseContentScanTask.java#L100-L117](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/core/src/main/java/org/apache/iceberg/BaseContentScanTask.java#L100-L117)),
 so a single-row-group file is never divided.
     - Example, 3 × 0.667T: the estimate is 2, so the split size is about 1.0T. 
Two files (1.33T) do not fit in one split, which gives 3 tasks and 3 files of 
the same size. Those files are selected again on the next run.
   - **Flink:** the whole group is read as one combined task, and the writer 
rolls to a new file at `inputSplitSize` 
([DataFileRewriteRunner.java#L193-L219](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/DataFileRewriteRunner.java#L193-L219),
 
[BaseTaskWriter.java#L428-L430](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/core/src/main/java/org/apache/iceberg/io/BaseTaskWriter.java#L428-L430)).
 The output sizes change, so the run stops, but it can leave a very small file.
   
   This looks like the case left for a follow-up in 
https://github.com/apache/iceberg/issues/3236#issuecomment-989504872 ("3 files 
and each has a single 100 MB row group … we won't be able to rewrite that into 
say, 2 150 MB files"). #9069 fixed an earlier loop of the same kind by clamping 
the split size.
   
   ## Options (both Spark-only)
   1. **Selection guard.** Select a group only if packing its splits with the 
runner's settings gives fewer tasks than input files. Groups that contain a 
file above max stay selected.
      - I prototyped this as a planner subclass. The repeating cases were 
skipped, and 3 → 2 and 5 → 3 were still rewritten.
      - It would also skip groups that rewrite once today without reducing the 
count. 3 × 0.667T with 128 MB row groups would stay as three 0.667T files 
instead of becoming 0.167T + 0.92T + 0.92T. I have not measured this.
      - It should not be in the shared core planner. On Flink, 3 × 0.55T and 5 
× 0.52T reduce, but with single-row-group files a Spark-style guard would skip 
them.
   2. **Choose a split size that reduces the group.** Search [target, writeMax] 
for a split size that gives fewer tasks, and skip the group only if none does.
      - For 3 × 0.667T, a split of at least 1.34T should give 2 files. This is 
computed, not measured.
      - The writer rolls on an estimated size, so an output near writeMax may 
still be split into a small tail file.
      - Depending on the design, it may also change output sizes for groups 
that rewrite fine today. It is a larger change.
   
   Both options have to handle `max-files-to-rewrite`, which trims groups after 
selection. They also have to handle groups that change partition spec, which 
are written with a range shuffle, and delete files, which add to the packing 
weight.
   
   Questions:
   1. Option 1 or option 2? Spark bin-pack currently uses the core 
`BinPackRewriteFilePlanner` directly 
([RewriteDataFilesSparkAction.java#L225-L228](https://github.com/apache/iceberg/blob/2e12b22aea8e43af1d3bc1559decc6bbe2e92ac0/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/RewriteDataFilesSparkAction.java#L225-L228)),
 so either option would need a Spark-side planner.
   2. #19163 is in the shared `SizeBasedFileRewritePlanner.enoughContent`, so 
it also changes sort/z-order, Flink and position-delete planning. Should it be 
narrowed to Spark bin-pack as well, or dropped in favor of the chosen option?
   
   #19163 stays a draft until there is a direction. Test code and raw results 
are available if useful.
   


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to