dtenedor commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4636436294
##########
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 (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.
*
- * This method is shared between the fixed-point analyzer rule and the
single-pass resolver.
+ * These are the shapes that ExtractGenerator's AliasedGenerator plans,
without the Alias the
+ * parser puts above the PipeExpression; keep the two in sync.
+ */
+ private def isReadyGeneratorItem(e: Expression): Boolean = e match {
+ case MultiAlias(g: Generator, _) => isReadyGeneratorItem(g)
Review Comment:
Thanks, confirmed and fixed in 00f41eee. Your first query reproduced it: `b`
was lost, the proposals listed `col`, and `|> SELECT b` failed with
`UNRESOLVED_COLUMN`. Your second query happened to keep its names because of
rule timing. I kept the parser unchanged. When an alias-list item is ready, the
rule now replaces the whole `UnresolvedAlias(PipeExpression(MultiAlias(...)))`
with the `MultiAlias`, as ResolveAliases would. `ExtractGenerator` then sees
the bare `MultiAlias` plain SQL produces, which `trimNonTopLevelAliases` keeps.
The rule no longer strips a `PipeExpression` directly above a `MultiAlias` any
other way. I added both of your queries as golden cases; the analyzer output
shows `Generate posexplode(array(x, y)), false, [pos#x, b#x]` and `Generate
posexplode(array(1, 2)), true, [p#x, v#x]`. With the fix reverted, the first
one fails with `UNRESOLVED_COLUMN`.
##########
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 (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.
*
- * This method is shared between the fixed-point analyzer rule and the
single-pass resolver.
+ * These are the shapes that ExtractGenerator's AliasedGenerator plans,
without the Alias the
+ * parser puts above the PipeExpression; keep the two in sync.
+ */
+ private def isReadyGeneratorItem(e: Expression): Boolean = e match {
Review Comment:
Done in 00f41eee. I added a `GeneratorItem` extractor next to
`GeneratorOuter` in generators.scala. It peels an optional `MultiAlias` and
then an optional `GeneratorOuter` (matched before `Generator`), and returns
`(generator, names: Option[Seq[String]], outer)`. `AliasedGenerator` now uses
it with `g.resolved` (`Alias(GeneratorItem(g, None, outer), name)` and
`GeneratorItem(g, Some(names), outer)`), and `ValidateAndStripPipeExpressions`
uses it with `g.childrenResolved`. I removed the keep-in-sync comments. I kept
`trimAliasAndPipeExpression`, since it peels aliases and the `PipeExpression`
from a nested generator item to build the error message, and that item is not a
generator item by definition.
--
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]