Skip to content

Commit

Permalink
Merge pull request #4972 from BohuTANG/dev-mysqldump
Browse files Browse the repository at this point in the history
chore(mysqldump): support mysqldump dump schema
  • Loading branch information
BohuTANG authored Apr 20, 2022
2 parents 2ca3014 + fe8c5b3 commit b7f4704
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 46 deletions.
3 changes: 3 additions & 0 deletions query/src/interpreters/interpreter_table_describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Interpreter for DescribeTableInterpreter {
let mut types: Vec<String> = vec![];
let mut nulls: Vec<String> = vec![];
let mut default_exprs: Vec<String> = vec![];
let mut extras: Vec<String> = vec![];

for field in schema.fields().iter() {
names.push(field.name().to_string());
Expand All @@ -78,6 +79,7 @@ impl Interpreter for DescribeTableInterpreter {
default_exprs.push(format!("{}", value));
}
}
extras.push("".to_string());
}

let desc_schema = self.plan.schema();
Expand All @@ -87,6 +89,7 @@ impl Interpreter for DescribeTableInterpreter {
Series::from_data(types),
Series::from_data(nulls),
Series::from_data(default_exprs),
Series::from_data(extras),
]);

Ok(Box::pin(DataBlockStream::create(desc_schema, None, vec![
Expand Down
7 changes: 6 additions & 1 deletion query/src/servers/mysql/mysql_federated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,12 @@ impl MySQLFederated {
("(?i)^(SET AUTOCOMMIT(.*))", None),
("(?i)^(SET sql_mode(.*))", None),
("(?i)^(SET @@(.*))", None),
("(?i)^(SET SESSION TRANSACTION ISOLATION LEVEL(.*))", None),
// mysqldump.
("(?i)^(SET SESSION(.*))", None),
("(?i)^(SET SQL_QUOTE_SHOW_CREATE(.*))", None),
("(?i)^(LOCK TABLES(.*))", None),
("(?i)^(UNLOCK TABLES(.*))", None),
("(?i)^(SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES(.*))", None),
// DBeaver.
("(?i)^(SHOW WARNINGS)", None),
("(?i)^(/\\* ApplicationName=(.*)SHOW WARNINGS)", None),
Expand Down
13 changes: 13 additions & 0 deletions query/src/sql/parsers/parser_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use sqlparser::keywords::Keyword;
use sqlparser::parser::ParserError;
use sqlparser::tokenizer::Token;

use crate::sql::statements::DfDescribeTable;
use crate::sql::statements::DfShowDatabases;
use crate::sql::statements::DfShowFunctions;
use crate::sql::statements::DfShowKind;
Expand Down Expand Up @@ -124,4 +125,16 @@ impl<'a> DfParser<'a> {
_ => self.expected("like or where", tok),
}
}

// parse `show fields from` statement
// Convert it to the `desc <table>`
pub(crate) fn parse_show_fields(&mut self) -> Result<DfStatement<'a>, ParserError> {
if !self.consume_token("FROM") {
self.expect_token("from")?;
}

let table_name = self.parser.parse_object_name()?;
let desc = DfDescribeTable { name: table_name };
Ok(DfStatement::DescribeTable(desc))
}
}
2 changes: 2 additions & 0 deletions query/src/sql/sql_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ impl<'a> DfParser<'a> {
Ok(DfStatement::ShowSettings(DfShowSettings))
} else if self.consume_token("CREATE") {
self.parse_show_create()
} else if self.consume_token("FIELDS") {
self.parse_show_fields()
} else if self.consume_token("PROCESSLIST") {
Ok(DfStatement::ShowProcessList(DfShowProcessList))
} else if self.consume_token("METRICS") {
Expand Down
1 change: 1 addition & 0 deletions query/src/sql/statements/statement_describe_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl DfDescribeTable {
DataField::new("Type", Vu8::to_data_type()),
DataField::new("Null", Vu8::to_data_type()),
DataField::new("Default", Vu8::to_data_type()),
DataField::new("Extra", Vu8::to_data_type()),
])
}
}
40 changes: 31 additions & 9 deletions query/tests/it/interpreters/interpreter_table_describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,37 @@ async fn interpreter_describe_table_test() -> Result<()> {
let stream = executor.execute(None).await?;
let result = stream.try_collect::<Vec<_>>().await?;
let expected = vec![
"+-------+----------+------+---------+",
"| Field | Type | Null | Default |",
"+-------+----------+------+---------+",
"| a | BIGINT | NO | 0 |",
"| b | INT | NO | 0 |",
"| c | VARCHAR | NO | |",
"| d | SMALLINT | NO | 0 |",
"| e | DATE | NO | 0 |",
"+-------+----------+------+---------+",
"+-------+----------+------+---------+-------+",
"| Field | Type | Null | Default | Extra |",
"+-------+----------+------+---------+-------+",
"| a | BIGINT | NO | 0 | |",
"| b | INT | NO | 0 | |",
"| c | VARCHAR | NO | | |",
"| d | SMALLINT | NO | 0 | |",
"| e | DATE | NO | 0 | |",
"+-------+----------+------+---------+-------+",
];
common_datablocks::assert_blocks_sorted_eq(expected, result.as_slice());
}

