sqruff_lib/rules/ambiguous/
am02.rs1use ahash::AHashMap;
2use sqruff_lib_core::dialects::init::DialectKind;
3use sqruff_lib_core::dialects::syntax::{SyntaxKind, SyntaxSet};
4use sqruff_lib_core::lint_fix::LintFix;
5use sqruff_lib_core::parser::segments::base::SegmentBuilder;
6
7use crate::core::config::Value;
8use crate::core::rules::base::{CloneRule, ErasedRule, LintResult, Rule, RuleGroups};
9use crate::core::rules::context::RuleContext;
10use crate::core::rules::crawlers::{Crawler, SegmentSeekerCrawler};
11
12#[derive(Clone, Debug, Default)]
13pub struct RuleAM02;
14
15impl Rule for RuleAM02 {
16 fn load_from_config(&self, _config: &AHashMap<String, Value>) -> Result<ErasedRule, String> {
17 Ok(RuleAM02.erased())
18 }
19
20 fn name(&self) -> &'static str {
21 "ambiguous.union"
22 }
23
24 fn description(&self) -> &'static str {
25 "Look for UNION keyword not immediately followed by DISTINCT or ALL"
26 }
27
28 fn long_description(&self) -> &'static str {
29 r#"
30**Anti-pattern**
31
32In this example, `UNION DISTINCT` should be preferred over `UNION`, because explicit is better than implicit.
33
34
35```sql
36SELECT a, b FROM table_1
37UNION
38SELECT a, b FROM table_2
39```
40
41**Best practice**
42
43Specify `DISTINCT` or `ALL` after `UNION` (note that `DISTINCT` is the default behavior).
44
45```sql
46SELECT a, b FROM table_1
47UNION DISTINCT
48SELECT a, b FROM table_2
49```
50"#
51 }
52
53 fn groups(&self) -> &'static [RuleGroups] {
54 &[RuleGroups::All, RuleGroups::Core, RuleGroups::Ambiguous]
55 }
56
57 fn dialect_skip(&self) -> &'static [DialectKind] {
58 &[
61 DialectKind::Bigquery,
62 DialectKind::Postgres,
63 DialectKind::Snowflake,
64 DialectKind::Clickhouse,
65 DialectKind::Sparksql,
66 DialectKind::Duckdb,
67 ]
68 }
69
70 fn eval(&self, rule_cx: &RuleContext) -> Vec<LintResult> {
71 let raw = rule_cx.segment.raw();
72 let raw_upper = raw.to_uppercase();
73
74 if rule_cx.segment.raw().contains("union")
75 && !(raw_upper.contains("ALL") || raw_upper.contains("DISTINCT"))
76 {
77 let edits = vec![
78 SegmentBuilder::keyword(rule_cx.tables.next_id(), "union"),
79 SegmentBuilder::whitespace(rule_cx.tables.next_id(), " "),
80 SegmentBuilder::keyword(rule_cx.tables.next_id(), "distinct"),
81 ];
82
83 let segments = rule_cx.segment.clone();
84 let fixes = vec![LintFix::replace(
85 rule_cx.segment.segments()[0].clone(),
86 edits,
87 None,
88 )];
89
90 return vec![LintResult::new(Some(segments), fixes, None, None)];
91 } else if raw_upper.contains("UNION")
92 && !(raw_upper.contains("ALL") || raw_upper.contains("DISTINCT"))
93 {
94 let edits = vec![
95 SegmentBuilder::keyword(rule_cx.tables.next_id(), "UNION"),
96 SegmentBuilder::whitespace(rule_cx.tables.next_id(), " "),
97 SegmentBuilder::keyword(rule_cx.tables.next_id(), "DISTINCT"),
98 ];
99
100 let segments = rule_cx.segment.clone();
101 let fixes = vec![LintFix::replace(
102 rule_cx.segment.segments()[0].clone(),
103 edits,
104 None,
105 )];
106
107 return vec![LintResult::new(Some(segments), fixes, None, None)];
108 }
109
110 Vec::new()
111 }
112
113 fn is_fix_compatible(&self) -> bool {
114 true
115 }
116
117 fn crawl_behaviour(&self) -> Crawler {
118 SegmentSeekerCrawler::new(const { SyntaxSet::new(&[SyntaxKind::SetOperator]) }).into()
119 }
120}