Skip to main content

Module traversal

Module traversal 

Source
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.
ParentInfo
Information about a node’s parent relationship
TreeContext
External parent-tracking context for an expression tree.

Traits§

ExpressionWalk
Extension trait that adds traversal and search methods to Expression.

Functions§

contains_aggregate
Returns true if the expression tree contains any aggregate function calls.
contains_subquery
Returns true if the expression tree contains any subquery nodes.
contains_window_function
Returns true if the expression tree contains any window function calls.
find_ancestor
Find the first ancestor of target matching predicate, walking from parent toward root.
find_parent
Find the parent of target within the tree rooted at root.
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 true if expr matches the expected AST variant(s).
is_aggregate
Returns true if expr is an aggregate function (Expression::AggregateFunction).
is_alias
Returns true if expr matches the expected AST variant(s).
is_alter_table
Returns true if expr matches the expected AST variant(s).
is_and
Returns true if expr matches the expected AST variant(s).
is_arithmetic
Returns true if expr is an arithmetic operator.
is_avg
Returns true if expr matches the expected AST variant(s).
is_between
Returns true if expr matches the expected AST variant(s).
is_boolean
Returns true if expr matches the expected AST variant(s).
is_case
Returns true if expr matches the expected AST variant(s).
is_cast
Returns true if expr matches the expected AST variant(s).
is_coalesce
Returns true if expr matches the expected AST variant(s).
is_column
Returns true if expr is a column reference (Expression::Column).
is_comparison
Returns true if expr is a comparison operator.
is_concat
Returns true if expr matches the expected AST variant(s).
is_count
Returns true if expr matches the expected AST variant(s).
is_create_index
Returns true if expr matches the expected AST variant(s).
is_create_table
Returns true if expr matches the expected AST variant(s).
is_create_view
Returns true if expr matches the expected AST variant(s).
is_cte
Returns true if expr matches the expected AST variant(s).
is_ddl
Returns true if expr is a DDL statement.
is_delete
Returns true if expr matches the expected AST variant(s).
is_div
Returns true if expr matches the expected AST variant(s).
is_drop_index
Returns true if expr matches the expected AST variant(s).
is_drop_table
Returns true if expr matches the expected AST variant(s).
is_drop_view
Returns true if expr matches the expected AST variant(s).
is_eq
Returns true if expr matches the expected AST variant(s).
is_except
Returns true if expr matches the expected AST variant(s).
is_exists
Returns true if expr matches the expected AST variant(s).
is_from
Returns true if expr matches the expected AST variant(s).
is_function
Returns true if expr is a function call (regular or aggregate).
is_group_by
Returns true if expr matches the expected AST variant(s).
is_gt
Returns true if expr matches the expected AST variant(s).
is_gte
Returns true if expr matches the expected AST variant(s).
is_having
Returns true if expr matches the expected AST variant(s).
is_identifier
Returns true if expr matches the expected AST variant(s).
is_ilike
Returns true if expr matches the expected AST variant(s).
is_in
Returns true if expr matches the expected AST variant(s).
is_insert
Returns true if expr matches the expected AST variant(s).
is_intersect
Returns true if expr matches the expected AST variant(s).
is_is_null
Returns true if expr matches the expected AST variant(s).
is_join
Returns true if expr matches the expected AST variant(s).
is_like
Returns true if expr matches the expected AST variant(s).
is_limit
Returns true if expr matches the expected AST variant(s).
is_literal
Returns true if expr is a literal value (number, string, boolean, or NULL).
is_logical
Returns true if expr is a logical operator (AND, OR, NOT).
is_lt
Returns true if expr matches the expected AST variant(s).
is_lte
Returns true if expr matches the expected AST variant(s).
is_max_func
Returns true if expr matches the expected AST variant(s).
is_min_func
Returns true if expr matches the expected AST variant(s).
is_mod
Returns true if expr matches the expected AST variant(s).
is_mul
Returns true if expr matches the expected AST variant(s).
is_neq
Returns true if expr matches the expected AST variant(s).
is_not
Returns true if expr matches the expected AST variant(s).
is_null_if
Returns true if expr matches the expected AST variant(s).
is_null_literal
Returns true if expr matches the expected AST variant(s).
is_offset
Returns true if expr matches the expected AST variant(s).
is_or
Returns true if expr matches the expected AST variant(s).
is_order_by
Returns true if expr matches the expected AST variant(s).
is_ordered
Returns true if expr matches the expected AST variant(s).
is_paren
Returns true if expr matches the expected AST variant(s).
is_query
Returns true if expr is a query statement (SELECT, INSERT, UPDATE, or DELETE).
is_safe_cast
Returns true if expr matches the expected AST variant(s).
is_select
Returns true if expr is a SELECT statement (Expression::Select).
is_set_operation
Returns true if expr is a set operation (UNION, INTERSECT, or EXCEPT).
is_star
Returns true if expr matches the expected AST variant(s).
is_sub
Returns true if expr matches the expected AST variant(s).
is_subquery
Returns true if expr is a subquery (Expression::Subquery).
is_sum
Returns true if expr matches the expected AST variant(s).
is_table
Returns true if expr matches the expected AST variant(s).
is_try_cast
Returns true if expr matches the expected AST variant(s).
is_union
Returns true if expr matches the expected AST variant(s).
is_update
Returns true if expr matches the expected AST variant(s).
is_where
Returns true if expr matches the expected AST variant(s).
is_window_function
Returns true if expr is a window function (Expression::WindowFunction).
is_with
Returns true if expr matches 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