sql/parsers/
explain_parser.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 snafu::ResultExt;
16use sqlparser::ast::DescribeAlias;
17
18use crate::error::{self, Result};
19use crate::parser::ParserContext;
20use crate::statements::explain::Explain;
21use crate::statements::statement::Statement;
22
23/// EXPLAIN statement parser implementation
24impl ParserContext<'_> {
25    pub(crate) fn parse_explain(&mut self) -> Result<Statement> {
26        let explain_statement = self
27            .parser
28            .parse_explain(DescribeAlias::Explain)
29            .with_context(|_| error::UnexpectedSnafu {
30                expected: "a query statement",
31                actual: self.peek_token_as_string(),
32            })?;
33
34        Ok(Statement::Explain(Explain::try_from(explain_statement)?))
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use sqlparser::ast::helpers::attached_token::AttachedToken;
41    use sqlparser::ast::{
42        GroupByExpr, Query as SpQuery, Statement as SpStatement, WildcardAdditionalOptions,
43    };
44
45    use super::*;
46    use crate::dialect::GreptimeDbDialect;
47    use crate::parser::ParseOptions;
48
49    #[test]
50    pub fn test_explain() {
51        let sql = "EXPLAIN select * from foo";
52        let result =
53            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
54        let stmts = result.unwrap();
55        assert_eq!(1, stmts.len());
56
57        let select = sqlparser::ast::Select {
58            distinct: None,
59            top: None,
60            projection: vec![sqlparser::ast::SelectItem::Wildcard(
61                WildcardAdditionalOptions::default(),
62            )],
63            into: None,
64            from: vec![sqlparser::ast::TableWithJoins {
65                relation: sqlparser::ast::TableFactor::Table {
66                    name: sqlparser::ast::ObjectName(vec![sqlparser::ast::Ident::new("foo")]),
67                    alias: None,
68                    args: None,
69                    with_hints: vec![],
70                    partitions: vec![],
71                    version: None,
72                    with_ordinality: false,
73                    json_path: None,
74                    sample: None,
75                },
76                joins: vec![],
77            }],
78            lateral_views: vec![],
79            selection: None,
80            group_by: GroupByExpr::Expressions(vec![], vec![]),
81            cluster_by: vec![],
82            distribute_by: vec![],
83            sort_by: vec![],
84            having: None,
85            qualify: None,
86            named_window: vec![],
87            value_table_mode: None,
88            top_before_distinct: false,
89            prewhere: None,
90            window_before_qualify: false,
91            connect_by: None,
92            select_token: AttachedToken::empty(),
93        };
94
95        let sp_statement = SpStatement::Query(Box::new(SpQuery {
96            with: None,
97            body: Box::new(sqlparser::ast::SetExpr::Select(Box::new(select))),
98            order_by: None,
99            limit: None,
100            limit_by: vec![],
101            offset: None,
102            fetch: None,
103            locks: vec![],
104            for_clause: None,
105            settings: None,
106            format_clause: None,
107        }));
108
109        let explain = Explain::try_from(SpStatement::Explain {
110            describe_alias: DescribeAlias::Explain,
111            analyze: false,
112            verbose: false,
113            statement: Box::new(sp_statement),
114            format: None,
115            query_plan: false,
116            options: None,
117            estimate: false,
118        })
119        .unwrap();
120
121        assert_eq!(stmts[0], Statement::Explain(explain))
122    }
123}