gengliangwang commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4636225682
##########
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:
**[P1] Stripping PipeExpression above a MultiAlias lets ExtractGenerator
drop the user's alias names**
For |> EXTEND and |> AGGREGATE, the parser wraps an `AS (a, b)` item as
UnresolvedAlias(PipeExpression(MultiAlias(g, names))). This PR strips the
PipeExpression once the generator's arguments are resolved, which leaves
UnresolvedAlias(MultiAlias(g, names)). The names only survive if ResolveAliases
(rule 769) turns that back into MultiAlias before ExtractGenerator (rule 760)
next rebuilds the project list. ExtractGenerator's Project branch runs every
item through trimNonTopLevelAliases. For an UnresolvedAlias that falls through
to trimAliases, which recurses into the child and removes the MultiAlias,
leaving UnresolvedAlias(g). AliasResolution.resolve then turns that into
MultiAlias(g, Nil), so the generator gets default column names. No error is
raised. Before this PR the PipeExpression stayed in place and the query failed
with an error. It now succeeds with the wrong column names. Plain SQL is not
affected because there the parser produces a bare MultiAlias, which
trimNonTopLevel
Aliases keeps. Possible fixes: have the parser wrap only the MultiAlias child
(MultiAlias(PipeExpression(g), names)), or have the pipe rule/ExtractGenerator
keep UnresolvedAlias(MultiAlias) intact. This was traced through the rule
order, not run.
**Failure scenario:** `FROM VALUES (1, array('a')) AS t(id, arr) |> EXTEND
explode(array(1, 2)) AS a, posexplode(array('x', 'y')) AS (pos, b)`. The same
happens with the PR's own temp view: `table outer_gen_test |> extend
explode_outer(tags) as t |> extend explode(array(1)) as e,
posexplode_outer(array(1, 2)) as (p, v)`. Iteration 1: the generators'
arguments are literals, so ResolveFunctions builds both generators and the pipe
rule strips both PipeExpressions. The input is still unresolved, so
ResolveAliases skips the Project. Iteration 2: the input has resolved,
ResolveReferences expands the star, and ExtractGenerator plans the single-alias
generator. The rebuilt list contains UnresolvedAlias(PosExplode) without the
names. ResolveAliases makes it MultiAlias(PosExplode, Nil). The output columns
are (..., pos, col) instead of (..., pos, b), and a later `|> SELECT b` 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:
**[P2] Design: isReadyGeneratorItem re-encodes AliasedGenerator's generator
shapes, kept in sync only by comments**
`isReadyGeneratorItem` (pipeOperators.scala:93-98) matches the same item
shapes that `ExtractGenerator.AliasedGenerator.unapply` already matches
(Analyzer.scala:3452-3458): `g`, `GeneratorOuter(g)`, `MultiAlias(g, _)` and
`MultiAlias(GeneratorOuter(g), _)`. The only differences are that the top Alias
is missing and `childrenResolved` is used where AliasedGenerator uses
`resolved`. The PR knows these are two copies: it adds 'keep the two in sync'
comments at pipeOperators.scala:91 and Analyzer.scala:3443-3444. It also adds a
third encoding of the same structure, `trimAliasAndPipeExpression`
(Analyzer.scala:3436-3441), which peels PipeExpression and MultiAlias.
AliasedGenerator is a private object inside the Analyzer class, so
pipeOperators.scala cannot reuse it today. Suggested change: lift the shape
peeling into one extractor in catalyst.expressions next to `GeneratorOuter`
(generators.scala:335). It would peel an optional MultiAlias and an optional
GeneratorOuter and return (gene
rator, names, outer). AliasedGenerator would use it under its Alias/MultiAlias
match with `g.resolved`, and ValidateAndStripPipeExpressions with
`g.childrenResolved`. The 'keep in sync' comments could then go.
**Why it matters:** The shapes ExtractGenerator plans are what decides
whether the PipeExpression gets stripped. If someone adds a shape to
AliasedGenerator, for example another generator wrapper or alias form, and
doesn't update isReadyGeneratorItem, the pipe operators fail again with
UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS while plain SQL works. That is the
exact bug this PR fixes, and only a comment in another file guards against it.
The GeneratorOuter detail makes drift easy: GeneratorOuter is itself a
Generator, and its `childrenResolved` means the inner generator's `resolved`,
so the case order matters.
--
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]