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


##########
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)
+    case g: Generator => isReadyGenerator(g)
+    case _ => false
+  }
+
+  private def isReadyGenerator(g: Generator): Boolean = g match {
+    case GeneratorOuter(inner) => inner.childrenResolved
+    case _ => g.childrenResolved
+  }
+
+  /**
+   * Validates aggregate function constraints for a [[PipeExpression]] and 
returns its child
+   * expression (stripping the [[PipeExpression]] wrapper).
    *
    * This method is shared between the fixed-point analyzer rule and the 
single-pass resolver.
    *
    * @param pipeExpression The [[PipeExpression]] containing metadata about 
the pipe clause.
-   * @param resolvedChild The resolved child expression to validate and return.
-   * @return The resolved child expression after validation.
+   * @param resolvedChild The child expression to validate and return. It is 
resolved, except for

Review Comment:
   Done in 4c4083db. I renamed the parameter to `child`, removed the 
single-pass resolver sentence (this rule is the only caller), and the scaladoc 
now describes the accepted shapes itself instead of pointing to the private 
helper.



##########
sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql:
##########
@@ -2000,6 +2004,132 @@ from values (1, named_struct('a', 10, 'b', 20)) as 
tab(id, h)
 |> as x
 |> select x.h.a;
 
+-- Outer generators: positive tests.
+--------------------------------------
+
+-- Outer generators are allowed as top-level items in the pipe SELECT list, as 
in a plain SELECT.
+table outer_gen_test
+|> select id, explode_outer(tags) as tag;
+
+table outer_gen_test
+|> select id, posexplode_outer(tags);
+
+table outer_gen_test
+|> select id, inline_outer(transform(tags, t -> named_struct('tag', t)));
+
+table outer_gen_test
+|> select id, explode_outer(map_from_arrays(tags, tags));
+
+-- Outer generators in the pipe EXTEND list.
+table outer_gen_test
+|> extend explode_outer(tags) as tag
+|> select id, tag;
+
+-- Outer generators in the pipe SET list.
+table outer_gen_test
+|> set tags = explode_outer(tags)

Review Comment:
   Added both in 4c4083db: `|> set id = stack(2, id, null)`, and the chained 
`|> extend explode_outer(tags) as t |> extend posexplode_outer(array(1, 2)) as 
(p, v)`. Both return the expected rows.



##########
sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql:
##########
@@ -2000,6 +2004,132 @@ from values (1, named_struct('a', 10, 'b', 20)) as 
tab(id, h)
 |> as x
 |> select x.h.a;
 
+-- Outer generators: positive tests.
+--------------------------------------
+
+-- Outer generators are allowed as top-level items in the pipe SELECT list, as 
in a plain SELECT.
+table outer_gen_test
+|> select id, explode_outer(tags) as tag;
+
+table outer_gen_test
+|> select id, posexplode_outer(tags);
+
+table outer_gen_test
+|> select id, inline_outer(transform(tags, t -> named_struct('tag', t)));
+
+table outer_gen_test
+|> select id, explode_outer(map_from_arrays(tags, tags));
+
+-- Outer generators in the pipe EXTEND list.
+table outer_gen_test
+|> extend explode_outer(tags) as tag
+|> select id, tag;
+
+-- Outer generators in the pipe SET list.
+table outer_gen_test
+|> set tags = explode_outer(tags)
+|> select id, tags;
+
+-- Outer generators over aggregate functions in the pipe AGGREGATE list.
+table outer_gen_test
+|> aggregate explode_outer(collect_list(id)) as id group by author;

Review Comment:
   Added in 4c4083db: `|> aggregate explode_outer(collect_list(if(id > 10, id, 
null))) as id group by author` returns one NULL row per author, and the same 
query with `explode` right after it returns no rows.



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