tests_fuzz/translator/mysql/
insert_expr.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
15use crate::error::{Error, Result};
16use crate::ir::insert_expr::InsertIntoExpr;
17use crate::translator::DslTranslator;
18
19pub struct InsertIntoExprTranslator;
20
21impl DslTranslator<InsertIntoExpr, String> for InsertIntoExprTranslator {
22    type Error = Error;
23
24    fn translate(&self, input: &InsertIntoExpr) -> Result<String> {
25        Ok(format!(
26            "INSERT INTO {} {} VALUES\n{};",
27            input.table_name,
28            Self::format_columns(input),
29            Self::format_values(input)
30        ))
31    }
32}
33
34impl InsertIntoExprTranslator {
35    fn format_columns(input: &InsertIntoExpr) -> String {
36        if input.omit_column_list {
37            "".to_string()
38        } else {
39            let list = input
40                .columns
41                .iter()
42                .map(|c| c.name.to_string())
43                .collect::<Vec<_>>()
44                .join(", ")
45                .to_string();
46
47            format!("({})", list)
48        }
49    }
50
51    fn format_values(input: &InsertIntoExpr) -> String {
52        input
53            .values_list
54            .iter()
55            .map(|value| {
56                format!(
57                    "({})",
58                    value
59                        .iter()
60                        .map(|v| v.to_string())
61                        .collect::<Vec<_>>()
62                        .join(", ")
63                )
64            })
65            .collect::<Vec<_>>()
66            .join(",\n")
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use std::sync::Arc;
73
74    use rand::{Rng, SeedableRng};
75
76    use super::*;
77    use crate::generator::insert_expr::InsertExprGeneratorBuilder;
78    use crate::generator::Generator;
79    use crate::test_utils;
80    use crate::translator::DslTranslator;
81
82    #[test]
83    fn test_insert_into_translator() {
84        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0);
85        let omit_column_list = rng.random_bool(0.2);
86
87        let test_ctx = test_utils::new_test_ctx();
88        let insert_expr_generator = InsertExprGeneratorBuilder::default()
89            .table_ctx(Arc::new(test_ctx))
90            .omit_column_list(omit_column_list)
91            .rows(2)
92            .build()
93            .unwrap();
94
95        let insert_expr = insert_expr_generator.generate(&mut rng).unwrap();
96
97        let output = InsertIntoExprTranslator.translate(&insert_expr).unwrap();
98        let expected = r#"INSERT INTO test (cpu_util, ts, host) VALUES
99(0.494276426950336, '+210328-02-20 15:44:23.848+0000', 'aut'),
100(0.5240550121500691, '-78231-02-16 05:32:41.400+0000', 'in');"#;
101        assert_eq!(output, expected);
102
103        let insert_expr = insert_expr_generator.generate(&mut rng).unwrap();
104        let output = InsertIntoExprTranslator.translate(&insert_expr).unwrap();
105        let expected = r#"INSERT INTO test (ts, host) VALUES
106('+137972-11-29 18:23:19.505+0000', 'repellendus'),
107('-237884-01-11 09:44:43.491+0000', 'a');"#;
108        assert_eq!(output, expected);
109
110        let insert_expr = insert_expr_generator.generate(&mut rng).unwrap();
111        let output = InsertIntoExprTranslator.translate(&insert_expr).unwrap();
112        let expected = r#"INSERT INTO test (disk_util, ts) VALUES
113(0.399415030703252, '+154545-01-21 09:38:13.768+0000'),
114(NULL, '-227688-03-19 14:23:24.582+0000');"#;
115        assert_eq!(output, expected);
116    }
117}