Skip to content
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

Merged
merged 4 commits into from Jul 10, 2022

Conversation

qgallet
Copy link
Contributor

@qgallet qgallet commented Jul 8, 2022

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 in test/fixtures/rules/std_rule_cases.
    • .sql/.yml parser test cases in test/fixtures/dialects (note YML files can be auto generated with tox -e generate-fixture-yml).
    • Full autofix test cases in test/fixtures/linter/autofix.
    • Other.
  • Added appropriate documentation for the change.

  • Created GitHub issues for any relevant followup/future enhancements if appropriate.

@@ -710,7 +717,7 @@ class IntervalExpressionSegment(BaseSegment):
[
RegexLexer(
"at_sign",
r"[@][a-zA-Z0-9_]*",
r"@@?[a-zA-Z0-9_.]*",
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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
Copy link

codecov bot commented Jul 8, 2022

Codecov Report

Merging #3576 (f9a071b) into main (50f0638) will not change coverage.
The diff coverage is n/a.

@@            Coverage Diff            @@
##              main     #3576   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          174       174           
  Lines        13144     13144           
=========================================
  Hits         13144     13144           
Impacted Files Coverage Δ
src/sqlfluff/dialects/dialect_mysql.py 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 50f0638...f9a071b. Read the comment docs.

qgallet and others added 2 commits July 9, 2022 12:19
$ sign is now supported, and a `.` cannot be at the start/end of a variable name.
Copy link
Member

@barrywhart barrywhart left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@barrywhart barrywhart merged commit faa1c83 into sqlfluff:main Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add missing support for MySQL system variables
2 participants