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: Views and named notations #4073

Merged
merged 3 commits into from Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 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),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Sequence("OR", "REPLACE", optional=True),
Ref("OrReplaceGrammar", optional=True),

"MATERIALIZED",
"VIEW",
Ref("IfNotExistsGrammar", optional=True),
Expand All @@ -2043,6 +2058,11 @@ class CreateMaterializedViewStatementSegment(BaseSegment):
Delimited(
Sequence(
Ref("ParameterNameSegment"),
Sequence(
Ref("DotSegment"),
Ref("ParameterNameSegment"),
optional=True,
),
Sequence(
Ref("EqualsSegment"),
Ref("LiteralGrammar"),
Expand Down Expand Up @@ -4610,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 @@ -145,3 +145,13 @@ FROM table_2

ORDER BY field_1, field_2
WITH DATA;

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add newlines at end of files. Your IDE probably has a setting.

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: b0eb41bbfff930069d2dee1c38806061e4d02c975049a911ff45b4c6f5f2f892
_hash: 241ca7aedfaff431b1ffae209bc65103e8caf6896b36324a8d4fb38b27c4bfb4
file:
- statement:
create_materialized_view_statement:
Expand Down Expand Up @@ -675,3 +675,56 @@ file:
- keyword: WITH
- keyword: DATA
- statement_terminator: ;
- statement:
create_materialized_view_statement:
- keyword: CREATE
- keyword: MATERIALIZED
- keyword: VIEW
- table_reference:
naked_identifier: my_mat_view
- keyword: WITH
- bracketed:
- start_bracket: (
- parameter: left
- dot: .
- parameter: right
- end_bracket: )
- 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: ;
- 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: ;