query/query_engine/
options.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 session::context::QueryContextRef;
16use snafu::ensure;
17
18use crate::error::{QueryAccessDeniedSnafu, Result};
19
20#[derive(Default, Clone)]
21pub struct QueryOptions {
22    pub disallow_cross_catalog_query: bool,
23}
24
25// TODO(shuiyisong): remove one method after #559 is done
26pub fn validate_catalog_and_schema(
27    catalog: &str,
28    schema: &str,
29    query_ctx: &QueryContextRef,
30) -> Result<()> {
31    ensure!(
32        catalog == query_ctx.current_catalog(),
33        QueryAccessDeniedSnafu {
34            catalog: catalog.to_string(),
35            schema: schema.to_string(),
36        }
37    );
38
39    Ok(())
40}
41
42#[cfg(test)]
43mod tests {
44
45    use std::sync::Arc;
46
47    use session::context::QueryContext;
48
49    use super::*;
50
51    #[test]
52    fn test_validate_catalog_and_schema() {
53        let context = Arc::new(QueryContext::with("greptime", "public"));
54
55        validate_catalog_and_schema("greptime", "public", &context).unwrap();
56        let re = validate_catalog_and_schema("greptime", "private_schema", &context);
57        assert!(re.is_ok());
58        let re = validate_catalog_and_schema("wrong_catalog", "public", &context);
59        assert!(re.is_err());
60        let re = validate_catalog_and_schema("wrong_catalog", "wrong_schema", &context);
61        assert!(re.is_err());
62
63        validate_catalog_and_schema("greptime", "information_schema", &context).unwrap();
64    }
65}