Skip to content

Commit 9286571

Browse files
committed
chore(query): unify the accessor check
1 parent f02aa84 commit 9286571

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-13
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ fmt:
1818

1919
lint:
2020
cargo fmt --all
21+
cargo clippy --workspace --all-targets -- -D warnings
22+
2123
# Cargo.toml file formatter(make setup to install)
2224
taplo fmt
2325
# Python file formatter(make setup to install)
2426
yapf -ri tests/
2527
# Bash file formatter(make setup to install)
2628
shfmt -l -w scripts/*
27-
cargo clippy --workspace --all-targets -- -D warnings
2829

2930
lint-yaml:
3031
yamllint -f auto .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2022 Datafuse Labs.
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+
15+
use std::collections::HashMap;
16+
use std::sync::Arc;
17+
18+
use common_exception::Result;
19+
use common_legacy_planners::PlanNode;
20+
21+
use crate::interpreters::ManagementModeAccess;
22+
use crate::sessions::QueryContext;
23+
use crate::sql::plans::Plan;
24+
25+
pub trait AccessChecker: Sync + Send {
26+
// Check the access permission for the old plan.
27+
// TODO(bohu): Remove after new plan done.
28+
fn check(&self, plan: &PlanNode) -> Result<()>;
29+
30+
// Check the access permission for the old plan.
31+
fn check_new(&self, _plan: &Plan) -> Result<()>;
32+
}
33+
34+
pub struct Accessor {
35+
accessors: HashMap<String, Box<dyn AccessChecker>>,
36+
}
37+
38+
impl Accessor {
39+
pub fn create(ctx: Arc<QueryContext>) -> Self {
40+
let mut accessors: HashMap<String, Box<dyn AccessChecker>> = Default::default();
41+
accessors.insert("management".to_string(), ManagementModeAccess::create(ctx));
42+
Accessor { accessors }
43+
}
44+
45+
pub fn check(&self, new_plan: &Option<Plan>, plan: &PlanNode) -> Result<()> {
46+
for accessor in self.accessors.values() {
47+
match new_plan {
48+
None => {
49+
accessor.check(plan)?;
50+
}
51+
Some(new) => {
52+
accessor.check_new(new)?;
53+
}
54+
}
55+
}
56+
Ok(())
57+
}
58+
}

src/query/service/src/interpreters/access/management_mode_access.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use common_exception::ErrorCode;
1818
use common_exception::Result;
1919
use common_legacy_planners::PlanNode;
2020

21+
use crate::interpreters::access::AccessChecker;
2122
use crate::sessions::QueryContext;
2223
use crate::sessions::TableContext;
2324
use crate::sql::plans::Plan;
@@ -27,12 +28,14 @@ pub struct ManagementModeAccess {
2728
}
2829

2930
impl ManagementModeAccess {
30-
pub fn create(ctx: Arc<QueryContext>) -> Self {
31-
ManagementModeAccess { ctx }
31+
pub fn create(ctx: Arc<QueryContext>) -> Box<dyn AccessChecker> {
32+
Box::new(ManagementModeAccess { ctx })
3233
}
34+
}
3335

36+
impl AccessChecker for ManagementModeAccess {
3437
// Check what we can do if in management mode.
35-
pub fn check(&self, plan: &PlanNode) -> Result<()> {
38+
fn check(&self, plan: &PlanNode) -> Result<()> {
3639
// Allows for management-mode.
3740
if self.ctx.get_config().query.management_mode {
3841
return match plan {
@@ -47,7 +50,7 @@ impl ManagementModeAccess {
4750
}
4851

4952
// Check what we can do if in management mode.
50-
pub fn check_new(&self, plan: &Plan) -> Result<()> {
53+
fn check_new(&self, plan: &Plan) -> Result<()> {
5154
// Allows for management-mode.
5255
if self.ctx.get_config().query.management_mode {
5356
let ok = match plan {

src/query/service/src/interpreters/access/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
mod accessor;
1516
mod management_mode_access;
1617

18+
pub use accessor::AccessChecker;
19+
pub use accessor::Accessor;
1720
pub use management_mode_access::ManagementModeAccess;

src/query/service/src/interpreters/interpreter_factory_interceptor.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use common_streams::ProgressStream;
2323
use common_streams::SendableDataBlockStream;
2424
use parking_lot::Mutex;
2525

26-
use crate::interpreters::access::ManagementModeAccess;
26+
use crate::interpreters::access::Accessor;
2727
use crate::interpreters::Interpreter;
2828
use crate::interpreters::InterpreterPtr;
2929
use crate::interpreters::InterpreterQueryLog;
@@ -41,7 +41,7 @@ pub struct InterceptorInterpreter {
4141
inner: InterpreterPtr,
4242
query_log: InterpreterQueryLog,
4343
source_pipe_builder: Mutex<Option<SourcePipeBuilder>>,
44-
management_mode_access: ManagementModeAccess,
44+
accessor_checker: Accessor,
4545
}
4646

4747
impl InterceptorInterpreter {
@@ -59,7 +59,7 @@ impl InterceptorInterpreter {
5959
inner,
6060
query_log: InterpreterQueryLog::create(ctx.clone(), query_kind),
6161
source_pipe_builder: Mutex::new(None),
62-
management_mode_access: ManagementModeAccess::create(ctx),
62+
accessor_checker: Accessor::create(ctx),
6363
}
6464
}
6565
}
@@ -75,11 +75,8 @@ impl Interpreter for InterceptorInterpreter {
7575
}
7676

7777
async fn execute(&self, ctx: Arc<QueryContext>) -> Result<SendableDataBlockStream> {
78-
// Management mode access check.
79-
match &self.new_plan {
80-
Some(p) => self.management_mode_access.check_new(p)?,
81-
_ => self.management_mode_access.check(&self.plan)?,
82-
}
78+
// Access check.
79+
self.accessor_checker.check(&self.new_plan, &self.plan)?;
8380

8481
let _ = self
8582
.inner

0 commit comments

Comments
 (0)