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 PostgreSQL specfic "CREATE TYPE t AS ENUM (...)" support. #1460

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

caldwell
Copy link
Contributor

@caldwell caldwell commented Oct 7, 2024

See: https://www.postgresql.org/docs/current/sql-createtype.html

I implemented this as a separate Statement::CreateTypeAsEnum { name, labels } so it wouldn't be as invasive to the existing Statement::CreateType (which I would imagine helps forwards/backwards compatibility for the crate's users).

I used the PostgreSqlDialect's comment as a blueprint but that means that the generic dialect doesn't get this feature (unless I'm missing something). I didn't see parse_statement() implemented in GenericDialect though I suppose it could be (and call out to other dialects' parse_statement()s). I didn't want to set that precedent without asking.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you for this contribution @caldwell

implemented in GenericDialect though I suppose it could be (and call out to other dialects' parse_statement()s). I didn't want to set that precedent without asking.

I agree -- I think unless there is a good reason to start migrating the code to a different pattern, following the existing patterns is probably the best

cc @iffyio

@coveralls
Copy link

Pull Request Test Coverage Report for Build 11221533804

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 34 of 38 (89.47%) changed or added relevant lines in 3 files are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.003%) to 89.305%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/mod.rs 3 5 60.0%
src/dialect/postgresql.rs 17 19 89.47%
Files with Coverage Reduction New Missed Lines %
tests/sqlparser_postgres.rs 2 88.57%
src/ast/mod.rs 2 82.43%
Totals Coverage Status
Change from base Build 11186281805: -0.003%
Covered Lines: 30019
Relevant Lines: 33614

💛 - Coveralls

Comment on lines +3184 to +3190
/// CREATE TYPE <name> AS ENUM
/// ```
/// Note: this is a PostgreSQL-specific statement. See <https://www.postgresql.org/docs/current/sql-createtype.html>
CreateTypeAsEnum {
name: ObjectName,
labels: Vec<Ident>,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking it could make sense to include it as part of the existing CreateType, especially since its already an enum there wouldn't be as much of disadvantage in terms of backwards compatibility? then going forward we'd be able to extend it in a similar manner with future extensions to the statement. something like:

pub enum UserDefinedTypeRepresentation {
    Composite {
        attributes: Vec<UserDefinedTypeCompositeAttributeDef>,
    },
    // new variant
    Enum(CreateTypeEnum)
}

Comment on lines +142 to +147
match parse_create(parser) {
Some(result) => Some(result),
None => {
parser.prev_token(); // unconsume the CREATE if we didn't end up parsing anything
None
}
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be possible to simplify this if we call prev_token() before calling parse_create? thinking since the latter already returns an Option representing the same result

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.

4 participants