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

New rule L069: Consistent syntax for sql type casting #3747

Merged
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fa4ba5e
enforce consistent syntax for SQL type casting
Aug 9, 2022
adc6281
fixed casting in case statements
Aug 10, 2022
5790397
regenerated fixtures
Aug 15, 2022
0bcfed4
removed unused modules, reformatted and added better comments
Aug 15, 2022
f37f2b6
added fixes for multicast
Aug 15, 2022
20d0b54
adjusted new_segment
Aug 15, 2022
cece598
added test cases, renamed rule to 69
Aug 15, 2022
9cda200
Merge branch 'main' of https://github.com/sqlfluff/sqlfluff into 3713…
Aug 15, 2022
ebe121b
added crawl_behaviour and updated functional
Aug 15, 2022
03a74e6
fixed linting errors
Aug 15, 2022
885b079
fixed crawl_behaviour
Aug 15, 2022
d9f00e6
added config for rule L069
Aug 17, 2022
972d791
added support for functional cast_expression in L013
Aug 17, 2022
8cf4337
skip L048 for cast_expression
Aug 17, 2022
ea61481
allow non standard type casting in L034 fixtures
Aug 17, 2022
feb2cd9
added allow_non_standard_type_casting config for rule L069, updated t…
Aug 17, 2022
fe7b88c
regenerated fixture for jinja templater
Aug 17, 2022
4579f81
added more test cases for rule L069
Aug 17, 2022
223c347
Merge branch 'main' of https://github.com/sqlfluff/sqlfluff into 3713…
Aug 17, 2022
7c289c7
config type hints
Aug 17, 2022
a03b38d
skip teradata, updated documentation
Aug 17, 2022
3c61bcc
added test coverage for skipping teradata
Aug 17, 2022
59108bd
Merge branch 'main' of https://github.com/sqlfluff/sqlfluff into 3713…
Aug 17, 2022
1f7933d
discarded config override in L034 tests
Aug 18, 2022
51cca4c
Merge branch 'main' of https://github.com/sqlfluff/sqlfluff into 3713…
Aug 18, 2022
875093d
added cast_expression to L034
Aug 18, 2022
ebeb4ad
added support for different type casting style
Aug 29, 2022
26afac7
added tests
Aug 30, 2022
bbff11a
Update src/sqlfluff/rules/L069.py
bolajiwahab Sep 14, 2022
b70c6c5
Merge branch 'main' of github.com:sqlfluff/sqlfluff into 3713/consist…
bolajiwahab Nov 7, 2022
552bfbc
added more tests for comments, fixed comments raised by @
bolajiwahab Nov 7, 2022
5af5acf
added more tests for comments, fixed comments raised by @
bolajiwahab Nov 7, 2022
b880817
Merge branch 'main' of github.com:sqlfluff/sqlfluff into 3713/consist…
bolajiwahab Nov 7, 2022
4b9baf0
fixing linting errors
bolajiwahab Nov 9, 2022
c26bd6f
Merge branch 'main' of github.com:sqlfluff/sqlfluff into 3713/consist…
bolajiwahab Nov 9, 2022
e317b72
fixing linting errors
bolajiwahab Nov 9, 2022
f850373
Merge branch 'main' of github.com:sqlfluff/sqlfluff into 3713/consist…
bolajiwahab Nov 12, 2022
90cfc3c
regenerated test yaml files
bolajiwahab Nov 12, 2022
457bcc2
added more tests
bolajiwahab Nov 12, 2022
0c5b485
Update src/sqlfluff/dialects/dialect_ansi.py
bolajiwahab Nov 15, 2022
83e3d3f
Update src/sqlfluff/dialects/dialect_ansi.py
bolajiwahab Nov 15, 2022
3855660
Update src/sqlfluff/dialects/dialect_ansi.py
bolajiwahab Nov 15, 2022
1fe15ca
Merge branch 'main' into 3713/consistent_syntax_for_SQL_type_casting
barrywhart Nov 15, 2022
d54506c
Merge branch 'main' into 3713/consistent_syntax_for_SQL_type_casting
tunetheweb Nov 17, 2022
5d30c05
Merge branch 'main' into 3713/consistent_syntax_for_SQL_type_casting
tunetheweb Nov 17, 2022
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
4 changes: 4 additions & 0 deletions src/sqlfluff/core/default_config.cfg
Expand Up @@ -266,3 +266,7 @@ force_enable = False
[sqlfluff:rules:L066]
min_alias_length = None
max_alias_length = None

