AveryQi115 opened a new pull request, #60364:
URL: https://github.com/apache/spark/pull/60364
### What changes were proposed in this pull request?
`PullOutNestedDataOuterRefExpressions` (SPARK-40615) projects a correlated
outer *map* member (`outer_map[1]`) onto the outer plan and rewrites the
correlation to a bare `OuterReference` on that projection, so that
`DecorrelateInnerQuery` sees a plain outer attribute. This PR extends the rule
to (possibly nested) `GetStructField` / `VariantGet` / `GetMapValue` chains
rooted at an `OuterReference`, so `c.home.zip` and `variant_get(c.home_v,
'$.zip', 'int')` are handled the same way:
- The matcher accepts any extract chain that bottoms out at an
`OuterReference` and references no inner attribute (`e.references.isEmpty`,
since outer references are not part of `references`). A member is pulled only
when its base column is in the output of the plan the projection lands on
(`plan.child`).
- The pruning also matches the `VARIANT_GET` tree pattern (`VariantGet` is
not an `ExtractValue`).
- Each subquery's `outerAttrs` are recomputed from the rewritten plan and
de-duplicated, since several correlations on the same member now share one
projected attribute.
- The rule is added to `nonExcludableRules`: a correlation it does not
normalize is mis-keyed on the whole outer column, so
`spark.sql.optimizer.excludedRules` must not be able to reinstate that.
`spark.sql.optimizer.pullOutNestedDataOuterRefExpressions.enabled` remains the
switch for the rule.
### Example plan
```sql
SELECT c.id, (SELECT max(o.amount) FROM osf_o o WHERE o.ship.zip =
c.home.zip) FROM osf_c c;
```
The analyzer resolves the outer access inside-out, so the correlation is a
struct access over the outer reference and the subquery's outer attribute is
the whole struct:
```
Project [id#936, scalar-subquery#935 [home#937] AS scalarsubquery(home)#947]
: +- Aggregate [max(amount#939) AS max(amount)#946]
: +- Filter (ship#940.zip = outer(home#937).zip)
: +- Relation osf_o[amount#939,ship#940,pzip#941] parquet
+- Relation osf_c[id#936,home#937,home_v#938] parquet
```
After the rule, the member is projected onto the outer plan and the
correlation is a bare outer reference on it:
```
Project [id#936, scalarsubquery(home)#977]
+- Project [id#936, scalar-subquery#972 [home.zip#983] AS
scalarsubquery(home)#977]
: +- Aggregate [max(amount#939) AS max(amount)#976]
: +- Filter (ship#940.zip = outer(home.zip#983))
: +- Relation osf_o[amount#939,ship#940,pzip#941] parquet
+- Project [id#936, home#937, home_v#938, home#937.zip AS home.zip#983]
+- Relation osf_c[id#936,home#937,home_v#938] parquet
```
Optimized, before (domain join keyed on the whole `home` struct, `Join
LeftOuter, (home#965 <=> home#937)`, aggregate grouped by the struct):
```
Project [id#936, max(amount)#958 AS scalarsubquery(home)#959]
+- Join LeftOuter, (home#965 <=> home#937)
:- Project [id#936, home#937]
: +- Relation osf_c[id#936,home#937,home_v#970] parquet
+- Aggregate [home#965], [max(amount#939) AS max(amount)#958, home#965]
+- Project [home#965, amount#939]
+- Join Inner, (_extract_zip#966 = home#965.zip)
:- Aggregate [home#937], [home#937 AS home#965]
: +- Project [home#937]
: +- Filter isnotnull(home#937.zip)
: +- Relation osf_c[id#936,home#937,home_v#971] parquet
+- Project [amount#939, ship#940.zip AS _extract_zip#966]
+- Filter isnotnull(ship#940.zip)
+- Relation osf_o[amount#939,ship#940,pzip#941] parquet
```
and after (keyed on `home.zip`):
```
Project [id#936, max(amount)#988 AS scalarsubquery(home)#989]
+- Join LeftOuter, (home.zip#996 <=> _extract_zip#997)
:- Project [id#936, home#937.zip AS _extract_zip#997]
: +- Relation osf_c[id#936,home#937,home_v#1009] parquet
+- Aggregate [home.zip#996], [max(amount#939) AS max(amount)#988,
home.zip#996]
+- Project [home.zip#996, amount#939]
+- Join Inner, (_extract_zip#998 = home.zip#996)
:- Aggregate [_extract_zip#999], [_extract_zip#999 AS
home.zip#996]
: +- Project [home#937.zip AS _extract_zip#999]
: +- Filter isnotnull(home#937.zip)
: +- Relation osf_c[id#936,home#937,home_v#1010] parquet
+- Project [amount#939, ship#940.zip AS _extract_zip#998]
+- Filter isnotnull(ship#940.zip)
+- Relation osf_o[amount#939,ship#940,pzip#941] parquet
```
### Why are the changes needed?
The whole outer struct is treated as the correlated key when the query
correlates on one of its fields. In a correlated join condition (`JOIN ... ON
o.pzip = c.home.zip`, with
`spark.sql.optimizer.decorrelateJoinPredicate.enabled`) the pulled-up predicate
is left unresolved and the query fails with `[INTERNAL_ERROR] Unsupported
correlated subquery`. In a filter, the equality on the field is not recognized
as an equality on an outer attribute, so decorrelation goes through a domain
join keyed on the entire struct (the result is only correct because the struct
is a finer partition than the field). Projecting the member onto the outer plan
fixes both: the correlation becomes a plain outer reference on the field.
### Does this PR introduce _any_ user-facing change?
Yes, bug fix: the join-condition query above now succeeds, and correlations
on a struct/variant field are decorrelated on the field instead of the whole
column.
### How was this patch tested?
New `PullOutNestedDataOuterRefExpressionsSuite`: an outer struct field
correlated with a plain inner column in a filter and in a join condition (rule
on and off, the off case reproducing the `INTERNAL_ERROR`); the rule cannot be
excluded through `spark.sql.optimizer.excludedRules`; inner struct/variant
field equal to an outer struct/variant field (`max`, `count`, `<=>` with `ORDER
BY ... LIMIT`, `variant_get` on the outer side) returning one row per outer
row; an inequality correlation staying correct through the domain join; a join
condition over an aggregate; and a plan-level check that the member is
projected once, no struct access over an outer reference remains, and the
subquery's outer attribute is the projected member. Existing `SubquerySuite`
map-correlation tests (SPARK-40615) and the `subquery/` golden files pass.
### Was this patch authored or co-authored using generative AI tooling?
Yes, co-authored with Claude Code (Isaac).
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]