Skip to main content

Scope

Struct Scope 

Source
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: Expression

The expression at the root of this scope

§scope_type: ScopeType

Type 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”]

§can_be_correlated: bool

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

Source

pub fn new(expression: Expression) -> Self

Create a new root scope

Source

pub fn branch(&self, expression: Expression, scope_type: ScopeType) -> Self

Create a child scope branching from this one

Source

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

Source

pub fn clear_cache(&mut self)

Clear all cached properties

Source

pub fn add_source( &mut self, name: String, expression: Expression, is_scope: bool, )

Add a source to this scope

Source

pub fn add_lateral_source( &mut self, name: String, expression: Expression, is_scope: bool, )

Add a lateral source to this scope

Source

pub fn add_cte_source(&mut self, name: String, expression: Expression)

Add a CTE source to this scope

Source

pub fn rename_source(&mut self, old_name: &str, new_name: String)

Rename a source

Source

pub fn remove_source(&mut self, name: &str)

Remove a source

Source

pub fn columns(&mut self) -> &[ColumnRef]

Collect all column references in this scope

Source

pub fn source_names(&self) -> HashSet<String>

Get all source names in this scope

Source

pub fn external_columns(&mut self) -> Vec<ColumnRef>

Get columns that reference sources outside this scope

Source

pub fn local_columns(&mut self) -> Vec<ColumnRef>

Get columns that reference sources in this scope (not external)

Source

pub fn unqualified_columns(&mut self) -> Vec<ColumnRef>

Get unqualified columns (columns without table qualifier)

Source

pub fn source_columns(&mut self, source_name: &str) -> Vec<ColumnRef>

Get columns for a specific source

Source

pub fn is_correlated_subquery(&mut self) -> bool

Determine if this scope is a correlated subquery

A subquery is correlated if:

  1. It can be correlated (is a subquery or UDTF), AND
  2. It references columns from outer scopes
Source

pub fn is_subquery(&self) -> bool

Check if this is a subquery scope

Source

pub fn is_derived_table(&self) -> bool

Check if this is a derived table scope

Source

pub fn is_cte(&self) -> bool

Check if this is a CTE scope

Source

pub fn is_root(&self) -> bool

Check if this is the root scope

Source

pub fn is_udtf(&self) -> bool

Check if this is a UDTF scope

Source

pub fn is_union(&self) -> bool

Check if this is a union/set operation scope

Source

pub fn traverse(&self) -> Vec<&Scope>

Traverse all scopes in this tree (depth-first post-order)

Source

pub fn ref_count(&self) -> HashMap<usize, usize>

Count references to each scope in this tree

Trait Implementations§

Source§

impl Clone for Scope

Source§

fn clone(&self) -> Scope

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Scope

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Scope

§

impl RefUnwindSafe for Scope

§

impl Send for Scope

§

impl Sync for Scope

§

impl Unpin for Scope

§

impl UnwindSafe for Scope

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.