[sqlfluff:rules:L069]
# SQL type casting
preferred_type_casting_style = consistent
4 changes: 4 additions & 0 deletions src/sqlfluff/core/rules/config_info.py
Expand Up @@ -202,6 +202,10 @@
"validation": ["single", "multiple"],
"definition": "Treatment of wildcards. Defaults to ``single``.",
},
"preferred_type_casting_style": {
"validation": ["consistent", "shorthand", "convert", "cast"],
"definition": ("The expectation for using sql type casting"),
},
}


Expand Down
17 changes: 14 additions & 3 deletions src/sqlfluff/dialects/dialect_ansi.py
Expand Up @@ -1075,7 +1075,18 @@ class ShorthandCastSegment(BaseSegment):

type = "cast_expression"
match_grammar: Matchable = Sequence(
Ref("CastOperatorSegment"), Ref("DatatypeSegment"), allow_gaps=True
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
OneOf(
Ref("Expression_D_Grammar"),
Ref("CaseExpressionSegment"),
),
AnyNumberOf(
Sequence(
Ref("CastOperatorSegment"),
Ref("DatatypeSegment"),
Ref("TimeZoneGrammar", optional=True),
),
bolajiwahab marked this conversation as resolved.
Show resolved Hide resolved
min_times=1,
),
)


Expand Down Expand Up @@ -1885,8 +1896,9 @@ class CaseExpressionSegment(BaseSegment):
Ref("Expression_D_Grammar"),
Ref("CaseExpressionSegment"),
),
AnyNumberOf(Ref("ShorthandCastSegment"), Ref("TimeZoneGrammar")),
AnyNumberOf(Ref("TimeZoneGrammar")),
),
Ref("ShorthandCastSegment"),
),
# Expression_D_Grammar
# https://www.cockroachlabs.com/docs/v20.2/sql-grammar.htm#d_expr
Expand Down Expand Up @@ -3298,7 +3310,6 @@ class SetClauseSegment(BaseSegment):
Ref("ValuesClauseSegment"),
"DEFAULT",
),
AnyNumberOf(Ref("ShorthandCastSegment")),
)


Expand Down
13 changes: 12 additions & 1 deletion src/sqlfluff/rules/L013.py
Expand Up @@ -64,6 +64,18 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
):
return None

# Ignore if it's a cast_expression with non-function enclosed children
# For example, we do not want to ignore something like func()::type
# but we can ignore something like a::type
if children.children().select(
bolajiwahab marked this conversation as resolved.
Show resolved Hide resolved
sp.is_type("cast_expression")
) and not children.children().select(
sp.is_type("cast_expression")
).children().any(
sp.is_type("function")
):
return None

parent_stack = functional_context.parent_stack

# Ignore if it is part of a CTE with column names
Expand Down Expand Up @@ -101,7 +113,6 @@ def _recursively_check_is_complex(select_clause_or_exp_children: Segments) -> bo
"newline",
"column_reference",
"wildcard_expression",
"cast_expression",
"bracketed",
]
selector = sp.not_(sp.is_type(*forgiveable_types))
Expand Down
8 changes: 7 additions & 1 deletion src/sqlfluff/rules/L034.py
Expand Up @@ -141,9 +141,15 @@ def _eval(self, context: RuleContext) -> EvalResultType:
"column_reference",
"object_reference",
"literal",
"cast_expression",
)
# len == 2 to ensure the expression is 'simple'
and len(segment.get_child("expression").segments) == 2
and (
len(segment.get_child("expression").segments) == 2
# cast_expression is one length
or len(segment.get_child("expression").segments)
== 1
)
):
self._validate(i, segment)
except AttributeError:
Expand Down