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] Add support for lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand by hvbtup · Pull Request #3161 · pmd/pmd

Conversation

@hvbtup

@hvbtup hvbtup commented Mar 5, 2021 •
edited by adangel
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Describe the PR

Support lexical parameters

A lot of the source codes analyzed by PMD language/pl-sql are actually SQL*Plus scripts.
That is, a mixture of SQL, DML and DDL commands and SQL*Plus commands.
Among the SQL*Plus features, the support for "lexical parameters" is problematic in particular (regarding parsing).
Those lexical parameters are very similar to environment variables in shell scripts or to #define in C/C++.
The lexical parameters can be set/deleted with DEFINE/UNDEFINE and COLUMN ... NEW_VALUE ... and as arguments when calling scripts.
They can be referenced with & or &&. A backslash character \ can be used to treat an & literally.
The interpretation of the ampersand and the backslash depends on the SQL*Plus settings DEFINE and ESCAPE.

These two characters are not part of SQL or PL/SQL, but only have a meaning in SQL*Plus.
Previously, the PMD PL/SQL parser failed when it found one of these characters (depending on where exactly the occur).

With this PR, the parser accepts source code including these characters, at least for typical use cases (tested this with a a code base of >1000 scripts of a commercial product).

Excluding lines from the parser.

A second problem is that the parser is far from perfect - in aforementioned code base approx 10% of the files could not be parsed, though they are perfectly valid.
One example where the parser fails is a scalar subquery in the WHERE-clause, another example is calling DBMS_LOB.TRIM.
Because it seems impossible (at least for me) to actually fix the parser (the grammar is really complicated), this PR allows simply excluding those lines where the parser fails.

Excluding lines from the parser can be achieved like this:

begin
  do_something();
  -- pmd-exclude-begin: PMD does not like dbms_lob.trim (clash with TrimExpression)
  dbms_lob.trim(the_blob, 1000);
  -- pmd-exclude-end
  do_something_else(x);
end;
/

Note that this completely different from -- NOPMD comments, which require the parser to succeed!

Related issues

Several issues where users report that the parser fails for their scripts.
Probably a lot of potential users try out PMD for their scripts and find that the parser doesn't work for their scripts, so they turn away from PMD.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by travis)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hvbargen and others added 8 commits February 24, 2021 09:48
@hvbargen
Basic support for ampersand and backslash characters. At least the pa…
fd973e7
…rser now accepts these characters and can parse a lot more typical scripts.
@hvbargen
Accept double ampersand as well
017e40b
@hvbargen
Replaced StringBuffer with StringBuilder again
c4fe303
@hvbargen
No longer accept arbitrary lines as SQL*Plus commands.
387dee8
@hvbargen
Support for common SQL*Plus command abbreviations, support for specia…
475f750
…l characters in HOST, REMARK, REM commands.
@hvbargen
Allow excluding lines from the parser using comments -- PMD-EXCLUDE-B…
46e368a
…EGIN and -- PMD-EXCLUDE-END. This is to workaround bugs in the PL/SQL parser - some valid statements cannot be parsed. by wrapping them inside PMD-EXCLUDE-BEGIN/END the parser just ignores them and continues parsing with the next line.
@hvbtup
Merge pull request #1 from hvbargen/master
f07feaf
Add support lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand.
@hvbtup
Test cases and bug fixes for lexical parameters and exclusion
64dfebf
@ghost

ghost commented Mar 5, 2021

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel
adangel self-requested a review March 9, 2021 19:10
@adangel

adangel commented Mar 9, 2021

Copy link
Copy Markdown
Member

Thanks for the PR. I hope, I can have a look at it in the next days.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel changed the title Add support for lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand. [plsql] Add support for lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand. Mar 9, 2021
hvbargen and others added 2 commits March 10, 2021 11:36
@hvbargen
Support for detecting excluded ranges in the top-level Input node wit…
3040c34
…h rules.
@hvbtup
Merge pull request #2 from hvbargen/master
56fed34
Support for detecting excluded ranges in the top-level Input node
@hvbtup

hvbtup commented Mar 12, 2021

Copy link
Copy Markdown
Contributor Author

I don't think that these build timeouts have anything to do with my changes...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hvbtup
Avoid PMD warning.
9d66782
adangel
adangel reviewed Mar 14, 2021
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The "-- pmd-exclude-begin" is a interesting idea. In the documentation, we should make sure, to use this only as a workaround and encourage people to report the problems they working around. Because if we don't know the problems, they can't be fixed. Since usage of this feature is exposed in the Input node, we can suggest in the documentation a simple XPath rule that flags files, which use such excludes.

Please have a look at my comments on the individual files.

One advice for future PRs: Your PR solves two things (lexical parameters in SQL*Plus scripts and skip parsing of certain lines). It would be easier, if these separate things would be really separate... solve one problem at a time, not everything at the same time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/ast/SelectIntoStatementTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/src/test/resources/net/sourceforge/pmd/lang/plsql/ast/IsOfType.pls Resolved

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel added this to the 6.33.0 milestone Mar 14, 2021
@adangel adangel added the in:grammar About the grammar of a lexer or parser, eg, a parse/lex exception label Mar 14, 2021
@hvbtup

