Repository navigation
[Data] Skip redundant schema unification for identical Arrow schemas - #67935
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a fast path to skip schema unification when all input schemas match, along with corresponding unit tests. The feedback suggests optimizing the schema comparison by iterating over the entire list of blocks instead of slicing it, which avoids unnecessary memory allocation and overhead.
8a65260 to
c7a8b2a
Compare
Signed-off-by: Yin-1022 <[email protected]>
Signed-off-by: Yin-1022 <[email protected]>
hikaru-212
left a comment
There was a problem hiding this comment.
I independently compared commits 8b69afe and 0958928 across 40 Arrow fixtures (160 comparisons), covering schema metadata, tensor/extension types, nulls, chunked arrays, type promotion, and mismatched schemas. Outputs and expected exceptions matched.
Focused benchmarks also showed consistent improvements for identical schemas, with no reproducible material regression in the mismatched-schema cases.
No correctness regression found within the tested scope. Nice improvement!
There was a problem hiding this comment.
I think we can modify the unify_schema as well, AFAIK, it already have a fast path in it, the problem with that is remove_metadata create a new schema object, while we can just use equal(check_metadata=false) , with that, other places that use this function can also benefit from it.
Signed-off-by: Yin-1022 <[email protected]>
Thanks for the suggestions! I've updated unify_schemas() to use Schema.equals(check_metadata=False) and removed the redundant late fast path in concat(). The test_transform_pyarrow.py tests passed, and I reran focused benchmarks against the pre-PR baseline using the updated implementation.
I was surprised by the improvement of speed by changing a few lines of code in
All three configurations seemed to produce equivalent deduplication results. |
…dArray (#68052) ## Why are these changes needed? When concatenating PyArrow tables whose struct columns have mismatched fields (e.g. one block has `struct<x: int64>` and another has `struct<x: int64, y: string>`), `transform_pyarrow.concat` calls `_concat_mismatched_blocks` -> `_align_struct_fields` -> `_backfill_missing_fields`. Inside `_backfill_missing_fields`, `ChunkedArray` columns were flattened via: ```python if isinstance(column, pa.ChunkedArray): column = pa.concat_arrays(column.chunks) ``` When an upstream filter produces a 0-row `pa.Table` (such as `table.filter(mask)` where all rows are filtered out), its struct column is a `pa.ChunkedArray` with `num_chunks == 0` (`column.chunks == []`). Passing `[]` to `pa.concat_arrays` raises: ``` pyarrow.lib.ArrowInvalid: Must pass at least one array ``` Replacing `pa.concat_arrays(column.chunks)` with `column.combine_chunks()` handles both non-empty and zero-chunk `ChunkedArray`s (returning an empty typed `StructArray` when `num_chunks == 0`). ### Acknowledgments Building on the PyArrow schema unification and struct field alignment work in `transform_pyarrow.py` by @dragongu (#66521), @Arnavsharma2 (#65961), @947132885 (#58364), and @Yin-1022 (#67935). ## Related issue number N/A (self-discovered while inspecting `ChunkedArray` handling in `_backfill_missing_fields`). ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests (`pytest python/ray/data/tests/test_transform_pyarrow.py -k test_concat_empty_chunked_struct_column_with_extra_field`) --------- Signed-off-by: Parth Vadera <[email protected]> Co-authored-by: Copilot <[email protected]>
fellowship: 提名 Yin-1022 (ray, 2026-11) 建議等級:2 萬 - [x] 關係人宣告:提名人與被提名者無親屬、僱傭、財務、業務合作或其他可能影響公正判斷之利害關係 - 在兩個月內自己找到 8 個 Ray Data Issue 並開了 PR,其中 6 個 PR 被 Merged ([Merged PRs](https://github.com/ray-project/ray/pulls?q=is%3Apr+author%3AYin-1022+is%3Amerged+merged%3A%3C%3D2026-11-24)) - 兩個月內共 review 28 個 PR,其中 10 個提出的問題有被作者採納修正([#68402](https://github.com/ray-project/ray/pull/68402)、[#68290](https://github.com/ray-project/ray/pull/68290)、[#67524](https://github.com/ray-project/ray/pull/67524)) - 大部分 PR 都有清楚列出 Before/After 的差距,performance 相關的會附上數據 ([#67935](ray-project/ray#67935), [#68403](ray-project/ray#68403)) - 對於 Maintainer 的 Review 會清楚了解之後回覆 ([#67935 comment](ray-project/ray#67935 (comment)), [#66878 comment](ray-project/ray#66878 (comment)), [#67505 comment](ray-project/ray#67505 (comment)))
Description
Optimize Ray Data's
transform_pyarrow.concat()by introducing a fast path for Arrow tables with identical schemas.Previously,
concat()performed schema unification even when all input schemas already matched. Local profiling showed thatunify_schemas()accounted for approximately 71% of the original concatenation time in an identical-schema workload.This PR avoids redundant schema unification for matching schemas while retaining the existing reconciliation path for mismatched schemas.
Changes
pa.concat_tables().Before / After Comparison
All timings below are from local Windows benchmarks using PyArrow 25.0.1. Speedups are calculated as baseline time divided by optimized time.
1. Direct Arrow concatenation
512 distinct Arrow tables, each containing 32 columns and 16 rows, with identical schemas.
Profiling of the original implementation:
unify_schemas()pa.concat_tables()Schema unification accounted for approximately 71% of the measured original concatenation time.
A separate baseline comparison also measured 4.931 ms for Ray concat versus 1.222 ms for native PyArrow, representing 4.04x overhead.
2. ArrowBlockBuilder
Each configuration performs one table concatenation.
The optimized implementation achieved 1.61x–2.68x speedups in these block-building benchmarks.
3. Batch assembly
Batch assembly showed 1.18x–1.49x improvements across the tested configurations.
4. Dataset iteration (end-to-end)
Configuration:
Although the concat-only portion improved by 1.15x, the Dataset iteration benchmark measured 0.92x overall performance (108.362 ms before vs. 118.373 ms after).
Since concatenation accounts for only approximately 3–4% of total iteration time, the expected end-to-end benefit is small relative to other execution costs. The observed slowdown has not been attributed to the fast path, and additional controlled measurements would be needed to distinguish benchmark variability from a genuine regression.
5. Additional Dataset iteration validation
To investigate that 0.92x overall performance despite a 1.15x improvement in concat-only time,
I performed additional interleaved A/B measurements using a workload that exercises the optimized concatenation path.
Configuration:
Dataset iteration timings exhibited substantial variability in both implementations.
The median paired-trial speedup was 0.952x, with individual trial ratios ranging from 0.459x to 2.477x.
These results confirm the local concatenation improvement
but do not establish a consistent end-to-end performance improvement or regression.
The earlier 0.92x observation could not be reliably reproduced as a stable slowdown.
6. Validation
These benchmarks demonstrate improvements in direct concatenation, block building, and batch assembly, but do not establish an end-to-end Dataset performance improvement. Results may vary depending on input schemas, table sizes, and workload characteristics.
Related issues
Closes #67814
Additional information
The optimization only bypasses schema unification when all input tables have identical schemas. Tables with differing schemas continue through the existing reconciliation logic, including type promotion and tensor extension handling.
The fast path reduces unnecessary overhead without changing the expected concatenation semantics.