GeezerVison is showing github.com — [plsql] Parsing exception EXECUTE IMMEDIATE l_sql BULK COLLECT INTO statement · Issue #3687 · pmd/pmd
[plsql] Parsing exception EXECUTE IMMEDIATE l_sql BULK COLLECT INTO statement #3687Labels a:bugPMD crashes or fails to analyse a file. DescriptionAffects PMD Version:
This error was testing in the following versions:
Description:
Trying to analyze pl/sql code that has EXECUTE IMMEDIATE l_sql BULK COLLECT INTO statement, PMD raises an exception error that indicates "Error while parsing".
Exception Stacktrace:
net.sourceforge.pmd.PMDException: Error while parsing C:\testfolder\example_procedure.prc
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCodeWithoutCache(SourceCodeProcessor.java:124)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:100)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:62)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:85)
at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:29)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: net.sourceforge.pmd.lang.plsql.ast.ParseException: Encountered " "BULK" "BULK "" at line 26, column 36.
Was expecting one of:
";" ...
"||" ...
"INTO" ...
"RETURN" ...
"RETURNING" ...
"USING" ...
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.generateParseException(PLSQLParser.java)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.jj_consume_token(PLSQLParser.java)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.EmbeddedSqlStatement(PLSQLParser.java:36684)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.UnlabelledStatement(PLSQLParser.java:23959)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.Statement(PLSQLParser.java:24754)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.ProgramUnit(PLSQLParser.java:6835)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.Global(PLSQLParser.java:879)
at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.Input(PLSQLParser.java:153)
at net.sourceforge.pmd.lang.plsql.PLSQLParser.parse(PLSQLParser.java:61)
at net.sourceforge.pmd.lang.AbstractParser.doParse(AbstractParser.java:44)
at net.sourceforge.pmd.SourceCodeProcessor.parse(SourceCodeProcessor.java:136)
at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:200)
at net.sourceforge.pmd.SourceCodeProcessor.processSourceCodeWithoutCache(SourceCodeProcessor.java:118)
... 10 more
Code Sample demonstrating the issue:
CREATE OR REPLACE PROCEDURE EXAMPLE_PROCEDURE IS
--
TYPE r_data IS RECORD(
data_key NUMBER(10),
data_description VARCHAR2(100));
--
TYPE t_data IS TABLE OF r_data INDEX BY BINARY_INTEGER;
--
l_data t_data;
l_sql VARCHAR2(500);
--
BEGIN
--
l_sql := 'SELECT 1, ' || CHR(39) || 'First key' || CHR(39) ||
' FROM DUAL '||
'UNION ALL '||
'SELECT 2, ' || CHR(39) || 'Second key ' || CHR(39) ||
' FROM DUAL';
--
EXECUTE IMMEDIATE l_sql BULK COLLECT
INTO l_data;
--
FOR x IN l_data.first .. l_data.last LOOP
DBMS_OUTPUT.Put_Line(l_data(x).data_key);
DBMS_OUTPUT.Put_Line(l_data(x).data_description);
END LOOP;
--
END EXAMPLE_PROCEDURE;
Steps to reproduce:
- Put the code above ("Code Sample demonstrating the issue" section) in example_procedure.prc
- Execute PMD analyzer from CLI.
- See the error showed in the "Exception Stacktrace" section.
Running PMD through: [CLI] ActivityScrsloota commented on Apr 22, 2022 I'm new here, and I want to contribute to this. Could you please assign this to me? 😊 adangel commented on Apr 22, 2022 Scrsloota commented on Apr 23, 2022 I am really sorry for this!😭 I clicked the wrong place and removed the assgin. Could you please assign this to me again?🥺 Scrsloota commented on Apr 23, 2022
-
Considering the sample code, the main problem happens in:
BEGIN
--
l_sql := 'SELECT 1, ' || CHR(39) || 'First key' || CHR(39) ||
' FROM DUAL '||
'UNION ALL '||
'SELECT 2, ' || CHR(39) || 'Second key ' || CHR(39) ||
' FROM DUAL';
--
EXECUTE IMMEDIATE l_sql BULK COLLECT
INTO l_data;
--
FOR x IN l_data.first .. l_data.last LOOP
DBMS_OUTPUT.Put_Line(l_data(x).data_key);
DBMS_OUTPUT.Put_Line(l_data(x).data_description);
END LOOP;
--
END EXAMPLE_PROCEDURE;
-
The exception message reported in this statement :
EXECUTE IMMEDIATE l_sql BULK COLLECT
INTO l_data;
-
The related BNF for parsing the EXECUTE IMMEDIATE is :
ASTEmbeddedSqlStatement EmbeddedSqlStatement() :
{}
{
<EXECUTE> <IMMEDIATE> StringExpression()
[ <INTO> Name() ("," Name())* ]
[ UsingClause() ]
[ ( <RETURN> | <RETURNING> ) <INTO> Expression() ("," Expression())*] ";"
// PIPELINED FUNCTION OUTPUT
{ return jjtThis ; }
}
These three productions are unable to parse BULK COLLECT INTO l_data.
This method only supported for something like : INTO, RETURN,RETURNING, USING
-
When searching in the PldocAST.jjt, I found that there is a method called BulkCollectIntoClause() can parse BULK COLLECT INTO l_data
ASTBulkCollectIntoClause BulkCollectIntoClause() :
{}
{
<BULK> <COLLECT> <INTO> BulkCollectIntoClauseEntry() ("," BulkCollectIntoClauseEntry())*
{ return jjtThis; }
}
void BulkCollectIntoClauseEntry() #void :
{}
{
CollectionName() | ":" HostArrayName()
}
ASTCollectionName CollectionName() :
{ ASTID id; }
{
id = ID() {jjtThis.setImage(id.getImage());}
{ return jjtThis; }
}
-
In order to fix this issue, I edit the EmbeddedSqlStatement() by invoking the BulkCollectIntoClause():
ASTEmbeddedSqlStatement EmbeddedSqlStatement() :
{}
{
<EXECUTE> <IMMEDIATE> StringExpression()
[ LOOKAHEAD(<BULK>) BulkCollectIntoClause() ]
//#3687 Invoke this method to parse "BULK COLLECT"
[ <INTO> Name() ("," Name())* ]
[ UsingClause() ]
[ ( <RETURN> | <RETURNING> ) <INTO> Expression() ("," Expression())*] ";"
// PIPELINED FUNCTION OUTPUT
{ return jjtThis ; }
}
MetadataMetadataa:bugPMD crashes or fails to analyse a file. DevelopmentNo branches or pull requestsIssue actions
|