hvbtup commented Mar 15, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Andreas, thanks for your very helpful comments. I'll need some time to work through this, though - probably not before April.
Please see my comments on some individual topics (I'l add them soon).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hvbtup
Reacted to comments in the PR:Some refactoring
2eb2ce8
@hvbtup

hvbtup commented Mar 22, 2021

Copy link
Copy Markdown
Contributor Author

Did a bit of refactoring. However, I did not introduce a new package ....ast.internal. Added a new test case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel
adangel self-requested a review March 22, 2021 19:43
adangel
adangel reviewed Mar 25, 2021
View reviewed changes
Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/src/test/java/net/sourceforge/pmd/lang/plsql/ast/ParsingExclusionTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/src/test/resources/net/sourceforge/pmd/lang/plsql/ast/IsOfType.pls Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-plsql/etc/grammar/PldocAST.jjt Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel approved these changes Mar 25, 2021
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
I'll look at a few things and hopefully merge this for 6.33.0.
Maybe this also fixes #195 as well.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@adangel adangel self-assigned this Mar 25, 2021
adangel added 4 commits March 25, 2021 16:09
@adangel
[plsql] Add test for pmd#195 - SqlPlus lexical variables
f776088
@adangel
[plsql] Move ParsingExclusion test to PlsqlTreeDumpTest
595ab39
@adangel
[plsql] Move ParsingExclusion to internal package
90675d9
@adangel
[plsql] Document PMD Exclusions
a5478c7
adangel added 2 commits March 25, 2021 16:51
@adangel
Fix pmd warning
b35f22d
@adangel
[plsql] Rework SQLCommand keywords vs. identifiers
efbf023
@adangel adangel changed the title [plsql] Add support for lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand. [plsql] Add support for lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand Mar 25, 2021
adangel added 2 commits March 25, 2021 19:59
@adangel
[doc] Update release notes, refs pmd#195, refs pmd#3161
813a0b1
@adangel
Merge branch 'master' into pr-3161
3579070
@adangel adangel linked an issue Mar 25, 2021 that may be closed by this pull request
[plsql] Ampersand '&' causes PMD processing error in sql file - Lexical error in file #195
Closed
@adangel
adangel merged commit d08d1d1 into pmd:master Mar 26, 2021
@hvbtup

hvbtup commented Mar 26, 2021

Copy link
Copy Markdown
Contributor Author

Many thanks for improving some of the details and adding documentation

One thing that is noteworthy:

This solution improves the possibilities for PMD (the parser can process a lot more more files and more things can be checked by rules), but it is not perfect:

At first I tried to let the SQL*Plus commands accept any characters in the rest of the line (similar to string literals or line comments). But my knowledge of JAVACC is probably too restricted - I failed to make this work.

So I chose the to accept any token sequence for the rest of the line instead. That's why the image and canonicalImage doesn't exactly match the line, which can be seen in SqlPlusLexicalVariablesIssue195.txt:

define patch_name = acme_module
spool &patch_name..log

becomes

SqlPlusCommand[@CanonicalImage = "DEFINE PATCH_NAME = ACME_MODULE", @Image = "define patch_name = acme_module "]
SqlPlusCommand[@CanonicalImage = "SPOOL &PATCH_NAME. . LOG", @Image = "spool &patch_name. . log "]

Nevertheless it is useful to be able to access the command's arguments somehow at least.
A better solution I could imagine would be to have the arguments splitted - more or less white-space separated, but with special handling for double quotes - similar to a shell command line parser. But this would be a bit of work, because depending on the actual command, the rest of the line may require special handling, e.g. for a PROMPT command the rest of the line after the first whitespace should be handled as a single argument, while the command-line-like approach is right for SPOOL, START and HOST commands.

With the current solution it is at least possible to reconstruct the arguments if one makes some assumptions about the file names (e.g. they don't contain blanks and only one dot). And it possible to detect with a rule if e.g. a START command uses lexical parameters.

As a rule of thumb, one should keep in mind that the image and canonicalImage attributes are not exact represenations of the command, because they introduce additional space characters and replace sequences of whitespace with a single space.
A future PMD version might change this, so one should better not rely on the space characters in rules.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel

adangel commented Mar 26, 2021

Copy link
Copy Markdown
Member

A future PMD version might change this, so one should better not rely on the space characters in rules.

That's true. Currently we still skip the SQL*Plus Commands instead of parsing it - which means, that the SqlPlusCommand AST node has no structure at all. E.g. spool &patch_name..log would be something like

SqlPlusCommand
   |- Arguments
       |- Concatentation
           |- SubstitutionVariable[@Name = "patch_name"]
           |- StringLiteral[@Image = ".log"

Producing such a result is a lot harder than just skipping the tokens to the end of the line. And the concatenation character depends additionally on the current config (SET CONCAT).

So in summary, there are always things to improve. Pull requests are welcome.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@oowekyala oowekyala oowekyala left review comments
@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adangel adangel

Labels

in:grammar About the grammar of a lexer or parser, eg, a parse/lex exception

Projects

None yet

Milestone

6.33.0

Development

Successfully merging this pull request may close these issues.

[plsql] Ampersand '&' causes PMD processing error in sql file - Lexical error in file

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@hvbtup @adangel @oowekyala @hvbargen
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.