gengliangwang commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4637138319
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/pipeOperators.scala:
##########
@@ -62,36 +63,58 @@ object EliminatePipeOperators extends Rule[LogicalPlan] {
}
/**
- * Validates and strips PipeExpression nodes from a logical plan once the
child expressions are
- * resolved.
+ * Validates and strips PipeExpression nodes from a logical plan once their
children are ready:
+ * either resolved, or a generator item whose arguments are resolved.
+ *
+ * A generator item ([[GeneratorItem]]) is a generator, optionally wrapped in
GeneratorOuter (for
+ * the *_outer functions) and then in MultiAlias (for a parenthesized alias
list). Neither wrapper
+ * ever reports itself resolved, and the generator itself may only become
resolved after this rule
+ * runs, when type coercion fixes its arguments (for example, stack(2, id,
null)). So the
+ * PipeExpression is stripped as soon as the generator's arguments are
resolved, keeping both
+ * wrappers in place for ExtractGenerator to plan; otherwise ExtractGenerator
sees the
+ * PipeExpression above the generator and rejects it as nested inside another
expression.
*/
case object ValidateAndStripPipeExpressions extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan =
plan.resolveOperatorsUpWithPruning(
_.containsPattern(PIPE_EXPRESSION), ruleId) {
case node: LogicalPlan =>
node.resolveExpressions {
- case p: PipeExpression if p.child.resolved =>
+ // |> EXTEND and |> AGGREGATE wrap a parenthesized alias list item
whole, as
+ // UnresolvedAlias(PipeExpression(MultiAlias(...))). Replace the whole
item with the
+ // MultiAlias, as ResolveAliases would, to get the shape plain SQL
produces. Leaving
+ // UnresolvedAlias(MultiAlias(...)) instead lets ExtractGenerator trim
the MultiAlias, and
+ // with it the names, if it rebuilds the list before ResolveAliases
runs.
+ case UnresolvedAlias(p @ PipeExpression(m @ GeneratorItem(g, Some(_),
_), _, _), _)
Review Comment:
**[P1] Alias-list names are lost when another generator in the same EXTEND
is planned first**
The new `UnresolvedAlias` case only protects an alias-list item after it has
been stripped. Before that, the item is
`UnresolvedAlias(PipeExpression(MultiAlias(UnresolvedFunction)))`. The
Resolution batch runs ResolveReferences, then ExtractGenerator
(Analyzer.scala:760), then ResolveFunctions (762), then
ValidateAndStripPipeExpressions (766). When the input plan resolves in a later
iteration, ResolveReferences resolves the generator's column argument.
ExtractGenerator then plans another, already-stripped generator in the same
list and rebuilds the project list with `trimNonTopLevelAliases`. `trimAliases`
recurses through UnresolvedAlias and PipeExpression and drops the nested
MultiAlias (AliasHelper.scala:126), so the names are gone before this case can
fire. ResolveAliases later produces `MultiAlias(g, Nil)`, and the default names
(`pos`, `col`) are used. Plain SQL is not affected because its MultiAlias is
top-level. The comment at lines 82-86 says this hazard is avoided, but it
still applies before the strip. A root-cause fix is to have the |> EXTEND and
|> AGGREGATE parsers (AstBuilder.scala:8467/8575) build
`MultiAlias(PipeExpression(m.child), names)`, the same way they handle Alias.
That keeps the MultiAlias top-level and makes the special case unnecessary. The
new tests at pipe-operators.sql:2119 and 2124 miss this because they only use
literal-argument alias-list generators, which are ready in the first iteration.
**Failure scenario:** `from values (1, array('a')) as tab(id, arr) |> extend
explode(array(1, 2)) as a, posexplode(arr) as (pos, b) |> select id, a, pos,
b`. In iteration 1, `a` is stripped, but `posexplode(arr)` stays unresolved
because the inline table resolves only at the end of the iteration. In
iteration 2, ExtractGenerator plans `a` and trims the MultiAlias off the second
item, so the output columns are `pos`/`col` and the query fails with
UNRESOLVED_COLUMN `b`. Without the trailing select, the output column names are
silently wrong. Stacked pipes also hit this: `table outer_gen_test |> extend
explode_outer(tags) as t |> extend explode(array(1)) as e,
posexplode_outer(tags) as (p, v)`.
--
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]