cloud-fan commented on code in PR #60353:
URL: https://github.com/apache/spark/pull/60353#discussion_r4588608920


##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -83,7 +83,9 @@ options { tokenVocab = SqlBaseLexer; }
            la == ANTI || la == ASOF || la == JOIN || la == UNION || la == 
EXCEPT ||
            la == SETMINUS || la == INTERSECT || la == ORDER || la == CLUSTER ||
            la == DISTRIBUTE || la == SORT || la == LIMIT || la == OFFSET ||
-           la == AGGREGATE || la == WINDOW || la == LATERAL || la == BIN;
+           la == AGGREGATE || la == WINDOW || la == LATERAL || la == BIN ||
+           // GAP starts a pipe step only before FILL (, so `a | gap` stays a 
bitwise OR.
+           (la == GAP && _input.LA(3) == FILL && _input.LA(4) == LEFT_PAREN);

Review Comment:
   **Blocking (P1):** This check returns true as soon as the three tokens after 
`|` are GAP, FILL and `(`, and the bitwise-OR alternative of `valueExpression` 
(line 1704) is disabled whenever it returns true. Those three tokens also occur 
in SQL that parses on the base branch: an inline-table row ending in `| gap`, 
followed by a table alias `fill` written without AS and with a column list.
   
   In a repro with ANTLR 4.13.1 parsers generated from the base and head 
grammars and driven like Spark's two-stage parser, `SELECT * FROM VALUES 1 | 
gap fill(a)`, `VALUES 1 | gap fill(a)`, `INSERT INTO t VALUES 1 | gap fill(a)`, 
`INSERT INTO t SELECT * FROM VALUES 1 | gap fill(a)` and the same shape with 
two rows or a two-column list parse in the first stage on the base branch and 
fail at head with a syntax error at the `|`, in both keyword modes. The same 
text with `AS fill(a)`, without the column list, or with the row in parentheses 
still parses at head. Because session variables are accepted in VALUES, such a 
statement runs on the base branch when gap is a variable, and the v4.2.1 and 
v4.3.0 releases parse it the same way, since their pipe-start check has no GAP 
entry. At head it fails whenever 
`spark.sql.parser.singleCharacterPipeOperator.enabled`, which is public and on 
by default, is set, and the grammar is not behind the GAP FILL conf. The PR 
description says statements witho
 ut GAP FILL parse as before, and the tests at PlanParserSuite.scala lines 
3785-3786 check `SELECT 1 | gap` and `SELECT a | gap fill` only without a 
column list.
   
   A GAP FILL clause always continues with PARTITION BY or ORDER BY after the 
parenthesis, and BY can never be the second token of a column list, so also 
checking the next two tokens tells the two cases apart:
   
   ```
              (la == GAP && _input.LA(3) == FILL && _input.LA(4) == LEFT_PAREN 
&&
               (_input.LA(5) == PARTITION || _input.LA(5) == ORDER) && 
_input.LA(6) == BY);
   ```
   
   In the same repro, a parser with this check parses all six statements as on 
the base branch and still parses both `| GAP FILL (...)` pipe forms. 
Single-pipe text such as `FROM t | GAP FILL (BIN WIDTH 1)`, which is invalid 
either way, would then be reported at the `|` as on the base branch rather than 
inside the clause. The comment above the check could mention the inline-table 
alias case too.
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/60353#discussion_r4557248399).
   
   **Verification:**
   - **Regression:** With the single-character pipe enabled, in both keyword 
modes, an inline table whose last row ends in `| gap` followed by the alias 
fill with a one-column and a two-column list parses, on its own, inside a FROM 
clause and under INSERT, to the same plan as on the base branch, with no GAP 
FILL node; in the default keyword mode this also holds for a column list whose 
first name is order or partition, and bitwise OR with a column named gap, with 
or without a following alias named fill, still builds no GAP FILL node.
   - **Behavior:** With the single-character pipe enabled, `| GAP FILL (...)` 
steps that start with ORDER BY and with PARTITION BY, on their own and followed 
by another pipe step, still parse to the same plans as the `|>` spelling.



##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -1331,6 +1367,7 @@ relationExtension
     | pivotClause
     | unpivotClause
     | binByClause
+    | gapFillClause

Review Comment:
   **Nit (P3):** Because GAP can now start a clause after a relation (this 
relationExtension alternative) and a pipe step (SqlBaseParser.g4 line 2435 and 
the single-pipe check at line 88), Spark's first (SLL) parsing stage has to 
look past `gap` wherever it is used as a name in a position where `GAP FILL (` 
could also begin. In a repro with parsers generated from the base and head 
grammars, summed first-stage lookahead grows by 2 tokens for gap as a table 
alias with or without AS, the right operand of `|`, a BETWEEN bound and the 
first word of a WHERE condition, and by 4 for `SELECT a | gap fill FROM t`, in 
both keyword modes, with no LL retry. gap as a select-list column, select 
alias, function name or table name costs nothing extra, and the same statements 
with gq are unchanged. Each occurrence costs well under a microsecond and parse 
results do not change.
   
   This is the same cost BIN already carries on the base branch, where `FROM t 
bin` takes the same 2 extra tokens, and reserving GAP, the only way to avoid 
it, would break queries that use gap as a name. The PR description, however, 
says statements without GAP FILL parse as before. Consider stating there that 
using gap as a name in these positions takes a few more tokens of first-stage 
lookahead with unchanged results, and that GAP stays non-reserved because 
reserving it would break existing names.
   
   **Verification:**
   - **Inspection:** The PR description says that using gap as a name where a 
GAP FILL clause could start, such as a table alias or the right operand of `|`, 
costs a few more tokens of first-stage parser lookahead per occurrence with 
unchanged parse results, names reserving GAP as the alternative that would 
avoid it, and says why the PR rejects that alternative.



-- 
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]

Reply via email to