sqruff-lib 0.39.0

A high-speed SQL linter.
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::cell::RefCell;

use hashbrown::{HashMap, HashSet};
use smol_str::SmolStr;
use sqruff_lib_core::dialects::Dialect;
use sqruff_lib_core::dialects::common::AliasInfo;
use sqruff_lib_core::dialects::init::DialectKind;
use sqruff_lib_core::dialects::syntax::{SyntaxKind, SyntaxSet};
use sqruff_lib_core::lint_fix::LintFix;
use sqruff_lib_core::parser::segments::ErasedSegment;
use sqruff_lib_core::parser::segments::object_reference::ObjectReferenceLevel;
use sqruff_lib_core::utils::analysis::query::{Query, QueryInner};
use sqruff_lib_core::utils::analysis::select::{
    SelectStatementColumnsAndTables, get_select_statement_info,
};

use crate::core::config::Value;
use crate::core::rules::context::RuleContext;
use crate::core::rules::crawlers::{Crawler, SegmentSeekerCrawler};
use crate::core::rules::{Erased, ErasedRule, LintResult, Rule, RuleGroups};

#[derive(Default, Clone)]
struct AL05QueryData {
    aliases: Vec<AliasInfo>,
    tbl_refs: Vec<SmolStr>,
}

type QueryKey<'a> = *const RefCell<QueryInner<'a>>;
type AL05State<'a> = HashMap<QueryKey<'a>, AL05QueryData>;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
enum AliasCaseCheck {
    #[default]
    Dialect,
    CaseInsensitive,
    QuotedCaseSensitiveNakedUpper,
    QuotedCaseSensitiveNakedLower,
    CaseSensitive,
}

impl AliasCaseCheck {
    fn from_config(value: &str) -> Result<Self, String> {
        match value {
            "dialect" => Ok(Self::Dialect),
            "case_insensitive" => Ok(Self::CaseInsensitive),
            "quoted_cs_naked_upper" => Ok(Self::QuotedCaseSensitiveNakedUpper),
            "quoted_cs_naked_lower" => Ok(Self::QuotedCaseSensitiveNakedLower),
            "case_sensitive" => Ok(Self::CaseSensitive),
            other => Err(format!("Invalid alias_case_check value: {other}")),
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct RuleAL05 {
    alias_case_check: AliasCaseCheck,
}

impl Rule for RuleAL05 {
    fn load_from_config(&self, config: &HashMap<String, Value>) -> Result<ErasedRule, String> {
        Ok(RuleAL05 {
            alias_case_check: AliasCaseCheck::from_config(
                config["alias_case_check"].as_string().unwrap(),
            )?,
        }
        .erased())
    }

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

    fn description(&self) -> &'static str {
        "Tables should not be aliased if that alias is not used."
    }

    fn long_description(&self) -> &'static str {
        r#"
**Anti-pattern**

In this example, alias `zoo` is not used.

```sql
SELECT
    a
FROM foo AS zoo
```

**Best practice**

Use the alias or remove it. An unused alias makes code harder to read without changing any functionality.

```sql
SELECT
    zoo.a
FROM foo AS zoo

-- Alternatively...

SELECT
    a
FROM foo
```
"#
    }

    fn groups(&self) -> &'static [RuleGroups] {
        &[RuleGroups::All, RuleGroups::Core, RuleGroups::Aliasing]
    }

