cloud-fan commented on code in PR #60660:
URL: https://github.com/apache/spark/pull/60660#discussion_r4626131629
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -509,6 +514,107 @@ trait ColumnResolutionHelper extends Logging with
DataTypeErrorsBase {
resolveVariables(resolveOuterRef(e))
}
+ /**
+ * Applies [[resolveColsLastResort]] to a SELECT list (the project list of a
`Project` or the
+ * aggregate expressions of an `Aggregate`), but keeps the columns that
reference the output of
+ * a preceding generator in the same SELECT list unresolved, such as `tag` in
+ * `SELECT explode(tags) AS tag, upper(tag)`, or `col` in `SELECT
explode(tags), upper(col)`.
+ * In a `Project`, the rule `ExtractGenerator` will pull the generator out
into a `Generate`
+ * node below the `Project`, and then these columns are resolved to the
generator output. In an
+ * `Aggregate`, the generator output can not be referenced, as
`ExtractGenerator` puts the
+ * generator in a `Project` above the `Aggregate`. These columns stay
unresolved, and the query
+ * fails with `UNRESOLVED_COLUMN` instead of reading an outer query column
or a SQL variable.
+ *
+ * This follows the lateral column alias rule: the output of a generator has
higher priority
+ * than outer references and SQL variables only for the SELECT items after
the generator. A
+ * reference that comes before the generator, or in the arguments of the
generator, keeps its
+ * current resolution.
+ *
+ * The output names of a generator are its alias names, or the field names
of its element schema
+ * if it has no alias. As this runs before the rule `ResolveFunctions` in an
analyzer iteration,
+ * the function call of a generator is not resolved yet in the first
iteration, and a call to a
+ * built-in generator function is recognized by its name. If such a call has
no alias, its
+ * output names are not known until it is resolved, so all the columns after
it are kept
+ * unresolved in this iteration.
+ *
+ * TODO: A generator call that [[isBuiltinGeneratorCall]] can not recognize,
such as a call to a
+ * temporary or persistent function, or a call with a qualified function
name, is not protected
+ * before it is resolved. With an alias, the lateral column alias resolution
still keeps the
+ * references to the alias from being resolved as outer references or SQL
variables, unless
+ * `spark.sql.lateralColumnAlias.enableImplicitResolution` is false. Without
an alias, the
+ * references to its output names can still be resolved as outer references
or SQL variables in
+ * the first analyzer iteration.
+ */
+ protected def resolveSelectListColsLastResort(selectList: Seq[Expression]):
Seq[Expression] = {
+ if
(!conf.getConf(SQLConf.PREFER_GENERATOR_ALIAS_OVER_OUTER_REFERENCE_AND_VARIABLE))
{
+ return selectList.map(resolveColsLastResort)
+ }
+ // The output names of the generators before the current SELECT item.
+ val precedingGeneratorOutputNames = mutable.ArrayBuffer.empty[String]
+ // Whether a generator before the current SELECT item has output names
that are not known yet.
+ var hasPrecedingGeneratorWithUnknownOutputNames = false
+ selectList.map { e =>
+ val result = if (hasPrecedingGeneratorWithUnknownOutputNames) {
+ // Any column may reference the output of that generator. Keep them
all unresolved until
+ // the generator is resolved in this analyzer iteration, and its
output names are known
+ // in the next one.
+ val skipAll = (_: UnresolvedAttribute) => true
+ resolveVariables(resolveOuterRef(e, skipAll), skipAll)
+ } else if (precedingGeneratorOutputNames.isEmpty) {
+ resolveColsLastResort(e)
+ } else {
+ val names = precedingGeneratorOutputNames.toSeq
Review Comment:
Done in 6fb34b2df99. The names and the check are now rebuilt only when a
SELECT item adds generator output names.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -509,6 +514,107 @@ trait ColumnResolutionHelper extends Logging with
DataTypeErrorsBase {
resolveVariables(resolveOuterRef(e))
}
+ /**
+ * Applies [[resolveColsLastResort]] to a SELECT list (the project list of a
`Project` or the
+ * aggregate expressions of an `Aggregate`), but keeps the columns that
reference the output of
+ * a preceding generator in the same SELECT list unresolved, such as `tag` in
+ * `SELECT explode(tags) AS tag, upper(tag)`, or `col` in `SELECT
explode(tags), upper(col)`.
+ * In a `Project`, the rule `ExtractGenerator` will pull the generator out
into a `Generate`
+ * node below the `Project`, and then these columns are resolved to the
generator output. In an
+ * `Aggregate`, the generator output can not be referenced, as
`ExtractGenerator` puts the
+ * generator in a `Project` above the `Aggregate`. These columns stay
unresolved, and the query
+ * fails with `UNRESOLVED_COLUMN` instead of reading an outer query column
or a SQL variable.
+ *
+ * This follows the lateral column alias rule: the output of a generator has
higher priority
+ * than outer references and SQL variables only for the SELECT items after
the generator. A
+ * reference that comes before the generator, or in the arguments of the
generator, keeps its
+ * current resolution.
+ *
+ * The output names of a generator are its alias names, or the field names
of its element schema
+ * if it has no alias. As this runs before the rule `ResolveFunctions` in an
analyzer iteration,
+ * the function call of a generator is not resolved yet in the first
iteration, and a call to a
+ * built-in generator function is recognized by its name. If such a call has
no alias, its
+ * output names are not known until it is resolved, so all the columns after
it are kept
+ * unresolved in this iteration.
+ *
+ * TODO: A generator call that [[isBuiltinGeneratorCall]] can not recognize,
such as a call to a
+ * temporary or persistent function, or a call with a qualified function
name, is not protected
+ * before it is resolved. With an alias, the lateral column alias resolution
still keeps the
+ * references to the alias from being resolved as outer references or SQL
variables, unless
+ * `spark.sql.lateralColumnAlias.enableImplicitResolution` is false. Without
an alias, the
+ * references to its output names can still be resolved as outer references
or SQL variables in
+ * the first analyzer iteration.
Review Comment:
Updated in 6fb34b2df99 with your wording.
--
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]