sql_query_analyzer 0.14.0

Static analysis tool for SQL queries with 33 built-in rules for performance, security, and style
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
use super::{Rule, RuleCategory, RuleInfo, Severity, Violation};
use crate::query::{Query, QueryType};

/// Scalar subquery in SELECT (N+1 pattern)
pub struct ScalarSubquery;

impl Rule for ScalarSubquery {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF007",
            name:     "Scalar subquery in SELECT",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        if let Some(from_pos) = upper.find(" FROM ") {
            let select_part = &upper[..from_pos];
            if select_part.contains("SELECT")
                && select_part.matches('(').count() > 0
                && query.has_subquery
            {
                let info = self.info();
                return vec![Violation {
                    rule_id: info.id,
                    rule_name: info.name,
                    message: "Scalar subquery in SELECT causes N+1 query pattern".to_string(),
                    severity: info.severity,
                    category: info.category,
                    suggestion: Some("Use JOIN or window function instead".to_string()),
                    query_index
                }];
            }
        }
        vec![]
    }
}

/// Function call on column prevents index usage
pub struct FunctionOnColumn;

impl Rule for FunctionOnColumn {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF008",
            name:     "Function on indexed column",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        let upper = query.raw.to_uppercase();
        let patterns = [
            "WHERE YEAR(",
            "WHERE MONTH(",
            "WHERE DAY(",
            "WHERE DATE(",
            "WHERE UPPER(",
            "WHERE LOWER(",
            "WHERE TRIM(",
            "WHERE SUBSTRING(",
            "WHERE CAST(",
            "WHERE CONVERT(",
            "WHERE COALESCE("
        ];
        for pattern in patterns {
            if upper.contains(pattern) {
                let info = self.info();
                return vec![Violation {
                    rule_id: info.id,
                    rule_name: info.name,
                    message: "Function call on column in WHERE prevents index usage".to_string(),
                    severity: info.severity,
                    category: info.category,
                    suggestion: Some(
                        "Use computed column, functional index, or rewrite condition".to_string()
                    ),
                    query_index
                }];
            }
        }
        vec![]
    }
}

/// NOT IN with subquery can have NULL issues
pub struct NotInWithSubquery;

impl Rule for NotInWithSubquery {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF009",
            name:     "NOT IN with subquery",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        let upper = query.raw.to_uppercase();
        if upper.contains("NOT IN") && upper.contains("SELECT") {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "NOT IN with subquery can return unexpected results with NULL"
                    .to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some("Use NOT EXISTS or LEFT JOIN with IS NULL instead".to_string()),
                query_index
            }];
        }
        vec![]
    }
}

/// UNION instead of UNION ALL when duplicates don't matter
pub struct UnionWithoutAll;

impl Rule for UnionWithoutAll {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF010",
            name:     "UNION without ALL",
            severity: Severity::Info,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if !query.has_union {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        if upper.contains(" UNION ") && !upper.contains(" UNION ALL ") {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "UNION removes duplicates which requires sorting".to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some("Use UNION ALL if duplicates are acceptable".to_string()),
                query_index
            }];
        }
        vec![]
    }
}

/// SELECT without WHERE on large table
pub struct SelectWithoutWhere;

impl Rule for SelectWithoutWhere {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF011",
            name:     "SELECT without WHERE",
            severity: Severity::Info,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        if query.where_cols.is_empty() && query.limit.is_none() && !query.tables.is_empty() {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "SELECT without WHERE or LIMIT scans entire table".to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some("Add WHERE clause or LIMIT to restrict results".to_string()),
                query_index
            }];
        }
        vec![]
    }
}

/// SELECT * without LIMIT can return unbounded results
pub struct SelectStarWithoutLimit;

impl Rule for SelectStarWithoutLimit {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF001",
            name:     "SELECT * without LIMIT",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        let has_star = query.raw.to_uppercase().contains("SELECT *")
            || query.raw.to_uppercase().contains("SELECT  *");
        if has_star && query.limit.is_none() {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "Query uses SELECT * without LIMIT clause".to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some("Add LIMIT clause or specify explicit columns".to_string()),
                query_index
            }];
        }
        vec![]
    }
}

