Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/planners/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod plan_revoke_privilege;
mod plan_rewriter;
mod plan_select;
mod plan_setting;
mod plan_show_create_database;
mod plan_show_grants;
mod plan_show_table_create;
mod plan_sink;
Expand Down Expand Up @@ -135,6 +136,7 @@ pub use plan_rewriter::RewriteHelper;
pub use plan_select::SelectPlan;
pub use plan_setting::SettingPlan;
pub use plan_setting::VarValue;
pub use plan_show_create_database::ShowCreateDatabasePlan;
pub use plan_show_grants::ShowGrantsPlan;
pub use plan_show_table_create::ShowCreateTablePlan;
pub use plan_sink::SinkPlan;
Expand Down
4 changes: 4 additions & 0 deletions common/planners/src/plan_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::RemotePlan;
use crate::RevokePrivilegePlan;
use crate::SelectPlan;
use crate::SettingPlan;
use crate::ShowCreateDatabasePlan;
use crate::ShowCreateTablePlan;
use crate::ShowGrantsPlan;
use crate::SinkPlan;
Expand Down Expand Up @@ -98,6 +99,7 @@ pub enum PlanNode {
CreateUserStage(CreateUserStagePlan),
DropUserStage(DropUserStagePlan),
ShowGrants(ShowGrantsPlan),
ShowCreateDatabase(ShowCreateDatabasePlan),
}

impl PlanNode {
Expand Down Expand Up @@ -143,6 +145,7 @@ impl PlanNode {
PlanNode::CreateUserStage(v) => v.schema(),
PlanNode::DropUserStage(v) => v.schema(),
PlanNode::ShowGrants(v) => v.schema(),
PlanNode::ShowCreateDatabase(v) => v.schema(),
}
}

Expand Down Expand Up @@ -187,6 +190,7 @@ impl PlanNode {
PlanNode::CreateUserStage(_) => "CreateUserStagePlan",
PlanNode::DropUserStage(_) => "DropUserStagePlan",
PlanNode::ShowGrants(_) => "ShowGrantsPlan",
PlanNode::ShowCreateDatabase(_) => "ShowCreateDatabasePlan",
}
}

Expand Down
6 changes: 6 additions & 0 deletions common/planners/src/plan_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use crate::RemotePlan;
use crate::RevokePrivilegePlan;
use crate::SelectPlan;
use crate::SettingPlan;
use crate::ShowCreateDatabasePlan;
use crate::ShowCreateTablePlan;
use crate::ShowGrantsPlan;
use crate::SinkPlan;
Expand Down Expand Up @@ -126,6 +127,7 @@ pub trait PlanRewriter {
PlanNode::Sink(plan) => self.rewrite_sink(plan),
PlanNode::ShowGrants(plan) => self.rewrite_show_grants(plan),
PlanNode::DropUserStage(plan) => self.rewrite_drop_stage(plan),
PlanNode::ShowCreateDatabase(plan) => self.rewrite_show_create_database(plan),
}
}

Expand Down Expand Up @@ -403,6 +405,10 @@ pub trait PlanRewriter {
fn rewrite_sink(&mut self, plan: &SinkPlan) -> Result<PlanNode> {
Ok(PlanNode::Sink(plan.clone()))
}

fn rewrite_show_create_database(&mut self, plan: &ShowCreateDatabasePlan) -> Result<PlanNode> {
Ok(PlanNode::ShowCreateDatabase(plan.clone()))
}
}

pub struct RewriteHelper {}
Expand Down
27 changes: 27 additions & 0 deletions common/planners/src/plan_show_create_database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 Datafuse Labs.
//
// 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 common_datavalues::DataSchemaRef;

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq)]
pub struct ShowCreateDatabasePlan {
pub db: String,
pub schema: DataSchemaRef,
}

impl ShowCreateDatabasePlan {
pub fn schema(&self) -> DataSchemaRef {
self.schema.clone()
}
}
6 changes: 6 additions & 0 deletions common/planners/src/plan_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::RemotePlan;
use crate::RevokePrivilegePlan;
use crate::SelectPlan;
use crate::SettingPlan;
use crate::ShowCreateDatabasePlan;
use crate::ShowCreateTablePlan;
use crate::ShowGrantsPlan;
use crate::SinkPlan;
Expand Down Expand Up @@ -139,6 +140,7 @@ pub trait PlanVisitor {
PlanNode::CreateUserStage(plan) => self.visit_create_stage(plan),
PlanNode::ShowGrants(plan) => self.visit_show_grants(plan),
PlanNode::DropUserStage(plan) => self.visit_drop_stage(plan),
PlanNode::ShowCreateDatabase(plan) => self.visit_show_create_database(plan),
}
}