// `show fields from ` is same as `describe` table.
{
let plan = PlanParser::parse(ctx.clone(), "show fields from a").await?;
let executor = InterpreterFactory::get(ctx.clone(), plan.clone())?;
assert_eq!(executor.name(), "DescribeTableInterpreter");

let stream = executor.execute(None).await?;
let result = stream.try_collect::<Vec<_>>().await?;
let expected = vec![
"+-------+----------+------+---------+-------+",
"| Field | Type | Null | Default | Extra |",
"+-------+----------+------+---------+-------+",
"| a | BIGINT | NO | 0 | |",
"| b | INT | NO | 0 | |",
"| c | VARCHAR | NO | | |",
"| d | SMALLINT | NO | 0 | |",
"| e | DATE | NO | 0 | |",
"+-------+----------+------+---------+-------+",
];
common_datablocks::assert_blocks_sorted_eq(expected, result.as_slice());
}
Expand Down
12 changes: 12 additions & 0 deletions query/tests/it/sql/parsers/parser_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use common_exception::Result;
use databend_query::sql::statements::DfDescribeTable;
use databend_query::sql::statements::DfShowDatabases;
use databend_query::sql::statements::DfShowEngines;
use databend_query::sql::statements::DfShowFunctions;
Expand Down Expand Up @@ -337,3 +338,14 @@ fn show_tab_stat_test() -> Result<()> {

Ok(())
}

#[test]
fn show_fields_from() -> Result<()> {
expect_parse_ok(
"show fields from t2",
DfStatement::DescribeTable(DfDescribeTable {
name: ObjectName(vec![Ident::new("t2")]),
}),
)?;
Ok(())
}
52 changes: 26 additions & 26 deletions tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
4
====BEGIN TEST CREATE TABLE LIKE STATEMENT====
8
a INT NO 0
b INT YES NULL
a INT NO 0
b INT YES NULL
====END TEST CREATE TABLE LIKE STATEMENT====
====BEGIN TEST CREATE TABLE AS SELECT STATEMENT====
a VARCHAR YES NULL
y VARCHAR YES NULL
b INT YES NULL
a VARCHAR YES NULL
y VARCHAR YES NULL
b INT YES NULL
1
2
3
a VARCHAR YES NULL
y VARCHAR YES NULL
b INT YES NULL
a VARCHAR YES NULL
y VARCHAR YES NULL
b INT YES NULL
1
2
3
Expand All @@ -25,21 +25,21 @@ NULL
====END TEST CREATE TABLE AS SELECT STATEMENT====
====TIMESTAMP====
====CREATE ALL DATA TYPE TABLE====
tiny TINYINT NO 0
tiny_unsigned TINYINT UNSIGNED NO 0
smallint SMALLINT NO 0
smallint_unsigned SMALLINT UNSIGNED NO 0
int INT NO 0
int_unsigned INT UNSIGNED NO 0
bigint BIGINT NO 0
bigint_unsigned BIGINT UNSIGNED NO 0
float FLOAT NO 0
double DOUBLE NO 0
date DATE NO 0
datetime DATETIME_0 NO 0
ts DATETIME_3 NO 0
str VARCHAR NO 3
bool BOOLEAN NO false
array ARRAY NO []
obj OBJECT NO {}
variant VARIANT NO null
tiny TINYINT NO 0
tiny_unsigned TINYINT UNSIGNED NO 0
smallint SMALLINT NO 0
smallint_unsigned SMALLINT UNSIGNED NO 0
int INT NO 0
int_unsigned INT UNSIGNED NO 0
bigint BIGINT NO 0
bigint_unsigned BIGINT UNSIGNED NO 0
float FLOAT NO 0
double DOUBLE NO 0
date DATE NO 0
datetime DATETIME_0 NO 0
ts DATETIME_3 NO 0
str VARCHAR NO 3
bool BOOLEAN NO false
array ARRAY NO []
obj OBJECT NO {}
variant VARIANT NO null
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a INT NO 0
10 changes: 10 additions & 0 deletions tests/suites/0_stateless/06_show/06_0010_show_fields.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP DATABASE IF EXISTS ss;

CREATE DATABASE ss;

USE ss;

CREATE TABLE t1(a INT);
SHOW FIELDS FROM t1;

DROP DATABASE IF EXISTS ss;
20 changes: 10 additions & 10 deletions tests/suites/0_stateless/20+_others/20_0000_describe_table.result
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
a BIGINT YES NULL
b INT YES NULL
c VARCHAR YES NULL
d SMALLINT NO 0
e DATE NO 0
a BIGINT YES NULL
b INT YES NULL
c VARCHAR YES NULL
d SMALLINT NO 0
e DATE NO 0
a BIGINT YES NULL
b INT YES NULL
c VARCHAR YES NULL
d SMALLINT NO 0
e DATE NO 0
a BIGINT YES NULL
b INT YES NULL
c VARCHAR YES NULL
d SMALLINT NO 0
e DATE NO 0

0 comments on commit b7f4704

Please sign in to comment.