tests_integration/
opentsdb.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
15#[cfg(test)]
16mod tests {
17    use std::sync::Arc;
18
19    use client::OutputData;
20    use common_recordbatch::RecordBatches;
21    use frontend::instance::Instance;
22    use itertools::Itertools;
23    use servers::opentsdb::codec::DataPoint;
24    use servers::query_handler::sql::SqlQueryHandler;
25    use servers::query_handler::OpentsdbProtocolHandler;
26    use session::context::QueryContext;
27
28    use crate::standalone::GreptimeDbStandaloneBuilder;
29    use crate::tests;
30
31    #[tokio::test(flavor = "multi_thread")]
32    async fn test_standalone_exec() {
33        let standalone = GreptimeDbStandaloneBuilder::new("test_standalone_exec")
34            .build()
35            .await;
36        let instance = standalone.fe_instance();
37
38        test_exec(instance).await;
39    }
40
41    #[tokio::test(flavor = "multi_thread")]
42    async fn test_distributed_exec() {
43        let distributed = tests::create_distributed_instance("test_distributed_exec").await;
44        test_exec(&distributed.frontend()).await;
45    }
46
47    async fn test_exec(instance: &Arc<Instance>) {
48        let ctx = QueryContext::arc();
49
50        // should create new table "my_metric_1" directly
51        let data_point1 = DataPoint::new(
52            "my_metric_1".to_string(),
53            1000,
54            1.0,
55            vec![
56                ("tagk1".to_string(), "tagv1".to_string()),
57                ("tagk2".to_string(), "tagv2".to_string()),
58            ],
59        );
60
61        // should create new column "tagk3" directly
62        let data_point2 = DataPoint::new(
63            "my_metric_1".to_string(),
64            2000,
65            2.0,
66            vec![
67                ("tagk2".to_string(), "tagv2".to_string()),
68                ("tagk3".to_string(), "tagv3".to_string()),
69            ],
70        );
71
72        // should handle null tags properly
73        let data_point3 = DataPoint::new("my_metric_1".to_string(), 3000, 3.0, vec![]);
74
75        let data_points = vec![data_point1, data_point2, data_point3];
76        instance.exec(data_points, ctx.clone()).await.unwrap();
77
78        let output = instance
79            .do_query(
80                "select * from my_metric_1 order by greptime_timestamp",
81                QueryContext::arc(),
82            )
83            .await
84            .remove(0)
85            .unwrap();
86        match output.data {
87            OutputData::Stream(stream) => {
88                let recordbatches = RecordBatches::try_collect(stream).await.unwrap();
89                let pretty_print = recordbatches.pretty_print().unwrap();
90                let expected = vec![
91                    "+-------+-------+----------------+---------------------+-------+",
92                    "| tagk1 | tagk2 | greptime_value | greptime_timestamp  | tagk3 |",
93                    "+-------+-------+----------------+---------------------+-------+",
94                    "| tagv1 | tagv2 | 1.0            | 1970-01-01T00:00:01 |       |",
95                    "|       | tagv2 | 2.0            | 1970-01-01T00:00:02 | tagv3 |",
96                    "|       |       | 3.0            | 1970-01-01T00:00:03 |       |",
97                    "+-------+-------+----------------+---------------------+-------+",
98                ]
99                .into_iter()
100                .join("\n");
101                assert_eq!(pretty_print, expected);
102            }
103            _ => unreachable!(),
104        };
105    }
106}