dongjoon-hyun commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4635841745


##########
sql/core/src/test/resources/sql-tests/results/pipe-operators.sql.out:
##########
@@ -4995,6 +5005,540 @@ struct<a:int>
 10
 
 
+-- !query
+table outer_gen_test
+|> select id, explode_outer(tags) as tag
+-- !query schema
+struct<id:int,tag:string>
+-- !query output
+1      spark
+1      sql
+2      delta
+3      NULL
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> select id, posexplode_outer(tags)
+-- !query schema
+struct<id:int,pos:int,col:string>
+-- !query output
+1      0       sql
+1      1       spark
+2      0       delta
+3      NULL    NULL
+4      NULL    NULL
+
+
+-- !query
+table outer_gen_test
+|> select id, inline_outer(transform(tags, t -> named_struct('tag', t)))
+-- !query schema
+struct<id:int,tag:string>
+-- !query output
+1      spark
+1      sql
+2      delta
+3      NULL
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> select id, explode_outer(map_from_arrays(tags, tags))
+-- !query schema
+struct<id:int,key:string,value:string>
+-- !query output
+1      spark   spark
+1      sql     sql
+2      delta   delta
+3      NULL    NULL
+4      NULL    NULL
+
+
+-- !query
+table outer_gen_test
+|> extend explode_outer(tags) as tag
+|> select id, tag
+-- !query schema
+struct<id:int,tag:string>
+-- !query output
+1      spark
+1      sql
+2      delta
+3      NULL
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> set tags = explode_outer(tags)
+|> select id, tags
+-- !query schema
+struct<id:int,tags:string>
+-- !query output
+1      spark
+1      sql
+2      delta
+3      NULL
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> aggregate explode_outer(collect_list(id)) as id group by author
+-- !query schema
+struct<author:string,id:int>
+-- !query output
+ann    1
+ann    3
+bob    2
+bob    4
+
+
+-- !query
+table outer_gen_test
+|> where id > 10
+|> aggregate collect_list(id) as ids
+|> select explode_outer(ids)
+-- !query schema
+struct<col:int>
+-- !query output
+NULL
+
+
+-- !query
+table outer_gen_test
+|> extend posexplode_outer(tags) as (pos, tag)
+|> select id, pos, tag
+-- !query schema
+struct<id:int,pos:int,tag:string>
+-- !query output
+1      0       sql
+1      1       spark
+2      0       delta
+3      NULL    NULL
+4      NULL    NULL
+
+
+-- !query
+table outer_gen_test
+|> extend explode_outer(map_from_arrays(tags, tags)) as (k, v)
+|> select id, k, v
+-- !query schema
+struct<id:int,k:string,v:string>
+-- !query output
+1      spark   spark
+1      sql     sql
+2      delta   delta
+3      NULL    NULL
+4      NULL    NULL
+
+
+-- !query
+table outer_gen_test
+|> extend inline_outer(transform(tags, t -> named_struct('tag', t, 'len', 
length(t)))) as (tag, len)
+|> select id, tag, len
+-- !query schema
+struct<id:int,tag:string,len:int>
+-- !query output
+1      spark   5
+1      sql     3
+2      delta   5
+3      NULL    NULL
+4      NULL    NULL
+
+
+-- !query
+table outer_gen_test
+|> extend posexplode(tags) as (pos, tag)
+|> select id, pos, tag
+-- !query schema
+struct<id:int,pos:int,tag:string>
+-- !query output
+1      0       sql
+1      1       spark
+2      0       delta
+
+
+-- !query
+table outer_gen_test
+|> aggregate posexplode_outer(collect_list(id)) as (pos, id) group by author
+-- !query schema
+struct<author:string,pos:int,id:int>
+-- !query output
+ann    0       1
+ann    1       3
+bob    0       2
+bob    1       4
+
+
+-- !query
+table outer_gen_test
+|> aggregate explode_outer(map_from_arrays(collect_list(id), 
collect_list(id))) as (k, v)
+   group by author
+-- !query schema
+struct<author:string,k:int,v:int>
+-- !query output
+ann    1       1
+ann    3       3
+bob    2       2
+bob    4       4
+
+
+-- !query
+table outer_gen_test
+|> aggregate inline_outer(collect_list(named_struct('id', id, 'n', 
size(tags)))) as (id, n)
+   group by author
+-- !query schema
+struct<author:string,id:int,n:int>
+-- !query output
+ann    1       2
+ann    3       0
+bob    2       1
+bob    4       NULL
+
+
+-- !query
+table outer_gen_test
+|> aggregate posexplode(collect_list(id)) as (pos, id) group by author
+-- !query schema
+struct<author:string,pos:int,id:int>
+-- !query output
+ann    0       1
+ann    1       3
+bob    0       2
+bob    1       4
+
+
+-- !query
+table outer_gen_test
+|> extend stack(2, id, null) as (a)
+|> select id, a
+-- !query schema
+struct<id:int,a:int>
+-- !query output
+1      1
+1      NULL
+2      2
+2      NULL
+3      3
+3      NULL
+4      4
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> aggregate stack(2, count(*), null) as (c) group by author
+-- !query schema
+struct<author:string,c:bigint>
+-- !query output
+ann    2
+ann    NULL
+bob    2
+bob    NULL
+
+
+-- !query
+table outer_gen_test
+|> select id, stack(2, id, null) as a
+-- !query schema
+struct<id:int,a:int>
+-- !query output
+1      1
+1      NULL
+2      2
+2      NULL
+3      3
+3      NULL
+4      4
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> select id, stack(2, id, null)
+-- !query schema
+struct<id:int,col0:int>
+-- !query output
+1      1
+1      NULL
+2      2
+2      NULL
+3      3
+3      NULL
+4      4
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> extend stack(2, id, null) as a
+|> select id, a
+-- !query schema
+struct<id:int,a:int>
+-- !query output
+1      1
+1      NULL
+2      2
+2      NULL
+3      3
+3      NULL
+4      4
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> extend stack(2, id, null)
+|> select id, col0
+-- !query schema
+struct<id:int,col0:int>
+-- !query output
+1      1
+1      NULL
+2      2
+2      NULL
+3      3
+3      NULL
+4      4
+4      NULL
+
+
+-- !query
+table outer_gen_test
+|> select upper(explode_outer(tags))
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+  "errorClass" : "UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS",
+  "sqlState" : "42K0E",
+  "messageParameters" : {
+    "expression" : "\"pipeexpression(upper(generatorouter(explode(tags))))\""
+  }
+}
+
+
+-- !query
+table outer_gen_test
+|> aggregate explode_outer(tags)
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+  "errorClass" : 
"PIPE_OPERATOR_AGGREGATE_EXPRESSION_CONTAINS_NO_AGGREGATE_FUNCTION",
+  "sqlState" : "0A000",
+  "messageParameters" : {
+    "expr" : "generatorouter(explode(tags#x))"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 35,
+    "stopIndex" : 53,
+    "fragment" : "explode_outer(tags)"
+  } ]
+}
+
+
+-- !query
+table outer_gen_test
+|> extend explode_outer(collect_list(tags))
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+  "errorClass" : "PIPE_OPERATOR_CONTAINS_AGGREGATE_FUNCTION",
+  "sqlState" : "0A000",
+  "messageParameters" : {
+    "clause" : "EXTEND",
+    "expr" : "collect_list(tags#x)"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 46,
+    "stopIndex" : 63,
+    "fragment" : "collect_list(tags)"
+  } ]
+}
+
+
+-- !query
+table outer_gen_test
+|> extend posexplode_outer(collect_list(tags)) as (pos, tag)
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+  "errorClass" : "PIPE_OPERATOR_CONTAINS_AGGREGATE_FUNCTION",
+  "sqlState" : "0A000",
+  "messageParameters" : {
+    "clause" : "EXTEND",
+    "expr" : "collect_list(tags#x)"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 49,
+    "stopIndex" : 66,
+    "fragment" : "collect_list(tags)"
+  } ]
+}
+
+
+-- !query
+table outer_gen_test
+|> aggregate posexplode_outer(tags) as (pos, tag)
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+  "errorClass" : 
"PIPE_OPERATOR_AGGREGATE_EXPRESSION_CONTAINS_NO_AGGREGATE_FUNCTION",
+  "sqlState" : "0A000",
+  "messageParameters" : {
+    "expr" : "generatorouter(posexplode(tags#x)) AS (pos, tag)"
+  },
+  "queryContext" : [ {
+    "objectType" : "",
+    "objectName" : "",
+    "startIndex" : 35,
+    "stopIndex" : 70,
+    "fragment" : "posexplode_outer(tags) as (pos, tag)"
+  } ]
+}
+
+
+-- !query
+table outer_gen_test
+|> extend upper(explode_outer(tags)) as (a, b)
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.AnalysisException
+{
+  "errorClass" : "UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS",
+  "sqlState" : "42K0E",
+  "messageParameters" : {
+    "expression" : 
"\"pipeexpression(multialias(upper(generatorouter(explode(tags)))))\""

Review Comment:
   This new golden output exposes internal wrapper names in a user-facing error 
message: `pipeexpression(multialias(...))` here and `pipeexpression(...)` at 
L5321. The base branch has no such output, and plain SQL reports only the 
expression itself, e.g. `"stack(2, explode(array(1, 2, 3)))"` in 
`generators.sql.out`.
   
   `ExtractGenerator` builds this message with 
`nestedGeneratorError(trimAlias(...))` (Analyzer.scala:3455), and `trimAlias` 
(Analyzer.scala:3424) removes only the top-level alias, not the 
`PipeExpression` and `MultiAlias` below it. Could we strip them before building 
the message, so that pipe syntax reports the same message as plain SQL?



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

Review Comment:
   `isReadyGeneratorItem` repeats the generator shapes that 
`ExtractGenerator.AliasedGenerator` plans (Analyzer.scala:3438-3443): 
`Alias`/`MultiAlias`, each with or without `GeneratorOuter`. If 
`ExtractGenerator` supports a new shape later, this list has to change too; 
otherwise that shape fails with `NESTED_IN_EXPRESSIONS` only in pipe syntax. 
Could we share an extractor with `ExtractGenerator`, or at least add a comment 
on `AliasedGenerator` that points here?



##########
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:
   nit: the parameter is still named `resolvedChild` (L115) even though it can 
now be an unresolved generator item. Renaming it to `child` would match this 
description. Also, the sentence at L105 that says this method is shared with 
the single-pass resolver is not true: this rule (L75) is the only caller. And 
since `isReadyGeneratorItem` is private, this public method's doc could 
describe the accepted shapes itself instead of pointing to the helper.



##########
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:
   An alternative to consider: this `MultiAlias` arm (and the new 
`analysis.MultiAlias` import) is needed only because the `|> EXTEND` and `|> 
AGGREGATE` visitors wrap a `MultiAlias` item whole in `PipeExpression` 
(AstBuilder.scala:8468-8469, 8576-8577), while they wrap only the child of an 
`Alias` (L8463-8466, L8571-8574). If `AstBuilder` wrapped the child of a 
`MultiAlias` too (`m.copy(child = PipeExpression(m.child, ...))`), this rule 
would not need to know about `MultiAlias`. The `|> AGGREGATE` error's `expr` 
would also stop including the alias list: today it is 
`generatorouter(explode(tags#x))` for an unaliased item (golden L5337) but 
`generatorouter(posexplode(tags#x)) AS (pos, tag)` for an alias list (L5408).
   
   I know the earlier discussion kept the parser unchanged, so please ignore 
this if you still prefer that.



##########
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) =>

Review Comment:
   With this guard, a generator item is stripped and validated as soon as its 
arguments are resolved, i.e., before the generator's own type check. So, by my 
reading, `table outer_gen_test |> aggregate explode_outer(id)` (INT `id`) fails 
with `PIPE_OPERATOR_AGGREGATE_EXPRESSION_CONTAINS_NO_AGGREGATE_FUNCTION` 
instead of `DATATYPE_MISMATCH`. A non-generator item with a type error keeps 
its `PipeExpression` and gets `DATATYPE_MISMATCH` from `CheckAnalysis` instead.
   
   I see the previous review listed this as an accepted risk. Could we add a 
golden case for it, so the intended error for a wrongly typed generator 
argument in `|> AGGREGATE` is pinned down?



##########
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:
   In this case and the other `|> AGGREGATE` `*_outer` cases (L2061-2068), 
every group's `collect_list` has at least one element. So the results golden 
file is the same as with the non-outer generators, and only the `outer` flag in 
the analyzer-results file tells them apart. Could we add a case where a group 
produces an empty array? For example, `|> aggregate 
explode_outer(collect_list(if(id > 10, id, null))) as id group by author` 
should return one NULL row per author, while `explode` returns no rows.



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

Review Comment:
   nit: `isReadyGenerator` can be folded into `isReadyGeneratorItem` with a 
single match:
   
   ```scala
   private def isReadyGeneratorItem(e: Expression): Boolean = e match {
     case MultiAlias(g: Generator, _) => isReadyGeneratorItem(g)
     case GeneratorOuter(g) => g.childrenResolved
     case g: Generator => g.childrenResolved
     case _ => false
   }
   ```



##########
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:
   Two more cases might be worth adding:
   1. `|> SET` with a generator that needs type coercion, e.g. `|> set id = 
stack(2, id, null)`. The `stack(2, id, null)` cases at L2077-2094 cover only 
EXTEND, AGGREGATE, and SELECT.
   2. A chained pipe where the generator's arguments resolve before its input 
plan does, e.g. `|> extend explode_outer(tags) as t |> extend 
posexplode_outer(array(1, 2)) as (p, v)`. The `PipeExpression` of the second 
item is stripped while the `Project` below it is still unresolved, so 
`UnresolvedAlias(MultiAlias(...))` stays until a later iteration. No current 
case covers this ordering.



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