sql/parsers/
admin_parser.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use snafu::ResultExt;

use crate::ast::Expr;
use crate::error::{Result, SyntaxSnafu};
use crate::parser::ParserContext;
use crate::statements::admin::Admin;
use crate::statements::statement::Statement;

/// `admin` extension parser: `admin function(arg1, arg2, ...)`
/// or `admin function`
impl ParserContext<'_> {
    /// Parse `admin function(arg1, arg2, ...)` or `admin function` statement
    pub(crate) fn parse_admin_command(&mut self) -> Result<Statement> {
        let _token = self.parser.next_token();

        let object_name = self.parser.parse_object_name(false).context(SyntaxSnafu)?;

        let func = match self
            .parser
            .parse_function(object_name)
            .context(SyntaxSnafu)?
        {
            Expr::Function(f) => f,
            _ => {
                return self.unsupported(self.peek_token_as_string());
            }
        };

        Ok(Statement::Admin(Admin::Func(func)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::{Expr, Function, FunctionArg, FunctionArgExpr, Value};
    use crate::dialect::GreptimeDbDialect;
    use crate::parser::ParseOptions;

    #[test]
    fn test_parse_admin_function() {
        let sql = "ADMIN flush_table('test')";

        let mut result =
            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
                .unwrap();
        assert_eq!(1, result.len());
        let stmt = result.remove(0);
        match &stmt {
            Statement::Admin(Admin::Func(Function { name, args, .. })) => {
                assert_eq!("flush_table", name.to_string());
                assert_eq!(args.len(), 1);
                assert!(matches!(&args[0],
                                 FunctionArg::Unnamed(FunctionArgExpr::Expr(
                                     Expr::Value(Value::SingleQuotedString(s))
                                 )) if s == "test"));
            }
            _ => unreachable!(),
        }

        assert_eq!(sql, stmt.to_string());
    }

    #[test]
    fn test_parse_admin_function_without_args() {
        let sql = "ADMIN test()";

        let mut result =
            ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
                .unwrap();
        assert_eq!(1, result.len());
        let stmt = result.remove(0);
        match &stmt {
            Statement::Admin(Admin::Func(Function { name, args, .. })) => {
                assert_eq!("test", name.to_string());
                assert_eq!(args.len(), 0);
            }
            _ => unreachable!(),
        }

        assert_eq!("ADMIN test()", stmt.to_string());
    }

    #[test]
    fn test_invalid_admin_statement() {
        let sql = "ADMIN";
        assert!(ParserContext::create_with_dialect(
            sql,
            &GreptimeDbDialect {},
            ParseOptions::default()
        )
        .is_err());

        let sql = "ADMIN test";
        assert!(ParserContext::create_with_dialect(
            sql,
            &GreptimeDbDialect {},
            ParseOptions::default()
        )
        .is_err());

        let sql = "ADMIN test test";
        assert!(ParserContext::create_with_dialect(
            sql,
            &GreptimeDbDialect {},
            ParseOptions::default()
        )
        .is_err());
    }
}