GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [plsql] Parsing exception CURSOR statement with parenthesis groupings · Issue #3706 · pmd/pmd

[plsql] Parsing exception CURSOR statement with parenthesis groupings #3706

New issue
Copy link
New issue
Copy link
Closed
#3928
Closed
[plsql] Parsing exception CURSOR statement with parenthesis groupings#3706
#3928
Copy link
Assignees
LiGaOg
Labels
a:bugPMD crashes or fails to analyse a file.in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception
Milestone
6.45.0

Description

@hgodinez89
hgodinez89
opened on Dec 29, 2021
Issue body actions

Affects PMD Version:

This error was testing in the following versions:

  • 6.41.0
  • 6.40.0
  • 6.39.0

Description:

Trying to analyze pl/sql code that has CURSOR statement with parenthesis groupings, PMD raises an exception error that indicates "Error while parsing".

Note: The purpose of parenthesis groupings is only for the purpose of making the code more readable.

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 "  "USER_OBJECTS "" at line 5, column 16.
Was expecting one of:
    "(" ...
    "SELECT" ...
    "WITH" ...
    "WITH" ...
    "SELECT" ...
    "(" ...
	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.Subquery(PLSQLParser.java:10774)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.QueryTableExpression(PLSQLParser.java:22715)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.TableReferenceInJoin(PLSQLParser.java:23219)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.JoinClause(PLSQLParser.java:23341)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.FromClauseEntry(PLSQLParser.java:21545)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.FromClause(PLSQLParser.java:21447)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.RestOfStatement(PLSQLParser.java:10603)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.SelectStatement(PLSQLParser.java:28448)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.CursorUnit(PLSQLParser.java:38877)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.CursorSpecification(PLSQLParser.java:38919)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.DeclarativeUnit(PLSQLParser.java:2728)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.DeclarativeSection(PLSQLParser.java:3154)
	at net.sourceforge.pmd.lang.plsql.ast.PLSQLParser.ProgramUnit(PLSQLParser.java:6446)
	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
   --
   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;
   --
BEGIN
   --
   FOR l_object IN c_example LOOP
      --
      DBMS_OUTPUT.Put_Line(l_object.owner);
      DBMS_OUTPUT.Put_Line(l_object.object_name);
      DBMS_OUTPUT.Put_Line(l_object.aggregate);
      --
   END LOOP;
   --
END EXAMPLE_PROCEDURE;

Note: By removing all the parentheses in the example c_example cursor, you will not have parsing problems.

Steps to reproduce:

  1. Put the code above ("Code Sample demonstrating the issue" section) in example_procedure.prc
  2. Execute PMD analyzer from CLI.
  3. See the error showed in the "Exception Stacktrace" section.

Running PMD through: [CLI]

Activity

  1. hgodinez89
    added
    a:bugPMD crashes or fails to analyse a file.
    on Dec 29, 2021
  2. oowekyala
    added
    in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception
    on Feb 12, 2022
  3. LiGaOg commented on Apr 5, 2022

    @LiGaOg
    LiGaOg
    on Apr 5, 2022
    More actions

    Please assign this issue to me

  4. LiGaOg commented on Apr 19, 2022

    @LiGaOg
    LiGaOg
    on Apr 19, 2022
    More actions

    The main problem in this issue is as follows:

    • 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() ")"
        )
      }
  5. adangel
    assigned
    LiGaOg
    on Apr 21, 2022
  6. adangel
    added this to the 6.45.0 milestone on Apr 21, 2022
  7. adangel
    added a commit that references this issue on Apr 21, 2022

    [doc] Update release notes (#3706, #3928)

    ebbc017
  8. adangel
    closed this as completedin #3928on Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @LiGaOg
    LiGaOg

Labels

a:bugPMD crashes or fails to analyse a file.in:grammarAbout the grammar of a lexer or parser, eg, a parse/lex exception

Type

No type

Projects

No projects

    Milestone

    • 6.45.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.