VaibhaveS opened a new pull request, #12350: URL: https://github.com/apache/arrow-rs/pull/12350
# Which issue does this PR close? <!-- It is not necessary to raise an issue, but it is encouraged if it is a non-trivial enhancement/bug. If this PR relates to an existing issue, but does not fully resolve it, please change the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of #NNN" to avoid false closing an issue. --> - Part of #12187. # Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please try to explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. --> To write only some rows of a `RecordBatch`, a caller has to build a filtered copy with `filter_record_batch` and pass it to `ArrowWriter::write`. That copies every selected value of every column, only for the writer to encode them again. `ArrowWriter::write_with_selection` writes the selected rows from the original arrays instead. # What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> One new public item, `ArrowWriter::write_with_selection`: ```rust pub fn write_with_selection(&mut self, batch: &RecordBatch, selection: &RowSelection) -> Result<()> ``` - The selection must span exactly `batch.num_rows()` rows. Otherwise the method returns an error before writing anything. Rows are written in order, selecting no rows writes nothing, and selecting all rows calls `write`. - Selected rows count toward the row group limits, so one call can span row groups, as with `write`. The existing loop in `write` that splits rows between row groups now serves both methods through a private `RowsToWrite` trait. A selection is split at its n-th selected row. - Definition and repetition levels, and the indices of the values to write, are built for the selected rows of the original arrays (`calculate_array_levels_for_rows`). Leaf and struct columns do this in one pass over the selected rows. List, map, fixed-size list and list view columns call the existing level builder once for each run of selected rows. - Required fields are checked for nulls only at selected rows. - Content-defined chunking hashes the values at the written indices. - For the Arrow types that the writer converts, such as `Int8`, `Date64` and decimals stored as `INT32` or `INT64`, `WritePrimitive::map` now converts only the values that are written. This also applies to `write`, which no longer converts the values of null slots. - A new `write_with_selection` benchmark group in `parquet/benches/arrow_writer.rs`. It is in the first commit, 62532cb430707b5c48523cecd3cb1c676a4858ad, so that commit can serve as the benchmark baseline. The method docs describe one difference from filter + write. For a nullable column, the writer can split pages, and row groups by size, at different rows when the selected rows that it writes to a row group are all valid or all null. It then uses `data_page_row_count_limit` instead of `write_batch_size` as its mini-batch size. Rows, values, row counts and column chunk statistics are the same, but the page index can differ in those cases. Not included, and left for follow-ups to #12187: `AsyncArrowWriter::write_with_selection`, and a selection-aware `compute_leaves` for callers of `ArrowColumnWriter`. # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests, if so identify these tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. DO NOT simply paste a list of commands ran to verify the code; this is what our CI is for and it pollutes the PR. ONLY explain if testing was included with this PR, or why it wasn't. --> Yes, in `parquet/src/arrow/arrow_writer/mod.rs`: - `write_with_selection_matches_filter_and_write` writes a batch sliced from a larger one, with nulls, through 7 selections: alternate rows, random rows, a few scattered rows, one range, an offset bitmap, all rows and no rows. The batch has 19 columns: primitive, decimal, boolean, string, string view, fixed-size binary, dictionary, run-end encoded, struct, list, map, fixed-size list and list view. Each selection is passed both as a bitmap and as runs, and the test asserts that the file is byte for byte the same as with `filter_record_batch` + `write`. It runs three writer configurations: the default properties; a 17-row row group limit with bloom filters; and data page v2 with a 4 KiB row group byte limit. - `write_with_selection_matches_filter_and_write_with_cdc` does the same with content-defined chunking. It leaves out the fixed-size list and list view columns, and also the list and map columns when rows are split between row groups. For those columns, `write` already hashes values by their position rather than the values that it writes. This PR doesn't change that, and the test comment explains it. - `write_with_selection_checks_required_fields_at_selected_rows` also covers a required child of a nullable struct. - `write_with_selection_requires_selection_of_batch_rows` covers selections that span too few or too many rows, run lengths that overflow, and selections of no rows. - The method has a doc example. Benchmark: `cargo bench -p parquet --bench arrow_writer -- write_with_selection`. The batch has 1,048,576 rows of `Int64`, `Int32`, `Decimal128`, `Float64`, `Utf8` (up to 400 bytes) and `Timestamp` columns, with 10% nulls in the nullable columns. I ran the bench binary pinned to one core at high priority, on a shared 8-core VM: | `write_with_selection/...` | `filter_then_write` | `write_with_selection` | change | |---|---:|---:|---:| | `skip_10pct_random` | 258.4 ms | 242.4 ms | -6% | | `skip_50pct_random` | 177.8 ms | 178.8 ms | +1% | | `skip_99pct_random` | 6.59 ms | 6.21 ms | -6% | | `skip_50pct_runs_of_64` | 169.6 ms | 148.4 ms | -12% | I also measured CPU time separately, with a harness that is not included. Four threads each wrote 1M rows of a 9-column table shaped like the one we write, using data page v2 and no compression. I read the per-thread CPU time over 21 rounds that alternated between the two methods, and ran the measurement twice. The ranges below are across the two runs, and the CPU utilization of the two methods was the same. | rows skipped | CPU time, 8,192-row batches | CPU time, 1M-row batches | |---|---|---| | 10% | 5-10% less | 9-13% less | | 30% | 6-10% less | 16-19% less | | 50% | 4-10% less | 14-19% less | | 99% | 3-7% more | 0-8% more | The gain comes from not building the filtered copy. The rest of the change keeps the selected path from costing more than the copy saves: - Leaf and struct columns build levels in one pass. An earlier version called the existing level builder once for each run of selected rows. It was no faster than filter + write with 10% of rows skipped, and slower with 30-50%, because random skips leave many short runs (about 262k runs per 1M rows at 50%). - With 99% of rows skipped, converting the skipped values dominated. Converting only the written values in `WritePrimitive::map` made `write_with_selection` about 2x faster there, and an A/B comparison of `write` showed no regression (0-6% faster). Inputs that get slower, or don't gain: - With 50% of rows skipped at random, the two methods are about even on the benchmark table. Gathering long strings from scattered rows costs about as much as the copy saves. - With 99% of rows skipped, up to 8% more CPU in the threaded runs (6% faster in the Criterion run above). Both take a few milliseconds per 1M rows. - List, map, fixed-size list and list view columns still build levels once for each run of selected rows. They are not benchmarked here, and are likely slower than filter + write when a selection has many short runs. # Are there any significant user-facing changes? <!-- Significant user-facing changes include: - Breaking public API change - Significant change to existing functionality (e.g. function returns something different) These need to be explicitly called out to ensure we have proper documentation if required and to ensure we follow proper semantic versioning. DO NOT restate the changes of this PR; if a bug was fixed and already explained above, we don't need this to be restated as a user-facing change. --> A new public method, `ArrowWriter::write_with_selection`. There are no breaking changes, and `write` writes the same files as before. # AI usage disclosure <!-- If you used AI tools, briefly describe what they helped generate. See https://github.com/apache/arrow-rs/blob/main/CONTRIBUTING.md#ai-generated-submissions. Delete this section if you did not use AI tools. --> GitHub Copilot (agent mode) generated the implementation, tests, benchmark and this description at my direction. It also did the profiling and measurements behind the optimizations. -- 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]