/// LIKE patterns starting with % prevent index usage
pub struct LeadingWildcard;

impl Rule for LeadingWildcard {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF002",
            name:     "Leading wildcard in LIKE",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        let upper = query.raw.to_uppercase();
        if upper.contains("LIKE '%") || upper.contains("LIKE \"%") {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "LIKE pattern starts with wildcard, preventing index usage".to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some("Consider full-text search or restructure query".to_string()),
                query_index
            }];
        }
        vec![]
    }
}

/// Multiple OR conditions on same column should use IN
pub struct OrInsteadOfIn;

impl Rule for OrInsteadOfIn {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF003",
            name:     "OR instead of IN",
            severity: Severity::Info,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        let upper = query.raw.to_uppercase();
        let or_count = upper.matches(" OR ").count();
        if or_count >= 3 {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: format!(
                    "Query has {} OR conditions, consider using IN clause",
                    or_count
                ),
                severity: info.severity,
                category: info.category,
                suggestion: Some(
                    "Replace multiple OR conditions with IN (val1, val2, ...)".to_string()
                ),
                query_index
            }];
        }
        vec![]
    }
}

/// Large OFFSET values cause performance issues
pub struct LargeOffset;

impl Rule for LargeOffset {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF004",
            name:     "Large OFFSET value",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if let Some(offset) = query.offset
            && offset > 1000
        {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: format!(
                    "OFFSET {} is large, causing performance degradation",
                    offset
                ),
                severity: info.severity,
                category: info.category,
                suggestion: Some("Use keyset pagination (WHERE id > last_id) instead".to_string()),
                query_index
            }];
        }
        vec![]
    }
}

/// Missing JOIN condition creates Cartesian product
pub struct MissingJoinCondition;

impl Rule for MissingJoinCondition {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF005",
            name:     "Potential Cartesian product",
            severity: Severity::Error,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        let table_count = query.tables.len();
        let has_conditions = !query.join_cols.is_empty() || !query.where_cols.is_empty();
        if table_count > 1 && !has_conditions {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: format!(
                    "Query references {} tables without apparent JOIN conditions",
                    table_count
                ),
                severity: info.severity,
                category: info.category,
                suggestion: Some(
                    "Add JOIN conditions or WHERE clause to prevent Cartesian product".to_string()
                ),
                query_index
            }];
        }
        vec![]
    }
}

/// ORDER BY RAND() forces a full scan and sort of every candidate row
///
/// The database must generate a random value per row and sort the whole
/// result set before applying LIMIT, so cost stays O(n log n) regardless
/// of how few rows are returned. Detects the MySQL, PostgreSQL, SQL Server,
/// and Oracle spellings.
pub struct OrderByRandom;

impl Rule for OrderByRandom {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF013",
            name:     "ORDER BY RAND() detected",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        let Some(order_pos) = upper.find("ORDER BY") else {
            return vec![];
        };
        let order_part = &upper[order_pos..];
        let random_funcs = ["RAND()", "RANDOM()", "NEWID()", "DBMS_RANDOM"];
        if random_funcs.iter().any(|f| order_part.contains(f)) {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "ORDER BY RAND() scans and sorts every row before applying LIMIT"
                    .to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some(
                    "For random selection use a random id range (WHERE id >= FLOOR(RAND() * max_id)) or a pre-generated indexed random column"
                        .to_string()
                ),
                query_index
            }];
        }
        vec![]
    }
}

/// COUNT(*) without WHERE scans the whole table
///
/// Counting every row cannot use an index shortcut on most engines; the
/// query time grows linearly with table size and can block writes on busy
/// tables. Existence checks and cached or estimated counts are cheaper.
pub struct CountWithoutWhere;

impl Rule for CountWithoutWhere {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF012",
            name:     "COUNT(*) without WHERE",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        if !query.where_cols.is_empty() || query.tables.is_empty() {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        if !upper.contains("COUNT(") {
            return vec![];
        }
        let info = self.info();
        vec![Violation {
            rule_id: info.id,
            rule_name: info.name,
            message: "COUNT without WHERE clause scans the entire table".to_string(),
            severity: info.severity,
            category: info.category,
            suggestion: Some(
                "Add a WHERE clause, use EXISTS for existence checks, or cache/estimate counts for large tables"
                    .to_string()
            ),
            query_index
        }]
    }
}

