-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
77 lines (69 loc) · 2.98 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Ways to transform an [`ast::SqlProgram`] into another `ast::SqlProgram`.
//!
//! These work entirely in terms of parsed BigQuery SQL. Normally, the ideal
//! transform should take a `SqlProgram` containing BigQuery-specific features,
//! and return one that uses the equivalent standard SQL features. This allows
//! us to use the same transforms for as many databases as possible.
use crate::{ast, errors::Result};
pub use self::{
clean_up_temp_manually::CleanUpTempManually, countif_to_case::CountifToCase,
expand_except::ExpandExcept, in_unnest_to_contains::InUnnestToContains,
index_from_one::IndexFromOne, is_bool_to_case::IsBoolToCase,
or_replace_to_drop_if_exists::OrReplaceToDropIfExists, qualify_to_subquery::QualifyToSubquery,
rename_functions::RenameFunctionsBuilder,
special_date_functions_to_trino::SpecialDateFunctionsToTrino,
standardize_current_time_unit::StandardizeCurrentTimeUnit,
standardize_literal_types::StandardizeLiteralTypes,
};
mod clean_up_temp_manually;
mod countif_to_case;
mod expand_except;
mod in_unnest_to_contains;
mod index_from_one;
mod is_bool_to_case;
mod or_replace_to_drop_if_exists;
mod qualify_to_subquery;
mod rename_functions;
mod special_date_functions_to_trino;
mod standardize_current_time_unit;
mod standardize_literal_types;
/// A transform that modifies an [`SqlProgram`].
pub trait Transform {
/// A human-readable name for this transform.
fn name(&self) -> &'static str;
/// Does this transform require currently valid type information?
///
/// This is sort of analogous to an LLVM analysis pass that can be
/// invalidated and recreated, except we can only perform a single type of
/// analysis.
fn requires_types(&self) -> bool {
false
}
/// Apply this transform to an [`SqlProgram`].
///
/// Returns a list of extra SQL statements that need to be executed before
/// the transformed program. These statements must be in the target dialect
/// of SQL, and typically include custom UDFs or similar temporary
/// definitions.
///
/// A transform should only be used once, as it may modify itself in the
/// process of transforming the AST. To enforce this, the transform takes
/// `self: Box<Self>` rather than `&mut self`.
fn transform(self: Box<Self>, sql_program: &mut ast::SqlProgram) -> Result<TransformExtra>;
}
/// Extra SQL returned by a [`Transform`].
#[derive(Debug, Default)]
pub struct TransformExtra {
/// Individual statements that should be run before the transformed program.
pub native_setup_sql: Vec<String>,
/// Individual statements that should be run after the transformed program,
/// even if it fails. These may individually fail.
pub native_teardown_sql: Vec<String>,
}
impl TransformExtra {
/// Merge in another `TransformExtra`.
pub fn extend(&mut self, other: TransformExtra) {
self.native_setup_sql.extend(other.native_setup_sql);
self.native_teardown_sql.extend(other.native_teardown_sql);
}
}