Skip to content

Commit

Permalink
Merge pull request #4757 from TCeason/ISSUE-4698/support_show_table_s…
Browse files Browse the repository at this point in the history
…tatus

support query: show table status
  • Loading branch information
mergify[bot] authored Apr 8, 2022
2 parents b0c7f01 + 1cdd0f8 commit 4bfa099
Show file tree
Hide file tree
Showing 31 changed files with 694 additions and 76 deletions.
2 changes: 2 additions & 0 deletions common/planners/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod plan_show_metrics;
mod plan_show_processlist;
mod plan_show_roles;
mod plan_show_settings;
mod plan_show_tab_stat;
mod plan_show_tables;
mod plan_show_users;
mod plan_sink;
Expand Down Expand Up @@ -191,6 +192,7 @@ pub use plan_show_metrics::ShowMetricsPlan;
pub use plan_show_processlist::ShowProcessListsPlan;
pub use plan_show_roles::ShowRolesPlan;
pub use plan_show_settings::ShowSettingsPlan;
pub use plan_show_tab_stat::ShowTabStatPlan;
pub use plan_show_tables::ShowTablesPlan;
pub use plan_show_users::ShowUsersPlan;
pub use plan_sink::SinkPlan;
Expand Down
5 changes: 2 additions & 3 deletions common/planners/src/plan_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;
use common_datavalues::DataSchema;
use common_datavalues::DataSchemaRef;

use crate::plan_show_tab_stat::ShowTabStatPlan;
use crate::ShowDatabasesPlan;
use crate::ShowEnginesPlan;
use crate::ShowFunctionsPlan;
Expand All @@ -37,9 +38,6 @@ pub enum PlanShowKind {

// show tables where name like '%xx%'
Where(String),

// show tables from db1 [or in db1]
FromOrIn(String),
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq)]
Expand All @@ -54,6 +52,7 @@ pub enum ShowPlan {
ShowUsers(ShowUsersPlan),
ShowGrants(ShowGrantsPlan),
ShowRoles(ShowRolesPlan),
ShowTabStat(ShowTabStatPlan),
}

impl ShowPlan {
Expand Down
22 changes: 22 additions & 0 deletions common/planners/src/plan_show_tab_stat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 crate::PlanShowKind;

#[derive(serde::Serialize, serde::Deserialize, PartialEq, Clone, Debug)]
pub struct ShowTabStatPlan {
pub kind: PlanShowKind,
// show tables from db1 [or in db1]
pub fromdb: Option<String>,
}
2 changes: 2 additions & 0 deletions common/planners/src/plan_show_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ use crate::PlanShowKind;
pub struct ShowTablesPlan {
pub kind: PlanShowKind,
pub showfull: bool,
// show tables from db1 [or in db1]
pub fromdb: Option<String>,
}
130 changes: 130 additions & 0 deletions docs/doc/30-reference/30-sql/40-show/show-table-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
title: SHOW TABLE STATUS
---

Shows the list of table status in the currently selected database.

## Syntax

```
SHOW TABLE STATUS
[{FROM | IN} db_name]
[LIKE 'pattern' | WHERE expr]
```

## Examples

```sql
mysql> CREATE TABLE t(id int);
mysql> SHOW TABLE STATUS\G
*************************** 1. row ***************************
Name: t
Engine: FUSE
Version: 0
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2022-04-08 04:13:48.988 +0000
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Comment:
```

Showing the tables with table name `"t"`:
```sql
mysql> SHOW TABLE STATUS LIKE 't'\G
*************************** 1. row ***************************
Name: t
Engine: FUSE
Version: 0
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2022-04-08 04:13:48.988 +0000
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Comment:
```

Showing the tables begin with `"t"`:
```sql
mysql> SHOW TABLE STATUS LIKE 't%'\G
*************************** 1. row ***************************
Name: t
Engine: FUSE
Version: 0
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2022-04-08 04:13:48.988 +0000
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Comment:
```

Showing the tables begin with `"t"` with `WHERE`:
```sql
mysql> SHOW TABLE STATUS WHERE table_name LIKE 't%'\G
*************************** 1. row ***************************
Name: t
Engine: FUSE
Version: 0
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2022-04-08 04:13:48.988 +0000
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Comment:
```

Showing the tables are inside `"default"`:
```sql
mysql> SHOW TABLE STATUS FROM 'default'\G
*************************** 1. row ***************************
Name: t
Engine: FUSE
Version: 0
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2022-04-08 04:13:48.988 +0000
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Comment:
```
12 changes: 7 additions & 5 deletions docs/doc/30-reference/30-sql/40-show/show-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Shows the list of tables in the currently selected database.
## Syntax

```
SHOW [FULL] TABLES [LIKE 'pattern' | WHERE expr | FROM 'pattern' | IN 'pattern']
SHOW [EXTENDED] [FULL] TABLES
[{FROM | IN} db_name]
[LIKE 'pattern' | WHERE expr]
```

## Examples
Expand Down Expand Up @@ -38,7 +40,7 @@ mysql> SHOW TABLES;
+------------------+
```

Showing the tables with table name `"numbers_local"`:
Showing the tables with table name `"settings"`:
```sql
mysql> SHOW TABLES LIKE 'settings';
+------------------+
Expand All @@ -48,7 +50,7 @@ mysql> SHOW TABLES LIKE 'settings';
+------------------+
```

Showing the tables begin with `"numbers"`:
Showing the tables begin with `"co"`:
```sql
mysql> SHOW TABLES LIKE 'co%';
+------------------+
Expand All @@ -60,7 +62,7 @@ mysql> SHOW TABLES LIKE 'co%';
+------------------+
```

Showing the tables begin with `"numbers"` with `WHERE`:
Showing the tables begin with `"co"` with `WHERE`:
```sql
mysql> SHOW TABLES WHERE table_name LIKE 'co%';
+------------------+
Expand All @@ -72,7 +74,7 @@ mysql> SHOW TABLES WHERE table_name LIKE 'co%';
+------------------+
```

Showing the tables are inside `"ss"`:
Showing the tables are inside `"system"`:
```sql
mysql> SHOW TABLES FROM 'system';
+------------------+
Expand Down
4 changes: 4 additions & 0 deletions query/src/interpreters/interpreter_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use crate::interpreters::ShowMetricsInterpreter;
use crate::interpreters::ShowProcessListInterpreter;
use crate::interpreters::ShowRolesInterpreter;
use crate::interpreters::ShowSettingsInterpreter;
use crate::interpreters::ShowTabStatInterpreter;
use crate::interpreters::ShowTablesInterpreter;
use crate::interpreters::ShowUsersInterpreter;
use crate::interpreters::TruncateTableInterpreter;
Expand Down Expand Up @@ -98,6 +99,9 @@ impl InterpreterFactory {
PlanNode::Show(ShowPlan::ShowTables(v)) => {
ShowTablesInterpreter::try_create(ctx_clone, v)
}
PlanNode::Show(ShowPlan::ShowTabStat(v)) => {
ShowTabStatInterpreter::try_create(ctx_clone, v)
}
PlanNode::Show(ShowPlan::ShowEngines(v)) => {
ShowEnginesInterpreter::try_create(ctx_clone, v)
}
Expand Down
4 changes: 0 additions & 4 deletions query/src/interpreters/interpreter_show_databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ impl ShowDatabasesInterpreter {
"SELECT name As Database FROM system.databases WHERE {} ORDER BY name",
v
)),
kind => Err(ErrorCode::UnImplement(format!(
"Show databases unsupported: {:?}",
kind
))),
};
}
}
Expand Down
4 changes: 0 additions & 4 deletions query/src/interpreters/interpreter_show_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ impl ShowFunctionsInterpreter {
"SELECT name, is_builtin, is_aggregate, definition, description FROM system.functions WHERE {} ORDER BY name",
v
)),
kind => Err(ErrorCode::UnImplement(format!(
"Show functions unsupported: {:?}",
kind
))),
};
}
}
Expand Down
95 changes: 95 additions & 0 deletions query/src/interpreters/interpreter_show_tab_stat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2022 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 std::sync::Arc;

