pub struct Scope {Show 13 fields
pub expression: Expression,
pub scope_type: ScopeType,
pub sources: HashMap<String, SourceInfo>,
pub lateral_sources: HashMap<String, SourceInfo>,
pub cte_sources: HashMap<String, SourceInfo>,
pub outer_columns: Vec<String>,
pub can_be_correlated: bool,
pub subquery_scopes: Vec<Scope>,
pub derived_table_scopes: Vec<Scope>,
pub cte_scopes: Vec<Scope>,
pub udtf_scopes: Vec<Scope>,
pub table_scopes: Vec<Scope>,
pub union_scopes: Vec<Scope>,
/* private fields */
}Expand description
Represents a scope in a SQL query
A scope is the context of a SELECT statement and its sources. Scopes can be nested (subqueries, CTEs, derived tables) and form a tree.
Fields§
§expression: ExpressionThe expression at the root of this scope
scope_type: ScopeTypeType of this scope relative to its parent
sources: HashMap<String, SourceInfo>Mapping of source names to their info
lateral_sources: HashMap<String, SourceInfo>Sources from LATERAL views (have access to preceding sources)
cte_sources: HashMap<String, SourceInfo>CTE sources available to this scope
outer_columns: Vec<String>If this is a derived table or CTE with alias columns, this is that list
e.g., SELECT * FROM (SELECT ...) AS y(col1, col2) => [“col1”, “col2”]
Whether this scope can potentially be correlated (true for subqueries and UDTFs)
subquery_scopes: Vec<Scope>Child subquery scopes
derived_table_scopes: Vec<Scope>Child derived table scopes
cte_scopes: Vec<Scope>Child CTE scopes
udtf_scopes: Vec<Scope>Child UDTF (User Defined Table Function) scopes
table_scopes: Vec<Scope>Combined derived_table_scopes + udtf_scopes in definition order
union_scopes: Vec<Scope>Union/set operation scopes (left and right)
Implementations§
Source§impl Scope
impl Scope
Sourcepub fn new(expression: Expression) -> Self
pub fn new(expression: Expression) -> Self
Create a new root scope
Sourcepub fn branch(&self, expression: Expression, scope_type: ScopeType) -> Self
pub fn branch(&self, expression: Expression, scope_type: ScopeType) -> Self
Create a child scope branching from this one
Sourcepub fn branch_with_options(
&self,
expression: Expression,
scope_type: ScopeType,
sources: Option<HashMap<String, SourceInfo>>,
lateral_sources: Option<HashMap<String, SourceInfo>>,
outer_columns: Option<Vec<String>>,
) -> Self
pub fn branch_with_options( &self, expression: Expression, scope_type: ScopeType, sources: Option<HashMap<String, SourceInfo>>, lateral_sources: Option<HashMap<String, SourceInfo>>, outer_columns: Option<Vec<String>>, ) -> Self
Create a child scope with additional options
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear all cached properties
Sourcepub fn add_source(
&mut self,
name: String,
expression: Expression,
is_scope: bool,
)
pub fn add_source( &mut self, name: String, expression: Expression, is_scope: bool, )
Add a source to this scope
Sourcepub fn add_lateral_source(
&mut self,
name: String,
expression: Expression,
is_scope: bool,
)
pub fn add_lateral_source( &mut self, name: String, expression: Expression, is_scope: bool, )
Add a lateral source to this scope
Sourcepub fn add_cte_source(&mut self, name: String, expression: Expression)
pub fn add_cte_source(&mut self, name: String, expression: Expression)
Add a CTE source to this scope
Sourcepub fn rename_source(&mut self, old_name: &str, new_name: String)
pub fn rename_source(&mut self, old_name: &str, new_name: String)
Rename a source
Sourcepub fn remove_source(&mut self, name: &str)
pub fn remove_source(&mut self, name: &str)
Remove a source
Sourcepub fn source_names(&self) -> HashSet<String>
pub fn source_names(&self) -> HashSet<String>
Get all source names in this scope
Sourcepub fn external_columns(&mut self) -> Vec<ColumnRef>
pub fn external_columns(&mut self) -> Vec<ColumnRef>
Get columns that reference sources outside this scope
Sourcepub fn local_columns(&mut self) -> Vec<ColumnRef>
pub fn local_columns(&mut self) -> Vec<ColumnRef>
Get columns that reference sources in this scope (not external)
Sourcepub fn unqualified_columns(&mut self) -> Vec<ColumnRef>
pub fn unqualified_columns(&mut self) -> Vec<ColumnRef>
Get unqualified columns (columns without table qualifier)
Sourcepub fn source_columns(&mut self, source_name: &str) -> Vec<ColumnRef>
pub fn source_columns(&mut self, source_name: &str) -> Vec<ColumnRef>
Get columns for a specific source
Determine if this scope is a correlated subquery
A subquery is correlated if:
- It can be correlated (is a subquery or UDTF), AND
- It references columns from outer scopes
Sourcepub fn is_subquery(&self) -> bool
pub fn is_subquery(&self) -> bool
Check if this is a subquery scope
Sourcepub fn is_derived_table(&self) -> bool
pub fn is_derived_table(&self) -> bool
Check if this is a derived table scope