[ 
https://issues.apache.org/jira/browse/DERBY-2374?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773230#action_12773230
 ] 

Mamta A. Satoor commented on DERBY-2374:
----------------------------------------

Just wanted to post results of parser behavior when I push the consumption of ( 
further down in the derived table rules for the query below.
select c1 from ((select c1 from t1) union (select c1 from t2)) t1t2; 

For the query above, Derby currently chooses one of the following 2 rules for 
the string after the first "from" clause
1) <table reference> = <left paren> <table reference> <right paren>
2) <table reference> = <derived table> [AS] <correlation name>
The decision is based on the fact if there is a "select" clause following ( or 
not. If there is (Select combination, then Derby used the 2nd rule. Based on 
this, since right after the first "from" clause, we just have ( and not 
(select, we choose rule 1). Next, the tokens are (select and we choose rule 2). 
The <derived table> leads to  consuming (select c1 from t1) and next we look 
for [AS] <correlation name> but what we have is UNION and hence parser error. I 
changed the rule for <derived table> such that if there is a UNION clause after 
the ), then we should continue handling UNION grammar. This was a progress 
because now <derived table> can consume (select c1 from t1) union (select c1 
from t2). But after this, we look for [AS] <correlation name> again but what we 
get is the last ) for the query above. This ) would have been consumed by rule 
1 which was applied first but we are never going to get to that point because 
we are still working on the <derived table> grammar and that grammar does not 
see [AS] <correlation name> and because of that parser throws an exception for 
missing correlation name even with my changes. I think what is happeneing is we 
are incorrectly identifying the queries around UNION clause to be not derived 
tables. Changing that might fix the problem but at this point, I am not sure 
how involved that is. 

Also, there is following piece of code in the parser for tableFactor
        // There is a grammar ambiguity with nested parentheses here.
        // A series of left parentheses could introduce either a table
        // reference or a derived table.  For example:
        //
        //              (((select c from t) a inner join (select d from s) b ))
        //
        // and:
        //
        //              (((select c from t) a)))
        //
        // To distinguish these two cases, we consider anything that starts
        // with a single parenthesis and either SELECT or VALUES to be
        // a derived table, and anything else to be a table reference.
        // Note that we can't use the subqueryFollows() lookahead method,
        // because it skips over all leading left parentheses to decide
        // whether a subquery follows.
        LOOKAHEAD( {
                        getToken(1).kind == LEFT_PAREN &&
                        (
                                getToken(2).kind == SELECT ||
                                getToken(2).kind == VALUES
                        )
                } )
        derivedTable = derivedTable() [ <AS> ] correlationName = 
identifier(Limits.MAX_IDENTIFIER_LENGTH, true)
                [ <LEFT_PAREN> derivedRCL = derivedColumnList() <RIGHT_PAREN> ]
                [ optionalTableClauses = optionalTableProperties() ]
I tried writing a query using inner join to see how this code path is used but 
couldn't quite come up with a query satisfying the comment 
        //              (((select c from t) a inner join (select d from s) b ))
It will be interesting to see what query are we trying to solve by this special 
code as per the comments.

> UNION PROBLEM WITH PARENTHESIS
> ------------------------------
>
>                 Key: DERBY-2374
>                 URL: https://issues.apache.org/jira/browse/DERBY-2374
>             Project: Derby
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 10.1.3.3, 10.2.2.0, 10.3.3.1, 10.4.2.1, 10.5.3.1, 
> 10.6.0.0
>            Reporter: Kenneth Gee
>            Priority: Minor
>
> The following query shows the error using the Derby demo toursDB:
> SELECT FLIGHT_ID , SEGMENT_NUMBER , ARRIVE_TIME
>   FROM (
>          (SELECT HW.FLIGHT_ID , HW.SEGMENT_NUMBER , HW.ARRIVE_TIME FROM 
> FLIGHTS HW)
>            UNION
>          (SELECT SW.FLIGHT_ID , SW.SEGMENT_NUMBER , SW.ARRIVE_TIME FROM 
> FLIGHTS SW)
>        ) SRC;
> ERROR 42X01: Syntax error: Encountered "UNION" at line 4, column 12.
> The following query works:
> SELECT FLIGHT_ID , SEGMENT_NUMBER , ARRIVE_TIME
>   FROM (
>          SELECT HW.FLIGHT_ID , HW.SEGMENT_NUMBER , HW.ARRIVE_TIME FROM 
> FLIGHTS HW
>           UNION
>          SELECT SW.FLIGHT_ID , SW.SEGMENT_NUMBER , SW.ARRIVE_TIME FROM 
> FLIGHTS SW
>        )  SRC;

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to