Expand Down Expand Up @@ -332,4 +334,8 @@ pub trait PlanVisitor {
fn visit_show_grants(&mut self, _: &ShowGrantsPlan) -> Result<()> {
Ok(())
}

fn visit_show_create_database(&mut self, _: &ShowCreateDatabasePlan) -> Result<()> {
Ok(())
}
}
2 changes: 1 addition & 1 deletion query/src/catalogs/impls/immutable_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::catalogs::InMemoryMetas;
use crate::catalogs::SYS_TBL_ID_BEGIN;
use crate::configs::Config;
use crate::databases::Database;
use crate::storages::SystemDatabase;
use crate::databases::SystemDatabase;
use crate::storages::Table;

/// System Catalog contains ... all the system databases (no surprise :)
Expand Down
18 changes: 7 additions & 11 deletions query/src/catalogs/impls/mutable_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ impl MutableCatalog {
meta: self.ctx.meta.clone(),
in_memory_data: self.ctx.in_memory_data.clone(),
};
self.ctx
.database_factory
.get_database(ctx, &db_info.db, &db_info.meta)
self.ctx.database_factory.get_database(ctx, db_info)
}
}

Expand Down Expand Up @@ -156,14 +154,12 @@ impl Catalog for MutableCatalog {
tracing::error!("db name: {}, engine: {}", &req.db, &req.meta.engine);

// Initial the database after creating.
let db_ctx = DatabaseContext {
meta: self.ctx.meta.clone(),
in_memory_data: self.ctx.in_memory_data.clone(),
};
let database = self
.ctx
.database_factory
.get_database(db_ctx, &req.db, &req.meta)?;
let db_info = Arc::new(DatabaseInfo {
database_id: res.database_id,
db: req.db.clone(),
meta: req.meta.clone(),
});
let database = self.build_db_instance(&db_info)?;
database.init_database().await?;
Ok(CreateDatabaseReply {
database_id: res.database_id,
Expand Down
17 changes: 17 additions & 0 deletions query/src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;

use common_exception::Result;
use common_meta_types::DatabaseInfo;
use dyn_clone::DynClone;

#[async_trait::async_trait]
pub trait Database: DynClone + Sync + Send {
/// Database name.
fn name(&self) -> &str;

fn engine(&self) -> &str {
self.get_db_info().engine()
}

fn engine_options(&self) -> &HashMap<String, String> {
&self.get_db_info().meta.engine_options
}

fn options(&self) -> &HashMap<String, String> {
&self.get_db_info().meta.options
}

fn get_db_info(&self) -> &DatabaseInfo;

// Initial a database.
async fn init_database(&self) -> Result<()> {
Ok(())
Expand Down
27 changes: 8 additions & 19 deletions query/src/databases/database_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::sync::Arc;
use common_exception::ErrorCode;
use common_exception::Result;
use common_infallible::RwLock;
use common_meta_types::DatabaseMeta;
use common_meta_types::DatabaseInfo;

use crate::configs::Config;
use crate::databases::default::DefaultDatabase;
Expand All @@ -28,26 +28,16 @@ use crate::databases::Database;
use crate::databases::DatabaseContext;

pub trait DatabaseCreator: Send + Sync {
fn try_create(
&self,
ctx: DatabaseContext,
db_name: &str,
db_meta: DatabaseMeta,
) -> Result<Box<dyn Database>>;
fn try_create(&self, ctx: DatabaseContext, db_info: DatabaseInfo) -> Result<Box<dyn Database>>;
}

impl<T> DatabaseCreator for T
where
T: Fn(DatabaseContext, &str, DatabaseMeta) -> Result<Box<dyn Database>>,
T: Fn(DatabaseContext, DatabaseInfo) -> Result<Box<dyn Database>>,
T: Send + Sync,
{
fn try_create(
&self,
ctx: DatabaseContext,
db_name: &str,
db_meta: DatabaseMeta,
) -> Result<Box<dyn Database>> {
self(ctx, db_name, db_meta)
fn try_create(&self, ctx: DatabaseContext, db_info: DatabaseInfo) -> Result<Box<dyn Database>> {
self(ctx, db_info)
}
}

Expand All @@ -72,10 +62,9 @@ impl DatabaseFactory {
pub fn get_database(
&self,
ctx: DatabaseContext,
db_name: &str,
db_meta: &DatabaseMeta,
db_info: &DatabaseInfo,
) -> Result<Arc<dyn Database>> {
let db_engine = &db_meta.engine;
let db_engine = &db_info.engine();
let engine = if db_engine.is_empty() {
"DEFAULT".to_string()
} else {
Expand All @@ -87,7 +76,7 @@ impl DatabaseFactory {
ErrorCode::UnknownDatabaseEngine(format!("Unknown database engine {}", engine))
})?;

let db: Arc<dyn Database> = factory.try_create(ctx, db_name, db_meta.clone())?.into();
let db: Arc<dyn Database> = factory.try_create(ctx, db_info.clone())?.into();
Ok(db)
}
}
20 changes: 9 additions & 11 deletions query/src/databases/default/default_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,28 @@
// limitations under the License.

use common_exception::Result;
use common_meta_types::DatabaseMeta;
use common_meta_types::DatabaseInfo;

use crate::databases::Database;
use crate::databases::DatabaseContext;

#[derive(Clone)]
pub struct DefaultDatabase {
db_name: String,
db_info: DatabaseInfo,
}

impl DefaultDatabase {
pub fn try_create(
_ctx: DatabaseContext,
db_name: &str,
_db_meta: DatabaseMeta,
) -> Result<Box<dyn Database>> {
Ok(Box::new(Self {
db_name: db_name.to_string(),
}))
pub fn try_create(_ctx: DatabaseContext, db_info: DatabaseInfo) -> Result<Box<dyn Database>> {
Ok(Box::new(Self { db_info }))
}
}

impl Database for DefaultDatabase {
fn name(&self) -> &str {
&self.db_name
&self.db_info.db
}

fn get_db_info(&self) -> &DatabaseInfo {
&self.db_info
}
}
39 changes: 18 additions & 21 deletions query/src/databases/github/github_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use common_exception::Result;
use common_meta_types::DatabaseMeta;
use common_meta_types::DatabaseInfo;
use common_tracing::tracing;
use octocrab::params;

Expand All @@ -30,38 +30,35 @@ use crate::storages::StorageContext;
#[derive(Clone)]
pub struct GithubDatabase {
ctx: DatabaseContext,
db_name: String,
token: String,
db_info: DatabaseInfo,
}

impl GithubDatabase {
pub fn try_create(
ctx: DatabaseContext,
db_name: &str,
db_meta: DatabaseMeta,
) -> Result<Box<dyn Database>> {
let token = db_meta
.engine_options
.get("token")
.unwrap_or(&"".to_string())
.clone();
Ok(Box::new(Self {
ctx,
db_name: db_name.to_string(),
token,
}))
pub fn try_create(ctx: DatabaseContext, db_info: DatabaseInfo) -> Result<Box<dyn Database>> {
Ok(Box::new(Self { ctx, db_info }))
}
}

#[async_trait::async_trait]
impl Database for GithubDatabase {
fn name(&self) -> &str {
&self.db_name
&self.db_info.db
}

fn get_db_info(&self) -> &DatabaseInfo {
&self.db_info
}

async fn init_database(&self) -> Result<()> {
let token = self
.get_db_info()
.meta
.engine_options
.get("token")
.unwrap_or(&"".to_string())
.clone();
// 1. get all repos in this organization
let instance = create_github_client(&self.token)?;
let instance = create_github_client(&token)?;
let repos = instance
.orgs(self.name())
.list_repos()
Expand All @@ -82,7 +79,7 @@ impl Database for GithubDatabase {
let options = RepoTableOptions {
owner: self.name().to_string(),
repo: repo.name.clone(),
token: self.token.clone(),
token: token.clone(),
table_type: "".to_string(),
};

Expand Down
2 changes: 2 additions & 0 deletions query/src/databases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ mod database_context;
mod database_factory;
mod default;
mod github;
mod system;

pub use database::Database;
pub use database_context::DatabaseContext;
pub use database_factory::DatabaseFactory;
pub use system::SystemDatabase;
Loading