Skip to content

[Data] Skip redundant schema unification for identical Arrow schemas - #67935

Merged
owenowenisme merged 3 commits into
ray-project:masterfrom
Yin-1022:perf/ray-data-concat-fast-path
Nov 9, 2026
Merged

owenowenisme merged 3 commits into
ray-project:masterfrom
Yin-1022:perf/ray-data-concat-fast-path

Conversation

@Yin-1022

@Yin-1022 Yin-1022 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Add an early identical-schema fast path that directly calls pa.concat_tables().
  • Preserve the existing schema unification and type reconciliation behavior for differing schemas.
  • Add regression coverage for the optimized path and fallback behavior.

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.

Implementation Median time Speedup
Before 5.237 ms 1.00x
After 1.242 ms 4.22x

Profiling of the original implementation:

Operation Time
unify_schemas() 3.724 ms
Schema equality comparison 0.050 ms
Native pa.concat_tables() 0.959 ms

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.

Tables × Columns Before After Speedup
64 × 8 0.424 ms 0.263 ms 1.61x
512 × 8 3.156 ms 1.878 ms 1.68x
512 × 32 6.883 ms 3.082 ms 2.23x
512 × 64 10.701 ms 3.992 ms 2.68x
2048 × 32 26.524 ms 12.131 ms 2.19x

The optimized implementation achieved 1.61x–2.68x speedups in these block-building benchmarks.

3. Batch assembly

Tables × Columns Before After Speedup
128 × 8 2.801 ms 2.379 ms 1.18x
512 × 8 11.430 ms 9.559 ms 1.20x
512 × 32 15.332 ms 11.464 ms 1.34x
512 × 64 20.169 ms 13.542 ms 1.49x
1024 × 32 30.420 ms 23.753 ms 1.28x

Batch assembly showed 1.18x–1.49x improvements across the tested configurations.

4. Dataset iteration (end-to-end)

Configuration:

  • 128 blocks
  • 2,048 rows per block
  • 32 columns
  • Batch size: 512
  • 30 benchmark rounds
Metric Before After Speedup
Dataset iteration 108.362 ms 118.373 ms 0.92x
Concat-only time 3.973 ms 3.453 ms 1.15x
Concat share of iteration 3.67% 2.92% —

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:

  • 128 Arrow blocks
  • 2,048 rows per block
  • 32 columns
  • Batch size: 3,000
  • 8 ABBA/BAAB trials (32 iteration runs)
  • 87 observed concat calls per iteration
Metric Before After Speedup
Dataset iteration median 268.815 ms 266.180 ms 1.010x
Concat time median 8.472 ms 7.431 ms 1.140x

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

  • Passed 119 existing regression tests during local validation.
  • Added two regression test functions, with 13 test cases passing in the corresponding test run.
  • Verified the identical-schema fast path and mismatched-schema fallback behavior.
  • Checked existing schema reconciliation and tensor extension handling.

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.

@Yin-1022
Yin-1022 requested a review from a team as a code owner November 7, 2026 05:46

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Yin-1022
Yin-1022 force-pushed the perf/ray-data-concat-fast-path branch from 8a65260 to c7a8b2a Compare November 7, 2026 05:55
@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 7, 2026

@hikaru-212 hikaru-212 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@owenowenisme owenowenisme left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Yin-1022

Yin-1022 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

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.

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.

  • For 512 Arrow tables with 32 columns and 16 rows each:
    • Direct concat improved from 5.324 ms to 1.854 ms (2.87×).
      • The difference from the original PR's 4.22× speedup appears to be due to benchmark variability. An additional A/B/C comparison confirmed a consistent ~4× improvement over the pre-PR baseline, with no clear performance difference between the original and updated implementations.
    • unify_schemas() improved from 3.038 ms to 0.0345 ms (88.1×).
  • All five ArrowBlockBuilder configurations also improved by 2.43–4.26×.
  • For Dataset iteration with batch size 3,000, concat-only time decreased from 8.938 ms to 6.490 ms, while overall iteration timings remained variable without a consistent end-to-end speedup.

I was surprised by the improvement of speed by changing a few lines of code in unify_schemas()
so I additional validated the schema deduplication optimization using 512 schemas with 32 fields under three configurations:

  • Shared schema objects: 3.5591 → 0.0298 ms (119.43×)
  • Independently constructed schemas: 3.6240 → 0.0469 ms (77.27×)
  • Schemas with differing schema-level metadata: 4.3607 → 0.0416 ms (104.95×)

All three configurations seemed to produce equivalent deduplication results.
These measurements cover the schema deduplication loop specifically, rather than general schema reconciliation workloads.

@owenowenisme owenowenisme added the go add ONLY when ready to merge, run all tests label Nov 9, 2026
@owenowenisme
owenowenisme enabled auto-merge (squash) November 9, 2026 15:19
@owenowenisme
owenowenisme disabled auto-merge November 9, 2026 16:59
@owenowenisme
owenowenisme merged commit 9a3911a into ray-project:master Nov 9, 2026
8 checks passed
richardliaw pushed a commit that referenced this pull request Nov 24, 2026
…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]>
chia7712 pushed a commit to opensource4you/readme that referenced this pull request Nov 25, 2026
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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] Redundant schema unification overhead when concatenating Arrow tables with identical schemas

3 participants