Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support like syntax in show tables statement #331

Merged
merged 42 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
82d5ca4
Show tables statement support like syntax
QuintinTao Oct 21, 2022
5e546e0
Merge branch 'main' into sql-suporting
QuintinTao Oct 21, 2022
c21b904
add supporting for show tables
QuintinTao Oct 21, 2022
7b942ab
Merge branch 'sql-suporting' of https://github.com/QuintinTao/ceresdb…
QuintinTao Oct 21, 2022
ad3bf17
Merge branch 'main' into sql-suporting
QuintinTao Oct 21, 2022
9dc3d7c
modify fmt
QuintinTao Oct 21, 2022
90b7a32
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Oct 21, 2022
abf7f9d
Merge branch 'sql-suporting' of https://github.com/QuintinTao/ceresdb…
QuintinTao Oct 21, 2022
2065728
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Oct 22, 2022
7a0c2f7
code fmt modify
QuintinTao Oct 22, 2022
5c9d13e
code fmt modify
QuintinTao Oct 22, 2022
4bb01cb
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Oct 24, 2022
be74294
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Oct 24, 2022
bd12a04
reduce show tables param
QuintinTao Oct 24, 2022
314695d
add integration test and regex dependency
QuintinTao Oct 24, 2022
48ad5de
modify test case
QuintinTao Oct 24, 2022
222a0e3
modify test case
QuintinTao Oct 24, 2022
83c6261
modify test case fmt
QuintinTao Oct 25, 2022
cff208f
modify test case fmt
QuintinTao Oct 25, 2022
2c16051
modify test case
QuintinTao Oct 25, 2022
083cd52
modify test case
QuintinTao Oct 25, 2022
2d61f95
modify param name
QuintinTao Oct 26, 2022
65f1a07
modify test case
QuintinTao Oct 26, 2022
b809369
modify test case
QuintinTao Oct 26, 2022
faa45e6
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Oct 28, 2022
ff18d1b
test result update
QuintinTao Oct 28, 2022
a2854ea
Merge branch 'sql-suporting' of https://github.com/QuintinTao/ceresdb…
QuintinTao Oct 28, 2022
b0686d3
add cargo.lock
QuintinTao Oct 28, 2022
a4381cc
update cargo.lock
QuintinTao Oct 28, 2022
03bdecc
update cargo.lock
QuintinTao Oct 28, 2022
3c55ef3
modify test case result
QuintinTao Oct 28, 2022
054752f
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Oct 31, 2022
845e3cd
modify test result
QuintinTao Oct 31, 2022
7d3614a
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Nov 1, 2022
0dac07a
modify test reuslt
QuintinTao Nov 1, 2022
b583abf
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Nov 2, 2022
52bfa59
modify is_table_matched method and test result
QuintinTao Nov 3, 2022
250abbb
Merge branch 'CeresDB:main' into sql-suporting
QuintinTao Nov 4, 2022
5de3b02
modify is_table_matched and test cases
QuintinTao Nov 4, 2022
b744274
remove type conversion
QuintinTao Nov 8, 2022
e80f843
rollback
QuintinTao Nov 8, 2022
a271371
remove whitespace
QuintinTao Nov 8, 2022
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion interpreters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sql = { workspace = true }
table_engine = { workspace = true }
query_engine = { workspace = true }
arrow = { workspace = true }

regex = "1"
[dev-dependencies]
analytic_engine = { workspace = true, features = ["test"] }
catalog_impls = { workspace = true }
Expand Down
98 changes: 86 additions & 12 deletions interpreters/src/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use arrow::{
};
use async_trait::async_trait;
use catalog::{manager::ManagerRef, schema::Schema, Catalog};
use regex::Regex;
use snafu::{Backtrace, OptionExt, ResultExt, Snafu};
use sql::{
ast::ShowCreateObject,
plan::{ShowCreatePlan, ShowPlan},
plan::{ShowCreatePlan, ShowPlan, ShowTablesPlan},
};

