cloud-fan commented on code in PR #60620:
URL: https://github.com/apache/spark/pull/60620#discussion_r4603774126
##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -1308,7 +1308,7 @@ binByClause
(BIN_START AS binStartAlias=errorCapturingIdentifier)?
(BIN_END AS binEndAlias=errorCapturingIdentifier)?
(BIN_DISTRIBUTE_RATIO AS binRatioAlias=errorCapturingIdentifier)?
- RIGHT_PAREN (AS? tblAlias=errorCapturingIdentifier)?
+ RIGHT_PAREN (AS? tblAlias=strictIdentifier)?
Review Comment:
**Non-blocking (P2):** `pivotClause` (SqlBaseParser.g4 line 1240) and
`unpivotClause` (line 1255) still end with the `(AS?
errorCapturingIdentifier)?` that this line used to have, so in the default
keyword mode they still take a following join keyword as their alias. A parser
repro built from this grammar shows `SELECT * FROM t PIVOT (sum(x) FOR y IN (1,
2)) LEFT JOIN u ON t.k = u.k` parsing as a PIVOT aliased `LEFT` followed by a
join with no join type. RIGHT, FULL, INNER, CROSS, LEFT SEMI, LEFT ANTI, and
NATURAL [LEFT] JOIN do the same, as do the same joins after UNPIVOT. The OUTER
spellings and the ANSI keyword mode parse correctly.
So the query silently changes. LEFT, RIGHT, and FULL JOIN become inner
joins, or fail to resolve when the condition qualifies the left side by its
original name. NATURAL is dropped. INNER, CROSS, SEMI, and ANTI keep their
meaning but rename the pivot output to the keyword. PIVOT and UNPIVOT are
documented and on by default, so this reaches more users than the BIN BY case
this PR fixes. Changing these two rules the same way would also reject aliases
that released versions accept, such as `PIVOT (...) AS left` (UNPIVOT since
3.4.0, PIVOT since 4.2.0), which is why the suggested fix adds a legacy
setting. If you would rather keep this PR to BIN BY, a TODO at both rules that
names the remaining cases also addresses this.
**Recommended change:** Change the trailing alias of pivotClause and
unpivotClause from errorCapturingIdentifier to strictIdentifier, the rule BIN
BY, GAP FILL, and table aliases use, and update withPivot and withUnpivot in
AstBuilder to read the new alias. PIVOT and UNPIVOT are documented, on by
default, and accept these keyword aliases in released versions (UNPIVOT since
3.4.0, PIVOT since 4.2.0). So also add an internal spark.sql.legacy.* SQL
configuration, default false, whose true value restores the old alias rule for
these two clauses, wired into the parser like the existing legacy parser
settings, plus a migration-guide entry that describes the change, the quoting
workaround, and the configuration. Add parser tests for PIVOT and UNPIVOT
followed by each affected join form, covering both keyword modes and the legacy
configuration on and off.
**Why this works:** strictIdentifier does not admit LEFT, RIGHT, FULL,
INNER, CROSS, NATURAL, SEMI, or ANTI in the default keyword mode, so a join
keyword after PIVOT or UNPIVOT has only one valid parse, the start of the join,
and the parser keeps the written join type. strictIdentifier is the branch of
the old rule that carries ordinary, non-reserved, quoted, and IDENTIFIER('...')
names, so those aliases keep parsing as before. The legacy configuration
re-enables the old alternative only when it is set.
**Risks:** Unquoted strict-non-reserved aliases on PIVOT and UNPIVOT, such
as `PIVOT (...) AS left`, stop parsing in the default keyword mode, including
in persisted view text. The legacy configuration rolls this back. A hyphenated
PIVOT or UNPIVOT alias such as `AS a-b` reports a generic syntax error instead
of INVALID_IDENTIFIER, as table aliases already do. Queries that relied on the
old mis-parse, an inner or condition-less join, now get the join they wrote, so
their results change; that is the intended correction.
**Constraints:** The legacy configuration's default, false, gives the
corrected parse, and its true value matches the base branch's PIVOT and UNPIVOT
alias behavior exactly. In the ANSI keyword mode (spark.sql.ansi.enabled and
spark.sql.ansi.enforceReservedKeywords both true), every PIVOT and UNPIVOT
query parses as on the base branch, except that a hyphenated alias such as `AS
a-b` fails with a syntax error instead of INVALID_IDENTIFIER, as in the default
keyword mode. The per-value aliases inside PIVOT and UNPIVOT keep their current
rule.
**Exclusions:**
- Whether the BIN BY alias change also gets a rollback setting is a separate
open decision and is not part of this repair.
- Moving all relation-extension aliases into one shared grammar rule is not
needed to close the defect.
**Verification:**
- **Regression:** In both keyword modes, parsing a PIVOT or UNPIVOT clause
without an alias followed by each affected join form (LEFT, RIGHT, FULL, INNER,
CROSS, LEFT SEMI, LEFT ANTI, NATURAL, and NATURAL LEFT JOIN) yields the written
join type, with the PIVOT or UNPIVOT node rather than an alias as the join's
left side.
- **Behavior:** Ordinary, quoted keyword, and IDENTIFIER('...') aliases on
PIVOT and UNPIVOT still produce a SubqueryAlias over the PIVOT or UNPIVOT node,
including when the alias is written before a join keyword.
- **Compatibility:** An unquoted strict-non-reserved alias on PIVOT and
UNPIVOT fails to parse with the legacy configuration at its default, and parses
with that alias, as on the base branch, when the configuration is true.
--
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]