Skip to content

[data] Fix join schema validation for asymmetric and multi-key joins - #67505

Merged
owenowenisme merged 2 commits into
ray-project:masterfrom
Yin-1022:fix/join-schema-validation
Oct 30, 2026
Merged

owenowenisme merged 2 commits into
ray-project:masterfrom
Yin-1022:fix/join-schema-validation

Conversation

@Yin-1022

Copy link
Copy Markdown
Contributor

Description

Dataset.join(..., validate_schemas=True) can mishandle join-key schema validation in two cases:

  1. Asymmetric on / right_on keys are looked up against the wrong input schemas, allowing missing or incompatible join keys to pass validation.
  2. Multi-key join types are collected in physical schema order instead of the configured join-key order, which can reject valid joins when the two input schemas store columns in different orders.

Changes

  • Validate left join keys against the left schema and right join keys against the right schema.
  • Collect join-key types in the order specified by on / right_on instead of schema column order.
  • Explicitly treat missing join keys or unknown schemas as validation failures.
  • Add regression coverage for:
    • valid asymmetric joins;
    • missing asymmetric right_on keys;
    • incompatible asymmetric join-key types;
    • valid multi-key joins with different physical schema column orders.

Before / After Comparison

Before this change:

  • Missing asymmetric join keys could pass validate_schemas=True and fail only later during execution.
  • Incompatible asymmetric join-key types could also bypass schema validation.
  • Valid multi-key joins could raise a ValueError when the two schemas stored join-key columns in different orders.

After this change:

  • Missing and incompatible asymmetric join keys are rejected during schema validation.
  • Valid asymmetric joins continue to pass validation.
  • Multi-key validation follows the configured join-key order and no longer depends on physical schema column order.

The new regression tests fail on master across shuffle v1, shuffle v2, and disk shuffle:

  • 6 failures where missing or incompatible asymmetric keys did not raise ValueError.
  • 3 failures where valid multi-key joins were incorrectly rejected.

The same targeted schema-validation tests pass with this change.

Related issues

Fixes #67415

Additional information

The validation change is limited to join schema checking and does not modify join execution behavior.

@Yin-1022
Yin-1022 requested a review from a team as a code owner October 28, 2026 08:55

@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 corrects schema validation during join operations by ensuring that key column types are resolved and compared in the exact order of the specified join keys. It fixes a bug where the left and right key column names were swapped when retrieving column types, refactors _get_key_column_types to maintain key order, and adds corresponding unit tests for asymmetric join keys and multi-key ordering. There are no review comments to address, and we have no further feedback to provide.

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Oct 28, 2026

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

LGTM


