Skip to content

[SPARK-62246][SQL] Make a DataFrame as-of join with using columns return one key column - #60796

Open
LukaZdravic wants to merge 1 commit into
apache:masterfrom
LukaZdravic:asof-df-using-columns
Open

LukaZdravic wants to merge 1 commit into
apache:masterfrom
LukaZdravic:asof-df-using-columns

Conversation

@LukaZdravic

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Dataset.joinAsOf with usingColumns now builds an AsOfJoin with usingColumns set, the same node that SQL ASOF JOIN ... USING builds. Both analyzers already expand it, so the output has one column per name, placed first, as in SQL.
It used to turn each name into left(name) === right(name), which kept the key of both sides.
Spark Connect and PySpark _joinAsOf call Dataset.joinAsOf, so they change too. A migration guide line is added.

joinAsOf(quotes, ts, ts, usingColumns = Seq("sym"))
before: ts, sym, qty, ts, sym, bid   (select("sym") fails with AMBIGUOUS_REFERENCE)
after:  sym, ts, qty, ts, bid        (same as SQL ASOF JOIN quotes ... USING (sym))

Why are the changes needed?

The DataFrame API and SQL gave different columns for the same join, and the key could not be selected by name.

Does this PR introduce any user-facing change?

Yes, for the private DataFrame as-of join (DataFrame._joinAsOf in PySpark, and Spark Connect) when on is a column name or a list of names. The output has one key column per name instead of two. There is no switch for the old output. The key keeps the type of the left side, and names are matched against top-level columns as in a regular USING join (a nested, quoted or metadata name now fails). A join with a Column for on, and pandas.merge_asof, are not changed.

How was this patch tested?

  • DataFrameAsOfJoinSuite: columns, schema and rows equal SQL ASOF JOIN ... USING for INNER and LEFT, one and two keys. Also select/filter/group by the key, an unmatched LEFT row and a NULL key, a self join, a missing name (UNRESOLVED_USING_COLUMN_FOR_JOIN), tolerance and direction against the rows of the Column-condition join. Each test runs with the fixed-point and single-pass analyzers, the sort-merge flag off and on, and ANSI on and off.
  • Also: qualified access to the right key, INT vs BIGINT and STRING keys against SQL USING, case sensitivity, a nested field name.
  • SparkConnectProtoSuite: a Connect as-of join with using_columns returns one key column, resolves plan-id keys with rows, with both analyzers.
  • StreamingAsOfJoinSuite: a stream-static DataFrame join with using columns.
  • Existing ASOF, join and codegen suites, with expected rows updated for the new column order.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code

This pull request and its description were written by Isaac.

…urn one key column

`Dataset.joinAsOf` with `usingColumns` now builds an `AsOfJoin` with `usingColumns` set, the same node that SQL `ASOF JOIN ... USING` builds. Both analyzers already expand it, so the output has one column per name, placed first, as in SQL.
It used to turn each name into `left(name) === right(name)`, which kept the key of both sides.
Spark Connect and PySpark `_joinAsOf` call `Dataset.joinAsOf`, so they change too. A migration guide line is added.

```
joinAsOf(quotes, ts, ts, usingColumns = Seq("sym"))
before: ts, sym, qty, ts, sym, bid   (select("sym") fails with AMBIGUOUS_REFERENCE)
after:  sym, ts, qty, ts, bid        (same as SQL ASOF JOIN quotes ... USING (sym))
```

The DataFrame API and SQL gave different columns for the same join, and the key could not be selected by name.

Yes, for the private DataFrame as-of join (`DataFrame._joinAsOf` in PySpark, and Spark Connect) when `on` is a column name or a list of names. The output has one key column per name instead of two. There is no switch for the old output. The key keeps the type of the left side, and names are matched against top-level columns as in a regular USING join (a nested, quoted or metadata name now fails). A join with a Column for `on`, and `pandas.merge_asof`, are not changed.

- `DataFrameAsOfJoinSuite`: columns, schema and rows equal SQL `ASOF JOIN ... USING` for INNER and LEFT, one and two keys. Also select/filter/group by the key, an unmatched LEFT row and a NULL key, a self join, a missing name (`UNRESOLVED_USING_COLUMN_FOR_JOIN`), tolerance and direction against the rows of the Column-condition join. Each test runs with the fixed-point and single-pass analyzers, the sort-merge flag off and on, and ANSI on and off.
- Also: qualified access to the right key, INT vs BIGINT and STRING keys against SQL USING, case sensitivity, a nested field name.
- `SparkConnectProtoSuite`: a Connect as-of join with `using_columns` returns one key column, resolves plan-id keys with rows, with both analyzers.
- `StreamingAsOfJoinSuite`: a stream-static DataFrame join with using columns.
- Existing ASOF, join and codegen suites, with expected rows updated for the new column order.

Generated-by: Claude Code
Co-authored-by: Isaac <no-reply@databricks.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 00:41

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants