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 ALTER VIEW statement for Postgres dialect #2096

Merged
merged 4 commits into from Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -1647,6 +1647,83 @@ class DropMaterializedViewStatementSegment(BaseSegment):
)


@postgres_dialect.segment()
class AlterViewStatementSegment(BaseSegment):
"""An `ALTER VIEW` statement.

As specified in https://www.postgresql.org/docs/14/sql-alterview.html
"""

type = "alter_view_statement"

match_grammar = StartsWith(Sequence("ALTER", "VIEW"))

parse_grammar = Sequence(
"ALTER",
"VIEW",
Ref("IfExistsGrammar", optional=True),
Ref("TableReferenceSegment"),
OneOf(
Sequence(
"ALTER",
Ref.keyword("COLUMN", optional=True),
Ref("ColumnReferenceSegment"),
OneOf(
Sequence(
"SET",
"DEFAULT",
OneOf(
Ref("LiteralGrammar"),
Ref("FunctionSegment"),
Ref("BareFunctionSegment"),
Ref("ExpressionSegment"),
),
),
Comment on lines +1672 to +1681
Copy link
Contributor

Choose a reason for hiding this comment

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

You're correct here. I noticed that the existing AlterTableActionSegment only allows expression segments when it should also account for the segments you have here. If you're interested would you be able to create a separate PR to match that line to the SET DEFAULT grammar you have here and add the corresponding unit tests? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Raised here: #2101

Sequence("DROP", "DEFAULT"),
),
),
Sequence(
"OWNER",
"TO",
OneOf(
Ref("ObjectReferenceSegment"),
"CURRENT_ROLE",
"CURRENT_USER",
"SESSION_USER",
),
),
Sequence(
"RENAME",
Ref.keyword("COLUMN", optional=True),
Ref("ColumnReferenceSegment"),
"TO",
Ref("ColumnReferenceSegment"),
),
Sequence("RENAME", "TO", Ref("TableReferenceSegment")),
Sequence("SET", "SCHEMA", Ref("SchemaReferenceSegment")),
Sequence(
"SET",
Bracketed(
Delimited(
Sequence(
Ref("ParameterNameSegment"),
Sequence(
Ref("EqualsSegment"),
Ref("LiteralGrammar"),
optional=True,
),
)
)
),
),
Sequence(
"RESET",
Bracketed(Delimited(Ref("ParameterNameSegment"))),
),
),
)


@postgres_dialect.segment(replace=True)
class CreateDatabaseStatementSegment(BaseSegment):
"""A `CREATE DATABASE` statement.
Expand Down Expand Up @@ -2669,6 +2746,7 @@ class StatementSegment(BaseSegment):
Ref("AlterDatabaseStatementSegment"),
Ref("DropDatabaseStatementSegment"),
Ref("AlterFunctionStatementSegment"),
Ref("AlterViewStatementSegment"),
],
)

Expand Down
53 changes: 53 additions & 0 deletions test/fixtures/dialects/postgres/postgres_alter_view.sql
@@ -0,0 +1,53 @@
-- issue:2094
ALTER VIEW myview ALTER date_column SET DEFAULT NOW();
ALTER VIEW myview ALTER int_column SET DEFAULT 1;
ALTER VIEW myview ALTER text_column SET DEFAULT 'value';
ALTER VIEW myview ALTER bool_column SET DEFAULT false;
ALTER VIEW myview ALTER other_column SET DEFAULT other_value;
ALTER VIEW myview ALTER other_column SET DEFAULT CURRENT_TIMESTAMP;
ALTER VIEW myview ALTER other_column SET DEFAULT a_function(a_parameter);
ALTER VIEW myview ALTER other_column SET DEFAULT a_function('a_parameter');
ALTER VIEW myview ALTER other_column DROP DEFAULT;
ALTER VIEW IF EXISTS myview ALTER date_column SET DEFAULT NOW();
ALTER VIEW IF EXISTS myview ALTER int_column SET DEFAULT 1;
ALTER VIEW IF EXISTS myview ALTER text_column SET DEFAULT 'value';
ALTER VIEW IF EXISTS myview ALTER bool_column SET DEFAULT false;
ALTER VIEW IF EXISTS myview ALTER other_column SET DEFAULT other_value;
ALTER VIEW IF EXISTS myview ALTER other_column SET DEFAULT CURRENT_TIMESTAMP;
ALTER VIEW IF EXISTS myview ALTER other_column SET DEFAULT a_function(a_parameter);
ALTER VIEW IF EXISTS myview ALTER other_column SET DEFAULT a_function('a_parameter');
ALTER VIEW IF EXISTS myview ALTER other_column DROP DEFAULT;

ALTER VIEW myview OWNER TO baz_role;
ALTER VIEW myview OWNER TO "baz-role";
ALTER VIEW myview OWNER TO CURRENT_ROLE;
ALTER VIEW myview OWNER TO CURRENT_USER;
ALTER VIEW myview OWNER TO SESSION_USER;
ALTER VIEW IF EXISTS myview OWNER TO baz_role;
ALTER VIEW IF EXISTS myview OWNER TO "baz-role";
ALTER VIEW IF EXISTS myview OWNER TO CURRENT_ROLE;
ALTER VIEW IF EXISTS myview OWNER TO CURRENT_USER;
ALTER VIEW IF EXISTS myview OWNER TO SESSION_USER;

ALTER VIEW myview RENAME column_name TO new_column_name;
ALTER VIEW myview RENAME COLUMN column_name TO new_column_name;
ALTER VIEW IF EXISTS myview RENAME column_name TO new_column_name;
ALTER VIEW IF EXISTS myview RENAME COLUMN column_name TO new_column_name;

ALTER VIEW myview RENAME TO new_name;
ALTER VIEW IF EXISTS myview RENAME TO new_name;

ALTER VIEW myview SET SCHEMA new_schema;
ALTER VIEW IF EXISTS myview SET SCHEMA new_schema;

ALTER VIEW myview SET ( view_option_name );
ALTER VIEW myview SET ( view_option_name = 1);
ALTER VIEW myview SET ( view_option_name = 1, view_option_name2 = 'value', view_option_name3, view_option_name4 = false);
ALTER VIEW IF EXISTS myview SET ( view_option_name );
ALTER VIEW IF EXISTS myview SET ( view_option_name = 1);
ALTER VIEW IF EXISTS myview SET ( view_option_name, view_option_name2 = 1, view_option_name3);

ALTER VIEW myview RESET ( view_option_name );
ALTER VIEW myview RESET ( view_option_name, view_option_name2 );
ALTER VIEW IF EXISTS myview RESET ( view_option_name );
ALTER VIEW IF EXISTS myview RESET ( view_option_name, view_option_name2 );