Expand description
Traversal of an immutable AST.
Each method of the Visit
trait is a hook that can be overridden to
customize the behavior when visiting the corresponding type of node. By
default, every method recursively visits the substructure of the input
by invoking the right visitor method of each of its fields.
pub trait Visit<'ast, T: AstInfo> {
/* ... */
fn visit_function(&mut self, node: &'ast Function<T>) {
visit_function(self, node);
}
/* ... */
}
pub fn visit_function<'ast, V, T: AstInfo>(visitor: &mut V, node: &'ast Function<T>)
where
V: Visit<'ast, T> + ?Sized,
{
visitor.visit_item_name(&node.name);
visitor.visit_function_args(&node.args);
if let Some(filter) = &node.filter {
visitor.visit_expr(&*filter);
}
if let Some(over) = &node.over {
visitor.visit_window_spec(over);
}
}
See also the visit_mut
module for traversing mutable ASTs.
§Examples
This visitor will count the number of subqueries in a SQL statement.
use std::error::Error;
use mz_sql_parser::ast::{AstInfo, Query, Raw};
use mz_sql_parser::ast::visit::{self, Visit};
struct SubqueryCounter {
count: usize,
}
impl<'ast> Visit<'ast, Raw> for SubqueryCounter {
fn visit_query(&mut self, query: &'ast Query<Raw>) {
self.count += 1;
// Delegate to the default implementation to visit any nested
// subqueries. Placing this call at the end of the method results
// in a pre-order traversal. Place it at the beginning for a
// post-order traversal instead.
visit::visit_query(self, query);
}
}
fn main() -> Result<(), Box<dyn Error>> {
let sql = "SELECT (SELECT 1) FROM (SELECT 1) WHERE EXISTS (SELECT (SELECT 1))";
let stmts = mz_sql_parser::parser::parse_statements(sql.into())?;
let mut counter = SubqueryCounter { count: 0 };
for stmt in &stmts {
counter.visit_statement(&stmt.ast);
}
assert_eq!(counter.count, 5);
Ok(())
}
The 'ast
lifetime on the input references means that the syntax tree
outlives the complete recursive visit call, so the visitor is allowed to
hold on to references into the syntax tree.
use std::error::Error;
use mz_sql_parser::ast::{Ident, Raw, AstInfo, RawItemName};
use mz_sql_parser::ast::visit::{self, Visit};
struct IdentCollector<'ast> {
idents: Vec<&'ast Ident>,
}
impl<'ast> Visit<'ast, Raw> for IdentCollector<'ast> {
fn visit_ident(&mut self, node: &'ast Ident) {
self.idents.push(node);
visit::visit_ident(self, node);
}
fn visit_item_name(&mut self, name: &'ast <Raw as AstInfo>::ItemName) {
match name {
RawItemName::Name(n) | RawItemName::Id(_, n, _) => {
for node in &n.0 {
self.idents.push(node);
visit::visit_ident(self, node);
}
}
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
let sql = "SELECT a FROM b.c WHERE 1 + d(e)";
let stmts = mz_sql_parser::parser::parse_statements(sql.into())?;
let mut collector = IdentCollector { idents: vec![] };
for stmt in &stmts {
collector.visit_statement(&stmt.ast);
}
assert_eq!(collector.idents, &[
&Ident::new_unchecked("a"), &Ident::new_unchecked("b"),
&Ident::new_unchecked("c"), &Ident::new_unchecked("d"),
&Ident::new_unchecked("e"),
]);
Ok(())
}
The VisitNode
trait is implemented for every node in the AST and can be
used to write generic functions that apply a Visit
implementation to any
node in the AST.
§Implementation notes
This module is automatically generated by the crate’s build script. Changes to the AST will be automatically propagated to the visitor.
This approach to AST visitors is inspired by the syn
crate. These
module docs are directly derived from the syn::visit
module docs.
Traits§
Functions§
- visit_
abbreviated_ grant_ or_ revoke_ statement - visit_
abbreviated_ grant_ statement - visit_
abbreviated_ revoke_ statement - visit_
alter_ cluster_ action - visit_
alter_ cluster_ statement - visit_
alter_ connection_ action - visit_
alter_ connection_ option - visit_
alter_ connection_ option_ name - visit_
alter_ connection_ statement - visit_
alter_ default_ privileges_ statement - visit_
alter_ index_ action - visit_
alter_ index_ statement - visit_
alter_ network_ policy_ statement - visit_
alter_ object_ rename_ statement - visit_
alter_ object_ swap_ statement - visit_
alter_ owner_ statement - visit_
alter_ retain_ history_ statement - visit_
alter_ role_ option - visit_
alter_ role_ statement - visit_
alter_ secret_ statement - visit_
alter_ set_ cluster_ statement - visit_
alter_ sink_ action - visit_
alter_ sink_ statement - visit_
alter_ source_ action - visit_
alter_ source_ add_ subsource_ option - visit_
alter_ source_ add_ subsource_ option_ name - visit_
alter_ source_ statement - visit_
alter_ system_ reset_ all_ statement - visit_
alter_ system_ reset_ statement - visit_
alter_ system_ set_ statement - visit_
alter_ table_ add_ column_ statement - visit_
as_ of - visit_
assignment - visit_
avro_ doc_ on - visit_
avro_ schema - visit_
avro_ schema_ option - visit_
avro_ schema_ option_ name - visit_
catalog_ name - visit_
close_ statement - visit_
cluster_ alter_ option - visit_
cluster_ alter_ option_ name - visit_
cluster_ alter_ option_ value - visit_
cluster_ alter_ until_ ready_ option - visit_
cluster_ alter_ until_ ready_ option_ name - visit_
cluster_ feature - visit_
cluster_ feature_ name - visit_
cluster_ name - visit_
cluster_ option - visit_
cluster_ option_ name - visit_
cluster_ schedule_ option_ value - visit_
column_ def - visit_
column_ name - visit_
column_ option - visit_
column_ option_ def - visit_
column_ reference - visit_
column_ versioned - visit_
comment_ object_ type - visit_
comment_ statement - visit_
commit_ statement - visit_
connection_ default_ aws_ privatelink - visit_
connection_ option - visit_
connection_ option_ name - visit_
continual_ task_ option - visit_
continual_ task_ option_ name - visit_
continual_ task_ stmt - visit_
copy_ direction - visit_
copy_ option - visit_
copy_ option_ name - visit_
copy_ relation - visit_
copy_ statement - visit_
copy_ target - visit_
create_ cluster_ replica_ statement - visit_
create_ cluster_ statement - visit_
create_ connection_ option - visit_
create_ connection_ option_ name - visit_
create_ connection_ statement - visit_
create_ connection_ type - visit_
create_ continual_ task_ statement - visit_
create_ continual_ task_ sugar - visit_
create_ database_ statement - visit_
create_ index_ statement - visit_
create_ materialized_ view_ statement - visit_
create_ network_ policy_ statement - visit_
create_ role_ statement - visit_
create_ schema_ statement - visit_
create_ secret_ statement - visit_
create_ sink_ connection - visit_
create_ sink_ option - visit_
create_ sink_ option_ name - visit_
create_ sink_ statement - visit_
create_ source_ connection - visit_
create_ source_ option - visit_
create_ source_ option_ name - visit_
create_ source_ statement - visit_
create_ subsource_ option - visit_
create_ subsource_ option_ name - visit_
create_ subsource_ statement - visit_
create_ table_ from_ source_ statement - visit_
create_ table_ statement - visit_
create_ type_ as - visit_
create_ type_ list_ option - visit_
create_ type_ list_ option_ name - visit_
create_ type_ map_ option - visit_
create_ type_ map_ option_ name - visit_
create_ type_ statement - visit_
create_ view_ statement - visit_
create_ webhook_ source_ body - visit_
create_ webhook_ source_ check - visit_
create_ webhook_ source_ check_ options - visit_
create_ webhook_ source_ filter_ header - visit_
create_ webhook_ source_ header - visit_
create_ webhook_ source_ include_ headers - visit_
create_ webhook_ source_ map_ header - visit_
create_ webhook_ source_ secret - visit_
create_ webhook_ source_ statement - visit_
csr_ config_ option - visit_
csr_ config_ option_ name - visit_
csr_ connection - visit_
csr_ connection_ avro - visit_
csr_ connection_ protobuf - visit_
csr_ seed_ avro - visit_
csr_ seed_ protobuf - visit_
csr_ seed_ protobuf_ schema - visit_
csv_ columns - visit_
cte - visit_
cte_ block - visit_
cte_ id - visit_
cte_ mut_ rec - visit_
cte_ mut_ rec_ column_ def - visit_
data_ type - visit_
database_ name - visit_
date_ time_ field - visit_
deallocate_ statement - visit_
declare_ statement - visit_
deferred_ item_ name - visit_
delete_ statement - visit_
discard_ statement - visit_
discard_ target - visit_
distinct - visit_
doc_ on_ identifier - visit_
doc_ on_ schema - visit_
drop_ objects_ statement - visit_
drop_ owned_ statement - visit_
execute_ statement - visit_
explain_ analyze_ computation_ property - visit_
explain_ analyze_ property - visit_
explain_ analyze_ statement - visit_
explain_ format - visit_
explain_ plan_ option - visit_
explain_ plan_ option_ name - visit_
explain_ plan_ statement - visit_
explain_ pushdown_ statement - visit_
explain_ sink_ schema_ for - visit_
explain_ sink_ schema_ statement - visit_
explain_ stage - visit_
explain_ timestamp_ statement - visit_
explainee - visit_
expr - visit_
external_ reference_ export - visit_
external_ references - visit_
fetch_ direction - visit_
fetch_ option - visit_
fetch_ option_ name - visit_
fetch_ statement - visit_
format - visit_
format_ specifier - visit_
function - visit_
function_ args - visit_
grant_ privileges_ statement - visit_
grant_ role_ statement - visit_
grant_ target_ all_ specification - visit_
grant_ target_ specification - visit_
grant_ target_ specification_ inner - visit_
homogenizing_ function - visit_
ident - visit_
ident_ error - visit_
if_ exists_ behavior - visit_
index_ option - visit_
index_ option_ name - visit_
insert_ source - visit_
insert_ statement - visit_
inspect_ shard_ statement - visit_
interval_ value - visit_
is_ expr_ construct - visit_
item_ name - visit_
join - visit_
join_ constraint - visit_
join_ operator - visit_
kafka_ broker - visit_
kafka_ broker_ aws_ privatelink - visit_
kafka_ broker_ aws_ privatelink_ option - visit_
kafka_ broker_ aws_ privatelink_ option_ name - visit_
kafka_ broker_ tunnel - visit_
kafka_ sink_ config_ option - visit_
kafka_ sink_ config_ option_ name - visit_
kafka_ sink_ key - visit_
kafka_ source_ config_ option - visit_
kafka_ source_ config_ option_ name - visit_
key_ constraint - visit_
limit - visit_
load_ generator - visit_
load_ generator_ option - visit_
load_ generator_ option_ name - visit_
map_ entry - visit_
materialized_ view_ option - visit_
materialized_ view_ option_ name - visit_
mut_ rec_ block - visit_
mut_ rec_ block_ option - visit_
mut_ rec_ block_ option_ name - visit_
my_ sql_ config_ option - visit_
my_ sql_ config_ option_ name - visit_
named_ plan - visit_
nested_ statement - visit_
network_ policy_ name - visit_
network_ policy_ option - visit_
network_ policy_ option_ name - visit_
network_ policy_ rule_ definition - visit_
network_ policy_ rule_ option - visit_
network_ policy_ rule_ option_ name - visit_
notice_ severity - visit_
object_ name - visit_
object_ type - visit_
op - visit_
order_ by_ expr - visit_
pg_ config_ option - visit_
pg_ config_ option_ name - visit_
prepare_ statement - visit_
privilege - visit_
privilege_ specification - visit_
protobuf_ schema - visit_
qualified_ replica - visit_
query - visit_
raise_ statement - visit_
reader_ schema_ selection_ strategy - visit_
reassign_ owned_ statement - visit_
refresh_ at_ option_ value - visit_
refresh_ every_ option_ value - visit_
refresh_ option_ value - visit_
replica_ definition - visit_
replica_ option - visit_
replica_ option_ name - visit_
reset_ variable_ statement - visit_
revoke_ privileges_ statement - visit_
revoke_ role_ statement - visit_
role_ attribute - visit_
role_ name - visit_
rollback_ statement - visit_
schema - visit_
schema_ name - visit_
select - visit_
select_ item - visit_
select_ option - visit_
select_ option_ name - visit_
select_ statement - visit_
set_ expr - visit_
set_ operator - visit_
set_ role_ var - visit_
set_ transaction_ statement - visit_
set_ variable_ statement - visit_
set_ variable_ to - visit_
set_ variable_ value - visit_
show_ columns_ statement - visit_
show_ create_ cluster_ statement - visit_
show_ create_ connection_ statement - visit_
show_ create_ index_ statement - visit_
show_ create_ materialized_ view_ statement - visit_
show_ create_ sink_ statement - visit_
show_ create_ source_ statement - visit_
show_ create_ table_ statement - visit_
show_ create_ view_ statement - visit_
show_ object_ type - visit_
show_ objects_ statement - visit_
show_ statement - visit_
show_ statement_ filter - visit_
show_ variable_ statement - visit_
sink_ envelope - visit_
source_ envelope - visit_
source_ error_ policy - visit_
source_ include_ metadata - visit_
sql_ server_ config_ option - visit_
sql_ server_ config_ option_ name - visit_
start_ transaction_ statement - visit_
statement - visit_
subscribe_ option - visit_
subscribe_ option_ name - visit_
subscribe_ output - visit_
subscribe_ relation - visit_
subscribe_ statement - visit_
subscript_ position - visit_
system_ object_ type - visit_
table_ alias - visit_
table_ constraint - visit_
table_ factor - visit_
table_ from_ source_ columns - visit_
table_ from_ source_ option - visit_
table_ from_ source_ option_ name - visit_
table_ option - visit_
table_ option_ name - visit_
table_ with_ joins - visit_
target_ role_ specification - visit_
transaction_ access_ mode - visit_
transaction_ isolation_ level - visit_
transaction_ mode - visit_
unresolved_ database_ name - visit_
unresolved_ item_ name - visit_
unresolved_ object_ name - visit_
unresolved_ schema_ name - visit_
update_ statement - visit_
validate_ connection_ statement - visit_
value - visit_
value_ error - visit_
values - visit_
version - visit_
view_ definition - visit_
window_ frame - visit_
window_ frame_ bound - visit_
window_ frame_ units - visit_
window_ spec - visit_
with_ option_ value