Skip to content

Commit

Permalink
Bug fix: Disable L059 in snowflake dialect (#3260)
Browse files Browse the repository at this point in the history
* Bug fix: Disable L059 in snowflake dialect

* Add note explaining the rule is disabled for Snowflake

* Update src/sqlfluff/rules/L059.py

Co-authored-by: Danny Jones <51742311+WittierDinosaur@users.noreply.github.com>

Co-authored-by: Barry Hart <barry.hart@mailchimp.com>
Co-authored-by: Danny Jones <51742311+WittierDinosaur@users.noreply.github.com>
  • Loading branch information
3 people committed May 5, 2022
1 parent 8e23757 commit 6954d68
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sqlfluff/core/default_config.cfg
Expand Up @@ -169,6 +169,7 @@ ignore_words_regex = None
prefer_quoted_identifiers = False
ignore_words = None
ignore_words_regex = None
force_enable = False

[sqlfluff:rules:L062]
# Comma separated list of blocked words that should not be used
Expand Down
20 changes: 20 additions & 0 deletions src/sqlfluff/rules/L059.py
Expand Up @@ -24,6 +24,13 @@ class Rule_L059(BaseRule):
When ``prefer_quoted_identifiers = False`` (default behaviour), the quotes are
unnecessary, except for reserved keywords and special characters in identifiers.
.. note::
This rule is disabled by default for Snowflake because it allows quotes as
part of the column name. In other words, ``date`` and ``"date"`` are two
different columns.
It can be enabled with the ``force_enable = True`` flag.
**Anti-pattern**
In this example, a valid unquoted identifier,
Expand Down Expand Up @@ -75,7 +82,9 @@ class Rule_L059(BaseRule):
"prefer_quoted_identifiers",
"ignore_words",
"ignore_words_regex",
"force_enable",
]
_dialects_allowing_quotes_in_column_names = ["snowflake"]

# Ignore "password_auth" type to allow quotes around passwords within
# `CREATE USER` statements in Exasol dialect.
Expand All @@ -87,6 +96,17 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
self.prefer_quoted_identifiers: bool
self.ignore_words: str
self.ignore_words_regex: str
self.force_enable: bool
# Some dialects allow quotes as PART OF the column name. In other words,
# these are two different columns:
# - date
# - "date"
# For safety, disable this rule by default in those dialects.
if (
context.dialect.name in self._dialects_allowing_quotes_in_column_names
and not self.force_enable
):
return LintResult()

# Ignore some segment types
if context.functional.parent_stack.any(sp.is_type(*self._ignore_types)):
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/rules/std_rule_cases/L059.yml
Expand Up @@ -326,3 +326,34 @@ test_pass_insert_overwrite_directory:
configs:
core:
dialect: sparksql

test_fail_quoted_column_ansi:
fail_str: |
SELECT d."date"
FROM d
fix_str: |
SELECT d.date
FROM d
test_pass_quoted_column_snowflake:
# The rule is disabled by default in Snowflake.
pass_str: |
SELECT d."date"
FROM d
configs:
core:
dialect: snowflake

test_fail_quoted_column_snowflake_force_enable:
fail_str: |
SELECT d."date"
FROM d
fix_str: |
SELECT d.date
FROM d
configs:
core:
dialect: snowflake
rules:
L059:
force_enable: true

0 comments on commit 6954d68

Please sign in to comment.