Skip to content

[Data] Allow Dataset.union to fill missing columns - #68402

Open
WenDing-Y wants to merge 4 commits into
ray-project:masterfrom
WenDing-Y:feat/ray-data-union-missing-columns
Open

WenDing-Y wants to merge 4 commits into
ray-project:masterfrom
WenDing-Y:feat/ray-data-union-missing-columns

Conversation

@WenDing-Y

Copy link
Copy Markdown

Description

Add Dataset.union(..., allow_missing_columns=True) so pipelines can combine inputs with evolving columns and feed a consistent schema into batch transforms and filters.

Before

Dataset.union() expects matching input schemas. If columns differ, schema inference can list a field that some output blocks lack. Callers have to align the inputs before unioning them.

After

The opt-in path requires a known Arrow schema for each input. Planning chooses columns in first-seen order and validates that shared columns have matching types. Each input block is aligned to that schema, with typed nulls for missing fields. The streaming union preserves all rows, including duplicates. The option defaults to False.

flowchart LR
    subgraph Before
        B1[Input blocks] --> B2[Streaming Union] --> B3[Output blocks with input schemas]
    end
    subgraph After[After with allow_missing_columns enabled]
        A1[Input blocks] --> A2[Align each block to the target Arrow schema]
        A2 --> A3[Streaming Union] --> A4[Output blocks with the target schema]
        A0[Logical plan computes the target schema] --> A2
    end
    classDef added fill:#dcfce7,stroke:#15803d,color:#111827
    class A0,A2 added
Loading

Related issues

Related to #52939, which reports a missing-column failure during struct alignment and identifies cross-schema union as a possible enhancement. Searches for the proposed API and related union/schema work found no open PR implementing it.

Subsystems

API, logical planning, and physical planning. The physical planner uses the existing map execution path to align blocks before the streaming union.

Testing

  • python -m pytest -q --tb=short python/ray/data/tests/test_union.py passed all 12 tests in the cloud workspace using the branch's Python source with the Ray 2.60.0 native runtime. Coverage includes Parquet combined with CSV, JSON, JSONL, and ORC, plus typed nulls, output column order, row preservation, and downstream filtering.
  • The repository's pre-commit hooks passed on the signed commit.

AI assistance was used for implementation, tests, and drafting this description.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@WenDing-Y
WenDing-Y marked this pull request as ready for review November 20, 2026 07:58
@WenDing-Y
WenDing-Y requested a review from a team as a code owner November 20, 2026 07:58
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

for field in schema:
if field.name in names:
column = table[field.name]
if column.type != field.type:

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.

Potential failure when input blocks have promotable types

I found a reproducible issue through the public ray.data.from_arrow() and Dataset.union() APIs.

A Dataset created from int32 and int64 Arrow blocks infers id as int64. Unioning it with another Dataset whose id is also int64 passes planning-time validation, but fails during execution in AlignToSchema.

Minimal reproduction:

import ray
import pyarrow as pa

old = ray.data.from_arrow([
    pa.table({"id": pa.array([1], type=pa.int32()), "source": ["old32"]}),
    pa.table({"id": pa.array([2], type=pa.int64()), "source": ["old64"]}),
])

new = ray.data.from_arrow(
    pa.table({"id": pa.array([3], type=pa.int64()), "extra": ["new"]})
)

combined = old.union(new, allow_missing_columns=True)
combined.take_all()

This raises:

ValueError: Expected type int64 for union input column 'id', but found int32.

The original old.take_all() succeeds, so it appears that the Dataset-level schema inference permits type promotion, while the new block-level alignment requires exact type equality.

Could we support safely promotable block types during alignment, or otherwise reconcile the planning and execution behavior?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the clear reproduction. AlignToSchema now uses the same Arrow schema unification as Dataset schema inference to check whether a block can be promoted to the planned type, then applies a safe cast. Shared columns must still have matching types across the input Dataset schemas. Fix in e2b9e86181

The regression tests cover your int32/int64 case, floating-point and nested struct promotion, and rejection of a cast that would lose precision. All 18 tests in test_union.py passed locally using the Ray 2.60.0 native runtime with a compatibility shim.

@WenDing-Y
WenDing-Y force-pushed the feat/ray-data-union-missing-columns branch from c0dc691 to e2b9e86 Compare November 22, 2026 13:06
@WenDing-Y
WenDing-Y requested a review from Yin-1022 November 23, 2026 02:09
@WenDing-Y

Copy link
Copy Markdown
Author

Hi @Yin-1022, thanks again for the repro! The fix and regression test for the mixed int32/int64 blocks are in, and microcheck is green. Could you take another look when you have a chance?

@Yin-1022 Yin-1022 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.

Thanks for the follow-up! I checked the fix and the added regression tests. The schema unification and safe casting approach addresses my original concern. The changes look good to me.

@WenDing-Y

WenDing-Y commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Thanks for the follow-up! I checked the fix and the added regression tests. The schema unification and safe casting approach addresses my original concern. The changes look good to me.

Thanks for confirming the fix. Could you submit an approval if you have the required permission? If not, could you point me to a Ray Data committer or the PR assignee who can review and approve it? @Yin-1022

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants