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