spark-test commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4634948925
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/pipeOperators.scala:
##########
@@ -62,28 +63,46 @@ 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 that ExtractGenerator plans later (see
+ * `isResolvedGeneratorItem`).
*/
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 ||
isResolvedGeneratorItem(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 the generator items that ExtractGenerator plans but that
never report
+ * themselves resolved: an outer generator (GeneratorOuter is replaced by
ExtractGenerator), and
+ * a generator with a parenthesized alias list (MultiAlias is replaced by
ResolveAliases or
+ * ExtractGenerator). The PipeExpression must be stripped once the generator
inside is resolved,
+ * keeping GeneratorOuter and MultiAlias in place; otherwise
ExtractGenerator sees the
+ * PipeExpression above the generator and rejects it as nested inside
another expression.
+ */
+ private def isResolvedGeneratorItem(e: Expression): Boolean = e match {
+ case GeneratorOuter(g) => g.resolved
+ case MultiAlias(GeneratorOuter(g), _) => g.resolved
+ case MultiAlias(g: Generator, _) => g.resolved
Review Comment:
**Non-blocking (P2):** This arm, like the two `GeneratorOuter` arms above
it, strips the `PipeExpression` only if the generator is already resolved when
this rule runs, but a generator whose own type check passes only after type
coercion is not resolved yet at that point.
By the review's reading of the analyzer (the review did not run it): for
`table outer_gen_test |> extend stack(2, id, null) as (a)`, ResolveFunctions
builds `Stack(2, id, NULL)`, which fails its type check because its only column
mixes INT and NULL (generators.scala:217-232), so this arm returns false and
the `PipeExpression` stays. StackCoercion, which runs with the other type
coercion rules after this rule (Analyzer.scala:766, 805), then types the NULL,
and in the next iteration ExtractGenerator, which runs first
(Analyzer.scala:760), finds the generator under the `PipeExpression` and throws
`UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS` (Analyzer.scala:3407-3411,
3453-3459). `table outer_gen_test |> aggregate stack(2, count(*), null) as (c)
group by author` fails the same way. The plain `select *, stack(2, id, null) as
(a) from outer_gen_test` analyzes, and so do `|> select stack(2, id, null) as
(a)`, whose alias-list item the parser leaves unwrapped, and `|> extend
stack(2, id,
0) as (a)`, whose Stack is resolved as soon as it is built; generators.sql:77
already runs a plain SQL stack call with untyped NULLs.
So among the alias-list items the PR description says now work, whether an
item analyzes depends on whether its generator needs type coercion. Stack with
an untyped NULL is the traced example, not a complete list, and no new golden
case uses such a generator. The outer generators the PR targets are not
affected, because explode, posexplode and inline are resolved as soon as they
are built with valid arguments. The single-name and unaliased forms, such as
`|> extend stack(2, id, null) as a`, already fail the same way on the base
branch through the `p.child.resolved` guard, so a fix that covered only the
alias-list form would make the same item analyze or fail depending on how its
alias is written.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60697#discussion_r4623888068).
**Recommended change:** In ValidateAndStripPipeExpressions in
pipeOperators.scala, make the test that decides when a generator item's
PipeExpression is stripped wait only until the generator's arguments are
resolved, instead of until the generator itself is resolved, for every shape
the rule strips: an outer generator, an alias list over an outer or non-outer
generator, and a bare generator under a single-name alias or none. Keep
GeneratorOuter and MultiAlias under the alias and keep running the pipe
aggregate checks on the stripped item. Update the scaladocs of the readiness
helper and of validateAndStripPipeExpression to describe the new condition.
This drops the earlier review's suggestion to keep waiting until the generator
itself is resolved, which is what leaves these items failing. Add pipe operator
SQL golden cases for stack with an untyped NULL argument, with a parenthesized
alias list in |> EXTEND and |> AGGREGATE and with a single-name alias and with
none in |> SELECT a
nd |> EXTEND, and for a wrongly typed generator argument in a |> SELECT outer
generator item and a |> EXTEND alias-list item, then regenerate the golden
files.
**Why this works:** ResolveFunctions builds a function only after its
arguments are resolved and runs before the strip rule, so once the generator's
arguments are resolved the item's expression tree is complete and the aggregate
checks see every aggregate function in it. Stripping the PipeExpression at that
point means that when type coercion later resolves the generator in the same
iteration, ExtractGenerator's next pass sees the same alias shape plain SQL
produces: its nested-generator check accepts a single generator under an alias
or MultiAlias, and its planning and ResolveAliases wait until the generator is
resolved. A generator whose argument has the wrong type also loses the
PipeExpression, so the analyzer reports its type error as it does for the plain
SQL query.
**Compatibility:** Every pipe query that analyzes on the base branch, and
every pipe item this PR already makes work, keeps its plan, rows and errors;
generators nested inside other expressions stay rejected, and the pipe
aggregate checks still apply to every generator item.
**Risks:** Pipe items whose generator has a wrongly typed argument, which
fail on the base branch with UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS, will
fail with the error the equivalent plain SQL query reports, such as a
DATATYPE_MISMATCH error, or in |> AGGREGATE with the pipe aggregate-check
error, so their error class and SQLSTATE change; no query that analyzes on the
base branch changes, and reverting the readiness change restores the old
errors. Single-name and unaliased pipe items over a generator that only type
coercion resolves, such as |> EXTEND stack(2, id, null) AS a, which fail on the
base branch, start analyzing to the plain SQL plan, a fix beyond the PR's
stated problem that keeps every alias form consistent.
**Constraints:** Keep the PipeExpression on any item that is not one of the
generator shapes ExtractGenerator plans, including a generator nested inside
another expression, so nested generators are still rejected with
UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS. Keep the PipeExpression until
every argument of the generator is resolved, so the pipe aggregate checks
always run on the finished expression before the PipeExpression is stripped.
For an outer generator, with or without an alias list, test the arguments of
the generator inside GeneratorOuter: GeneratorOuter's own child is that
generator, so a test of GeneratorOuter's children would still wait until the
generator itself is resolved. Keep GeneratorOuter and MultiAlias under the
alias after the PipeExpression is stripped, so the *_outer functions still plan
Generate with the outer flag and an alias list still names the output columns.
**Verification:**
- **Regression:** A pipe |> EXTEND item and a pipe |> AGGREGATE item that
give stack with an untyped NULL argument a parenthesized alias list, which fail
with UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS before the fix, analyze to
Generate and return the same rows as the equivalent plain SQL query.
- **Behavior:** The single-name and unaliased forms of the same stack item
in |> SELECT and |> EXTEND analyze to the plain SQL plan and return the plain
SQL rows.
- **Behavior:** A pipe |> SELECT outer generator item and a pipe |> EXTEND
alias-list item whose generator argument has the wrong type fail with the same
error class as the equivalent plain SQL query.
- **Compatibility:** Regenerating the pipe-operators analyzer and result
golden files changes nothing but the added cases, so existing pipe queries,
including the outer-generator, nested-generator and aggregate-check cases this
PR adds, keep their plans, rows and errors.
- **Inspection:** The scaladocs of the generator readiness helper and of
validateAndStripPipeExpression in pipeOperators.scala say that the
PipeExpression is stripped once the generator's arguments are resolved, not
once the generator itself is resolved.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/pipeOperators.scala:
##########
@@ -62,28 +63,46 @@ 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 that ExtractGenerator plans later (see
+ * `isResolvedGeneratorItem`).
*/
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 ||
isResolvedGeneratorItem(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 the generator items that ExtractGenerator plans but that
never report
+ * themselves resolved: an outer generator (GeneratorOuter is replaced by
ExtractGenerator), and
+ * a generator with a parenthesized alias list (MultiAlias is replaced by
ResolveAliases or
Review Comment:
**Nit (P3):** ResolveAliases never replaces a `MultiAlias`. It rewrites only
`UnresolvedAlias` nodes (Analyzer.scala:900-952), and `AliasResolution.resolve`
returns an existing `MultiAlias` unchanged (`case ne: NamedExpression => ne`,
AliasResolution.scala:59): for these `|> EXTEND` and `|> AGGREGATE` items it
only removes the `UnresolvedAlias` above the `MultiAlias`, and for an unaliased
generator it creates a `MultiAlias` rather than replacing one
(AliasResolution.scala:60, 62). ExtractGenerator is the only rule that replaces
a `MultiAlias`, when it plans `Generate` (Analyzer.scala:3439-3442).
This comment exists to explain which rules consume the wrappers the strip
keeps in place, so naming ResolveAliases could lead a maintainer to expect the
alias names to be consumed before ExtractGenerator plans the item. Wording such
as "MultiAlias is replaced by ExtractGenerator; ResolveAliases only removes the
UnresolvedAlias above it" fixes it.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60697#discussion_r4623888073).
**Verification:**
- **Inspection:** The readiness helper's scaladoc in pipeOperators.scala no
longer says ResolveAliases replaces MultiAlias and attributes the replacement
to ExtractGenerator, matching AliasResolution.resolve, which returns an
existing MultiAlias unchanged.
--
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]