johannes-ehls-db opened a new pull request, #60719:
URL: https://github.com/apache/spark/pull/60719
### What changes were proposed in this pull request?
The single-pass resolver resolves the arguments of a function call first and
then resolves the replacement expression that the function builder produces
from them. The already resolved arguments are boundaries:
`ExpressionResolver.resolve` returns them as they are. So far the boundaries
were marked with the `SINGLE_PASS_SUBTREE_BOUNDARY` tag, which
`ExpressionResolver` removed on the first visit.
This PR tracks the boundaries in resolver state instead of tags:
- New `SubtreeBoundaryScopes`, owned by `ExpressionResolver` and shared with
`FunctionResolver` and `BinaryArithmeticResolver`. It maps each boundary node,
by object identity, to its number of open scopes.
- The `ProducesUnresolvedSubtree` helpers add the boundaries while the
produced subtree is resolved and remove them in a `finally`.
`ExpressionResolver.resolve` only checks whether a node is a boundary and no
longer changes anything.
- For a `NamedArgumentExpression` argument, the boundary is its value
instead of the wrapper, because argument rearrangement replaces the wrapper
with its value. Literals are not added, as before.
- `SINGLE_PASS_SUBTREE_BOUNDARY` and the tag cleanup code are removed.
### Why are the changes needed?
Removing the tag on the first visit breaks rewrites that use an argument
more than once. With `spark.sql.alwaysInlineCommonExpr=true`,
```sql
SELECT nullif((SELECT max(id) FROM range(1, 4)), 0)
```
is rewritten to `NullIfResult(EqualTo(a, 0), a)`, where both `a` are the
same resolved scalar subquery. The second occurrence is no longer tagged, so
its already resolved plan is resolved again. The same happens for `between` and
`decode`. The result is still correct, but the second resolution is wasted work
and breaks the single-pass invariant that every node is resolved once.
Tags have more problems as boundary markers. `TreeNode.transform` copies
tags onto rebuilt nodes, so a node that is not a boundary can become one.
Setting tags mutates nodes that may be shared. And a tag on a
`NamedArgumentExpression` wrapper is lost when the wrapper is replaced by its
value.
### Does this PR introduce _any_ user-facing change?
No. The single-pass resolver is internal and disabled by default, and query
results do not change.
### How was this patch tested?
- New `ProducesUnresolvedSubtreeSuite`:
- boundaries are removed when the rewrite throws;
- the value of a named argument is a boundary, a literal is not;
- a nested helper keeps the boundary of the enclosing helper;
- an argument that occurs twice stays a boundary until its helper returns;
- a node rebuilt from a boundary is not a boundary.
- New cases in `scalar-subquery-select.sql`: `nullif`, `between` and
`decode` over scalar subqueries (also correlated), an aggregate, a window
function and a lambda variable, with `spark.sql.alwaysInlineCommonExpr` on and
off.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
--
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]