New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support MySQL system variables #3576
Conversation
… provide fixtures for validation.
@@ -710,7 +717,7 @@ class IntervalExpressionSegment(BaseSegment): | |||
[ | |||
RegexLexer( | |||
"at_sign", | |||
r"[@][a-zA-Z0-9_]*", | |||
r"@@?[a-zA-Z0-9_.]*", |
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.
I think .
here means "match any character. Should this be \.
?
As written, I think it will also allow the name to begin and/or end with a .
. That worries me a little. Can we prevent this?
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.
I double checked, metacharacters (except \
) are matched literally inside a character class in Python regex. This fails as expected on a variable with an invalid char in the name : echo 'SELECT @var&broken | sqlfluff parse - --dialect mysql
.
For your second point, this indeed looks wrong, but I've found MySQL is so bizarre this is actually valid. The documentation states:
User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, ., _, and $. A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var', @"my-var", or @`my-var`).
I hardly believe anyone in their right mind would use a variable name with a .
at the start or end, so I'm happy to change it to something like r"@@?[a-zA-Z0-9_]+\.?[a-zA-Z0-9_]*"
. The $
sign should be added as well, just let me know what's preferable for the tool and I'll prepare the commit
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.
The above looks close. I think you want the dot and the remainder to look like ‘(.[a-zA-Z0-9_]+)?`, to prevent it ending with a dot.
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.
Alright, I've pushed the change with the $ sign also supported, and added more fixtures for session variables. Thanks for the feedback!
Codecov Report
@@ Coverage Diff @@
## main #3576 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 174 174
Lines 13144 13144
=========================================
Hits 13144 13144
Continue to review full report at Codecov.
|
$ sign is now supported, and a `.` cannot be at the start/end of a variable name.
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.
Looks great, thanks!
Brief summary of the change made
Fixes #3574
MySQL system variables
@@global.xxx
and@@session.xxx
are now recognized by the parser.Are there any other side effects of this change that we should be aware of?
None that I can think of.
Pull Request checklist
Please confirm you have completed any of the necessary steps below.
Included test cases to demonstrate any code changes, which may be one or more of the following:
.yml
rule test cases intest/fixtures/rules/std_rule_cases
..sql
/.yml
parser test cases intest/fixtures/dialects
(note YML files can be auto generated withtox -e generate-fixture-yml
).test/fixtures/linter/autofix
.Added appropriate documentation for the change.
Created GitHub issues for any relevant followup/future enhancements if appropriate.