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_add
- Returns
trueifexprmatches the expected AST variant(s). - is_
aggregate - Returns
trueifexpris an aggregate function (Expression::AggregateFunction). - is_
alias - Returns
trueifexprmatches the expected AST variant(s). - is_
alter_ table - Returns
trueifexprmatches the expected AST variant(s). - is_and
- Returns
trueifexprmatches the expected AST variant(s). - is_
arithmetic - Returns
trueifexpris an arithmetic operator. - is_avg
- Returns
trueifexprmatches the expected AST variant(s). - is_
between - Returns
trueifexprmatches the expected AST variant(s). - is_
boolean - Returns
trueifexprmatches the expected AST variant(s). - is_case
- Returns
trueifexprmatches the expected AST variant(s). - is_cast
- Returns
trueifexprmatches the expected AST variant(s). - is_
coalesce - Returns
trueifexprmatches the expected AST variant(s). - is_
column - Returns
trueifexpris a column reference (Expression::Column). - is_
comparison - Returns
trueifexpris a comparison operator. - is_
concat - Returns
trueifexprmatches the expected AST variant(s). - is_
count - Returns
trueifexprmatches the expected AST variant(s). - is_
create_ index - Returns
trueifexprmatches the expected AST variant(s). - is_
create_ table - Returns
trueifexprmatches the expected AST variant(s). - is_
create_ view - Returns
trueifexprmatches the expected AST variant(s). - is_cte
- Returns
trueifexprmatches the expected AST variant(s). - is_ddl
- Returns
trueifexpris a DDL statement. - is_
delete - Returns
trueifexprmatches the expected AST variant(s). - is_div
- Returns
trueifexprmatches the expected AST variant(s). - is_
drop_ index - Returns
trueifexprmatches the expected AST variant(s). - is_
drop_ table - Returns
trueifexprmatches the expected AST variant(s). - is_
drop_ view - Returns
trueifexprmatches the expected AST variant(s). - is_eq
- Returns
trueifexprmatches the expected AST variant(s). - is_
except - Returns
trueifexprmatches the expected AST variant(s). - is_
exists - Returns
trueifexprmatches the expected AST variant(s). - is_from
- Returns
trueifexprmatches the expected AST variant(s). - is_
function - Returns
trueifexpris a function call (regular or aggregate). - is_
group_ by - Returns
trueifexprmatches the expected AST variant(s). - is_gt
- Returns
trueifexprmatches the expected AST variant(s). - is_gte
- Returns
trueifexprmatches the expected AST variant(s). - is_
having - Returns
trueifexprmatches the expected AST variant(s). - is_
identifier - Returns
trueifexprmatches the expected AST variant(s). - is_
ilike - Returns
trueifexprmatches the expected AST variant(s). - is_in
- Returns
trueifexprmatches the expected AST variant(s). - is_
insert - Returns
trueifexprmatches the expected AST variant(s). - is_
intersect - Returns
trueifexprmatches the expected AST variant(s). - is_
is_ null - Returns
trueifexprmatches the expected AST variant(s). - is_join
- Returns
trueifexprmatches the expected AST variant(s). - is_like
- Returns
trueifexprmatches the expected AST variant(s). - is_
limit - Returns
trueifexprmatches the expected AST variant(s). - is_
literal - Returns
trueifexpris a literal value (number, string, boolean, or NULL). - is_
logical - Returns
trueifexpris a logical operator (AND, OR, NOT). - is_lt
- Returns
trueifexprmatches the expected AST variant(s). - is_lte
- Returns
trueifexprmatches the expected AST variant(s). - is_
max_ func - Returns
trueifexprmatches the expected AST variant(s). - is_
min_ func - Returns
trueifexprmatches the expected AST variant(s). - is_mod
- Returns
trueifexprmatches the expected AST variant(s). - is_mul
- Returns
trueifexprmatches the expected AST variant(s). - is_neq
- Returns
trueifexprmatches the expected AST variant(s). - is_not
- Returns
trueifexprmatches the expected AST variant(s). - is_
null_ if - Returns
trueifexprmatches the expected AST variant(s). - is_
null_ literal - Returns
trueifexprmatches the expected AST variant(s). - is_
offset - Returns
trueifexprmatches the expected AST variant(s). - is_or
- Returns
trueifexprmatches the expected AST variant(s). - is_
order_ by - Returns
trueifexprmatches the expected AST variant(s). - is_
ordered - Returns
trueifexprmatches the expected AST variant(s). - is_
paren - Returns
trueifexprmatches the expected AST variant(s). - is_
query - Returns
trueifexpris a query statement (SELECT, INSERT, UPDATE, or DELETE). - is_
safe_ cast - Returns
trueifexprmatches the expected AST variant(s). - is_
select - Returns
trueifexpris a SELECT statement (Expression::Select). - is_
set_ operation - Returns
trueifexpris a set operation (UNION, INTERSECT, or EXCEPT). - is_star
- Returns
trueifexprmatches the expected AST variant(s). - is_sub
- Returns
trueifexprmatches the expected AST variant(s). - is_
subquery - Returns
trueifexpris a subquery (Expression::Subquery). - is_sum
- Returns
trueifexprmatches the expected AST variant(s). - is_
table - Returns
trueifexprmatches the expected AST variant(s). - is_
try_ cast - Returns
trueifexprmatches the expected AST variant(s). - is_
union - Returns
trueifexprmatches the expected AST variant(s). - is_
update - Returns
trueifexprmatches the expected AST variant(s). - is_
where - Returns
trueifexprmatches the expected AST variant(s). - is_
window_ function - Returns
trueifexpris a window function (Expression::WindowFunction). - is_with
- Returns
trueifexprmatches the expected AST variant(s). - 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