spark-test commented on code in PR #60697:
URL: https://github.com/apache/spark/pull/60697#discussion_r4623888068
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/pipeOperators.scala:
##########
@@ -70,11 +70,19 @@ case object ValidateAndStripPipeExpressions extends
Rule[LogicalPlan] {
_.containsPattern(PIPE_EXPRESSION), ruleId) {
case node: LogicalPlan =>
node.resolveExpressions {
- case p: PipeExpression if p.child.resolved =>
+ case p: PipeExpression if p.child.resolved ||
isResolvedOuterGenerator(p.child) =>
validateAndStripPipeExpression(p, p.child)
}
}
+ // GeneratorOuter is never resolved because ExtractGenerator replaces it.
Strip the wrapper once
+ // the inner generator is resolved; otherwise ExtractGenerator sees
+ // PipeExpression(GeneratorOuter(...)) as a generator nested inside another
expression.
+ private def isResolvedOuterGenerator(e: Expression): Boolean = e match {
+ case GeneratorOuter(g) => g.resolved
Review Comment:
**Non-blocking (P2):** This arm only matches a GeneratorOuter that is the
PipeExpression's direct child, so `|> EXTEND` and `|> AGGREGATE` items that
name an outer generator's output columns with a parenthesized alias list still
fail. The AstBuilder visitors for those two clauses wrap every item other than
a single-name alias whole (AstBuilder.scala:8467-8469 and 8575-8577), so
`posexplode_outer(tags) AS (pos, tag)` reaches this rule as
`UnresolvedAlias(PipeExpression(MultiAlias(GeneratorOuter(PosExplode(tags)),
[pos, tag])))`. `MultiAlias.resolved` is always false (unresolved.scala:983),
so neither side of the guard at pipeOperators.scala:73 holds, the marker is
never removed, and ExtractGenerator rejects the item with
`UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS` (Analyzer.scala:3453-3459).
Reading the code (the review did not run it): `table outer_gen_test |>
extend posexplode_outer(tags)` now analyzes, but `table outer_gen_test |>
extend posexplode_outer(tags) as (pos, tag)` and `table outer_gen_test |>
aggregate posexplode_outer(collect_list(id)) as (pos, id) group by author`
still fail. The same holds for `explode_outer` over a map and `inline_outer`
over structs with several fields. Plain SQL accepts this form: generators.sql
runs `SELECT posexplode_outer(array(a, b)) AS (elem, elem2) FROM testData`, and
its golden output has rows. The PR description also says such EXTEND and
AGGREGATE items now return the plain SQL rows. `|> SELECT` is unaffected
because it leaves multi-name alias items unwrapped
(AstBuilder.scala:1968-1974), and `|> SET` takes no alias. The non-outer form,
such as `posexplode(tags) AS (pos, tag)`, fails the same way on the base
branch. None of the new golden cases uses a multi-name alias.
**Recommended change:** In ValidateAndStripPipeExpressions, extend the strip
condition (the guard at pipeOperators.scala:73 and its helper at
pipeOperators.scala:81-84) so that a PipeExpression whose child is a multi-name
alias over a resolved generator is also stripped. Accept a MultiAlias over
GeneratorOuter(g) and a MultiAlias over a non-outer generator g once g is
resolved; these are the shapes ExtractGenerator's AliasedGenerator already
plans (Analyzer.scala:3439-3442). Keep the aggregate validation running on the
stripped child, and keep GeneratorOuter under the alias. Add golden cases to
pipe-operators.sql for |> EXTEND and |> AGGREGATE items with a parenthesized
alias list over posexplode_outer, over explode_outer on a map, over
inline_outer on structs with several fields and over non-outer posexplode, plus
three negative cases: such an item over an aggregate in |> EXTEND, one without
an aggregate function in |> AGGREGATE, and one in |> EXTEND whose generator is
nested ins
ide another expression, such as upper(explode_outer(tags)) AS (a, b). Then
regenerate the analyzer and execution golden files.
**Why this works:** ExtractGenerator already plans a multi-name alias over a
resolved generator, outer or not. The only obstacle is the PipeExpression that
the |> EXTEND and |> AGGREGATE parser branches put around the multi-name alias:
the rule never removes it, because a MultiAlias never reports itself resolved.
Once the strip condition recognizes that shape, the PipeExpression comes off in
the same analyzer iteration in which ResolveFunctions builds the generator,
after the aggregate checks have run on it. Later in that iteration
ResolveAliases replaces the unresolved alias that the parser put around the
item with the multi-name alias itself, so ExtractGenerator's next pass sees the
same expression shape a plain SQL query produces and builds Generate, with the
outer flag for the *_outer functions. While the unresolved alias is still
there, ExtractGenerator neither rejects nor plans the item: its
nested-generator check drops the multi-name alias and finds a bare generator,
and it
s rewrites only match a generator directly under an Alias or MultiAlias, so
the item waits for the next iteration.
**Compatibility:** Queries that analyze today keep their plans and results.
|> SELECT multi-name alias items, which the parser never wraps, are untouched.
A generator whose argument fails its type check keeps its current error.
**Risks:** Non-outer multi-name alias generator items in |> EXTEND and |>
AGGREGATE, such as `|> EXTEND posexplode(tags) AS (pos, tag)` or `|> EXTEND
inline(arr) AS (a, b)`, start analyzing to the plan plain SQL gives, although
they failed with the nested-generator error on the base branch. This is kept
deliberately so outer and non-outer generators behave alike. Only queries that
fail today change, and reverting the added cases in the strip condition rolls
it back.
**Constraints:** The strip condition must keep requiring the generator
itself to be resolved, as ResolveAliases and ExtractGenerator do. That keeps
the aggregate checks running on resolved arguments and leaves a type-mismatched
generator with its current error. GeneratorOuter must stay under the alias
after the PipeExpression is removed, so the *_outer functions keep producing
Generate with the outer flag and their NULL rows.
**Exclusions:**
- Multi-name aliases over expressions that are not generators keep their
current behavior, because the strip condition still declines that shape.
- The parser's wrapping of pipe items stays unchanged, in line with the PR's
decision to fix the failure in the stripping rule.
- Reporting a type-mismatch error instead of the nested-generator error for
a generator with a wrongly typed argument in a pipe clause is left out; outer
and non-outer generators share that behavior on the base branch.
**Verification:**
- **Regression:** |> EXTEND and |> AGGREGATE items that name the output
columns of posexplode_outer, of explode_outer over a map, or of inline_outer
over structs with several fields with a parenthesized alias list, such as
posexplode_outer(tags) AS (pos, tag), no longer fail with
UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS: they analyze to Generate with the
outer flag and return the plain SQL rows, including the NULL rows for empty and
NULL inputs.
- **Behavior:** The same items written with non-outer posexplode analyze and
return the plain SQL rows in both |> EXTEND and |> AGGREGATE.
- **Behavior:** A multi-name alias outer generator over an aggregate
function in |> EXTEND fails with PIPE_OPERATOR_CONTAINS_AGGREGATE_FUNCTION, and
one without an aggregate function in |> AGGREGATE fails with
PIPE_OPERATOR_AGGREGATE_EXPRESSION_CONTAINS_NO_AGGREGATE_FUNCTION.
- **Behavior:** A multi-name alias over a generator nested inside another
expression, such as upper(explode_outer(tags)) AS (a, b) in |> EXTEND, is still
rejected with UNSUPPORTED_GENERATOR.NESTED_IN_EXPRESSIONS.
- **Inspection:** Regenerating the pipe-operators analyzer and execution
golden files changes nothing but the added cases, so no existing pipe query
changes its plan or result.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/pipeOperators.scala:
##########
@@ -70,11 +70,19 @@ case object ValidateAndStripPipeExpressions extends
Rule[LogicalPlan] {
_.containsPattern(PIPE_EXPRESSION), ruleId) {
case node: LogicalPlan =>
node.resolveExpressions {
- case p: PipeExpression if p.child.resolved =>
+ case p: PipeExpression if p.child.resolved ||
isResolvedOuterGenerator(p.child) =>
validateAndStripPipeExpression(p, p.child)
}
}
+ // GeneratorOuter is never resolved because ExtractGenerator replaces it.
Strip the wrapper once
Review Comment:
**Nit (P3):** Because the sentence before it names GeneratorOuter, "Strip
the wrapper once the inner generator is resolved" reads as removing
GeneratorOuter, which wraps "the inner generator". The code removes the
PipeExpression and must keep GeneratorOuter, because ExtractGenerator reads it
to set `outer = true` (Analyzer.scala:3439-3440). Both nodes are called
wrappers in this area: generators.scala:332 calls GeneratorOuter one, and the
scaladoc at pipeOperators.scala:88 says the method strips "the
[[PipeExpression]] wrapper". Only the last clause of the comment points to the
PipeExpression. Wording such as "Strip the PipeExpression, keeping
GeneratorOuter, once the generator inside it is resolved" removes the ambiguity.
The same branch also makes two unchanged scaladocs inaccurate. The rule's
scaladoc (pipeOperators.scala:64-66) says it strips PipeExpression nodes once
"the child expressions are resolved", and the scaladoc of
validateAndStripPipeExpression (pipeOperators.scala:86-94) calls its argument
"The resolved child expression". The new branch passes GeneratorOuter(g), whose
`resolved` is always false (generators.scala:344). The method does not rely on
that today, but a later change that trusts the documented precondition could
mishandle this input.
**Verification:**
- **Inspection:** In pipeOperators.scala, the comment on the strip
condition's helper (isResolvedOuterGenerator today) and the scaladoc of
ValidateAndStripPipeExpressions and validateAndStripPipeExpression name the
PipeExpression as the node removed, say GeneratorOuter is kept, and describe
every child shape the strip condition accepts, including an outer generator
that is never resolved itself.
--
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]