sql/statements/
transform.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod expand_interval;
16pub(crate) mod type_alias;
17
18use std::ops::ControlFlow;
19use std::sync::Arc;
20
21use expand_interval::ExpandIntervalTransformRule;
22use lazy_static::lazy_static;
23use sqlparser::ast::{visit_expressions_mut, Expr};
24use type_alias::TypeAliasTransformRule;
25
26use crate::error::Result;
27use crate::statements::statement::Statement;
28
29lazy_static! {
30    /// [TransformRule] registry
31    static ref RULES: Vec<Arc<dyn TransformRule>> = vec![
32        Arc::new(ExpandIntervalTransformRule{}),
33        Arc::new(TypeAliasTransformRule{}),
34    ];
35}
36
37/// Transform rule to transform statement or expr
38pub(crate) trait TransformRule: Send + Sync {
39    /// Visit a [Statement]
40    fn visit_statement(&self, _stmt: &mut Statement) -> Result<()> {
41        Ok(())
42    }
43
44    /// Visit an [Expr]
45    fn visit_expr(&self, _expr: &mut Expr) -> ControlFlow<()> {
46        ControlFlow::<()>::Continue(())
47    }
48}
49
50/// Transform statements by rules
51pub fn transform_statements(stmts: &mut Vec<Statement>) -> Result<()> {
52    for stmt in &mut *stmts {
53        for rule in RULES.iter() {
54            rule.visit_statement(stmt)?;
55        }
56    }
57
58    let _ = visit_expressions_mut(stmts, |expr| {
59        for rule in RULES.iter() {
60            rule.visit_expr(expr)?;
61        }
62
63        ControlFlow::<()>::Continue(())
64    });
65
66    Ok(())
67}