Expand description
Tree traversal utilities for SQL expression ASTs.
This module provides read-only traversal, search, and transformation utilities
for the Expression tree produced by the parser. Because Rust’s ownership
model does not allow parent pointers inside the AST, parent information is
tracked externally via TreeContext (built on demand).
§Traversal
Two iterator types are provided:
DfsIter– depth-first (pre-order) traversal using a stack. Visits a node before its children. Good for top-down analysis and early termination.BfsIter– breadth-first (level-order) traversal using a queue. Visits all nodes at depth N before any node at depth N+1. Good for level-aware analysis.
Both are available through the ExpressionWalk trait methods dfs
and bfs.
§Searching
The ExpressionWalk trait also provides convenience methods for finding expressions:
find, find_all,
contains, and count.
Common predicates are available as free functions: is_column, is_literal,
is_function, is_aggregate, is_window_function, is_subquery, and
is_select.
§Transformation
The transform and transform_map functions perform bottom-up (post-order)
tree rewrites, delegating to transform_recursive.
The ExpressionWalk::transform_owned method provides the same capability as
an owned method on Expression.
Based on traversal patterns from sqlglot/expressions.py.
Structs§
- BfsIter
- Level-order breadth-first iterator over an expression tree.
- DfsIter
- Pre-order depth-first iterator over an expression tree.
- Parent
Info - Information about a node’s parent relationship
- Tree
Context - External parent-tracking context for an expression tree.
Traits§
- Expression
Walk - Extension trait that adds traversal and search methods to
Expression.
Functions§
- contains_
aggregate - Returns
trueif the expression tree contains any aggregate function calls. - contains_
subquery - Returns
trueif the expression tree contains any subquery nodes. - contains_
window_ function - Returns
trueif the expression tree contains any window function calls. - find_
ancestor - Find the first ancestor of
targetmatchingpredicate, walking from parent toward root. - find_
parent - Find the parent of
targetwithin the tree rooted atroot. - get_
columns - Collects all column references (
Expression::Column) from the expression tree. - get_
tables - Collects all table references (
Expression::Table) from the expression tree. - is_
aggregate - Returns
trueifexpris an aggregate function (Expression::AggregateFunction). - is_
column - Returns
trueifexpris a column reference (Expression::Column). - is_
function - Returns
trueifexpris a function call (regular or aggregate). - is_
literal - Returns
trueifexpris a literal value (number, string, boolean, or NULL). - is_
select - Returns
trueifexpris a SELECT statement (Expression::Select). - is_
subquery - Returns
trueifexpris a subquery (Expression::Subquery). - is_
window_ function - Returns
trueifexpris a window function (Expression::WindowFunction). - transform
- Transforms an expression tree bottom-up, with optional node removal.
- transform_
map - Transforms an expression tree bottom-up without node removal.
Type Aliases§
- NodeId
- Unique identifier for expression nodes during traversal