Skip to main content

flowscope_core/linter/rules/
cv_012.rs

1//! LINT_CV_012: JOIN condition convention.
2//!
3//! Plain `JOIN` clauses without ON/USING should use explicit join predicates,
4//! not implicit relationships hidden in WHERE.
5
6use crate::linter::rule::{LintContext, LintRule};
7use crate::types::{issue_codes, Issue, IssueAutofixApplicability, IssuePatchEdit, Span};
8use sqlparser::ast::{
9    BinaryOperator, Expr, JoinConstraint, JoinOperator, Select, Spanned, Statement, TableFactor,
10    TableWithJoins,
11};
12use sqlparser::tokenizer::{Span as SqlParserSpan, Token, TokenWithSpan, Tokenizer};
13use std::collections::HashSet;
14
15use super::semantic_helpers::{table_factor_reference_name, visit_selects_in_statement};
16
17pub struct ConventionJoinCondition;
18
19impl LintRule for ConventionJoinCondition {
20    fn code(&self) -> &'static str {
21        issue_codes::LINT_CV_012
22    }
23
24    fn name(&self) -> &'static str {
25        "Join condition convention"
26    }
27
28    fn description(&self) -> &'static str {
29        "Use `JOIN ... ON ...` instead of `WHERE ...` for join conditions."
30    }
31
32    fn check(&self, statement: &Statement, ctx: &LintContext) -> Vec<Issue> {
33        let mut found_violation = false;
34        let mut autofix_edits: Vec<IssuePatchEdit> = Vec::new();
35
36        visit_selects_in_statement(statement, &mut |select| {
37            let fix_result = cv012_select_autofix_result(select, ctx);
38            if fix_result.has_violation {
39                found_violation = true;
40                autofix_edits.extend(fix_result.edits);
41            }
42        });
43
44        if !found_violation {
45            return Vec::new();
46        }
47
48        sort_and_dedup_patch_edits(&mut autofix_edits);
49        if patch_edits_overlap(&autofix_edits) {
50            autofix_edits.clear();
51        }
52
53        let mut issue = Issue::warning(
54            issue_codes::LINT_CV_012,
55            "JOIN clause appears to lack a meaningful join condition.",
56        )
57        .with_statement(ctx.statement_index);
58        if !autofix_edits.is_empty() {
59            issue = issue.with_autofix_edits(IssueAutofixApplicability::Safe, autofix_edits);
60        }
61        vec![issue]
62    }
63}
64
65#[derive(Default)]
66struct Cv12SelectFixResult {
67    has_violation: bool,
68    edits: Vec<IssuePatchEdit>,
69}
70
71#[derive(Clone)]
72struct Cv12JoinFixPlan {
73    join_index: usize,
74    predicates: Vec<Expr>,
75}
76
77fn cv012_select_autofix_result(select: &Select, ctx: &LintContext) -> Cv12SelectFixResult {
78    let Some(where_expr) = &select.selection else {
79        return Cv12SelectFixResult::default();
80    };
81
82    let select_abs_start = sqlparser_span_abs_offsets(ctx, select.span())
83        .map(|(start, _)| start)
84        .unwrap_or(ctx.statement_range.start);
85
86    let mut has_violation = false;
87    let mut extracted_predicates: Vec<Expr> = Vec::new();
88    let mut edits: Vec<IssuePatchEdit> = Vec::new();
89
90    for table in &select.from {
91        let Some(join_plans) = cv012_plan_join_chain(table, where_expr) else {
92            continue;
93        };
94        has_violation = true;
95
96        for plan in join_plans {
97            if plan.predicates.is_empty() {
98                continue;
99            }
100
101            let Some(join) = table.joins.get(plan.join_index) else {
102                return Cv12SelectFixResult {
103                    has_violation,
104                    edits: Vec::new(),
105                };
106            };
107
108            let Some((_, relation_end_abs)) = sqlparser_span_abs_offsets(ctx, join.relation.span())
109            else {
110                return Cv12SelectFixResult {
111                    has_violation,
112                    edits: Vec::new(),
113                };
114            };
115
116            let Some(on_expr) = combine_predicates_with_and(&plan.predicates) else {
117                continue;
118            };
119
120            edits.push(IssuePatchEdit::new(
121                Span::new(relation_end_abs, relation_end_abs),
122                format!(" ON {on_expr}"),
123            ));
124            extracted_predicates.extend(plan.predicates);
125        }
126    }
127
128    if !has_violation {
129        return Cv12SelectFixResult::default();
130    }
131
132    dedup_expressions(&mut extracted_predicates);
133    if extracted_predicates.is_empty() {
134        return Cv12SelectFixResult {
135            has_violation,
136            edits: Vec::new(),
137        };
138    }
139
140    let Some((where_expr_start, where_expr_end)) =
141        sqlparser_span_statement_offsets(ctx, where_expr.span())
142    else {
143        return Cv12SelectFixResult {
144            has_violation,
145            edits: Vec::new(),
146        };
147    };
148
149    let where_expr_abs_start = ctx.statement_range.start + where_expr_start;
150    let where_expr_abs_end =
151        (ctx.statement_range.start + where_expr_end).min(ctx.statement_range.end);
152
153    let Some(where_keyword_abs_start) =
154        locate_where_keyword_abs_start(ctx, select_abs_start, where_expr_abs_start)
155    else {
156        return Cv12SelectFixResult {
157            has_violation,
158            edits: Vec::new(),
159        };
160    };
161
162    if let Some(remaining_where) =
163        cv012_remove_predicates(where_expr.clone(), &extracted_predicates)
164    {
165        edits.push(IssuePatchEdit::new(
166            Span::new(where_keyword_abs_start, where_expr_abs_end),
167            format!("WHERE {remaining_where}"),
168        ));
169    } else {
170        edits.push(IssuePatchEdit::new(
171            Span::new(where_keyword_abs_start, where_expr_abs_end),
172            String::new(),
173        ));
174    }
175
176    Cv12SelectFixResult {
177        has_violation,
178        edits,
179    }
180}
181
182fn cv012_plan_join_chain(
183    table: &TableWithJoins,
184    where_expr: &Expr,
185) -> Option<Vec<Cv12JoinFixPlan>> {
186    let mut seen_sources = Vec::new();
187    collect_table_factor_sources(&table.relation, &mut seen_sources);
188
189    let mut pass_seen = seen_sources.clone();
190    let mut bare_join_indexes: Vec<(usize, Vec<String>)> = Vec::new();
191
192    for (idx, join) in table.joins.iter().enumerate() {
193        let join_sources = collect_table_factor_all_sources(&join.relation);
194        let Some(constraint) = join_constraint(&join.join_operator) else {
195            pass_seen.extend(join_sources);
196            continue;
197        };
198
199        let has_explicit_join_clause = matches!(
200            constraint,
201            JoinConstraint::On(_) | JoinConstraint::Using(_) | JoinConstraint::Natural
202        );
203        if has_explicit_join_clause {
204            pass_seen.extend(join_sources);
205            continue;
206        }
207
208        let matched_where_predicate = join_sources
209            .iter()
210            .any(|src| where_contains_join_predicate(where_expr, Some(src), &pass_seen))
211            || (join_sources.is_empty()
212                && where_contains_join_predicate(where_expr, None, &pass_seen));
213        if !matched_where_predicate {
214            return None;
215        }
216
217        bare_join_indexes.push((idx, join_sources.clone()));
218        pass_seen.extend(join_sources);
219    }
220
221    if bare_join_indexes.is_empty() {
222        return None;
223    }
224
225    let mut extraction_seen = seen_sources;
226    let mut plans = Vec::new();
227    for (idx, join_sources) in bare_join_indexes {
228        let mut predicates = Vec::new();
229        if join_sources.is_empty() {
230            cv012_collect_extractable_eqs(where_expr, None, &extraction_seen, &mut predicates);
231        } else {
232            for source in &join_sources {
233                cv012_collect_extractable_eqs(
234                    where_expr,
235                    Some(source),
236                    &extraction_seen,
237                    &mut predicates,
238                );
239            }
240        }
241        dedup_expressions(&mut predicates);
242        plans.push(Cv12JoinFixPlan {
243            join_index: idx,
244            predicates,
245        });
246        extraction_seen.extend(join_sources);
247    }
248
249    Some(plans)
250}
251
252fn combine_predicates_with_and(predicates: &[Expr]) -> Option<Expr> {
253    predicates
254        .iter()
255        .cloned()
256        .reduce(|acc, pred| Expr::BinaryOp {
257            left: Box::new(acc),
258            op: BinaryOperator::And,
259            right: Box::new(pred),
260        })
261}
262
263fn dedup_expressions(exprs: &mut Vec<Expr>) {
264    let mut seen = HashSet::new();
265    exprs.retain(|expr| seen.insert(format!("{expr}")));
266}
267
268fn sort_and_dedup_patch_edits(edits: &mut Vec<IssuePatchEdit>) {
269    edits.sort_by(|a, b| {
270        a.span
271            .start
272            .cmp(&b.span.start)
273            .then_with(|| a.span.end.cmp(&b.span.end))
274            .then_with(|| a.replacement.cmp(&b.replacement))
275    });
276    edits.dedup_by(|a, b| a.span == b.span && a.replacement == b.replacement);
277}
278
279fn patch_edits_overlap(edits: &[IssuePatchEdit]) -> bool {
280    edits
281        .windows(2)
282        .any(|pair| pair[0].span.end > pair[1].span.start)
283}
284
285/// Adds the reference name for a table factor to the list.
286fn collect_table_factor_sources(table_factor: &TableFactor, sources: &mut Vec<String>) {
287    if let Some(name) = table_factor_reference_name(table_factor) {
288        sources.push(name);
289    }
290}
291
292/// Collects all table source names from a table factor, including nested join
293/// members. For a simple table reference this returns a single name. For a
294/// `NestedJoin` it returns all inner table names.
295fn collect_table_factor_all_sources(table_factor: &TableFactor) -> Vec<String> {
296    let mut sources = Vec::new();
297    match table_factor {
298        TableFactor::NestedJoin {
299            table_with_joins,
300            alias,
301            ..
302        } => {
303            if let Some(alias) = alias {
304                sources.push(alias.name.value.to_ascii_uppercase());
305            } else {
306                collect_table_factor_sources(&table_with_joins.relation, &mut sources);
307                for join in &table_with_joins.joins {
308                    collect_table_factor_sources(&join.relation, &mut sources);
309                }
310            }
311        }
312        _ => {
313            collect_table_factor_sources(table_factor, &mut sources);
314        }
315    }
316    sources
317}
318
319fn join_constraint(join_operator: &JoinOperator) -> Option<&JoinConstraint> {
320    match join_operator {
321        JoinOperator::Join(constraint)
322        | JoinOperator::Inner(constraint)
323        | JoinOperator::Left(constraint)
324        | JoinOperator::LeftOuter(constraint)
325        | JoinOperator::Right(constraint)
326        | JoinOperator::RightOuter(constraint)
327        | JoinOperator::FullOuter(constraint)
328        | JoinOperator::CrossJoin(constraint)
329        | JoinOperator::Semi(constraint)
330        | JoinOperator::LeftSemi(constraint)
331        | JoinOperator::RightSemi(constraint)
332        | JoinOperator::Anti(constraint)
333        | JoinOperator::LeftAnti(constraint)
334        | JoinOperator::RightAnti(constraint)
335        | JoinOperator::StraightJoin(constraint) => Some(constraint),
336        JoinOperator::AsOf { constraint, .. } => Some(constraint),
337        JoinOperator::CrossApply | JoinOperator::OuterApply => None,
338    }
339}
340
341fn where_contains_join_predicate(
342    expr: &Expr,
343    current_source: Option<&String>,
344    seen_sources: &[String],
345) -> bool {
346    match expr {
347        Expr::BinaryOp { left, op, right } => {
348            let direct_match = matches!(
349                op,
350                BinaryOperator::Eq
351                    | BinaryOperator::NotEq
352                    | BinaryOperator::Lt
353                    | BinaryOperator::Gt
354                    | BinaryOperator::LtEq
355                    | BinaryOperator::GtEq
356            ) && is_column_reference(left)
357                && is_column_reference(right)
358                && references_joined_sources(left, right, current_source, seen_sources);
359
360            direct_match
361                || where_contains_join_predicate(left, current_source, seen_sources)
362                || where_contains_join_predicate(right, current_source, seen_sources)
363        }
364        Expr::UnaryOp { expr: inner, .. }
365        | Expr::Nested(inner)
366        | Expr::IsNull(inner)
367        | Expr::IsNotNull(inner)
368        | Expr::Cast { expr: inner, .. } => {
369            where_contains_join_predicate(inner, current_source, seen_sources)
370        }
371        Expr::InList { expr, list, .. } => {
372            where_contains_join_predicate(expr, current_source, seen_sources)
373                || list
374                    .iter()
375                    .any(|item| where_contains_join_predicate(item, current_source, seen_sources))
376        }
377        Expr::Between {
378            expr, low, high, ..
379        } => {
380            where_contains_join_predicate(expr, current_source, seen_sources)
381                || where_contains_join_predicate(low, current_source, seen_sources)
382                || where_contains_join_predicate(high, current_source, seen_sources)
383        }
384        Expr::Case {
385            operand,
386            conditions,
387            else_result,
388            ..
389        } => {
390            operand.as_ref().is_some_and(|expr| {
391                where_contains_join_predicate(expr, current_source, seen_sources)
392            }) || conditions.iter().any(|when| {
393                where_contains_join_predicate(&when.condition, current_source, seen_sources)
394                    || where_contains_join_predicate(&when.result, current_source, seen_sources)
395            }) || else_result.as_ref().is_some_and(|expr| {
396                where_contains_join_predicate(expr, current_source, seen_sources)
397            })
398        }
399        _ => false,
400    }
401}
402
403fn is_column_reference(expr: &Expr) -> bool {
404    matches!(expr, Expr::Identifier(_) | Expr::CompoundIdentifier(_))
405}
406
407fn references_joined_sources(
408    left: &Expr,
409    right: &Expr,
410    current_source: Option<&String>,
411    seen_sources: &[String],
412) -> bool {
413    let left_prefix = qualifier_prefix(left);
414    let right_prefix = qualifier_prefix(right);
415
416    match (left_prefix, right_prefix, current_source) {
417        (Some(left), Some(right), Some(current)) => {
418            (left == *current && seen_sources.contains(&right))
419                || (right == *current && seen_sources.contains(&left))
420        }
421        // Unqualified `a = b` in a plain join WHERE is still ambiguous and should fail.
422        (None, None, _) => true,
423        _ => false,
424    }
425}
426
427fn qualifier_prefix(expr: &Expr) -> Option<String> {
428    match expr {
429        Expr::CompoundIdentifier(parts) if parts.len() > 1 => {
430            // For `table.col` (2 parts), the qualifier is the first part.
431            // For `schema.table.col` (3+ parts), the qualifier is the
432            // penultimate part (the table name) since that is what
433            // `table_factor_reference_name` extracts as the source name.
434            let qualifier_index = parts.len() - 2;
435            Some(parts[qualifier_index].value.to_ascii_uppercase())
436        }
437        Expr::Nested(inner)
438        | Expr::UnaryOp { expr: inner, .. }
439        | Expr::Cast { expr: inner, .. } => qualifier_prefix(inner),
440        _ => None,
441    }
442}
443
444/// Collect top-level AND-chained equality predicates that join `current_source`
445/// to one of `seen_sources`.
446fn cv012_collect_extractable_eqs(
447    expr: &Expr,
448    current_source: Option<&str>,
449    seen_sources: &[String],
450    out: &mut Vec<Expr>,
451) {
452    match expr {
453        Expr::BinaryOp {
454            left,
455            op: BinaryOperator::And,
456            right,
457        } => {
458            cv012_collect_extractable_eqs(left, current_source, seen_sources, out);
459            cv012_collect_extractable_eqs(right, current_source, seen_sources, out);
460        }
461        Expr::BinaryOp {
462            left,
463            op: BinaryOperator::Eq,
464            right,
465        } if cv012_is_extractable_eq(left, right, current_source, seen_sources) => {
466            out.push(expr.clone());
467        }
468        Expr::Nested(inner) => {
469            if let Expr::BinaryOp {
470                left,
471                op: BinaryOperator::Eq,
472                right,
473            } = inner.as_ref()
474            {
475                if cv012_is_extractable_eq(left, right, current_source, seen_sources) {
476                    out.push(expr.clone());
477                }
478            }
479        }
480        _ => {}
481    }
482}
483
484fn cv012_is_extractable_eq(
485    left: &Expr,
486    right: &Expr,
487    current_source: Option<&str>,
488    seen_sources: &[String],
489) -> bool {
490    let Some(current) = current_source else {
491        return false;
492    };
493    let current_upper = current.to_ascii_uppercase();
494    let left_qual = cv012_qualifier(left);
495    let right_qual = cv012_qualifier(right);
496    if let (Some(lq), Some(rq)) = (left_qual, right_qual) {
497        return (lq == current_upper && seen_sources.iter().any(|s| s.to_ascii_uppercase() == rq))
498            || (rq == current_upper && seen_sources.iter().any(|s| s.to_ascii_uppercase() == lq));
499    }
500    false
501}
502
503/// Extract the table qualifier from a column reference expression.
504fn cv012_qualifier(expr: &Expr) -> Option<String> {
505    match expr {
506        Expr::CompoundIdentifier(parts) if parts.len() > 1 => {
507            let qualifier_index = parts.len() - 2;
508            parts
509                .get(qualifier_index)
510                .map(|part| part.value.to_ascii_uppercase())
511        }
512        _ => None,
513    }
514}
515
516/// Remove specific predicates from an AND-chain expression.  Returns `None`
517/// if the entire expression was consumed.
518fn cv012_remove_predicates(expr: Expr, to_remove: &[Expr]) -> Option<Expr> {
519    if to_remove.iter().any(|r| expr_eq(&expr, r)) {
520        return None;
521    }
522    match expr {
523        Expr::BinaryOp {
524            left,
525            op: BinaryOperator::And,
526            right,
527        } => {
528            let left_remaining = cv012_remove_predicates(*left, to_remove);
529            let right_remaining = cv012_remove_predicates(*right, to_remove);
530            match (left_remaining, right_remaining) {
531                (Some(l), Some(r)) => Some(Expr::BinaryOp {
532                    left: Box::new(l),
533                    op: BinaryOperator::And,
534                    right: Box::new(r),
535                }),
536                (Some(l), None) => Some(l),
537                (None, Some(r)) => Some(r),
538                (None, None) => None,
539            }
540        }
541        other => Some(other),
542    }
543}
544
545/// Structural equality check for expressions (used by predicate removal).
546fn expr_eq(a: &Expr, b: &Expr) -> bool {
547    format!("{a}") == format!("{b}")
548}
549
550fn locate_where_keyword_abs_start(
551    ctx: &LintContext,
552    select_abs_start: usize,
553    where_expr_abs_start: usize,
554) -> Option<usize> {
555    let tokens = positioned_statement_tokens(ctx)?;
556    tokens
557        .iter()
558        .filter(|token| {
559            token.start >= select_abs_start
560                && token.end <= where_expr_abs_start
561                && token_word_equals(&token.token, "WHERE")
562        })
563        .map(|token| token.start)
564        .max()
565}
566
567#[derive(Clone)]
568struct PositionedToken {
569    token: Token,
570    start: usize,
571    end: usize,
572}
573
574fn positioned_statement_tokens(ctx: &LintContext) -> Option<Vec<PositionedToken>> {
575    let from_document_tokens = ctx.with_document_tokens(|tokens| {
576        if tokens.is_empty() {
577            return None;
578        }
579
580        let mut positioned = Vec::new();
581        for token in tokens {
582            let (start, end) = token_with_span_offsets(ctx.sql, token)?;
583            if start < ctx.statement_range.start || end > ctx.statement_range.end {
584                continue;
585            }
586            positioned.push(PositionedToken {
587                token: token.token.clone(),
588                start,
589                end,
590            });
591        }
592        Some(positioned)
593    });
594    if let Some(tokens) = from_document_tokens {
595        return Some(tokens);
596    }
597
598    let dialect = ctx.dialect().to_sqlparser_dialect();
599    let mut tokenizer = Tokenizer::new(dialect.as_ref(), ctx.statement_sql());
600    let tokens = tokenizer.tokenize_with_location().ok()?;
601    let mut positioned = Vec::new();
602    for token in tokens {
603        let (start, end) = token_with_span_offsets(ctx.statement_sql(), &token)?;
604        positioned.push(PositionedToken {
605            token: token.token,
606            start: ctx.statement_range.start + start,
607            end: ctx.statement_range.start + end,
608        });
609    }
610    Some(positioned)
611}
612
613fn token_with_span_offsets(sql: &str, token: &TokenWithSpan) -> Option<(usize, usize)> {
614    let start = line_col_to_offset(
615        sql,
616        token.span.start.line as usize,
617        token.span.start.column as usize,
618    )?;
619    let end = line_col_to_offset(
620        sql,
621        token.span.end.line as usize,
622        token.span.end.column as usize,
623    )?;
624    Some((start, end))
625}
626
627fn sqlparser_span_statement_offsets(
628    ctx: &LintContext,
629    span: SqlParserSpan,
630) -> Option<(usize, usize)> {
631    if let Some((start, end)) = sqlparser_span_offsets(ctx.statement_sql(), span) {
632        return Some((start, end));
633    }
634    let (start, end) = sqlparser_span_offsets(ctx.sql, span)?;
635    if start < ctx.statement_range.start || end > ctx.statement_range.end {
636        return None;
637    }
638    Some((
639        start - ctx.statement_range.start,
640        end - ctx.statement_range.start,
641    ))
642}
643
644fn sqlparser_span_abs_offsets(ctx: &LintContext, span: SqlParserSpan) -> Option<(usize, usize)> {
645    if let Some((start, end)) = sqlparser_span_offsets(ctx.statement_sql(), span) {
646        return Some((
647            ctx.statement_range.start + start,
648            ctx.statement_range.start + end,
649        ));
650    }
651    let (start, end) = sqlparser_span_offsets(ctx.sql, span)?;
652    if start < ctx.statement_range.start || end > ctx.statement_range.end {
653        return None;
654    }
655    Some((start, end))
656}
657
658fn sqlparser_span_offsets(sql: &str, span: SqlParserSpan) -> Option<(usize, usize)> {
659    if span.start.line == 0 || span.start.column == 0 || span.end.line == 0 || span.end.column == 0
660    {
661        return None;
662    }
663
664    let start = line_col_to_offset(sql, span.start.line as usize, span.start.column as usize)?;
665    let end = line_col_to_offset(sql, span.end.line as usize, span.end.column as usize)?;
666    (end >= start).then_some((start, end))
667}
668
669fn line_col_to_offset(sql: &str, line: usize, column: usize) -> Option<usize> {
670    if line == 0 || column == 0 {
671        return None;
672    }
673
674    let mut current_line = 1usize;
675    let mut line_start = 0usize;
676    for (idx, ch) in sql.char_indices() {
677        if current_line == line {
678            break;
679        }
680        if ch == '\n' {
681            current_line += 1;
682            line_start = idx + ch.len_utf8();
683        }
684    }
685    if current_line != line {
686        return None;
687    }
688
689    let mut current_column = 1usize;
690    for (rel_idx, ch) in sql[line_start..].char_indices() {
691        if current_column == column {
692            return Some(line_start + rel_idx);
693        }
694        if ch == '\n' {
695            return None;
696        }
697        current_column += 1;
698    }
699    if current_column == column {
700        return Some(sql.len());
701    }
702    None
703}
704
705fn token_word_equals(token: &Token, word: &str) -> bool {
706    matches!(token, Token::Word(w) if w.value.eq_ignore_ascii_case(word))
707}
708
709#[cfg(test)]
710mod tests {
711    use super::*;
712    use crate::parser::parse_sql;
713    use crate::types::{IssueAutofixApplicability, IssuePatchEdit};
714
715    fn run(sql: &str) -> Vec<Issue> {
716        let statements = parse_sql(sql).expect("parse");
717        let rule = ConventionJoinCondition;
718        statements
719            .iter()
720            .enumerate()
721            .flat_map(|(index, statement)| {
722                rule.check(
723                    statement,
724                    &LintContext {
725                        sql,
726                        statement_range: 0..sql.len(),
727                        statement_index: index,
728                    },
729                )
730            })
731            .collect()
732    }
733
734    fn apply_edits(sql: &str, edits: &[IssuePatchEdit]) -> String {
735        let mut output = sql.to_string();
736        let mut ordered = edits.iter().collect::<Vec<_>>();
737        ordered.sort_by_key(|edit| edit.span.start);
738        for edit in ordered.into_iter().rev() {
739            output.replace_range(edit.span.start..edit.span.end, &edit.replacement);
740        }
741        output
742    }
743
744    // --- Edge cases adopted from sqlfluff CV12 ---
745
746    #[test]
747    fn allows_plain_join_without_where_clause() {
748        let issues = run("SELECT foo.a, bar.b FROM foo JOIN bar");
749        assert!(issues.is_empty());
750    }
751
752    #[test]
753    fn flags_plain_join_with_implicit_where_predicate() {
754        let issues = run("SELECT foo.a, bar.b FROM foo JOIN bar WHERE foo.x = bar.y");
755        assert_eq!(issues.len(), 1);
756        assert_eq!(issues[0].code, issue_codes::LINT_CV_012);
757    }
758
759    #[test]
760    fn flags_plain_join_with_unqualified_where_predicate() {
761        let issues = run("SELECT foo.a, bar.b FROM foo JOIN bar WHERE a = b");
762        assert_eq!(issues.len(), 1);
763    }
764
765    #[test]
766    fn allows_join_with_explicit_on_clause() {
767        let issues = run("SELECT foo.a, bar.b FROM foo LEFT JOIN bar ON foo.x = bar.x");
768        assert!(issues.is_empty());
769    }
770
771    #[test]
772    fn allows_cross_join() {
773        let issues = run("SELECT foo.a, bar.b FROM foo CROSS JOIN bar WHERE bar.x > 3");
774        assert!(issues.is_empty());
775    }
776
777    #[test]
778    fn flags_inner_join_without_on_with_where_predicate() {
779        let issues = run("SELECT foo.a, bar.b FROM foo INNER JOIN bar WHERE foo.x = bar.y");
780        assert_eq!(issues.len(), 1);
781        assert_eq!(issues[0].code, issue_codes::LINT_CV_012);
782    }
783
784    #[test]
785    fn does_not_flag_multi_join_chain_when_not_all_plain_joins_are_where_joined() {
786        let sql = "select a.id from a join b join c where a.a = b.a and b.b > 1";
787        assert!(run(sql).is_empty());
788    }
789
790    #[test]
791    fn flags_multi_join_chain_when_all_plain_joins_are_where_joined() {
792        let sql = "select a.id from a join b join c where a.a = b.a and b.b = c.b";
793        let issues = run(sql);
794        assert_eq!(issues.len(), 1);
795        assert_eq!(issues[0].code, issue_codes::LINT_CV_012);
796    }
797
798    #[test]
799    fn flags_schema_qualified_where_join() {
800        // SQLFluff: test_fail_missing_clause_and_stmt_qualified
801        let sql = "SELECT foo.a, bar.b FROM schema.foo JOIN schema.bar WHERE schema.foo.x = schema.bar.y AND schema.foo.x = 3";
802        let issues = run(sql);
803        assert_eq!(issues.len(), 1);
804        assert_eq!(issues[0].code, issue_codes::LINT_CV_012);
805    }
806
807    #[test]
808    fn flags_bracketed_join_with_where_predicate() {
809        // SQLFluff: test_fail_join_with_bracketed_join
810        let sql =
811            "SELECT * FROM bar JOIN (foo1 JOIN foo2 ON (foo1.id = foo2.id)) WHERE bar.id = foo1.id";
812        let issues = run(sql);
813        assert_eq!(issues.len(), 1);
814        assert_eq!(issues[0].code, issue_codes::LINT_CV_012);
815    }
816
817    #[test]
818    fn autofix_moves_where_join_predicate_into_on() {
819        let sql = "SELECT foo.a, bar.b FROM foo JOIN bar WHERE foo.x = bar.y";
820        let issues = run(sql);
821        assert_eq!(issues.len(), 1);
822        let autofix = issues[0]
823            .autofix
824            .as_ref()
825            .expect("expected CV12 core autofix metadata");
826        assert_eq!(autofix.applicability, IssueAutofixApplicability::Safe);
827        let fixed = apply_edits(sql, &autofix.edits);
828        assert_eq!(
829            fixed.trim_end(),
830            "SELECT foo.a, bar.b FROM foo JOIN bar ON foo.x = bar.y"
831        );
832    }
833
834    #[test]
835    fn autofix_preserves_non_join_where_predicate() {
836        let sql = "SELECT foo.a, bar.b FROM foo JOIN bar WHERE foo.x = bar.y AND foo.x = 3";
837        let issues = run(sql);
838        let autofix = issues[0]
839            .autofix
840            .as_ref()
841            .expect("expected CV12 core autofix metadata");
842        let fixed = apply_edits(sql, &autofix.edits);
843        assert_eq!(
844            fixed,
845            "SELECT foo.a, bar.b FROM foo JOIN bar ON foo.x = bar.y WHERE foo.x = 3"
846        );
847    }
848
849    #[test]
850    fn autofix_handles_bracketed_join_predicate() {
851        let sql = "SELECT foo.a, bar.b FROM foo JOIN bar WHERE (foo.x = bar.y) AND foo.t = 3";
852        let issues = run(sql);
853        let autofix = issues[0]
854            .autofix
855            .as_ref()
856            .expect("expected CV12 core autofix metadata");
857        let fixed = apply_edits(sql, &autofix.edits);
858        assert_eq!(
859            fixed,
860            "SELECT foo.a, bar.b FROM foo JOIN bar ON (foo.x = bar.y) WHERE foo.t = 3"
861        );
862    }
863
864    #[test]
865    fn autofix_handles_two_bare_joins() {
866        let sql = "SELECT foo.a, bar.b FROM foo JOIN bar JOIN baz WHERE foo.x = bar.y AND foo.x = baz.t AND foo.c = 3";
867        let issues = run(sql);
868        let autofix = issues[0]
869            .autofix
870            .as_ref()
871            .expect("expected CV12 core autofix metadata");
872        let fixed = apply_edits(sql, &autofix.edits);
873        assert_eq!(
874            fixed,
875            "SELECT foo.a, bar.b FROM foo JOIN bar ON foo.x = bar.y JOIN baz ON foo.x = baz.t WHERE foo.c = 3"
876        );
877    }
878
879    #[test]
880    fn autofix_handles_schema_qualified_references() {
881        let sql = "SELECT foo.a, bar.b FROM schema.foo JOIN schema.bar WHERE schema.foo.x = schema.bar.y AND schema.foo.x = 3";
882        let issues = run(sql);
883        let autofix = issues[0]
884            .autofix
885            .as_ref()
886            .expect("expected CV12 core autofix metadata");
887        let fixed = apply_edits(sql, &autofix.edits);
888        assert_eq!(
889            fixed,
890            "SELECT foo.a, bar.b FROM schema.foo JOIN schema.bar ON schema.foo.x = schema.bar.y WHERE schema.foo.x = 3"
891        );
892    }
893}