Skip to content

Commit

Permalink
refactor: implement desc tables/databases by reading system tables an…
Browse files Browse the repository at this point in the history
…d fix some word error (cnosdb#1397)

* refactor: implement desc tables&show databases by reading system tables

* show table/database column name to be in uppercase letters

* make clippy check

* fmt
  • Loading branch information
guojidan authored and ZuoTiJia committed Nov 2, 2023
1 parent d342b4f commit fdf2436
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 276 deletions.
2 changes: 1 addition & 1 deletion common/protocol_parser/src/line_protocol/parser.rs
Expand Up @@ -204,7 +204,7 @@ mod test {
let data = parser.parse(lines).unwrap();
assert_eq!(data.len(), 2);

let data_1 = data.get(0).unwrap();
let data_1 = data.first().unwrap();
assert_eq!(
*data_1,
Line {
Expand Down
78 changes: 0 additions & 78 deletions query_server/query/src/execution/ddl/describe_database.rs

This file was deleted.

115 changes: 0 additions & 115 deletions query_server/query/src/execution/ddl/describe_table.rs

This file was deleted.

11 changes: 0 additions & 11 deletions query_server/query/src/execution/ddl/mod.rs
Expand Up @@ -23,8 +23,6 @@ use crate::execution::ddl::checksum_group::ChecksumGroupTask;
use crate::execution::ddl::compact_vnode::CompactVnodeTask;
use crate::execution::ddl::copy_vnode::CopyVnodeTask;
use crate::execution::ddl::create_database::CreateDatabaseTask;
use crate::execution::ddl::describe_database::DescribeDatabaseTask;
use crate::execution::ddl::describe_table::DescribeTableTask;
use crate::execution::ddl::drop_vnode::DropVnodeTask;
use crate::execution::ddl::move_node::MoveVnodeTask;

Expand All @@ -42,8 +40,6 @@ mod create_stream_table;
mod create_table;
mod create_tenant;
mod create_user;
mod describe_database;
mod describe_table;
mod drop_database_object;
mod drop_global_object;
mod drop_tenant_object;
Expand Down Expand Up @@ -156,13 +152,6 @@ impl DDLDefinitionTaskFactory {
DDLPlan::CreateTenant(sub_plan) => Box::new(CreateTenantTask::new(*sub_plan.clone())),
DDLPlan::CreateUser(sub_plan) => Box::new(CreateUserTask::new(sub_plan.clone())),
DDLPlan::CreateRole(sub_plan) => Box::new(CreateRoleTask::new(sub_plan.clone())),
DDLPlan::DescribeDatabase(sub_plan) => Box::new(DescribeDatabaseTask::new(
sub_plan.clone(),
self.plan.schema(),
)),
DDLPlan::DescribeTable(sub_plan) => {
Box::new(DescribeTableTask::new(sub_plan.clone(), self.plan.schema()))
}
DDLPlan::AlterDatabase(sub_plan) => Box::new(AlterDatabaseTask::new(sub_plan.clone())),
DDLPlan::AlterTable(sub_plan) => Box::new(AlterTableTask::new(sub_plan.clone())),
DDLPlan::AlterTenant(sub_plan) => Box::new(AlterTenantTask::new(sub_plan.clone())),
Expand Down
Expand Up @@ -6,18 +6,29 @@ use datafusion::arrow::record_batch::RecordBatch;
use datafusion::error::DataFusionError;
use lazy_static::lazy_static;

pub const COLUMNS_TENANT_NAME: &str = "tenant_name";
pub const COLUMNS_DATABASE_NAME: &str = "database_name";
pub const COLUMNS_TABLE_NAME: &str = "table_name";
pub const COLUMNS_COLUMN_NAME: &str = "column_name";
pub const COLUMNS_COLUMN_TYPE: &str = "column_type";
pub const COLUMNS_ORDINAL_POSITION: &str = "ordinal_position";
pub const COLUMNS_COLUMN_DEFAULT: &str = "column_default";
pub const COLUMNS_IS_NULLABLE: &str = "is_nullable";
pub const COLUMNS_DATA_TYPE: &str = "data_type";
pub const COLUMNS_COMPRESSION_CODEC: &str = "compression_codec";

lazy_static! {
pub static ref COLUMN_SCHEMA: SchemaRef = Arc::new(Schema::new(vec![
Field::new("tenant_name", DataType::Utf8, false),
Field::new("database_name", DataType::Utf8, false),
Field::new("table_name", DataType::Utf8, false),
Field::new("column_name", DataType::Utf8, false),
Field::new("column_type", DataType::Utf8, false),
Field::new("ordinal_position", DataType::UInt64, false),
Field::new("column_default", DataType::Utf8, false),
Field::new("is_nullable", DataType::Boolean, false),
Field::new("data_type", DataType::Utf8, false),
Field::new("compression_codec", DataType::Utf8, true),
Field::new(COLUMNS_TENANT_NAME, DataType::Utf8, false),
Field::new(COLUMNS_DATABASE_NAME, DataType::Utf8, false),
Field::new(COLUMNS_TABLE_NAME, DataType::Utf8, false),
Field::new(COLUMNS_COLUMN_NAME, DataType::Utf8, false),
Field::new(COLUMNS_COLUMN_TYPE, DataType::Utf8, false),
Field::new(COLUMNS_ORDINAL_POSITION, DataType::UInt64, false),
Field::new(COLUMNS_COLUMN_DEFAULT, DataType::Utf8, false),
Field::new(COLUMNS_IS_NULLABLE, DataType::Boolean, false),
Field::new(COLUMNS_DATA_TYPE, DataType::Utf8, false),
Field::new(COLUMNS_COMPRESSION_CODEC, DataType::Utf8, true),
]));
}

Expand Down
Expand Up @@ -12,7 +12,7 @@ pub const DATABASES_TTL: &str = "ttl";
pub const DATABASES_SHARD: &str = "shard";
pub const DATABASES_VNODE_DURATION: &str = "vnode_duration";
pub const DATABASES_REPLICA: &str = "replica";
pub const DATABASES_PERCISION: &str = "percision";
pub const DATABASES_PRECISION: &str = "precision";

lazy_static! {
pub static ref DATABASE_SCHEMA: SchemaRef = Arc::new(Schema::new(vec![
Expand All @@ -22,7 +22,7 @@ lazy_static! {
Field::new(DATABASES_SHARD, DataType::UInt64, false),
Field::new(DATABASES_VNODE_DURATION, DataType::Utf8, false),
Field::new(DATABASES_REPLICA, DataType::UInt64, false),
Field::new(DATABASES_PERCISION, DataType::Utf8, false),
Field::new(DATABASES_PRECISION, DataType::Utf8, false),
]));
}

Expand Down
Expand Up @@ -25,7 +25,7 @@ use crate::metadata::information_schema_provider::builder::columns::{
};
use crate::metadata::information_schema_provider::InformationSchemaTableFactory;

const INFORMATION_SCHEMA_COLUMNS: &str = "COLUMNS";
pub const INFORMATION_SCHEMA_COLUMNS: &str = "COLUMNS";

/// This view only displays the column information of tables under the database that the current user has Read permission or higher.
pub struct ColumnsFactory {}
Expand Down
Expand Up @@ -2,15 +2,20 @@ use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
pub use builder::columns::{
COLUMNS_COLUMN_NAME, COLUMNS_COLUMN_TYPE, COLUMNS_COMPRESSION_CODEC, COLUMNS_DATABASE_NAME,
COLUMNS_DATA_TYPE, COLUMNS_TABLE_NAME,
};
pub use builder::databases::{
DATABASES_DATABASE_NAME, DATABASES_PERCISION, DATABASES_REPLICA, DATABASES_SHARD,
DATABASES_DATABASE_NAME, DATABASES_PRECISION, DATABASES_REPLICA, DATABASES_SHARD,
DATABASES_TENANT_NAME, DATABASES_TTL, DATABASES_VNODE_DURATION,
};
pub use builder::tables::{
TABLES_TABLE_DATABASE, TABLES_TABLE_ENGINE, TABLES_TABLE_NAME, TABLES_TABLE_OPTIONS,
TABLES_TABLE_TENANT, TABLES_TABLE_TYPE,
};
use datafusion::datasource::TableProvider;
pub use factory::columns::INFORMATION_SCHEMA_COLUMNS;
pub use factory::databases::INFORMATION_SCHEMA_DATABASES;
pub use factory::tables::INFORMATION_SCHEMA_TABLES;
use meta::error::MetaError;
Expand Down
37 changes: 35 additions & 2 deletions query_server/query/src/metadata/mod.rs
Expand Up @@ -12,8 +12,10 @@ use datafusion::logical_expr::{AggregateUDF, ScalarUDF, TableSource, WindowUDF};
use datafusion::sql::planner::ContextProvider;
use datafusion::sql::TableReference;
pub use information_schema_provider::{
DATABASES_DATABASE_NAME, DATABASES_PERCISION, DATABASES_REPLICA, DATABASES_SHARD,
DATABASES_TENANT_NAME, DATABASES_TTL, DATABASES_VNODE_DURATION, INFORMATION_SCHEMA_DATABASES,
COLUMNS_COLUMN_NAME, COLUMNS_COLUMN_TYPE, COLUMNS_COMPRESSION_CODEC, COLUMNS_DATABASE_NAME,
COLUMNS_DATA_TYPE, COLUMNS_TABLE_NAME, DATABASES_DATABASE_NAME, DATABASES_PRECISION,
DATABASES_REPLICA, DATABASES_SHARD, DATABASES_TENANT_NAME, DATABASES_TTL,
DATABASES_VNODE_DURATION, INFORMATION_SCHEMA_COLUMNS, INFORMATION_SCHEMA_DATABASES,
INFORMATION_SCHEMA_TABLES, TABLES_TABLE_DATABASE, TABLES_TABLE_ENGINE, TABLES_TABLE_NAME,
TABLES_TABLE_OPTIONS, TABLES_TABLE_TENANT, TABLES_TABLE_TYPE,
};
Expand Down Expand Up @@ -56,6 +58,13 @@ pub trait ContextProviderExtension: ContextProvider {
&self,
name: TableReference,
) -> datafusion::common::Result<Arc<TableSourceAdapter>>;
fn database_table_exist(
&self,
_database: &str,
_table: Option<&ResolvedTable>,
) -> Result<(), MetaError> {
Ok(())
}
}

pub type TableHandleProviderRef = Arc<dyn TableHandleProvider + Send + Sync>;
Expand Down Expand Up @@ -243,6 +252,30 @@ impl ContextProviderExtension for MetadataProvider {
table_handle,
)?))
}

fn database_table_exist(
&self,
database: &str,
table: Option<&ResolvedTable>,
) -> Result<(), MetaError> {
let data_info =
self.meta_client
.get_db_info(database)?
.ok_or(MetaError::DatabaseNotFound {
database: database.to_string(),
})?;

if let Some(table) = table {
data_info
.tables
.get(table.table())
.ok_or(MetaError::TableNotFound {
table: table.to_string(),
})?;
}

Ok(())
}
}

impl ContextProvider for MetadataProvider {
Expand Down

0 comments on commit fdf2436

Please sign in to comment.