Skip to content

Commit

Permalink
Merge branch 'main' into postgres_load_statement
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Dec 26, 2021
2 parents fdb6e30 + b900e9d commit f48b711
Show file tree
Hide file tree
Showing 25 changed files with 698 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/sqlfluff/dialects/dialect_ansi.py
Expand Up @@ -58,7 +58,10 @@

ansi_dialect.set_lexer_matchers(
[
RegexLexer("whitespace", r"[\t ]+", WhitespaceSegment),
# Match all forms of whitespace except newlines and carriage returns:
# https://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines
# This pattern allows us to also match non-breaking spaces (#2189).
RegexLexer("whitespace", r"[^\S\r\n]+", WhitespaceSegment),
RegexLexer(
"inline_comment",
r"(--|#)[^\n]*",
Expand All @@ -76,7 +79,7 @@
),
trim_post_subdivide=RegexLexer(
"whitespace",
r"[\t ]+",
r"[^\S\r\n]+",
WhitespaceSegment,
),
),
Expand Down
36 changes: 36 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -2765,6 +2765,8 @@ class StatementSegment(BaseSegment):
Ref("AlterFunctionStatementSegment"),
Ref("AlterViewStatementSegment"),
Ref("LoadStatementSegment"),
Ref("ResetStatementSegment"),
Ref("DiscardStatementSegment"),
],
)

Expand Down Expand Up @@ -3069,3 +3071,37 @@ class LoadStatementSegment(BaseSegment):
"LOAD",
Ref("QuotedLiteralSegment"),
)


@postgres_dialect.segment()
class ResetStatementSegment(BaseSegment):
"""A `RESET` statement.
As Specified in https://www.postgresql.org/docs/14/sql-reset.html
"""

type = "reset_statement"
match_grammar = Sequence(
"RESET",
OneOf("ALL", Ref("ParameterNameSegment")),
)


@postgres_dialect.segment()
class DiscardStatementSegment(BaseSegment):
"""A `DISCARD` statement.
As Specified in https://www.postgresql.org/docs/14/sql-discard.html
"""

type = "discard_statement"
match_grammar = Sequence(
"DISCARD",
OneOf(
"ALL",
"PLANS",
"SEQUENCES",
"TEMPORARY",
"TEMP",
),
)
4 changes: 2 additions & 2 deletions src/sqlfluff/dialects/dialect_redshift.py
Expand Up @@ -486,13 +486,13 @@ class AlterGroupSegment(BaseSegment):
OneOf("ADD", "DROP"),
"USER",
Delimited(
Ref("NakedIdentifierSegment"),
Ref("ObjectReferenceSegment"),
),
),
Sequence(
"RENAME",
"TO",
Ref("NakedIdentifierSegment"),
Ref("ObjectReferenceSegment"),
),
),
)
134 changes: 134 additions & 0 deletions src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -561,6 +561,9 @@ class StatementSegment(ansi_dialect.get_segment("StatementSegment")): # type: i
Ref("AlterFunctionStatementSegment"),
Ref("CreateStageSegment"),
Ref("AlterStageSegment"),
Ref("UnsetStatementSegment"),
Ref("UndropStatementSegment"),
Ref("CommentStatementSegment"),
],
remove=[
Ref("CreateTypeStatementSegment"),
Expand Down Expand Up @@ -3342,3 +3345,134 @@ class DescribeStatementSegment(BaseSegment):
),
),
)


@snowflake_dialect.segment()
class UnsetStatementSegment(BaseSegment):
"""An `UNSET` statement.
https://docs.snowflake.com/en/sql-reference/sql/unset.html
"""

type = "unset_statement"

match_grammar = Sequence(
"UNSET",
OneOf(
Ref("LocalVariableNameSegment"),
Bracketed(
Delimited(
Ref("LocalVariableNameSegment"),
)
),
),
)


@snowflake_dialect.segment()
class UndropStatementSegment(BaseSegment):
"""`UNDROP` statement.
DATABASE: https://docs.snowflake.com/en/sql-reference/sql/undrop-database.html
SCHEMA: https://docs.snowflake.com/en/sql-reference/sql/undrop-schema.html
TABLE: https://docs.snowflake.com/en/sql-reference/sql/undrop-table.html
"""

type = "undrop_statement"
match_grammar = Sequence(
"UNDROP",
OneOf(
Sequence(
"DATABASE",
Ref("DatabaseReferenceSegment"),
),
Sequence(
"SCHEMA",
Ref("SchemaReferenceSegment"),
),
Sequence(
"TABLE",
Ref("TableReferenceSegment"),
),
),
)


@snowflake_dialect.segment()
class CommentStatementSegment(BaseSegment):
"""`COMMENT` statement grammar.
https://docs.snowflake.com/en/sql-reference/sql/comment.html
N.B. this applies to all objects, so there may be some I've missed
here so add any others to the OneOf grammar below.
"""