/// Large IN value lists degrade planning and execution
///
/// Very long IN lists blow up parse and plan time, defeat plan caching, and
/// on some engines hit hard parameter limits. Severity scales with size:
/// more than 50 items is Info, more than 200 Warning, more than 1000 Error.
pub struct LargeInClause;

/// Counts top-level comma-separated items in an IN list body, returning
/// None when the list is a subquery rather than a value list.
fn in_list_item_count(body: &str) -> Option<usize> {
    if body.trim_start().starts_with("SELECT") {
        return None;
    }
    let mut depth = 0usize;
    let mut items = 1usize;
    for b in body.bytes() {
        match b {
            b'(' => depth += 1,
            b')' => {
                if depth == 0 {
                    break;
                }
                depth -= 1;
            }
            b',' if depth == 0 => items += 1,
            _ => {}
        }
    }
    Some(items)
}

/// Returns the largest IN value-list size found in the statement.
fn max_in_list_size(upper: &str) -> usize {
    let mut max_items = 0;
    let mut search_from = 0;
    while let Some(pos) = upper[search_from..].find(" IN (") {
        let body_start = search_from + pos + " IN (".len();
        if let Some(items) = in_list_item_count(&upper[body_start..]) {
            max_items = max_items.max(items);
        }
        search_from = body_start;
    }
    max_items
}

impl Rule for LargeInClause {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF019",
            name:     "Large IN clause",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        let upper = query.raw.to_uppercase();
        let items = max_in_list_size(&upper);
        if items <= 50 {
            return vec![];
        }
        let severity = if items > 1000 {
            Severity::Error
        } else if items > 200 {
            Severity::Warning
        } else {
            Severity::Info
        };
        let info = self.info();
        vec![Violation {
            rule_id: info.id,
            rule_name: info.name,
            message: format!("IN clause contains {} values", items),
            severity,
            category: info.category,
            suggestion: Some(
                "Load the values into a temporary table and JOIN against it, or split the query into batches"
                    .to_string()
            ),
            query_index
        }]
    }
}

/// HAVING without an aggregate belongs in WHERE
///
/// HAVING filters after grouping, so a condition on plain columns forces the
/// engine to group rows it could have discarded up front. Moving the
/// condition into WHERE prunes rows before the GROUP BY.
pub struct HavingWithoutAggregate;

/// Aggregate function openers that justify a HAVING clause.
const AGGREGATE_OPENERS: [&str; 9] = [
    "COUNT(",
    "SUM(",
    "AVG(",
    "MIN(",
    "MAX(",
    "GROUP_CONCAT(",
    "STRING_AGG(",
    "STDDEV",
    "VARIANCE"
];

impl Rule for HavingWithoutAggregate {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF018",
            name:     "HAVING without aggregate function",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select || query.having_cols.is_empty() {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        let Some(having_pos) = upper.find(" HAVING ") else {
            return vec![];
        };
        let having_part = &upper[having_pos + " HAVING ".len()..];
        let clause_end = [" ORDER BY ", " LIMIT ", " OFFSET "]
            .iter()
            .filter_map(|t| having_part.find(t))
            .min()
            .unwrap_or(having_part.len());
        let clause = &having_part[..clause_end];
        if AGGREGATE_OPENERS.iter().any(|agg| clause.contains(agg)) {
            return vec![];
        }
        let info = self.info();
        vec![Violation {
            rule_id: info.id,
            rule_name: info.name,
            message: "HAVING filters plain columns after grouping".to_string(),
            severity: info.severity,
            category: info.category,
            suggestion: Some(
                "Move non-aggregate conditions into WHERE so rows are pruned before GROUP BY"
                    .to_string()
            ),
            query_index
        }]
    }
}