use common_exception::ErrorCode;
use common_exception::Result;
use common_planners::PlanNode;
use common_planners::PlanShowKind;
use common_planners::ShowTabStatPlan;
use common_streams::SendableDataBlockStream;

use crate::interpreters::Interpreter;
use crate::interpreters::InterpreterPtr;
use crate::interpreters::SelectInterpreter;
use crate::optimizers::Optimizers;
use crate::sessions::QueryContext;
use crate::sql::PlanParser;

pub struct ShowTabStatInterpreter {
ctx: Arc<QueryContext>,
plan: ShowTabStatPlan,
}

impl ShowTabStatInterpreter {
pub fn try_create(ctx: Arc<QueryContext>, plan: ShowTabStatPlan) -> Result<InterpreterPtr> {
Ok(Arc::new(ShowTabStatInterpreter { ctx, plan }))
}

fn build_query(&self) -> Result<String> {
let mut database = self.ctx.get_current_database();
if let Some(v) = &self.plan.fromdb {
database = v.to_string();
}
let select_cols = "table_name AS Name, engine AS Engine, 0 AS Version, \
NULL AS Row_format, NULL AS Rows, NULL AS Avg_row_length, NULL AS Data_length, \
NULL AS Max_data_length, NULL AS Index_length, NULL AS Data_free, NULL AS Auto_increment, \
create_time AS Create_time, NULL AS Update_time, NULL AS Check_time, NULL AS Collation, \
NULL AS Checksum, '' AS Comment"
.to_string();
return match &self.plan.kind {
PlanShowKind::All => Ok(format!(
"SELECT {} FROM information_schema.TABLES WHERE table_schema = '{}' \
ORDER BY table_schema, table_name",
select_cols, database
)),
PlanShowKind::Like(v) => Ok(format!(
"SELECT {} FROM information_schema.TABLES WHERE table_schema = '{}' \
AND table_name LIKE {} ORDER BY table_schema, table_name",
select_cols, database, v
)),
PlanShowKind::Where(v) => Ok(format!(
"SELECT {} FROM information_schema.TABLES WHERE table_schema = '{}' \
AND ({}) ORDER BY table_schema, table_name",
select_cols, database, v
)),
};
}
}

#[async_trait::async_trait]
impl Interpreter for ShowTabStatInterpreter {
fn name(&self) -> &str {
"ShowTabStatInterpreter"
}

async fn execute(
&self,
input_stream: Option<SendableDataBlockStream>,
) -> Result<SendableDataBlockStream> {
let query = self.build_query()?;
let plan = PlanParser::parse(self.ctx.clone(), &query).await?;
let optimized = Optimizers::create(self.ctx.clone()).optimize(&plan)?;

if let PlanNode::Select(plan) = optimized {
let interpreter = SelectInterpreter::try_create(self.ctx.clone(), plan)?;
interpreter.execute(input_stream).await
} else {
return Err(ErrorCode::LogicalError(
"Show table status build query error",
));
}
}
}
Loading

1 comment on commit 4bfa099

@vercel
Copy link

@vercel vercel bot commented on 4bfa099 Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend-databend.vercel.app
databend-git-main-databend.vercel.app
databend.rs
databend.vercel.app

Please sign in to comment.