right_op_key_cols = _get_key_column_types(
right_op_schema, left_key_column_names
right_op_schema, right_key_column_names

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.

Good job!

@owenowenisme owenowenisme added the go add ONLY when ready to merge, run all tests label Oct 30, 2026
@owenowenisme
owenowenisme enabled auto-merge (squash) October 30, 2026 18:50
@owenowenisme
owenowenisme merged commit 73fd528 into ray-project:master Oct 30, 2026
8 checks passed
sampan-s-nayak pushed a commit that referenced this pull request Nov 13, 2026
…rom it (#67590)

## Description

`Dataset.join` only takes hash keys through `on`/`right_on`, so a second
equality between the two sides can only be written as a `filter` above
the join. TPC-H q5 does exactly that, `HashJoin(l_suppkey = s_suppkey)`
followed by `Filter(c_nationkey = s_nationkey)`, so every `l_suppkey`
match is materialized before the nation check drops most of it (the
issue estimates ~25x more probe matches than survive). The engine
already supports composite keys; what is missing is the decomposition
being automatic.

- Add `condition: Expr` to `Dataset.join` and the `Join` logical
operator. Column references name *input* columns (before any suffix); a
name present on both sides is ambiguous and raises. `on` stays required,
so a `condition` alone (theta join) is rejected, as the issue asks.
- Add `ExtractEquiJoinKeys`, which splits the top-level `AND` chain of
the condition: every `col(a) == col(b)` with `a` and `b` on different
sides becomes a condition key pair (either operand order, pairs already
in `on`/`right_on` not repeated). Condition keys are hashed on and
matched like `on` keys but not coalesced: both columns stay in the
output with their own values, the same columns as `join().filter()`, and
an unmatched row of an outer join has a null in the other side's column.
Everything else stays on the operator as the residual condition for
#66835.
- `validate_schemas=True` checks the keys the join actually runs with,
i.e. `on`/`right_on` plus the pairs extracted from `condition`.
- A column can be a key only once per side. A `condition` equality that
reuses a column already in `on`/`right_on`, or the same column twice,
raises with a pointer to `filter()` instead of producing duplicate key
names.
- Switch the TPC-H q2 and q5 scripts from a filter above the join to
`condition` (item 3 of the issue).

### TPC-H q2 / q5 evidence (sf1, single machine)

Ran the release scripts' `main()` through the `Benchmark` harness
against SF1 data generated with DuckDB's `dbgen` (exported with the
column names and types of the `s3://ray-benchmark-data/tpch/parquet`
files), and compared the output with DuckDB running the reference SQL.

| | planned condition key | residual | vs. DuckDB reference | wall time
|
|---|---|---|---|---|
| q2 | `ps_supplycost_f = min_supplycost` | none | 100 rows, identical
including order | 13.5 s |
| q5 | `c_nationkey = s_nationkey` | none | 5 rows, identical revenue |
11.7 s |

```mermaid
flowchart LR
    subgraph Before
        B1["Join(l_suppkey = s_suppkey)"] --> B2["Filter(c_nationkey = s_nationkey)"]
    end
    subgraph After
        A0["Join(on, condition)"] -->|ExtractEquiJoinKeys| A1["Join(on: l_suppkey = s_suppkey, condition key: c_nationkey = s_nationkey)"]
    end
    classDef removed fill:#ffcdd2,stroke:#c62828,color:#000
    classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000
    class B2 removed
    class A0,A1 added
```

### Subsystems

logical planning / query optimization (behavioral change), API
(`Dataset.join` parameter), physical planning (passes condition keys,
rejects a leftover condition), shuffle / aggregation (join reduce
matches condition keys without coalescing).

## Related issues

Closes #66834

## Additional information

**Condition columns are input names.** The condition has `ON`-clause
semantics: under an outer join `join(cond)` is not
`join().filter(cond)`, and #66835's probe-time evaluation needs input
names too. We resolve by name, not by attribute id as Spark's
`ExtractEquiJoinKeys` does, so a name present on both sides is an error
rather than a guess.

**Where errors surface.** `Dataset.join` resolves the condition
immediately when both input schemas are inferable without execution, so
an unknown or ambiguous column fails at the call site. When a schema is
unknown (e.g. after a row UDF) the planner reports it.
`Join.infer_schema()` itself never raises; `Dataset.__repr__` calls it
and expects `None` for "unknown". It does use the resolved condition
keys, so `ds.schema()` is the same before and after the rewrite.

**Residual.** Non-equality conjuncts stay in `Join.condition` and
`plan_join_op` raises `NotImplementedError` for them. I did not rewrite
them into a `Filter` above the join: that is only correct for inner
joins, and #66835 replaces this check with evaluation inside the reduce
task.

**Interaction with #67505 and #67122.** Both merged while this was open
and the branch is rebased on them. `validate_schemas=True` feeds
#67505's check every matched pair. #67122's pushdown only reads the `on`
keys to decide which right columns are coalesced away; condition keys
are ordinary output columns to it, so the two rules don't depend on each
other's order.

**Execution.** The reduce joins each condition pair on a same-named
temporary copy of the two columns and drops the copy afterwards, which
is what leaves the originals uncoalesced. Both shuffle paths hash on
`on` plus condition keys. A v2 full outer join with condition keys
doesn't advertise an output partitioning, because its unmatched right
rows hold nulls in the left condition columns.

---------

Signed-off-by: Jie-Kai Chang <[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] Dataset.join(validate_schemas=True) mishandles asymmetric and multi-key joins

3 participants