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

Snowflake: Add dollar quoted string literal #2770

Merged
merged 3 commits into from Mar 2, 2022
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
24 changes: 16 additions & 8 deletions src/sqlfluff/dialects/dialect_snowflake.py
Expand Up @@ -120,13 +120,6 @@
name="warehouse_size",
type="warehouse_size",
),
DoubleQuotedLiteralSegment=NamedParser(
"double_quote",
CodeSegment,
name="quoted_literal",
type="literal",
trim_chars=('"',),
),
Comment on lines -123 to -129
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't part of Snowflake and isn't used anywhere in the dialect so cleaning up.

ValidationModeOptionSegment=RegexParser(
r"'?RETURN_(?:\d+_ROWS|ERRORS|ALL_ERRORS)'?",
CodeSegment,
Expand Down Expand Up @@ -305,6 +298,21 @@
Ref("ColumnReferenceSegment"),
Ref("ExpressionSegment"),
),
QuotedLiteralSegment=OneOf(
# https://docs.snowflake.com/en/sql-reference/data-types-text.html#string-constants
NamedParser(
"single_quote",
CodeSegment,
name="quoted_literal",
type="literal",
),
NamedParser(
"dollar_quote",
CodeSegment,
name="quoted_literal",
type="literal",
),
),
)

# Add all Snowflake keywords
Expand Down Expand Up @@ -505,7 +513,7 @@ class FunctionDefinitionGrammar(BaseSegment):
type = "function_definition"
match_grammar = Sequence(
"AS",
OneOf(Ref("QuotedLiteralSegment"), Ref("DollarQuotedLiteralSegment")),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

DollarQuotedLiteralSegment isn't anywhere else in the code base. We used to have it in Postgres, but was since combined with QuotedLiteralSegment to simplify the dialect. This is probably just a holdover from when Snowflake inherited from Postgres.

Ref("QuotedLiteralSegment"),
Sequence(
"LANGUAGE",
# Not really a parameter, but best fit for now.
Expand Down
@@ -1,3 +1,6 @@
-- In snowflake, a double single quote resolves as a single quote in the string.
-- https://docs.snowflake.com/en/sql-reference/data-types-text.html#single-quoted-string-constants
SELECT '['']'
SELECT '['']';

-- Snowflake allows dollar quoted string literals
select $$abc$$;
12 changes: 10 additions & 2 deletions test/fixtures/dialects/snowflake/snowflake_string_literal.yml
Expand Up @@ -3,11 +3,19 @@
# 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: 46b2fd14241125da6aff2be7065dbf0ed67a3338d19d0ce0b6b66fa58fb31348
_hash: c3e7e29067a382358912c45b9e2ea1832d3e55137e8d40213f8ffa236b5d5596
file:
statement:
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
literal: "'['']'"
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: select
select_clause_element:
literal: $$abc$$
- statement_terminator: ;