    fn eval(&self, context: &RuleContext) -> Vec<LintResult> {
        let mut violations = Vec::new();
        let select_info = get_select_statement_info(&context.segment, context.dialect.into(), true);

        let Some(select_info) = select_info else {
            return Vec::new();
        };

        if select_info.table_aliases.is_empty() {
            return Vec::new();
        }

        let query = Query::from_segment(&context.segment, context.dialect, None);
        let mut payloads = AL05State::default();
        self.analyze_table_aliases(query.clone(), context.dialect, &mut payloads);

        let payload = payloads.get(&query.id()).cloned().unwrap_or_default();

        if matches!(
            context.dialect.name,
            DialectKind::Redshift | DialectKind::Bigquery
        ) {
            let mut references: HashSet<SmolStr> = HashSet::new();
            let mut aliases: HashSet<SmolStr> = HashSet::new();

            for alias in &payload.aliases {
                aliases.insert(self.alias_name(alias, context.dialect.name));
                if let Some(alias_segment) = &alias.segment {
                    aliases.insert(self.normalize_identifier(alias_segment, context.dialect.name));
                }
                if let Some(object_reference) = &alias.object_reference {
                    for seg in object_reference.segments() {
                        if const {
                            SyntaxSet::new(&[
                                SyntaxKind::Identifier,
                                SyntaxKind::NakedIdentifier,
                                SyntaxKind::QuotedIdentifier,
                                SyntaxKind::ObjectReference,
                            ])
                        }
                        .contains(seg.get_type())
                        {
                            references.insert(self.normalize_identifier(seg, context.dialect.name));
                        }
                    }
                }
            }

            if aliases.intersection(&references).next().is_some() {
                return Vec::new();
            }
        }

        let mut ref_counter: HashMap<SmolStr, usize> = HashMap::new();
        for alias in &payload.aliases {
            let Some(object_reference) = &alias.object_reference else {
                continue;
            };
            let Some(last_segment) = object_reference.segments().last() else {
                continue;
            };

            *ref_counter
                .entry(self.normalize_identifier(last_segment, context.dialect.name))
                .or_default() += 1;
        }

        for alias in &payload.aliases {
            if Self::is_alias_required(&alias.from_expression_element, context.dialect.name) {
                continue;
            }

            if let Some(object_reference) = &alias.object_reference
                && let Some(last_segment) = object_reference.segments().last()
                && ref_counter
                    .get(&self.normalize_identifier(last_segment, context.dialect.name))
                    .copied()
                    .unwrap_or_default()
                    > 1
            {
                continue;
            }

            if context.dialect.name == DialectKind::Redshift
                && alias.alias_expression.is_some()
                && self.followed_by_qualify(context, alias)
            {
                continue;
            }

            if self.has_function_alias_reference(alias, &select_info, context.dialect.name) {
                continue;
            }

            if alias.aliased
                && !payload
                    .tbl_refs
                    .contains(&self.alias_name(alias, context.dialect.name))
            {
                if Self::has_used_column_aliases(alias, &select_info, context.dialect.name) {
                    continue;
                }
                let violation = self.report_unused_alias(alias);
                violations.push(violation);
            }
        }

        violations
    }

    fn is_fix_compatible(&self) -> bool {
        true
    }

    fn crawl_behaviour(&self) -> Crawler {
        SegmentSeekerCrawler::new(const { SyntaxSet::new(&[SyntaxKind::SelectStatement]) }).into()
    }
}

