dtenedor commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4649007639


##########
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:
   Thanks, you were right, and my previous fix only covered literal arguments. 
Fixed in 7a82acd5 with the parser change you suggested. The `|> EXTEND` and `|> 
AGGREGATE` visitors now build `MultiAlias(PipeExpression(m.child), names)`, as 
they do for `Alias`, so the alias list stays top-level and 
`trimNonTopLevelAliases` keeps it, as in plain SQL. I removed the 
`UnresolvedAlias` special case from the pipe rule. I added golden cases with 
column arguments: your repro, the chained `|> EXTEND` version, and a chained 
`|> AGGREGATE` version. With the previous code, your repro fails with 
`UNRESOLVED_COLUMN` and proposes `col` instead of `b`; now the analyzer output 
shows `Generate posexplode(arr#x), false, [pos#x, b#x]`.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/pipeOperators.scala:
##########
@@ -62,28 +63,52 @@ 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 (see 
`isReadyGeneratorItem`).
  */
 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 =>
+        case p: PipeExpression if p.child.resolved || 
isReadyGeneratorItem(p.child) =>
           validateAndStripPipeExpression(p, p.child)
       }
   }
 
   /**
-   * Validates aggregate function constraints for a [[PipeExpression]] and 
returns the resolved
-   * child expression (stripping the [[PipeExpression]] wrapper).
+   * Returns true for a generator item that ExtractGenerator plans, once the 
generator's arguments
+   * are resolved: a generator, optionally wrapped in GeneratorOuter (for the 
*_outer functions)
+   * and then in MultiAlias (for a parenthesized alias list). ExtractGenerator 
replaces both
+   * wrappers when it plans Generate; ResolveAliases only removes the 
UnresolvedAlias above a
+   * MultiAlias. 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 
arguments are resolved,
+   * keeping both wrappers in place; otherwise ExtractGenerator sees the 
PipeExpression above the
+   * generator and rejects it as nested inside another expression.
+   */
+  private def isReadyGeneratorItem(e: Expression): Boolean = e match {
+    case MultiAlias(g: Generator, _) => isReadyGenerator(g)

Review Comment:
   Update: I changed course on this in 7a82acd5. Gengliang found that keeping 
the parser unchanged loses the alias-list names when the generator's arguments 
resolve after another generator in the same list is planned (see his P1 
thread). Your suggestion is the root-cause fix, so the `|> EXTEND` and `|> 
AGGREGATE` visitors now wrap only the `MultiAlias` child. As you noted, the `|> 
AGGREGATE` error's `expr` no longer includes the alias list.



-- 
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]

Reply via email to