[ 
https://issues.apache.org/jira/browse/CALCITE-5206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17564660#comment-17564660
 ] 

Abhishek Dasgupta edited comment on CALCITE-5206 at 7/10/22 10:10 AM:
----------------------------------------------------------------------

The bug exisits due to optional consideration of 
[Left|https://github.com/apache/calcite/blob/724eb032d0141c15d17422d50c5235be00ac989f/core/src/main/codegen/templates/Parser.jj#L1886]
 and 
[Right|https://github.com/apache/calcite/blob/724eb032d0141c15d17422d50c5235be00ac989f/core/src/main/codegen/templates/Parser.jj#L1889]
 parenthesis. This results in the incorrect query getting parsed due to 
existence of either of them. 

I think we should lookahead whether for a left parenthesis, a right parenthesis 
exists or not.

 
{code:java}
(
    LOOKAHEAD(4)
    <LPAREN>
    <VALUES> { valuesSpan = span(); }
    rowConstructor = RowConstructor()
    <RPAREN>
|
    <VALUES> { valuesSpan = span(); } rowConstructor = RowConstructor()
) {code}
 

This change results in the parse exception of the above two queries with 
correct exception message:

query#1:
{code:java}
Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered "<EOF>" 
at line 4, column 33.
Was expecting:
    ")" ... {code}
query#2:
{code:java}
Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered ")" at 
line 4, column 33.
Was expecting:
    <EOF>  {code}
Contributers, please let me know your suggestions, so that I assign this to 
myself and proceed to create a PR. 

 


was (Author: abhishek.dasgupta):
The bug exisits due to optional consideration of 
[Left|https://github.com/apache/calcite/blob/724eb032d0141c15d17422d50c5235be00ac989f/core/src/main/codegen/templates/Parser.jj#L1886]
 and 
[Right|https://github.com/apache/calcite/blob/724eb032d0141c15d17422d50c5235be00ac989f/core/src/main/codegen/templates/Parser.jj#L1889]
 parenthesis. This results in the incorrect query getting parsed due to 
existance of either of them. 

I think we should lookahead whether for a left parenthesis, a right parenthesis 
exists or not.

 
{code:java}
(
    LOOKAHEAD(4)
    <LPAREN>
    <VALUES> { valuesSpan = span(); }
    rowConstructor = RowConstructor()
    <RPAREN>
|
    <VALUES> { valuesSpan = span(); } rowConstructor = RowConstructor()
) {code}
 

This change results in the parse exception of the above two queries with 
correct exception message:

query#1:
{code:java}
Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered "<EOF>" 
at line 4, column 33.
Was expecting:
    ")" ... {code}
query#2:
{code:java}
Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered ")" at 
line 4, column 33.
Was expecting:
    <EOF>  {code}
Contributers, please let me know your suggestions, so that I assign this to 
myself and proceed to create a PR. 

 

> Parser allows MERGE with mismatched parentheses
> -----------------------------------------------
>
>                 Key: CALCITE-5206
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5206
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Julian Hyde
>            Priority: Major
>
> The SQL parser allows invalid MERGE statements with mismatched parentheses. 
> For example, the following invalid statement is treated as valid but is 
> missing a trailing ')':
> {code}
> merge into emps as e
> using temps as t on e.empno = t.empno
> when not matched
> then insert (a, b) (values (1, 2);
> {code}
> And the following invalid statement is treated as valid but has an unmatched 
> trailing ')':
> {code}
> merge into emps as e
> using temps as t on e.empno = t.empno
> when not matched
> then insert (a, b) values (1, 2));
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to