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

Add support for curly brackets in SnowSQL ampersand variables #1901

Merged

Conversation

chwiese
Copy link
Contributor

@chwiese chwiese commented Nov 14, 2021

Brief summary of the change made

Moves support for Snowsql variables from the dialect into the placeholder templater and adds support for curly brackets, as documented here. Since Snowsql variables can hold SQL logic such as optional WHERE clauses, the templater is the better home for them as discussed here on Slack.

Are there any other side effects of this change that we should be aware of?

Yes, anybody how currently has Snowsql variables in their scripts will run into parsing errors. They will need to switch to the placeholder templater and define the placeholders in their Sqlfluff config.

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 python test/generate_parse_fixture_yml.py or by running tox locally).
    • 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.

@@ -105,7 +107,7 @@
type="variable",
),
ReferencedVariableNameSegment=RegexParser(
r"(\$|\&)[A-Z][A-Z0-9_]*",
r'(?:\$[A-Z][A-Z0-9_]*)|(?:[A-Z0-9_\."]*&{[A-Z0-9_]*}[A-Z0-9_]*)|(?:&[A-Z0-9_\."]*)',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason (?:[\w\."]*&{[A-Z0-9_]*}[\w]*) does not work, while (?:[A-Z0-9_\."]*&{[A-Z0-9_]*}[A-Z0-9_]*) does. Does anybody know why?

Copy link
Member

Choose a reason for hiding this comment

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

Those should be identical. Can you define "does not work"? Have you been able to reproduce it on https://regex101.com/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, you are right, "does not work" is rarely helpful.
When I use r'(?:\$[A-Z][A-Z0-9_]*)|(?:[A-Z0-9_\."]*&{[A-Z0-9_]*}[A-Z0-9_]*)|(?:&[A-Z0-9_\."]*)',, then USE WAREHOUSE &WAREHOUSE; parses alright. If I use r'(?:\$[A-Z][A-Z0-9_]*)|(?:[\w\."]*&{[A-Z0-9_]*}[A-Z0-9_]*)|(?:&[\w\."]*)',, the same piece of SQL returns a Found unparsable section error.
I cannot reproduce this on regex101.com - over there both patterns produce the desired match &WAREHOUSE.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bug - not in your regex - but in the RegexParser that is being used. RegexParser inherits from StringParser, which calls this in its init (which is called in the RegexParser)

self.template = template.upper()

So, \w becomes \W, which matches every character that is not a word character. Basically ^[\w]

@codecov
Copy link

codecov bot commented Nov 20, 2021

Codecov Report

Merging #1901 (905752b) into main (c66aefd) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1901   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          147       147           
  Lines        10219     10219           
=========================================
  Hits         10219     10219           
Impacted Files Coverage Δ
src/sqlfluff/core/templaters/placeholder.py 100.00% <ø> (ø)
src/sqlfluff/dialects/dialect_snowflake.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 c66aefd...905752b. Read the comment docs.

@chwiese chwiese marked this pull request as ready for review November 20, 2021 17:35
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.

The code changes look good to me. I do have a question: This PR appears to add support for ampersand variables both in the placeholder templater as well as the Snowflake dialect. Do both of them use the same syntax? Is it possible to use both at once? Do we need to add or update documentation so users will understand what's supported and how to use it?

@chwiese
Copy link
Contributor Author

chwiese commented Nov 20, 2021

@barrywhart Actually this PR removes ampersand support from the Snowflake dialect and moves it to the placeholder templater only. There is still dollar sign variable support in the dialect though.

Both can be used concurrently. Dollar sign variables are part of the dialect and can be used with all clients, ampersand variables are specific to the official SnowSQL client.

@barrywhart
Copy link
Member

Got it! Merging now. Great work!

@barrywhart barrywhart merged commit 14f93b3 into sqlfluff:main Nov 20, 2021
@chwiese chwiese deleted the snowsql-variables-with-curly-brackets branch November 21, 2021 10:43
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.

None yet

4 participants