mz_sql/ast.rs
1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! SQL abstract syntax tree.
11
12use mz_sql_parser::ast::visit::Visit;
13pub use mz_sql_parser::ast::*;
14pub mod transform;
15
16/// A visitor that determines if a node is constant (does not contain references to external
17/// objects).
18#[derive(Debug)]
19pub struct ConstantVisitor {
20 pub constant: bool,
21}
22
23impl ConstantVisitor {
24 /// Whether the insert source is constant.
25 pub fn insert_source<T: AstInfo>(node: &InsertSource<T>) -> bool {
26 let mut visitor = Self { constant: true };
27 visitor.visit_insert_source(node);
28 visitor.constant
29 }
30}
31
32impl<'ast, T: AstInfo> Visit<'ast, T> for ConstantVisitor {
33 fn visit_set_expr(&mut self, node: &'ast SetExpr<T>) {
34 if matches!(node, SetExpr::Show(_) | SetExpr::Table(_)) {
35 self.constant = false;
36 }
37 visit::visit_set_expr(self, node);
38 }
39
40 fn visit_table_factor(&mut self, node: &'ast TableFactor<T>) {
41 // It's possible a table reference is referencing some constant table defined within the
42 // query, but we don't want the AST to have to figure that out yet. If that's required, the
43 // statement must be planned instead.
44 if matches!(node, TableFactor::Table { .. }) {
45 self.constant = false;
46 }
47 }
48}