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

Grammar: adds support for CREATE/ALTER/DROP DATABASE for Postgres dialect #2081

Merged
merged 13 commits into from Dec 10, 2021

Conversation

derickl
Copy link
Contributor

@derickl derickl commented Dec 9, 2021

Brief summary of the change made

Adds full support for [CREATE | ALTER | DROP] DATABASE statements.

Also fixes #2017

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

None

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.

@codecov
Copy link

codecov bot commented Dec 9, 2021

Codecov Report

Merging #2081 (7b4665f) into main (cd8aceb) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #2081   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          148       148           
  Lines        10575     10590   +15     
=========================================
+ Hits         10575     10590   +15     
Impacted Files Coverage Δ
src/sqlfluff/dialects/dialect_postgres_keywords.py 100.00% <ø> (ø)
src/sqlfluff/dialects/dialect_postgres.py 100.00% <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 cd8aceb...7b4665f. Read the comment docs.

Copy link
Contributor

@jpy-git jpy-git left a comment

Choose a reason for hiding this comment

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

@derickl a couple things for CREATE/DROP whilst I'm still reviewing ALTER 👍

src/sqlfluff/dialects/dialect_postgres.py Outdated Show resolved Hide resolved
src/sqlfluff/dialects/dialect_postgres.py Outdated Show resolved Hide resolved
src/sqlfluff/dialects/dialect_postgres.py Outdated Show resolved Hide resolved
Copy link
Contributor

@jpy-git jpy-git left a comment

Choose a reason for hiding this comment

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

A couple more suggestions for ALTER DATABASE.

It's looking great so far! 🙌

src/sqlfluff/dialects/dialect_postgres.py Outdated Show resolved Hide resolved
src/sqlfluff/dialects/dialect_postgres.py Outdated Show resolved Hide resolved
derickl and others added 4 commits December 10, 2021 08:52
Co-authored-by: Joseph Young <80432516+jpy-git@users.noreply.github.com>
Co-authored-by: Joseph Young <80432516+jpy-git@users.noreply.github.com>
Co-authored-by: Joseph Young <80432516+jpy-git@users.noreply.github.com>
Co-authored-by: Joseph Young <80432516+jpy-git@users.noreply.github.com>
@derickl
Copy link
Contributor Author

derickl commented Dec 10, 2021

Based on this #2081 (comment)

I've grep'd through the source code and haven't come across this usage pattern Ref("WITH", optional=True) but Ref.keyword("WITH", optional=True) does exist.

What would be the difference between Ref("WITH", optional=True) and Sequence("WITH", optional=True)?

When does one choose Ref.keyword over Sequence? Would Ref.keyword be for reserved keywords?

@derickl
Copy link
Contributor Author

derickl commented Dec 10, 2021

Thank you for the review @jpy-git!

derickl and others added 3 commits December 10, 2021 09:07
@derickl derickl requested a review from jpy-git December 10, 2021 06:40
@tunetheweb
Copy link
Member

tunetheweb commented Dec 10, 2021

Based on this #2081 (comment)

I've grep'd through the source code and haven't come across this usage pattern Ref("WITH", optional=True) but Ref.keyword("WITH", optional=True) does exist.

What would be the difference between Ref("WITH", optional=True) and Sequence("WITH", optional=True)?

When does one choose Ref.keyword over Sequence? Would Ref.keyword be for reserved keywords?

A good question and one I’ve wondered in the past!

As far as I can tell there is no real difference and it comes down to personal preference.

In theory using Ref.keyword might be slightly faster as the Sequence version effectively wraps the implied Ref.keyword in a (short) Sequence So it’s an extra layer to go through. In practice I would be VERY surprised if that created any real difference.

Personally I have a slight preference for Sequence (despite the above) for a few reasons:

  • it seems more recognisable as we use Sequence a lot.
  • It allows other grammar to more easily be added to this section (e.g. if you want to add a NOT before it).
  • It two less characters and all one word so easier to type!

But honestly it doesn’t make a big difference. Maybe we should standardise on a preferred style but when I looked before they were both used quite a bit and I’ll bet it’s got worse since then.

Copy link
Contributor

@jpy-git jpy-git left a comment

Choose a reason for hiding this comment

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

Looks good, couple final things to add then we should be good to get this going 👍

Ref("DatabaseReferenceSegment"),
OneOf(
Sequence(
Sequence("WITH", optional=True),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Sequence("WITH", optional=True),
Ref.keyword("WITH", optional=True),

To be consistent with CREATE/DROP

@jpy-git
Copy link
Contributor

jpy-git commented Dec 10, 2021

@derickl good spot on the Ref vs Ref.keyword!

Basically Ref.keyword is the syntax for explicitly specifying to search for the string in the keywords. You can actually think of any of the keywords as being wrapped by Ref.keyword
e.g.

parse_grammar = Sequence(
    Ref.keyword("CREATE"),
    Ref.keyword("DATABASE"),
...

but for shorthand you can exclude Ref.keyword in these cases.

However, in the specific example of the WITH in this PR we need a way to tell the parser that it is optional. That's why we pass the keyword via the more explicit Ref.keyword segment so that we can also pass an optional=True argument.

I agree that Sequence could work but think Ref.keyword is more accurate to the logic we're trying to express (the maintainers can have a discussion around consistency outside of this PR).

Copy link
Contributor

@jpy-git jpy-git left a comment

Choose a reason for hiding this comment

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

Awesome work @derickl and really robust test cases, LGTM 🚀 🌟

@derickl
Copy link
Contributor Author

derickl commented Dec 10, 2021

Thanks for the great review @jpy-git

@jpy-git jpy-git merged commit a0f82f7 into sqlfluff:main Dec 10, 2021
@derickl
Copy link
Contributor Author

derickl commented Dec 10, 2021

Thank you for sharing your thoughts here @tunetheweb. I hope we converge to some consistency down the road.

@tunetheweb
Copy link
Member

I hope we converge to some consistency down the road.

Or maybe not if @jpy-git and I can't even agree! 🤣

@derickl derickl deleted the 2017-alter-database-postgres branch December 11, 2021 21:21
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.

Alter Database Unparsable Section
3 participants