type = "comment_statement"
match_grammar = Sequence(
"COMMENT",
Sequence(
"IF",
"EXISTS",
optional=True,
),
"ON",
OneOf(
"COLUMN",
"TABLE",
"VIEW",
"SCHEMA",
"DATABASE",
"WAREHOUSE",
"USER",
"STAGE",
"FUNCTION",
"PROCEDURE",
"SEQUENCE",
"SHARE",
"PIPE",
"STREAM",
"TASK",
Sequence(
"NETWORK",
"POLICY",
),
Sequence(
OneOf(
"API",
"NOTIFICATION",
"SECURITY",
"STORAGE",
),
"INTEGRATION",
),
Sequence(
"SESSION",
"POLICY",
),
Sequence(
"EXTERNAL",
"TABLE",
),
Sequence(
"MATERIALIZED",
"VIEW",
),
Sequence(
"MASKING",
"POLICY",
),
Sequence(
"ROW",
"ACCESS",
"POLICY",
),
Sequence(
"FILE",
"FORMAT",
),
),
Ref("ObjectReferenceSegment"),
"IS",
Ref("QuotedLiteralSegment"),
)
1 change: 1 addition & 0 deletions src/sqlfluff/dialects/dialect_snowflake_keywords.py
Expand Up @@ -384,6 +384,7 @@
TRUNCATECOLUMNS
TYPE
UNBOUNDED
UNDROP
UNSET
UNSIGNED
URL
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/dialects/dialect_tsql.py
Expand Up @@ -120,7 +120,7 @@
),
trim_post_subdivide=RegexLexer(
"whitespace",
r"[\t ]+",
r"[^\S\r\n]+",
WhitespaceSegment,
),
),
Expand Down
1 change: 1 addition & 0 deletions src/sqlfluff/rules/L010.py
Expand Up @@ -44,6 +44,7 @@ class Rule_L010(BaseRule):
_target_elems: List[Tuple[str, str]] = [
("type", "keyword"),
("type", "binary_operator"),
("type", "date_part"),
]
config_keywords = ["capitalisation_policy"]
# Human readable target elem for description
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/rules/L041.py
@@ -1,4 +1,4 @@
"""Implementation of Rule L040."""
"""Implementation of Rule L041."""
from typing import Optional

from sqlfluff.core.parser import NewlineSegment, WhitespaceSegment
Expand Down
4 changes: 2 additions & 2 deletions test/core/parser/lexer_test.py
Expand Up @@ -83,9 +83,9 @@ def test__parser__lexer_string(raw, res):
("fsaljk", r"f", "f"),
("fsaljk", r"[fas]*", "fsa"),
# Matching whitespace segments
(" \t fsaljk", r"[\t ]*", " \t "),
(" \t fsaljk", r"[^\S\r\n]*", " \t "),
# Matching whitespace segments (with a newline)
(" \t \n fsaljk", r"[\t ]*", " \t "),
(" \t \n fsaljk", r"[^\S\r\n]*", " \t "),
# Matching quotes containing stuff
("'something boring' \t \n fsaljk", r"'[^']*'", "'something boring'"),
(
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/dialects/ansi/non_breaking_space.sql
@@ -0,0 +1,2 @@
#space before from is non-breaking space
SELECT a,b, c from sch."blah"
31 changes: 31 additions & 0 deletions test/fixtures/dialects/ansi/non_breaking_space.yml
@@ -0,0 +1,31 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# 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: bd4e28c926e3d5460408d9f53857a363b44ddaaba8da61153c9969cd9e37062a
file:
statement:
select_statement:
select_clause:
- keyword: SELECT
- select_clause_element:
column_reference:
identifier: a
- comma: ','
- select_clause_element:
column_reference:
identifier: b
- comma: ','
- select_clause_element:
column_reference:
identifier: c
from_clause:
keyword: from
from_expression:
from_expression_element:
table_expression:
table_reference:
- identifier: sch
- dot: .
- identifier: '"blah"'
5 changes: 5 additions & 0 deletions test/fixtures/dialects/postgres/postgres_discard.sql
@@ -0,0 +1,5 @@
DISCARD ALL;
DISCARD PLANS;
DISCARD SEQUENCES;
DISCARD TEMPORARY;
DISCARD TEMP;
32 changes: 32 additions & 0 deletions test/fixtures/dialects/postgres/postgres_discard.yml
@@ -0,0 +1,32 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# 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: 00eaafbc98c8ec708bf86bddd93c306cd4f60e77c39d8b9d01d8e7aa70b90d7f
file:
- statement:
discard_statement:
- keyword: DISCARD
- keyword: ALL
- statement_terminator: ;
- statement:
discard_statement:
- keyword: DISCARD
- keyword: PLANS
- statement_terminator: ;
- statement:
discard_statement:
- keyword: DISCARD
- keyword: SEQUENCES
- statement_terminator: ;
- statement:
discard_statement:
- keyword: DISCARD
- keyword: TEMPORARY
- statement_terminator: ;
- statement:
discard_statement:
- keyword: DISCARD
- keyword: TEMP
- statement_terminator: ;
2 changes: 2 additions & 0 deletions test/fixtures/dialects/postgres/postgres_reset.sql
@@ -0,0 +1,2 @@
RESET timezone;
RESET ALL;
17 changes: 17 additions & 0 deletions test/fixtures/dialects/postgres/postgres_reset.yml
@@ -0,0 +1,17 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# 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: e9e279911916c47d20deb5cce0ef040ce5e573f71b5044dc43b3a1f3b64bbc23
file:
- statement:
reset_statement:
keyword: RESET
parameter: timezone
- statement_terminator: ;
- statement:
reset_statement:
- keyword: RESET
- keyword: ALL
- statement_terminator: ;
3 changes: 3 additions & 0 deletions test/fixtures/dialects/redshift/redshift_alter_group.sql
Expand Up @@ -10,3 +10,6 @@ drop user dwuser1, dwuser2;

alter group admin_group
rename to administrators;

alter group admin_group
add user "test.user";

0 comments on commit f48b711

Please sign in to comment.