impl RuleAL05 {
    #[allow(clippy::only_used_in_recursion)]
    fn analyze_table_aliases<'a>(
        &self,
        query: Query<'a>,
        dialect: &Dialect,
        payloads: &mut AL05State<'a>,
    ) {
        payloads.entry(query.id()).or_default();
        let selectables = std::mem::take(&mut RefCell::borrow_mut(&query.inner).selectables);

        for selectable in &selectables {
            if let Some(select_info) = selectable.select_info() {
                let table_aliases = select_info.table_aliases;
                let reference_buffer = select_info.reference_buffer;
                let table_reference_buffer = select_info.table_reference_buffer;

                payloads
                    .entry(query.id())
                    .or_default()
                    .aliases
                    .extend(table_aliases);

                for r in reference_buffer.into_iter().chain(table_reference_buffer) {
                    for tr in
                        r.extract_possible_references(ObjectReferenceLevel::Table, dialect.name)
                    {
                        self.resolve_and_mark_reference(query.clone(), &tr, dialect.name, payloads);
                    }
                }
            }
        }

        RefCell::borrow_mut(&query.inner).selectables = selectables;

        for child in query.children() {
            self.analyze_table_aliases(child, dialect, payloads);
        }
    }

    fn resolve_and_mark_reference<'a>(
        &self,
        query: Query<'a>,
        reference: &sqruff_lib_core::parser::segments::object_reference::ObjectReferencePart,
        dialect: DialectKind,
        payloads: &mut AL05State<'a>,
    ) {
        let Some(reference_segment) = reference.segments.first() else {
            return;
        };
        let normalized_ref = self.normalize_identifier(reference_segment, dialect);

        if let Some(payload) = payloads.get_mut(&query.id())
            && payload
                .aliases
                .iter()
                .any(|it| self.alias_name(it, dialect) == normalized_ref)
        {
            payload.tbl_refs.push(normalized_ref);
            return;
        }

        if let Some(parent) = RefCell::borrow(&query.inner).parent.clone() {
            self.resolve_and_mark_reference(parent, reference, dialect, payloads);
        }
    }

    fn is_alias_required(
        from_expression_element: &ErasedSegment,
        dialect_name: DialectKind,
    ) -> bool {
        for segment in from_expression_element
            .iter_segments(const { &SyntaxSet::new(&[SyntaxKind::Bracketed]) }, false)
        {
            if segment.is_type(SyntaxKind::TableExpression) {
                return if segment
                    .child(const { &SyntaxSet::new(&[SyntaxKind::ValuesClause]) })
                    .is_some()
                {
                    matches!(
                        dialect_name,
                        DialectKind::Athena
                            | DialectKind::Snowflake
                            | DialectKind::Tsql
                            | DialectKind::Postgres
                    )
                } else {
                    segment
                        .iter_segments(const { &SyntaxSet::new(&[SyntaxKind::Bracketed]) }, false)
                        .iter()
                        .any(|seg| {
                            const {
                                SyntaxSet::new(&[
                                    SyntaxKind::SelectStatement,
                                    SyntaxKind::SetExpression,
                                    SyntaxKind::WithCompoundStatement,
                                ])
                            }
                            .contains(seg.get_type())
                        })
                };
            }
        }
        false
    }

    fn has_used_column_aliases(
        alias: &AliasInfo,
        select_info: &SelectStatementColumnsAndTables,
        dialect_name: DialectKind,
    ) -> bool {
        let Some(alias_expression) = &alias.alias_expression else {
            return false;
        };

        // Look for a Bracketed child in the alias expression (the column alias
        // list, e.g. `(value)` or `(c1, c2)`).
        let Some(bracketed) =
            alias_expression.child(const { &SyntaxSet::single(SyntaxKind::Bracketed) })
        else {
            return false;
        };

        // Collect all identifier names from the bracketed column alias list.
        let col_alias_names: Vec<SmolStr> = bracketed
            .recursive_crawl(
                const {
                    &SyntaxSet::new(&[
                        SyntaxKind::NakedIdentifier,
                        SyntaxKind::Identifier,
                        SyntaxKind::QuotedIdentifier,
                    ])
                },
                true,
                &SyntaxSet::EMPTY,
                true,
            )
            .into_iter()
            .map(|seg| seg.raw().to_uppercase().into())
            .collect();

        if col_alias_names.is_empty() {
            return false;
        }

        // Check if any *unqualified* reference (or one qualified with this
        // alias) has an Object-level part matching a column alias name.
        // Qualified references like `o.value` belong to table `o`, not to our
        // alias, so they must not count as usage of the column alias list.
        for reference in &select_info.reference_buffer {
            let table_refs =
                reference.extract_possible_references(ObjectReferenceLevel::Table, dialect_name);
            if let Some(tbl) = table_refs.first() {
                // Qualified reference — only count it if the qualifier is our
                // own table alias.
                if tbl.part.to_uppercase() != alias.ref_str.to_uppercase() {
                    continue;
                }
            }
            // Unqualified reference (no table part) or qualified with our alias.
            for obj_ref in
                reference.extract_possible_references(ObjectReferenceLevel::Object, dialect_name)
            {
                if col_alias_names.contains(&SmolStr::from(obj_ref.part.to_uppercase())) {
                    return true;
                }
            }
        }

        false
    }

    fn has_function_alias_reference(
        &self,
        alias: &AliasInfo,
        select_info: &SelectStatementColumnsAndTables,
        dialect_name: DialectKind,
    ) -> bool {
        let Some(table_expression) = alias
            .from_expression_element
            .child(const { &SyntaxSet::single(SyntaxKind::TableExpression) })
        else {
            return false;
        };

        if table_expression
            .child(const { &SyntaxSet::single(SyntaxKind::Function) })
            .is_none()
        {
            return false;
        }

        let alias_name = self.alias_name(alias, dialect_name);

        select_info.reference_buffer.iter().any(|reference| {
            let references = reference.iter_raw_references();
            if references.len() != 1 {
                return false;
            }

            references
                .first()
                .and_then(|reference_part| reference_part.segments.first())
                .is_some_and(|segment| {
                    self.normalize_identifier(segment, dialect_name) == alias_name
                })
        })
    }

    fn report_unused_alias(&self, alias: &AliasInfo) -> LintResult {
        let mut fixes = vec![LintFix::delete(alias.alias_expression.clone().unwrap())];

        // Delete contiguous whitespace/meta immediately preceding the alias expression
        // without allocating intermediate Segments collections.
        if let Some(alias_idx) = alias
            .from_expression_element
            .segments()
            .iter()
            .position(|s| s == alias.alias_expression.as_ref().unwrap())
        {
            for seg in alias.from_expression_element.segments()[..alias_idx]
                .iter()
                .rev()
                .take_while(|s| s.is_whitespace() || s.is_meta())
            {
                fixes.push(LintFix::delete(seg.clone()));
            }
        }

        LintResult::new(
            alias.segment.clone(),
            fixes,
            format!(
                "Alias '{}' is never used in SELECT statement.",
                self.display_alias_name(alias)
            )
            .into(),
            None,
        )
    }

    fn alias_name(&self, alias: &AliasInfo, dialect: DialectKind) -> SmolStr {
        alias
            .segment
            .as_ref()
            .map(|segment| self.normalize_identifier(segment, dialect))
            .unwrap_or_else(|| self.normalize_identifier_str(&alias.ref_str, None, dialect))
    }

    fn display_alias_name(&self, alias: &AliasInfo) -> String {
        alias
            .segment
            .as_ref()
            .map(|segment| {
                self.normalize_identifier_str(
                    segment.raw(),
                    Some(segment.get_type()),
                    DialectKind::Ansi,
                )
            })
            .unwrap_or_else(|| {
                self.normalize_identifier_str(&alias.ref_str, None, DialectKind::Ansi)
            })
            .to_string()
    }

    fn normalize_identifier(&self, identifier: &ErasedSegment, dialect: DialectKind) -> SmolStr {
        self.normalize_identifier_str(identifier.raw(), Some(identifier.get_type()), dialect)
    }

    fn normalize_identifier_str(
        &self,
        raw: &str,
        syntax_kind: Option<SyntaxKind>,
        dialect: DialectKind,
    ) -> SmolStr {
        let is_naked = syntax_kind.is_none_or(|kind| {
            matches!(kind, SyntaxKind::Identifier | SyntaxKind::NakedIdentifier)
        }) && !matches!(
            raw.chars().next(),
            Some('"') | Some('\'') | Some('`') | Some('[')
        );
        let mut normalized = if raw.starts_with('[') && raw.ends_with(']') && raw.len() >= 2 {
            raw[1..raw.len() - 1].to_string()
        } else if matches!(raw.chars().next(), Some('"') | Some('\'') | Some('`'))
            && raw.len() >= 2
            && raw.chars().next() == raw.chars().last()
        {
            let quote = raw.chars().next().unwrap();
            raw[1..raw.len() - 1].replace(&format!("{quote}{quote}"), &quote.to_string())
        } else {
            raw.to_string()
        };

        match self.alias_case_check {
            AliasCaseCheck::Dialect => {
                if is_naked {
                    normalized = match dialect {
                        DialectKind::Postgres | DialectKind::Redshift => normalized.to_lowercase(),
                        _ => normalized.to_uppercase(),
                    };
                }
            }
            AliasCaseCheck::CaseInsensitive => normalized = normalized.to_uppercase(),
            AliasCaseCheck::QuotedCaseSensitiveNakedUpper => {
                if is_naked {
                    normalized = normalized.to_uppercase();
                }
            }
            AliasCaseCheck::QuotedCaseSensitiveNakedLower => {
                if is_naked {
                    normalized = normalized.to_lowercase();
                }
            }
            AliasCaseCheck::CaseSensitive => {}
        }

        normalized.into()
    }

    fn followed_by_qualify(&self, context: &RuleContext, alias: &AliasInfo) -> bool {
        let Some(alias_expression) = &alias.alias_expression else {
            return false;
        };
        let mut current_from_seen = false;

        for seg in context.segment.segments() {
            if alias_expression.get_end_loc() == seg.get_end_loc() {
                current_from_seen = true;
            } else if current_from_seen && !seg.is_code() {
                continue;
            } else if current_from_seen && seg.is_type(SyntaxKind::QualifyClause) {
                return true;
            } else if current_from_seen {
                return false;
            }
        }

        false
    }
}

