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