sql/statements/
describe.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 std::fmt::Display;
16
17use serde::Serialize;
18use sqlparser::ast::ObjectName;
19use sqlparser_derive::{Visit, VisitMut};
20
21/// SQL structure for `DESCRIBE TABLE`.
22#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
23pub struct DescribeTable {
24    name: ObjectName,
25}
26
27impl DescribeTable {
28    /// Creates a statement for `DESCRIBE TABLE`
29    pub fn new(name: ObjectName) -> Self {
30        Self { name }
31    }
32
33    pub fn name(&self) -> &ObjectName {
34        &self.name
35    }
36}
37
38impl Display for DescribeTable {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        let name = self.name();
41        write!(f, r#"DESCRIBE TABLE {name}"#)
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use std::assert_matches::assert_matches;
48
49    use crate::dialect::GreptimeDbDialect;
50    use crate::parser::{ParseOptions, ParserContext};
51    use crate::statements::statement::Statement;
52    use crate::util::format_raw_object_name;
53
54    #[test]
55    pub fn test_describe_table() {
56        let sql = "DESCRIBE TABLE test";
57        let stmts: Vec<Statement> =
58            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
59                .unwrap();
60        assert_eq!(1, stmts.len());
61        assert_matches!(&stmts[0], Statement::DescribeTable { .. });
62        match &stmts[0] {
63            Statement::DescribeTable(show) => {
64                assert_eq!(format_raw_object_name(&show.name), "test");
65            }
66            _ => {
67                unreachable!();
68            }
69        }
70    }
71
72    #[test]
73    pub fn test_describe_schema_table() {
74        let sql = "DESCRIBE TABLE test_schema.test";
75        let stmts: Vec<Statement> =
76            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
77                .unwrap();
78        assert_eq!(1, stmts.len());
79        assert_matches!(&stmts[0], Statement::DescribeTable { .. });
80        match &stmts[0] {
81            Statement::DescribeTable(show) => {
82                assert_eq!(format_raw_object_name(&show.name), "test_schema.test");
83            }
84            _ => {
85                unreachable!();
86            }
87        }
88    }
89
90    #[test]
91    pub fn test_describe_catalog_schema_table() {
92        let sql = "DESCRIBE TABLE test_catalog.test_schema.test";
93        let stmts: Vec<Statement> =
94            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
95                .unwrap();
96        assert_eq!(1, stmts.len());
97        assert_matches!(&stmts[0], Statement::DescribeTable { .. });
98        match &stmts[0] {
99            Statement::DescribeTable(show) => {
100                assert_eq!(
101                    format_raw_object_name(&show.name),
102                    "test_catalog.test_schema.test"
103                );
104            }
105            _ => {
106                unreachable!();
107            }
108        }
109    }
110
111    #[test]
112    pub fn test_describe_missing_table_name() {
113        let sql = "DESCRIBE TABLE";
114        assert!(ParserContext::create_with_dialect(
115            sql,
116            &GreptimeDbDialect {},
117            ParseOptions::default()
118        )
119        .is_err());
120    }
121
122    #[test]
123    fn test_display_describe_table() {
124        let sql = r"describe table monitor;";
125        let stmts =
126            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
127                .unwrap();
128        assert_eq!(1, stmts.len());
129        assert_matches!(&stmts[0], Statement::DescribeTable { .. });
130
131        match &stmts[0] {
132            Statement::DescribeTable(set) => {
133                let new_sql = format!("\n{}", set);
134                assert_eq!(
135                    r#"
136DESCRIBE TABLE monitor"#,
137                    &new_sql
138                );
139            }
140            _ => {
141                unreachable!();
142            }
143        }
144    }
145}