Skip to main content

Module scope_analysis

Module scope_analysis 

Source
Expand description

Scope analysis for SQL queries.

Provides a Scope struct that tracks the sources, columns, and relationships within a SQL query. This is the foundation for optimizer passes like qualify_columns, pushdown_predicates, annotate_types, and column lineage analysis.

Inspired by Python sqlglot’s optimizer/scope.py.

§Example

use sqlglot_rust::parser::parse;
use sqlglot_rust::dialects::Dialect;
use sqlglot_rust::optimizer::scope_analysis::{build_scope, ScopeType};

let ast = parse("SELECT a, b FROM t WHERE a > 1", Dialect::Ansi).unwrap();
let scope = build_scope(&ast);
assert_eq!(scope.scope_type, ScopeType::Root);
assert!(!scope.columns.is_empty());

Structs§

ColumnRef
A column reference encountered during scope analysis.
Scope
Represents a single query scope and its relationships.

Enums§

ScopeType
The kind of scope a query fragment lives in.
Source
A source within a scope. Can be a concrete table or a reference to a child scope (derived table, CTE, etc.).

Functions§

build_scope
Build the scope tree from a parsed SQL statement.
find_all_in_scope
Find all expressions in a scope that match a predicate, respecting scope boundaries (does not descend into child scopes).