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

Postgres: CREATE COLLATION support #3571

Merged
merged 12 commits into from
Jul 10, 2022
Merged

Conversation

greg-finley
Copy link
Contributor

Brief summary of the change made

Closes #3494

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

No

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.

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@@ -925,18 +925,21 @@ def get_keywords(keyword_list, keyword_type):
("DATE", "non-reserved"),
("DEPTH", "non-reserved"),
("DESCRIBE", "non-reserved"),
("DETERMINISTIC", "non-reserved"),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If someone can explain what goes into postgres_docs_keywords vs postgres_nondocs_keywords, it would be great to document that in the code. I just guessed.

Copy link
Member

Choose a reason for hiding this comment

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

@WittierDinosaur ?

I'm guessing from the name that postgres_nondocs_keywords refers to keywords that appear to be keywords but are not listed in https://www.postgresql.org/docs/current/sql-keywords-appendix.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the distinction even matter? Does it matter if I've put in the "wrong" place for now?

Copy link
Member

Choose a reason for hiding this comment

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

No it doesn’t presently. Type is important but not which list it appears in.

Ref("ColumnConstraintSegment", optional=True)
OneOf(
Ref("ColumnConstraintSegment", optional=True),
Sequence(
Copy link
Member

Choose a reason for hiding this comment

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

If I read this correctly, this allows any number of COLLATEs, e.g.:

COLLATE a COLLATE b COLLATE c

Is this the intent? I don't see any tests doing this. Should we add one?

Copy link
Member

Choose a reason for hiding this comment

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

From reading the docs, I think only one COLLATE clause is permitted.

https://www.postgresql.org/docs/current/collation.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, it's not possible to do multiple COLLATE. I did it this way because COLLATE numeric NOT NULL and NOT NULL COLLATE numeric are both legal.

It could instead be ...

Sequence(
"COLLATE",
                                            Ref("ObjectReferenceSegment"),
                                            optional=True,
                                        ),
AnyNumberOf(
                                    Ref("ColumnConstraintSegment", optional=True)
),
Sequence(
"COLLATE",
                                            Ref("ObjectReferenceSegment"),
                                            optional=True,
                                        ),

... so the collate sequence can go either before or after constraints. Or is there a better way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Related to the test I put, just ran this in Postgres and it worked:

gregoryfinley=# CREATE TABLE orders3
(
order_number text NOT NULL COLLATE numeric,
order_number2 text COLLATE numeric NOT NULL
);
CREATE TABLE
gregoryfinley=# 

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense, and it should be okay. The dialect is often more permissive/flexible than what is actually allowed (or makes sense to do).

I'd like @tunetheweb to review this PR as well, since he is more the expert on dialect changes. He's on vacation right now, so I'm doing a preliminary review.

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 think this works better: 010cb8b

Copy link
Member

Choose a reason for hiding this comment

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

Ok but now we don’t have the CONSTRAINT + COLLATE + CONSTRAINT example so someone could try to clean this up in future using the AnySetOf syntax I proposed above and not realise we specifically choose NOT to use that. So I think you need all three tests for complete coverage?

Though, to be honest, think it’s pretty unlikely anyone would actually use that third type, but there’s all sorts out there!!

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 think my test is an example of CONSTRAINT + COLLATE + CONSTRAINT, isn't it?

order_number text UNIQUE COLLATE numeric NOT NULL

UNIQUE is a constraint, then COLLATE, then NOT NULL is a second constraint.

Or am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

Ah you’re right. What you’re missing then is COLLATE + CONSTRAINT example.

Though really getting picky now so happy enough to just merge if you prefer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be everything? Let me know if not: 0f95ac5

@barrywhart barrywhart requested a review from tunetheweb July 7, 2022 21:49
@codecov
Copy link

codecov bot commented Jul 7, 2022

Codecov Report

Merging #3571 (0f95ac5) into main (ec9af41) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main     #3571   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          174       174           
  Lines        13146     13149    +3     
=========================================
+ Hits         13146     13149    +3     
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 ec9af41...0f95ac5. Read the comment docs.

greg-finley and others added 8 commits July 7, 2022 14:53

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@tunetheweb tunetheweb merged commit e365dcc into sqlfluff:main Jul 10, 2022
@tunetheweb tunetheweb changed the title Postgres: CREATE COLLATION Postgres: CREATE COLLATION support Jul 11, 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.

Postgres PRS issue on COLLATION/COLLATE
3 participants