Skip to content

Commit

Permalink
Bugfix: ST02 - Compare entire condition expression (#5850)
Browse files Browse the repository at this point in the history
  • Loading branch information
WittierDinosaur committed May 6, 2024
1 parent 49d48a5 commit 99b09d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/sqlfluff/rules/structure/ST02.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,40 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
.children(sp.is_type("column_reference"))
.get()
)
array_accessor_segment = (
Segments(condition_expression)
.children(sp.is_type("array_accessor"))
.get()
)

# Return None if none found (this condition does not apply to functions)
if not column_reference_segment:
return None

if array_accessor_segment:
column_reference_segment_raw_upper = (
column_reference_segment.raw_upper
+ array_accessor_segment.raw_upper
)
else:
column_reference_segment_raw_upper = (
column_reference_segment.raw_upper
)

if else_clauses:
else_expression = else_clauses.children(sp.is_type("expression"))[0]
# Check if we can reduce the CASE expression to a single coalesce
# function.
if (
not is_not_prefix
and column_reference_segment.raw_upper
and column_reference_segment_raw_upper
== else_expression.raw_upper
):
coalesce_arg_1 = else_expression
coalesce_arg_2 = then_expression
elif (
is_not_prefix
and column_reference_segment.raw_upper
and column_reference_segment_raw_upper
== then_expression.raw_upper
):
coalesce_arg_1 = then_expression
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/rules/std_rule_cases/ST02.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ test_pass_case_cannot_be_reduced_13:
configs:
core:
dialect: postgres
test_pass_array_accessors:
pass_str: |
SELECT
CASE
WHEN genres[0] IS NULL
THEN 'x'
ELSE genres
END
AS g
FROM table_t
configs:
core:
dialect: snowflake
test_fail_unnecessary_case_1:
fail_str: |
select
Expand Down

0 comments on commit 99b09d4

Please sign in to comment.