use crate::{
Expand Down Expand Up @@ -94,16 +95,27 @@ impl ShowInterpreter {
show_create.execute_show_create()
}

fn show_tables(ctx: Context, catalog_manager: ManagerRef) -> Result<Output> {
fn show_tables(
ctx: Context,
catalog_manager: ManagerRef,
plan: ShowTablesPlan,
) -> Result<Output> {
let schema = get_default_schema(&ctx, &catalog_manager)?;

let tables_names = schema
.all_tables()
.context(FetchTables)?
.iter()
.map(|t| t.name().to_string())
.collect::<Vec<_>>();

let tables_names = match plan.pattern {
Some(sc) => schema
.all_tables()
.context(FetchTables)?
.iter()
.filter(|t| is_table_matched(t.name(), &sc).unwrap())
.map(|t| t.name().to_string())
.collect::<Vec<_>>(),
None => schema
.all_tables()
.context(FetchTables)?
.iter()
.map(|t| t.name().to_string())
.collect::<Vec<_>>(),
};
let schema = DataSchema::new(vec![Field::new(
SHOW_TABLES_COLUMN_SCHEMA,
DataType::Utf8,
Expand Down Expand Up @@ -146,13 +158,24 @@ impl ShowInterpreter {
}
}

fn is_table_matched(str: &str, search_re: &str) -> Result<bool> {
let regex_str = search_re.replace('_', ".");
let regex_st = if regex_str.contains('%') {
regex_str.replace('%', ".*")
} else {
format!("^{}$", &regex_str)
};
let re = Regex::new(&regex_st).unwrap();
Ok(re.is_match(str))
}

#[async_trait]
impl Interpreter for ShowInterpreter {
async fn execute(self: Box<Self>) -> InterpreterResult<Output> {
match self.plan {
ShowPlan::ShowCreatePlan(t) => Self::show_create(t).context(ShowCreateTable),
ShowPlan::ShowTables => {
Self::show_tables(self.ctx, self.catalog_manager).context(ShowTables)
ShowPlan::ShowTablesPlan(t) => {
Self::show_tables(self.ctx, self.catalog_manager, t).context(ShowTables)
}
ShowPlan::ShowDatabase => {
Self::show_databases(self.ctx, self.catalog_manager).context(ShowDatabases)
Expand Down Expand Up @@ -188,3 +211,54 @@ fn get_default_schema(
name: default_schema,
})
}

#[cfg(test)]
mod tests {
use crate::show::is_table_matched;
#[test]
QuintinTao marked this conversation as resolved.
Show resolved Hide resolved

fn test_is_table_matched() {
assert_eq!(
"true".to_string(),
is_table_matched("01_system_table1", "01%")
.unwrap()
.to_string()
);
assert_eq!(
"true".to_string(),
is_table_matched("01_system_table1", "01_%")
.unwrap()
.to_string()
);
assert_eq!(
"false".to_string(),
is_table_matched("01_system_table1", "01_system_table")
.unwrap()
.to_string()
);
assert_eq!(
"true".to_string(),
is_table_matched("01_system_table1", "01_system_table1")
.unwrap()
.to_string()
);
assert_eq!(
"true".to_string(),
is_table_matched("01_system_table1", "01_system_table.")
.unwrap()
.to_string()
);
assert_eq!(
"false".to_string(),
is_table_matched("01_system_table1", "01_system_tabl.")
.unwrap()
.to_string()
);
assert_eq!(
"true".to_string(),
is_table_matched("01_system_table1", "%system%")
.unwrap()
.to_string()
);
}
}
8 changes: 7 additions & 1 deletion sql/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum Statement {
/// SHOW CREATE TABLE
ShowCreate(ShowCreate),
ShowDatabases,
ShowTables,
ShowTables(ShowTables),
Exists(ExistsTable),
}

Expand Down Expand Up @@ -96,6 +96,12 @@ pub struct AlterAddColumn {
pub columns: Vec<ColumnDef>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ShowTables {
/// Like pattern
pub pattern: Option<String>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ShowCreate {
pub obj_type: ShowCreateObject,
Expand Down
15 changes: 13 additions & 2 deletions sql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use table_engine::ANALYTIC_ENGINE_TYPE;

use crate::ast::{
AlterAddColumn, AlterModifySetting, CreateTable, DescribeTable, DropTable, ExistsTable,
ShowCreate, ShowCreateObject, Statement,
ShowCreate, ShowCreateObject, ShowTables, Statement,
};

define_result!(ParserError);
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'a> Parser<'a> {

pub fn parse_show(&mut self) -> Result<Statement> {
if self.consume_token("TABLES") {
Ok(Statement::ShowTables)
Ok(self.parse_show_tables()?)
} else if self.consume_token("DATABASES") {
Ok(Statement::ShowDatabases)
} else if self.consume_token("CREATE") {
Expand All @@ -233,6 +233,17 @@ impl<'a> Parser<'a> {
}
}

fn parse_show_tables(&mut self) -> Result<Statement> {
let pattern = match self.parser.next_token() {
Token::Word(w) => match w.keyword {
Keyword::LIKE => Some(self.parser.parse_literal_string()?),
_ => None,
},
_ => None,
};
Ok(Statement::ShowTables(ShowTables { pattern }))
}

fn parse_show_create(&mut self) -> Result<Statement> {
let obj_type = match self.parser.expect_one_of_keywords(&[Keyword::TABLE])? {
Keyword::TABLE => Ok(ShowCreateObject::Table),
Expand Down
8 changes: 7 additions & 1 deletion sql/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,18 @@ pub struct ShowCreatePlan {
pub obj_type: ShowCreateObject,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ShowTablesPlan {
/// Like pattern
pub pattern: Option<String>,
}

#[derive(Debug)]
pub enum ShowPlan {
/// show create table
ShowCreatePlan(ShowCreatePlan),
/// show tables
ShowTables,
ShowTablesPlan(ShowTablesPlan),
/// show database
ShowDatabase,
}
Expand Down
20 changes: 13 additions & 7 deletions sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ use table_engine::table::TableRef;
use crate::{
ast::{
AlterAddColumn, AlterModifySetting, CreateTable, DescribeTable, DropTable, ExistsTable,
ShowCreate, Statement, TableName,
ShowCreate, ShowTables, Statement, TableName,
},
container::TableReference,
parser,
plan::{
AlterTableOperation, AlterTablePlan, CreateTablePlan, DescribeTablePlan, DropTablePlan,
ExistsTablePlan, InsertPlan, Plan, QueryPlan, ShowCreatePlan, ShowPlan,
ExistsTablePlan, InsertPlan, Plan, QueryPlan, ShowCreatePlan, ShowPlan, ShowTablesPlan,
},
promql::{ColumnNames, Expr as PromExpr},
provider::{ContextProviderAdapter, MetaProvider},
Expand Down Expand Up @@ -246,7 +246,7 @@ impl<'a, P: MetaProvider> Planner<'a, P> {
Statement::AlterModifySetting(s) => planner.alter_modify_setting_to_plan(s),
Statement::AlterAddColumn(s) => planner.alter_add_column_to_plan(s),
Statement::ShowCreate(s) => planner.show_create_to_plan(s),
Statement::ShowTables => planner.show_tables_to_plan(),
Statement::ShowTables(s) => planner.show_tables_to_plan(s),
Statement::ShowDatabases => planner.show_databases_to_plan(),
Statement::Exists(s) => planner.exists_table_to_plan(s),
}
Expand Down Expand Up @@ -598,8 +598,11 @@ impl<'a, P: MetaProvider> PlannerDelegate<'a, P> {
Ok(Plan::Show(ShowPlan::ShowCreatePlan(plan)))
}

fn show_tables_to_plan(&self) -> Result<Plan> {
Ok(Plan::Show(ShowPlan::ShowTables))
fn show_tables_to_plan(&self, show_tables: ShowTables) -> Result<Plan> {
let plan = ShowTablesPlan {
pattern: show_tables.pattern,
};
Ok(Plan::Show(ShowPlan::ShowTablesPlan(plan)))
}

fn show_databases_to_plan(&self) -> Result<Plan> {
Expand Down Expand Up @@ -1633,14 +1636,17 @@ mod tests {
)
.unwrap();
}

#[test]
fn test_show_tables_statement_to_plan() {
let sql = "SHOW TABLES;";
quick_test(
sql,
r#"Show(
ShowTables,
ShowTablesPlan(
ShowTablesPlan {
pattern: None,
},
),
)"#,
)
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions tests/cases/local/01_system/system_tables.result
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ timestamp,catalog,schema,table_name,engine,
Timestamp(Timestamp(0)),String(StringBytes(b"ceresdb")),String(StringBytes(b"public")),String(StringBytes(b"01_system_table1")),String(StringBytes(b"Analytic")),


SHOW TABLES `name` LIKE '01_system_table1';
SHOW TABLES LIKE '01%';

Tables,
QuintinTao marked this conversation as resolved.
Show resolved Hide resolved
String(StringBytes(b"01_system_table1")),

Failed to execute query, err: Server(ServerError { code: 400, msg: "failed to parse sql. Caused by: Invalid sql, sql: SHOW TABLES `name` LIKE '01_system_table1';, err:sql parser error: Expected end of statement, found: `name`" })

2 changes: 1 addition & 1 deletion tests/cases/local/01_system/system_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ WHERE


-- FIXME
SHOW TABLES `name` LIKE '01_system_table1';
SHOW TABLES LIKE '01%';