forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.rs
96 lines (78 loc) · 2.54 KB
/
validator.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
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
use query_frontend::plan::{Plan, ShowPlan};
use table_engine::partition;
use crate::interpreter::{PermissionDenied, Result};
macro_rules! is_sub_table {
($table_name:expr) => {{
let table_name = $table_name;
partition::is_sub_partition_table(table_name)
}};
}
/// Validator for [Plan]
#[derive(Debug)]
pub(crate) struct Validator {
ctx: ValidateContext,
}
impl Validator {
pub fn new(ctx: ValidateContext) -> Self {
Self { ctx }
}
pub fn validate(&self, plan: &Plan) -> Result<()> {
self.validate_partition_table_access(plan)?;
Ok(())
}
fn validate_partition_table_access(&self, plan: &Plan) -> Result<()> {
// Only can operate the sub tables(table partition) directly while enable
// partition table access.
if !self.ctx.enable_partition_table_access && Validator::contains_sub_tables(plan) {
PermissionDenied {
msg: "only can process sub tables in table partition directly when enable partition table access",
}
.fail()
} else {
Ok(())
}
}
// TODO: reduce duplicated codes.
fn contains_sub_tables(plan: &Plan) -> bool {
match plan {
Plan::Query(plan) => {
let res = plan.tables.visit::<_, ()>(|name, _| {
if partition::is_sub_partition_table(name.table.as_ref()) {
Err(())
} else {
Ok(())
}
});
res.is_err()
}
Plan::Create(plan) => {
is_sub_table!(&plan.table)
}
Plan::Drop(plan) => {
is_sub_table!(&plan.table)
}
Plan::Insert(plan) => {
is_sub_table!(plan.table.name())
}
Plan::Describe(plan) => {
is_sub_table!(plan.table.name())
}
Plan::AlterTable(plan) => {
is_sub_table!(plan.table.name())
}
Plan::Show(show_plan) => {
if let ShowPlan::ShowCreatePlan(show_create_plan) = show_plan {
is_sub_table!(show_create_plan.table.name())
} else {
false
}
}
Plan::Exists(_) => false,
}
}
}
#[derive(Debug, Default, Clone)]
pub struct ValidateContext {
pub enable_partition_table_access: bool,
}