dtenedor commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4635984923
##########
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:
Addressed in 4c4083db. `ExtractGenerator` now builds the nested-generator
message with a helper that strips the `PipeExpression`, and the `MultiAlias`
below it, after the top-level alias. Both golden outputs now read
`"upper(generatorouter(explode(tags)))"`. I added the equivalent plain SQL
queries next to them, and they report the same message.
##########
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:
Done in 4c4083db. I added `|> aggregate explode_outer(id)` (INT `id`), which
pins `PIPE_OPERATOR_AGGREGATE_EXPRESSION_CONTAINS_NO_AGGREGATE_FUNCTION`, and
`|> aggregate explode_outer(count(*)) group by author`, which passes the
aggregate check and then reports `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE`. The
scaladoc of `validateAndStripPipeExpression` now says that for these items the
aggregate checks run before the generator's own type check.
##########
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:
Done in 4c4083db: I added a comment on both sides. A shared extractor does
not fit well here: `AliasedGenerator` matches under an `Alias` and requires the
generator to be resolved, while this rule matches under the `PipeExpression`
and only requires its arguments to be resolved. It is also private to
`ExtractGenerator`. So `AliasedGenerator` now has a comment pointing to
`ValidateAndStripPipeExpressions.isReadyGeneratorItem`, and that helper's
scaladoc points back and asks to keep the two in sync.
##########
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:
Done in 4c4083db, using your single match.
--
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]