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
commented
Mar 5, 2021
•
edited by adangel
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
…rser now accepts these characters and can parse a lot more typical scripts.
…l characters in HOST, REMARK, REM commands.
…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.
Add support lexical parameters in SQL*Plus scripts, allow excluding lines which the parser does not understand.
|
1 Message
|
| 📖 |
No java rules are changed! |
Generated by 🚫 Danger
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the PR. I hope, I can have a look at it in the next days.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Support for detecting excluded ranges in the top-level Input node
|
I don't think that these build timeouts have anything to do with my changes...
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
hvbtup
commented
Mar 15, 2021
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
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).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Did a bit of refactoring. However, I did not introduce a new package ....ast.internal. Added a new test case.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
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
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Labels
in:grammar
About the grammar of a lexer or parser, eg, a parse/lex exception
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.
|