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

Added support for psql meta commands to Postgres #1423

Merged
merged 6 commits into from
Sep 20, 2021

Conversation

WittierDinosaur
Copy link
Contributor

Brief summary of the change made

This PR adds support for psql meta commands. Psql commands here start with , followed by at least one alphabet character, and are then terminated by newline or \. The File and Statement Segments had to be edited to accommodate for this new functionality.

Fixes #1327

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

No, Postgres-specific, and all dialect tests are passing

Pull Request checklist

Test cases added for psql meta commands, and sql commands in the same file (or same line for \)
Documented regex in file

@codecov
Copy link

codecov bot commented Sep 19, 2021

Codecov Report

Merging #1423 (6676f12) into main (ce4e5a3) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1423   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          127       127           
  Lines         8603      8604    +1     
=========================================
+ Hits          8603      8604    +1     
Impacted Files Coverage Δ
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 ce4e5a3...6676f12. Read the comment docs.

@tunetheweb
Copy link
Member

I was curious how comments were handled as seems like a similar use case.

From a quick look it looks like it basically trims the details as part of the lexing, so those symbols aren’t sent to the parser and are effectively ignored.

The downside would be that you can’t check the grammar of meta commands (as they will effectively not exist to the parser), but doesn’t look like you are doing that here anyway so for now that wouldn’t matter. But your solution at least opens that possibility later.

Any thoughts on whether following the comments process would be the better solution than what’s proposed here?

@WittierDinosaur
Copy link
Contributor Author

Yeah, that might be easier. The only thing I think could cause an issue, is:
\echo "thing" \\ SELECT 1;
Comments are easy to ignore because they can't be terminated mid-line, psql meta commands can. We would just need to make sure the whitespace is preserved during linting, if the command gets ignored in parsing. I could add an optional \s after the \\?

@tunetheweb
Copy link
Member

Comments are easy to ignore because they can't be terminated mid-line, psql meta commands can.

Sure they can, with /*...*/ syntax:

SELECT *
/* this is our main table */ FROM table 1

@WittierDinosaur
Copy link
Contributor Author

Good point. I've updated meta commands to function like comments.

@tunetheweb
Copy link
Member

Good point. I've updated meta commands to function like comments.

Wow that seems almost too simple now!!! I took ages trying to hunt for the code to figure out where the actual magic was happening - but guess it's all handled just by calling it a CommentSegment?

Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

LGTM! One small suggestion.

src/sqlfluff/dialects/dialect_postgres.py Outdated Show resolved Hide resolved
WittierDinosaur and others added 2 commits September 20, 2021 16:34
Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>
@tunetheweb tunetheweb merged commit fe9333f into sqlfluff:main Sep 20, 2021
@tunetheweb tunetheweb changed the title Added support for psql meta commands Added support for psql meta commands to Postgres Sep 20, 2021
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.

Getting unlexable error when parsing psql meta-commands
2 participants