cloud-fan opened a new pull request, #60660:
URL: https://github.com/apache/spark/pull/60660
### What changes were proposed in this pull request?
A SELECT item can reference the output alias of a generator that comes
before it in the same SELECT list, such as `tag` in `SELECT explode(tags) AS
tag, upper(tag)`. The reference can only be resolved after the rule
`ExtractGenerator` pulls the generator into a `Generate` node below the SELECT
list. Until then it is unresolved, and `ResolveReferences` resolves it with the
last-resort resolution (`resolveColsLastResort`): first as an outer reference,
then as a SQL variable. If the outer query has a column of the same name, or a
SQL variable of the same name exists, the reference binds to it before
`ExtractGenerator` runs.
This PR adds `ColumnResolutionHelper.resolveSelectListColsLastResort`, which
the `Project` branch of `ResolveReferences` and `ResolveReferencesInAggregate`
now use instead of mapping `resolveColsLastResort` over the SELECT list. It
keeps a column unresolved during the last-resort resolution if its first name
part matches the output name of a preceding generator in the same SELECT list,
that is a preceding `Alias(_: Generator, name)` or `MultiAlias(_, names)`.
After `ExtractGenerator` runs, the column resolves to the generator output.
This follows the lateral column alias rule:
- Only the generators before the reference are considered. A reference
before the generator keeps its current resolution, the same as a forward
reference to a lateral column alias.
- In the first analyzer iteration, before the function call is resolved to a
generator, the existing lateral column alias resolution already turns a
reference to the unresolved alias into a `LateralColumnAliasReference`, which
is not resolved as an outer reference.
- The output of a generator does not precede its own arguments, so `LATERAL
(SELECT explode(array(tag)) AS tag)` still reads the outer `tag`.
`resolveOuterRef` and `resolveVariables` take an optional predicate for the
columns to skip. A new internal config,
`spark.sql.analyzer.preferGeneratorAliasOverOuterReferenceAndVariable` (default
`true`), restores the previous behavior when set to `false`.
### Why are the changes needed?
The query silently returns wrong results. For example:
```sql
CREATE TEMPORARY VIEW users AS SELECT * FROM VALUES ('ann', 'vip'), ('bob',
'new') AS t(user_name, tag);
CREATE TEMPORARY VIEW sessions AS
SELECT * FROM VALUES ('ann', array('mobile', 'promo')), ('bob',
array('repeat')) AS t(user_name, tags);
SELECT u.user_name, x.tag, x.tag_uc
FROM users u, LATERAL (
SELECT explode(s.tags) AS tag, upper(tag) AS tag_uc
FROM sessions s WHERE s.user_name = u.user_name) x;
-- before: [ann,mobile,VIP] [ann,promo,VIP] [bob,repeat,NEW]
-- after: [ann,mobile,MOBILE] [ann,promo,PROMO] [bob,repeat,REPEAT]
DECLARE VARIABLE tag STRING DEFAULT 'var';
SELECT explode(array('a', 'b')) AS tag, upper(tag);
-- before: [a,VAR] [b,VAR]
-- after: [a,A] [b,B]
```
`tag_uc` should be computed from the exploded value, as it is when the outer
table has no `tag` column and no variable `tag` exists. A lateral column alias
in the same position already has higher priority than an outer reference (see
the comment in the `Project` branch of `ResolveReferences`), and so should a
generator output alias.
A follow-up of SPARK-47241 stopped resolving generator aliases as lateral
column aliases, because `ExtractGenerator` rewrites the generator output with
fresh attribute IDs, and the name is resolved normally after the generator is
extracted. That left the name open to the last-resort resolution before the
extraction. SPARK-45069 is the precedent for the resolution order here: it made
SQL variables resolve only after outer references, and this PR puts a preceding
generator output alias ahead of both.
### Does this PR introduce _any_ user-facing change?
Yes. It is a bug fix. The SPARK-47241 change that left the name open to the
last-resort resolution is in Spark 4.0.0 and later.
- A column in a SELECT list that references the output alias of a preceding
generator in the same SELECT list now resolves to the generator output, even if
an outer query column or a SQL variable has the same name. Previously, it
resolved to the outer query column or the SQL variable, as in the examples
above.
- A SELECT list with aggregation cannot reference the generator output. For
example, `SELECT explode(array('a', 'b')) AS tag, upper(tag), count(*) FROM
range(1)` fails with `UNRESOLVED_COLUMN.WITH_SUGGESTION` when no variable `tag`
exists. With a SQL variable `tag` declared, the same query used to return
`[a,VAR,1] [b,VAR,1]`. It now fails with the same error, instead of using the
variable.
- A reference that comes before the generator in the SELECT list keeps its
current resolution.
- Setting the internal config
`spark.sql.analyzer.preferGeneratorAliasOverOuterReferenceAndVariable` to
`false` restores the previous behavior. The migration guide has a new entry.
### How was this patch tested?
New tests in `GeneratorFunctionSuite`:
- LATERAL subqueries over an outer table with columns of the generator
output names: a single alias, a multi-alias (`explode(map) AS (page, ms)`), and
a generator with a function call in its arguments (`explode(split(s.csv,
','))`).
- The same with a SQL variable, plus a window `PARTITION BY` over a
`posexplode` output, and the aggregate case above.
- Forward references still resolve to the outer column or the SQL variable,
including `SELECT upper(tag) AS a, lower(a) AS tag`.
- A generator argument still references an outer column of the same name as
its own output.
- With the config set to `false`, the previous results.
The new tests fail without the fix. Existing tests also pass:
`GeneratorFunctionSuite`, `LateralColumnAliasSuite`, the catalyst
`ResolveSubquerySuite`, `AnalysisSuite`, `AnalysisErrorSuite`, and the
`SQLQueryTestSuite` golden files for generators, `join-lateral`,
`lateral-view`, SQL session variables, subqueries, `pipe-operators`,
`group-by`, `sql-udf` and column resolution, with no golden file changes.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Opus 5.5)
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]