1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use ahash::AHashMap;

use crate::core::config::Value;
use crate::core::parser::segments::keyword::KeywordSegment;
use crate::core::rules::base::{Erased, ErasedRule, LintResult, Rule};
use crate::core::rules::context::RuleContext;
use crate::core::rules::crawlers::{Crawler, SegmentSeekerCrawler};
use crate::helpers::ToErasedSegment;
use crate::utils::reflow::sequence::{Filter, ReflowSequence};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Aliasing {
    Explicit,
    Implicit,
}

#[derive(Debug, Clone)]
pub struct RuleAL01 {
    aliasing: Aliasing,
    target_parent_types: &'static [&'static str],
}

impl RuleAL01 {
    pub fn aliasing(mut self, aliasing: Aliasing) -> Self {
        self.aliasing = aliasing;
        self
    }

    pub fn target_parent_types(mut self, target_parent_types: &'static [&'static str]) -> Self {
        self.target_parent_types = target_parent_types;
        self
    }
}

impl Default for RuleAL01 {
    fn default() -> Self {
        Self {
            aliasing: Aliasing::Explicit,
            target_parent_types: &["from_expression_element", "merge_statement"],
        }
    }
}

impl Rule for RuleAL01 {
    fn load_from_config(&self, config: &AHashMap<String, Value>) -> ErasedRule {
        let aliasing = match config.get("aliasing").unwrap().as_string().unwrap() {
            "explicit" => Aliasing::Explicit,
            "implicit" => Aliasing::Implicit,
            _ => unreachable!(),
        };

        RuleAL01 { aliasing, target_parent_types: &["from_expression_element", "merge_statement"] }
            .erased()
    }

    fn name(&self) -> &'static str {
        "aliasing.table"
    }

    fn description(&self) -> &'static str {
        "Implicit/explicit aliasing of table."
    }

    fn eval(&self, rule_cx: RuleContext) -> Vec<LintResult> {
        let last_seg = rule_cx.parent_stack.last().unwrap();
        let last_seg_ty = last_seg.get_type();

        if self.target_parent_types.iter().any(|&it| last_seg_ty == it) {
            let as_keyword = rule_cx
                .segment
                .segments()
                .iter()
                .find(|seg| seg.get_raw_upper() == Some("AS".into()))
                .cloned();

            if let Some(as_keyword) = as_keyword {
                if self.aliasing == Aliasing::Implicit {
                    return vec![LintResult::new(
                        as_keyword.clone().into(),
                        ReflowSequence::from_around_target(
                            &as_keyword,
                            rule_cx.parent_stack[0].clone(),
                            "both",
                            rule_cx.config.unwrap(),
                        )
                        .without(&as_keyword)
                        .respace(false, Filter::All)
                        .fixes(),
                        None,
                        None,
                        None,
                    )];
                }
            } else if self.aliasing != Aliasing::Implicit {
                let identifier = rule_cx
                    .segment
                    .get_raw_segments()
                    .iter()
                    .find(|seg| seg.is_code())
                    .expect("Failed to find identifier. Raise this as a bug on GitHub.")
                    .clone();

                return vec![LintResult::new(
                    rule_cx.segment.clone().into(),
                    ReflowSequence::from_around_target(
                        &identifier,
                        rule_cx.parent_stack[0].clone(),
                        "before",
                        rule_cx.config.unwrap(),
                    )
                    .insert(
                        KeywordSegment::new("AS".into(), None).to_erased_segment(),
                        identifier.clone(),
                        "before",
                    )
                    .respace(false, Filter::All)
                    .fixes(),
                    None,
                    None,
                    None,
                )];
            }
        }

        Vec::new()
    }

    fn crawl_behaviour(&self) -> Crawler {
        SegmentSeekerCrawler::new(["alias_expression"].into()).into()
    }
}

#[cfg(test)]
mod tests {
    use crate::api::simple::fix;
    use crate::core::rules::base::Erased;
    use crate::rules::aliasing::AL01::{Aliasing, RuleAL01};

    #[test]
    fn test_fail_default_explicit() {
        let sql = "select foo.bar from table1 foo";
        let result = fix(sql.to_string(), vec![RuleAL01::default().erased()]);

        assert_eq!(result, "select foo.bar from table1 AS foo");
    }

