flowscope_core/analyzer/helpers/
alias.rs

1//! Alias visibility checking helpers.
2//!
3//! This module provides utilities for checking SQL alias visibility rules
4//! across different dialects. Different SQL dialects have varying rules about
5//! where SELECT list aliases can be referenced (GROUP BY, HAVING, ORDER BY, etc.).
6
7use crate::types::{issue_codes, Issue};
8use crate::Dialect;
9
10/// Emits a warning for unsupported alias usage in a SQL clause.
11///
12/// This helper centralizes the warning emission logic for alias visibility
13/// checks across different clauses (GROUP BY, HAVING, ORDER BY, lateral aliases).
14///
15/// # Arguments
16/// * `dialect` - The SQL dialect being used
17/// * `clause_name` - The name of the clause where the alias is used (e.g., "GROUP BY", "HAVING")
18/// * `alias_name` - The name of the alias being referenced
19/// * `statement_index` - The index of the statement in the analysis request
20///
21/// # Returns
22/// An `Issue` warning that can be pushed to the analyzer's issue list.
23pub fn alias_visibility_warning(
24    dialect: Dialect,
25    clause_name: &str,
26    alias_name: &str,
27    statement_index: usize,
28) -> Issue {
29    Issue::warning(
30        issue_codes::UNSUPPORTED_SYNTAX,
31        format!(
32            "Dialect '{dialect:?}' does not support referencing aliases in {clause_name} (alias '{alias_name}' used). This may fail at runtime."
33        ),
34    )
35    .with_statement(statement_index)
36}
37
38/// Emits a warning for unsupported lateral column alias usage.
39///
40/// Lateral column aliases allow referencing an alias defined earlier in the same
41/// SELECT list. Not all dialects support this feature.
42///
43/// # Arguments
44/// * `dialect` - The SQL dialect being used
45/// * `alias_name` - The name of the alias being referenced
46/// * `statement_index` - The index of the statement in the analysis request
47///
48/// # Returns
49/// An `Issue` warning that can be pushed to the analyzer's issue list.
50pub fn lateral_alias_warning(dialect: Dialect, alias_name: &str, statement_index: usize) -> Issue {
51    Issue::warning(
52        issue_codes::UNSUPPORTED_SYNTAX,
53        format!(
54            "Dialect '{dialect:?}' does not support lateral column aliases (referencing alias '{alias_name}' from earlier in the SELECT list). This may fail at runtime."
55        ),
56    )
57    .with_statement(statement_index)
58}