Skip to content

Commit

Permalink
Support CREATE OR REPLACE MATERIALIZED VIEW
Browse files Browse the repository at this point in the history
  • Loading branch information
davetapley committed Nov 17, 2022
1 parent 99adc93 commit f1d3cf4
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -22,6 +22,7 @@
Sequence,
SymbolSegment,
StartsWith,
StringParser,
)
from sqlfluff.core.parser.segments.base import BracketedSegment

Expand All @@ -40,6 +41,14 @@

postgres_dialect = ansi_dialect.copy_as("postgres")

postgres_dialect.insert_lexer_matchers(
# JSON Operators: https://www.postgresql.org/docs/9.5/functions-json.html
[
StringLexer("right_arrow", "=>", CodeSegment),
],
before="equals",
)

postgres_dialect.insert_lexer_matchers(
# JSON Operators: https://www.postgresql.org/docs/9.5/functions-json.html
[
Expand Down Expand Up @@ -236,6 +245,7 @@
Ref("NakedIdentifierFullSegment"),
),
CascadeRestrictGrammar=OneOf("CASCADE", "RESTRICT"),
RightArrowSegment=StringParser("=>", SymbolSegment, type="right_arrow"),
)

postgres_dialect.replace(
Expand Down Expand Up @@ -275,6 +285,10 @@
CodeSegment,
type="function_name_identifier",
),
FunctionContentsExpressionGrammar=OneOf(
Ref("ExpressionSegment"),
Ref("NamedArgumentSegment"),
),
QuotedLiteralSegment=OneOf(
# Postgres allows newline-concatenated string literals (#1488).
# Since these string literals can have comments between them,
Expand Down Expand Up @@ -2029,6 +2043,7 @@ class CreateMaterializedViewStatementSegment(BaseSegment):

match_grammar = Sequence(
"CREATE",
Sequence("OR", "REPLACE", optional=True),
"MATERIALIZED",
"VIEW",
Ref("IfNotExistsGrammar", optional=True),
Expand Down Expand Up @@ -4615,3 +4630,17 @@ class ColumnReferenceSegment(ObjectReferenceSegment):
),
allow_gaps=False,
)


class NamedArgumentSegment(BaseSegment):
"""Named argument to a function.
https://www.postgresql.org/docs/current/sql-syntax-calling-funcs.html#SQL-SYNTAX-CALLING-FUNCS-NAMED
"""

type = "named_argument"
match_grammar = Sequence(
Ref("NakedIdentifierSegment"),
Ref("RightArrowSegment"),
Ref("ExpressionSegment"),
)
Expand Up @@ -150,4 +150,8 @@ CREATE MATERIALIZED VIEW my_mat_view
WITH (left.right)
AS
SELECT a
FROM my_table;

CREATE OR REPLACE MATERIALIZED VIEW my_mat_view AS
SELECT a
FROM my_table;
Expand Up @@ -3,7 +3,7 @@
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 372bd8f828b87dd66ddcf9a78448d50ea8a681bef794b750256f3786a988f06e
_hash: 241ca7aedfaff431b1ffae209bc65103e8caf6896b36324a8d4fb38b27c4bfb4
file:
- statement:
create_materialized_view_statement:
Expand Down Expand Up @@ -704,3 +704,27 @@ file:
table_reference:
naked_identifier: my_table
- statement_terminator: ;
- statement:
create_materialized_view_statement:
- keyword: CREATE
- keyword: OR
- keyword: REPLACE
- keyword: MATERIALIZED
- keyword: VIEW
- table_reference:
naked_identifier: my_mat_view
- keyword: AS
- select_statement:
select_clause:
keyword: SELECT
select_clause_element:
column_reference:
naked_identifier: a
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
naked_identifier: my_table
- statement_terminator: ;
4 changes: 4 additions & 0 deletions test/fixtures/dialects/postgres/postgres_select.sql
Expand Up @@ -65,3 +65,7 @@ SELECT FROM test1;

-- keywords can be used as column names without quotes if qualified
select id, start, periods.end from periods;

SELECT concat_lower_or_upper('Hello', 'World', true);
SELECT concat_lower_or_upper(a => 'Hello', b => 'World');
SELECT concat_lower_or_upper('Hello', 'World', uppercase => true);
68 changes: 67 additions & 1 deletion test/fixtures/dialects/postgres/postgres_select.yml
Expand Up @@ -3,7 +3,7 @@
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: d6880cc6b5b12a791d3b0e258410ad4b4d37331250e9650447f5a0df2e059c39
_hash: 4c2313e0a846e6f2e7aae0410ba8e11c9815f03fdf9e8cb2869c93e945b38e6f
file:
- statement:
select_statement:
Expand Down Expand Up @@ -612,3 +612,69 @@ file:
table_reference:
naked_identifier: periods
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: concat_lower_or_upper
bracketed:
- start_bracket: (
- expression:
quoted_literal: "'Hello'"
- comma: ','
- expression:
quoted_literal: "'World'"
- comma: ','
- expression:
boolean_literal: 'true'
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: concat_lower_or_upper
bracketed:
- start_bracket: (
- named_argument:
naked_identifier: a
right_arrow: =>
expression:
quoted_literal: "'Hello'"
- comma: ','
- named_argument:
naked_identifier: b
right_arrow: =>
expression:
quoted_literal: "'World'"
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: concat_lower_or_upper
bracketed:
- start_bracket: (
- expression:
quoted_literal: "'Hello'"
- comma: ','
- expression:
quoted_literal: "'World'"
- comma: ','
- named_argument:
naked_identifier: uppercase
right_arrow: =>
expression:
boolean_literal: 'true'
- end_bracket: )
- statement_terminator: ;

0 comments on commit f1d3cf4

Please sign in to comment.