    #[test]
    fn test_fail_explicit() {
        let sql = "select foo.bar from table1 foo";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Explicit).erased()]);

        assert_eq!(result, "select foo.bar from table1 AS foo");
    }

    #[test]
    fn test_fail_implicit() {
        let sql = "select foo.bar from table1 AS foo";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Implicit).erased()]);

        assert_eq!(result, "select foo.bar from table1 foo");
    }

    #[test]
    fn test_fail_implicit_alias() {
        let sql = "select foo.bar from (select 1 as bar)foo";
        let result = fix(sql.to_string(), vec![RuleAL01::default().erased()]);

        assert_eq!(result, "select foo.bar from (select 1 as bar) AS foo");
    }

    #[test]
    fn test_fail_implicit_alias_space() {
        let sql = "select foo.bar from (select 1 as bar) foo";
        let result = fix(sql.to_string(), vec![RuleAL01::default().erased()]);

        assert_eq!(result, "select foo.bar from (select 1 as bar) AS foo");
    }

    #[test]
    fn test_fail_implicit_alias_explicit() {
        let sql = "select foo.bar from (select 1 as bar) foo";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Explicit).erased()]);

        assert_eq!(result, "select foo.bar from (select 1 as bar) AS foo");
    }

    #[test]
    fn test_fail_implicit_alias_implicit() {
        let sql = "select foo.bar from (select 1 as bar) AS foo";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Implicit).erased()]);

        assert_eq!(result, "select foo.bar from (select 1 as bar) foo");
    }

    #[test]
    fn test_fail_implicit_alias_implicit_multiple() {
        let sql = "select foo.bar from (select 1 as bar) AS bar, (select 1 as foo) AS foo";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Implicit).erased()]);

        assert_eq!(result, "select foo.bar from (select 1 as bar) bar, (select 1 as foo) foo");
    }

    #[test]
    fn test_fail_implicit_alias_implicit_newline() {
        let sql = "select foo.bar from (select 1 as bar)\nAS foo";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Implicit).erased()]);

        assert_eq!(result, "select foo.bar from (select 1 as bar)\nfoo");
    }

    #[test]
    #[ignore = "parser bug"]
    fn test_fail_default_explicit_alias_merge() {
        let sql = "MERGE dataset.inventory t\nUSING dataset.newarrivals s\n    ON t.product = \
                   s.product\nWHEN MATCHED THEN\n    UPDATE SET quantity = t.quantity + \
                   s.quantity;";
        let result = fix(sql.to_string(), vec![RuleAL01::default().erased()]);
        assert_eq!(
            result,
            "MERGE dataset.inventory AS t\nUSING dataset.newarrivals AS s\n    ON t.product = \
             s.product\nWHEN MATCHED THEN\n    UPDATE SET quantity = t.quantity + s.quantity;"
        );
    }

    #[test]
    #[ignore = "parser bug"]
    fn test_fail_explicit_alias_merge() {
        let sql = "MERGE dataset.inventory t\nUSING dataset.newarrivals s\n    ON t.product = \
                   s.product\nWHEN MATCHED THEN\n    UPDATE SET quantity = t.quantity + \
                   s.quantity;";
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Explicit).erased()]);
        assert_eq!(
            result,
            "MERGE dataset.inventory AS t\nUSING dataset.newarrivals AS s\n    ON t.product = \
             s.product\nWHEN MATCHED THEN\n    UPDATE SET quantity = t.quantity + s.quantity;"
        );
    }

    #[test]
    fn test_pass_implicit_alias_merge() {
        let sql = "MERGE dataset.inventory t\nUSING dataset.newarrivals s\n    ON t.product = \
                   s.product\nWHEN MATCHED THEN\n    UPDATE SET quantity = t.quantity + \
                   s.quantity;";
        // This test seems to expect the same SQL to be valid under implicit aliasing
        // settings, hence no change in the result. Assuming `fix` returns the
        // original SQL if no changes are required.
        let result =
            fix(sql.to_string(), vec![RuleAL01::default().aliasing(Aliasing::Implicit).erased()]);
        assert_eq!(result, sql);
    }

    #[test]
    fn test_alias_expression_4492() {
        let sql = "SELECT\n    voo.a\nFROM foo voo";
        let result = fix(sql.to_string(), vec![RuleAL01::default().erased()]);
        assert_eq!(result, "SELECT\n    voo.a\nFROM foo AS voo");
    }
}