Firstly, consider this code:
CREATE OR REPLACE PROCEDURE EXAMPLE_PROCEDURE IS
CURSOR c_example IS
SELECT a.owner, u.object_name, p.aggregate
FROM ((USER_OBJECTS u INNER JOIN ALL_OBJECTS a ON
u.object_name = a.object_name AND u.object_type = a.object_type AND u.object_id = a.object_id)
INNER JOIN ALL_PROCEDURES p ON
p.owner = a.owner AND p.object_name = a.object_name AND p.object_type = a.object_type)
WHERE a.owner = USER;
The related BNF for parsing this code is:
// FromClause -> <FROM> FromClauseEntry() ("," FromClauseEntry())*
void FromClauseEntry() #void :
{}
{
(
LOOKAHEAD([ SchemaName() "." ] TableName() [<IDENTIFIER>]
(<PARTITION>|<NATURAL>|<OUTER>|<INNER>|<JOIN>|<LEFT>|<RIGHT>|<CROSS>|<FULL>)) JoinClause()
|
LOOKAHEAD(<IDENTIFIER> "(", {isKeyword("XMLTABLE")}) XMLTable() [ LOOKAHEAD(2) TableAlias() ]
|
LOOKAHEAD(3) (TableReference() (JoinClausePart())*) #JoinClause(>1)
|
"(" JoinClause() ")"
)
}
Here, the parser will parse the following part as the JoinClause choosing the fourth production "(" JoinClause() ")"
(USER_OBJECTS u INNER JOIN ALL_OBJECTS a ON
u.object_name = a.object_name AND u.object_type = a.object_type AND u.object_id = a.object_id)
INNER JOIN ALL_PROCEDURES p ON
p.owner = a.owner AND p.object_name = a.object_name AND p.object_type = a.object_type
This JoinClause() is consists of two tables:
--First table
(USER_OBJECTS u INNER JOIN ALL_OBJECTS a ON
u.object_name = a.object_name AND u.object_type = a.object_type AND u.object_id = a.object_id)
--Second table
ALL_PROCEDURES p
--This two tables are operands of INNER JOIN operation
And the parser will choose the following BNF to parse it:
ASTJoinClause JoinClause() :
{}
{
TableReferenceInJoin()
(JoinClausePart())+
{ return jjtThis; }
}
Here in TableReferenceInJoin(), the parser will call QueryTableExpression():
void QueryTableExpression() #void :
{}
{
(
LOOKAHEAD(2) [ LOOKAHEAD(2) SchemaName() "." ] TableName()
|
TableCollectionExpression()
|
[ <LATERAL> ] "(" Subquery() [ SubqueryRestrictionClause() ] ")"
)
}
Here the parser will choose the third production, becase the "(" matches the first part of the JoinClause.
However, in Subquery():
void Subquery() #void :
{}
{
(
QueryBlock() (LOOKAHEAD(2) ( SubqueryOperation() ) Subquery() )*
| "(" Subquery() ")"
)
[ LOOKAHEAD(2) OrderByClause() ]
[ LOOKAHEAD(2) RowLimitingClause() ]
}
It only supports something like:SELECT * FROM SOMETABLE TABLEALIAS instead of SOMETABLE TABLEALIAS. So the parsing exception occurs.
The following code will also report the parsing exception, the reason is the same as above:
CREATE OR REPLACE PROCEDURE EXAMPLE_PROCEDURE IS
CURSOR c_example IS
SELECT a.owner, u.object_name, p.aggregate
FROM USER_OBJECTS u INNER JOIN ALL_OBJECTS a ON
u.object_name = a.object_name AND u.object_type = a.object_type AND u.object_id = a.object_id)
-- Add parenthesis here
INNER JOIN (ALL_PROCEDURES p) ON
p.owner = a.owner AND p.object_name = a.object_name AND p.object_type = a.object_type
WHERE a.owner = USER;
To fix this issue, I edit the QueryTableExpression()
void QueryTableExpression() #void :
{}
{
(
LOOKAHEAD(2) [ LOOKAHEAD(2) SchemaName() "." ] TableName()
|
TableCollectionExpression()
|
// Add this production to parse "(ALL_PROCEDURES p)"
LOOKAHEAD(5) "(" [ LOOKAHEAD(2) SchemaName() "." ] TableName() [TableAlias()] ")"
|
// Add LOOKAHEAD(3) to resolve conficts between the following two productions
LOOKAHEAD(3) [ <LATERAL> ] "(" Subquery() [ SubqueryRestrictionClause() ] ")"
// Add this production to parse "(TABLE_A INNER JOIN TABLE_B)" like the bug code listed at the beginning
|
LOOKAHEAD(3) "(" JoinClause() ")"
)
}