Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/lib-dialects/src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3953,7 +3953,11 @@ pub fn raw_dialect() -> Dialect {
];
})
.to_matchable(),
Ref::new("ElseClauseSegment").optional().to_matchable(),
Ref::new("ElseClauseSegment")
.optional()
.reset_terminators()
.terminators(vec![Ref::keyword("END").to_matchable()])
.to_matchable(),
MetaSegment::dedent().to_matchable(),
Ref::keyword("END").to_matchable(),
])
Expand All @@ -3971,7 +3975,11 @@ pub fn raw_dialect() -> Dialect {
];
})
.to_matchable(),
Ref::new("ElseClauseSegment").optional().to_matchable(),
Ref::new("ElseClauseSegment")
.optional()
.reset_terminators()
.terminators(vec![Ref::keyword("END").to_matchable()])
.to_matchable(),
MetaSegment::dedent().to_matchable(),
Ref::keyword("END").to_matchable(),
])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select case
when a = 1 then 'one'
when a = 2 then 'two'
else 'other' || 's'
end as b
from test;
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
file:
- statement:
- select_statement:
- select_clause:
- keyword: select
- select_clause_element:
- expression:
- case_expression:
- keyword: case
- when_clause:
- keyword: when
- expression:
- column_reference:
- naked_identifier: a
- comparison_operator:
- raw_comparison_operator: =
- numeric_literal: '1'
- keyword: then
- expression:
- quoted_literal: '''one'''
- when_clause:
- keyword: when
- expression:
- column_reference:
- naked_identifier: a
- comparison_operator:
- raw_comparison_operator: =
- numeric_literal: '2'
- keyword: then
- expression:
- quoted_literal: '''two'''
- else_clause:
- keyword: else
- expression:
- quoted_literal: '''other'''
- binary_operator:
- pipe: '|'
- pipe: '|'
- quoted_literal: '''s'''
- keyword: end
- alias_expression:
- keyword: as
- naked_identifier: b
- from_clause:
- keyword: from
- from_expression:
- from_expression_element:
- table_expression:
- table_reference:
- naked_identifier: test
- statement_terminator: ;
97 changes: 97 additions & 0 deletions crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,19 @@ mod tests {
use crate::core::config::FluffConfig;
use crate::core::linter::core::Linter;

fn postgres_all_rules_linter() -> Linter {
let config = FluffConfig::from_source(
r#"
[sqruff]
dialect = postgres
rules = all
"#,
None,
);

Linter::new(config, None, None, true).unwrap()
}

fn normalise_paths(paths: Vec<String>) -> Vec<String> {
paths
.into_iter()
Expand Down Expand Up @@ -1082,4 +1095,88 @@ mod tests {
"Should not have LT01 false positives on template syntax"
);
}

#[test]
fn test_postgres_case_else_concat_does_not_raise_lt01_and_fixes_cleanly() {
let sql = r#"select case
when a = 1 then 'one'
when a = 2 then 'two'
else 'other' || 's'
end as b
from test;
"#;
let expected = r#"select
case
when a = 1 then 'one'
when a = 2 then 'two'
else 'other' || 's'
end as b
from test;
"#;

let mut linter = postgres_all_rules_linter();
let linted = linter.lint_string_wrapped(sql, false).unwrap();
let violations = linted.violations();

assert!(
!violations.iter().any(|v| v.rule_code() == "LT01"),
"Expected no LT01 violations, got: {:?}",
violations
.iter()
.map(|v| (v.rule_code(), v.desc().to_string()))
.collect::<Vec<_>>()
);
assert!(
violations.iter().all(|v| v.rule_code() == "LT02"),
"Expected only LT02 violations, got: {:?}",
violations
.iter()
.map(|v| (v.rule_code(), v.desc().to_string()))
.collect::<Vec<_>>()
);

let fixed = postgres_all_rules_linter()
.lint_string_wrapped(sql, true)
.unwrap()
.fix_string();

assert_eq!(fixed, expected);
}

#[test]
fn test_postgres_case_else_binary_operator_spacing_still_triggers_lt01() {
let sql = r#"select case
when a = 1 then 'one'
else 1+2
end as b
from test;
"#;
let expected = r#"select
case
when a = 1 then 'one'
else 1 + 2
end as b
from test;
"#;

let mut linter = postgres_all_rules_linter();
let linted = linter.lint_string_wrapped(sql, false).unwrap();
let violations = linted.violations();

assert!(
violations.iter().any(|v| v.rule_code() == "LT01"),
"Expected LT01 violations, got: {:?}",
violations
.iter()
.map(|v| (v.rule_code(), v.desc().to_string()))
.collect::<Vec<_>>()
);

let fixed = postgres_all_rules_linter()
.lint_string_wrapped(sql, true)
.unwrap()
.fix_string();

assert_eq!(fixed, expected);
}
}
29 changes: 29 additions & 0 deletions crates/lib/test/fixtures/rules/std_rule_cases/LT01-operators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ pass_tsql_assignment_operator:
pass_concat_string:
pass_str: SELECT 'barry' || 'pollard'

pass_postgres_case_else_concat_string:
pass_str: |
select case
when a = 1 then 'one'
when a = 2 then 'two'
else 'other' || 's'
end as b
from test;
configs:
core:
dialect: postgres

fail_postgres_case_else_binary_operator_spacing:
fail_str: |
select case
when a = 1 then 'one'
else 1+2
end as b
from test;
fix_str: |
select case
when a = 1 then 'one'
else 1 + 2
end as b
from test;
configs:
core:
dialect: postgres

test_pass_placeholder_spacing:
# Test for spacing issues around placeholders
# https://github.com/sqlfluff/sqlfluff/issues/4253
Expand Down
Loading