#[cfg(test)]
mod tests {
    use crate::core::config::FluffConfig;
    use crate::core::linter::core::Linter;

    const POSTGRES_JSON_ALIAS_REPRODUCER: &str = r#"with stanza as (
    select
        data -> 'name' as name,
        data -> 'backup' -> (
            jsonb_array_length(data -> 'backup') - 1
        ) as last_backup,
        data -> 'archive' -> (
            jsonb_array_length(data -> 'archive') - 1
        ) as current_archive
    from jsonb_array_elements(monitor.pgbackrest_info()) as data
)

select
    name,
    to_timestamp(
        (last_backup -> 'timestamp' ->> 'stop')::numeric
    ) as last_successful_backup,
    current_archive ->> 'max' as last_archived_wal
from stanza;
"#;

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

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

    #[test]
    fn test_al05_postgres_json_operator_alias_is_used() {
        let mut linter = postgres_al05_linter();
        let linted = linter
            .lint_string_wrapped(POSTGRES_JSON_ALIAS_REPRODUCER, false)
            .unwrap();

        assert_eq!(linted.violations(), &[]);
    }

    #[test]
    fn test_al05_postgres_json_operator_fix_preserves_alias() {
        let mut linter = postgres_al05_linter();
        let linted = linter
            .lint_string_wrapped(POSTGRES_JSON_ALIAS_REPRODUCER, true)
            .unwrap();

        assert_eq!(linted.fix_string(), POSTGRES_JSON_ALIAS_REPRODUCER);
    }
}