/// DISTINCT that likely papers over a join fan-out
///
/// DISTINCT combined with JOIN usually hides duplicate rows produced by a
/// missing or too-loose join condition; deduplication then costs a sort or
/// hash over the whole result. `SELECT DISTINCT *` escalates to Warning —
/// deduplicating entire rows is almost never intended.
pub struct UnnecessaryDistinct;

impl Rule for UnnecessaryDistinct {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF014",
            name:     "Potentially unnecessary DISTINCT",
            severity: Severity::Info,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select || !query.has_distinct {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        let distinct_star = upper.contains("SELECT DISTINCT *");
        let distinct_with_join = query.tables.len() > 1;
        if !distinct_star && !distinct_with_join {
            return vec![];
        }
        let info = self.info();
        let (severity, message) = if distinct_star {
            (
                Severity::Warning,
                "SELECT DISTINCT * deduplicates entire rows".to_string()
            )
        } else {
            (
                info.severity,
                "DISTINCT combined with JOIN often hides join fan-out".to_string()
            )
        };
        vec![Violation {
            rule_id: info.id,
            rule_name: info.name,
            message,
            severity,
            category: info.category,
            suggestion: Some(
                "Check the join conditions for fan-out before deduplicating; select explicit columns instead of DISTINCT *"
                    .to_string()
            ),
            query_index
        }]
    }
}

/// Deeply nested subqueries defeat optimizers and readers alike
///
/// Each nesting level multiplies planning complexity and usually hides a
/// JOIN or CTE that would express the same logic flatter and faster.
/// Severity scales with total SELECT depth: three levels is Info, four
/// Warning, five or more Error.
pub struct DeeplyNestedSubqueries;

/// Returns the deepest count of parenthesized SELECTs enclosing each other.
fn max_subquery_depth(upper: &str) -> usize {
    let bytes = upper.as_bytes();
    let mut select_stack: Vec<usize> = Vec::new();
    let mut paren_depth = 0usize;
    let mut max_depth = 0usize;
    for (i, b) in bytes.iter().enumerate() {
        match b {
            b'(' => {
                paren_depth += 1;
                if upper[i + 1..].trim_start().starts_with("SELECT") {
                    select_stack.push(paren_depth);
                    max_depth = max_depth.max(select_stack.len());
                }
            }
            b')' => {
                if select_stack.last() == Some(&paren_depth) {
                    select_stack.pop();
                }
                paren_depth = paren_depth.saturating_sub(1);
            }
            _ => {}
        }
    }
    max_depth
}

impl Rule for DeeplyNestedSubqueries {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF020",
            name:     "Deeply nested subqueries",
            severity: Severity::Warning,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.query_type != QueryType::Select {
            return vec![];
        }
        let upper = query.raw.to_uppercase();
        let levels = max_subquery_depth(&upper) + 1;
        if levels < 3 {
            return vec![];
        }
        let severity = if levels >= 5 {
            Severity::Error
        } else if levels >= 4 {
            Severity::Warning
        } else {
            Severity::Info
        };
        let info = self.info();
        vec![Violation {
            rule_id: info.id,
            rule_name: info.name,
            message: format!("Query nests SELECTs {} levels deep", levels),
            severity,
            category: info.category,
            suggestion: Some(
                "Flatten nested subqueries into JOINs or name the steps with CTEs (WITH ...)"
                    .to_string()
            ),
            query_index
        }]
    }
}

/// DISTINCT with ORDER BY can be inefficient
pub struct DistinctWithOrderBy;

impl Rule for DistinctWithOrderBy {
    fn info(&self) -> RuleInfo {
        RuleInfo {
            id:       "PERF006",
            name:     "DISTINCT with ORDER BY",
            severity: Severity::Info,
            category: RuleCategory::Performance
        }
    }

    fn check(&self, query: &Query, query_index: usize) -> Vec<Violation> {
        if query.has_distinct && !query.order_cols.is_empty() {
            let info = self.info();
            return vec![Violation {
                rule_id: info.id,
                rule_name: info.name,
                message: "Query uses DISTINCT with ORDER BY".to_string(),
                severity: info.severity,
                category: info.category,
                suggestion: Some(
                    "Consider if both are necessary, or use GROUP BY instead".to_string()
                ),
                query_index
            }];
        }
        vec![]
    }
}