From f3ceb9eadc2c66f18f664af559b7f4beb41ef6d3 Mon Sep 17 00:00:00 2001 From: BohuTANG Date: Wed, 20 Apr 2022 19:36:59 +0800 Subject: [PATCH 1/2] chore(mysqldump): support mysqldump dump schema with:mysqldump --set-gtid-purged=OFF --no-tablespaces --no-data --triggers=0 --column-statistics=0 -uroot -h127.0.0.1 -P3307 dd1 --- .../interpreter_table_describe.rs | 3 ++ query/src/servers/mysql/mysql_federated.rs | 7 ++- query/src/sql/parsers/parser_show.rs | 13 +++++ query/src/sql/sql_parser.rs | 2 + .../statements/statement_describe_table.rs | 1 + .../interpreter_table_describe.rs | 40 ++++++++++---- query/tests/it/sql/parsers/parser_show.rs | 12 +++++ .../05_ddl/05_0000_ddl_create_tables.result | 52 +++++++++---------- .../06_show/06_0010_show_fields.result | 1 + .../06_show/06_0010_show_fields.sql | 10 ++++ .../20+_others/20_0000_describe_table.result | 20 +++---- 11 files changed, 115 insertions(+), 46 deletions(-) create mode 100644 tests/suites/0_stateless/06_show/06_0010_show_fields.result create mode 100644 tests/suites/0_stateless/06_show/06_0010_show_fields.sql diff --git a/query/src/interpreters/interpreter_table_describe.rs b/query/src/interpreters/interpreter_table_describe.rs index be96b4fc6394..6d1a4f6664f9 100644 --- a/query/src/interpreters/interpreter_table_describe.rs +++ b/query/src/interpreters/interpreter_table_describe.rs @@ -56,6 +56,7 @@ impl Interpreter for DescribeTableInterpreter { let mut types: Vec = vec![]; let mut nulls: Vec = vec![]; let mut default_exprs: Vec = vec![]; + let mut extras: Vec = vec![]; for field in schema.fields().iter() { names.push(field.name().to_string()); @@ -78,6 +79,7 @@ impl Interpreter for DescribeTableInterpreter { default_exprs.push(format!("{}", value)); } } + extras.push("".to_string()); } let desc_schema = self.plan.schema(); @@ -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![ diff --git a/query/src/servers/mysql/mysql_federated.rs b/query/src/servers/mysql/mysql_federated.rs index 2743fe349602..131e15caa88c 100644 --- a/query/src/servers/mysql/mysql_federated.rs +++ b/query/src/servers/mysql/mysql_federated.rs @@ -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), diff --git a/query/src/sql/parsers/parser_show.rs b/query/src/sql/parsers/parser_show.rs index c6685fafa83d..ba360509e867 100644 --- a/query/src/sql/parsers/parser_show.rs +++ b/query/src/sql/parsers/parser_show.rs @@ -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; @@ -124,4 +125,16 @@ impl<'a> DfParser<'a> { _ => self.expected("like or where", tok), } } + + // parse `show fields from` statement + // Convert it to the `desc ` + pub(crate) fn parse_show_fields(&mut self) -> Result, 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)) + } } diff --git a/query/src/sql/sql_parser.rs b/query/src/sql/sql_parser.rs index ee2aac16aa19..99853ae3fe09 100644 --- a/query/src/sql/sql_parser.rs +++ b/query/src/sql/sql_parser.rs @@ -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") { diff --git a/query/src/sql/statements/statement_describe_table.rs b/query/src/sql/statements/statement_describe_table.rs index 7fc088e91523..7b2252c9598e 100644 --- a/query/src/sql/statements/statement_describe_table.rs +++ b/query/src/sql/statements/statement_describe_table.rs @@ -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()), ]) } } diff --git a/query/tests/it/interpreters/interpreter_table_describe.rs b/query/tests/it/interpreters/interpreter_table_describe.rs index da4188f12474..dd9ec2bb81e3 100644 --- a/query/tests/it/interpreters/interpreter_table_describe.rs +++ b/query/tests/it/interpreters/interpreter_table_describe.rs @@ -45,15 +45,37 @@ async fn interpreter_describe_table_test() -> Result<()> { let stream = executor.execute(None).await?; let result = stream.try_collect::>().await?; let expected = vec![ - "+-------+----------+------+---------+", - "| Field | Type | Null | Default |", - "+-------+----------+------+---------+", - "| a | BIGINT | NO | 0 |", - "| b | INT | NO | 0 |", - "| c | VARCHAR | NO | |", - "| d | SMALLINT | NO | 0 |", - "| e | DATE16 | NO | 0 |", - "+-------+----------+------+---------+", + "+-------+----------+------+---------+-------+", + "| Field | Type | Null | Default | Extra |", + "+-------+----------+------+---------+-------+", + "| a | BIGINT | NO | 0 | |", + "| b | INT | NO | 0 | |", + "| c | VARCHAR | NO | | |", + "| d | SMALLINT | NO | 0 | |", + "| e | DATE16 | 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::>().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 | DATE16 | NO | 0 | |", + "+-------+----------+------+---------+-------+", ]; common_datablocks::assert_blocks_sorted_eq(expected, result.as_slice()); } diff --git a/query/tests/it/sql/parsers/parser_show.rs b/query/tests/it/sql/parsers/parser_show.rs index 743d30ba0505..2f002aa65d64 100644 --- a/query/tests/it/sql/parsers/parser_show.rs +++ b/query/tests/it/sql/parsers/parser_show.rs @@ -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; @@ -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(()) +} diff --git a/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result b/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result index 547cc5cc21c9..e48a51c62250 100644 --- a/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result +++ b/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result @@ -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 @@ -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 DATE16 NO 0 -datetime DATETIME32 NO 0 -ts DATETIME64(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 DATE16 NO 0 +datetime DATETIME32 NO 0 +ts DATETIME64(3) NO 0 +str VARCHAR NO 3 +bool BOOLEAN NO false +array ARRAY NO [] +obj OBJECT NO {} +variant VARIANT NO null diff --git a/tests/suites/0_stateless/06_show/06_0010_show_fields.result b/tests/suites/0_stateless/06_show/06_0010_show_fields.result new file mode 100644 index 000000000000..5dec0fce666d --- /dev/null +++ b/tests/suites/0_stateless/06_show/06_0010_show_fields.result @@ -0,0 +1 @@ +a INT NO 0 diff --git a/tests/suites/0_stateless/06_show/06_0010_show_fields.sql b/tests/suites/0_stateless/06_show/06_0010_show_fields.sql new file mode 100644 index 000000000000..47e306ce94ac --- /dev/null +++ b/tests/suites/0_stateless/06_show/06_0010_show_fields.sql @@ -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; diff --git a/tests/suites/0_stateless/20+_others/20_0000_describe_table.result b/tests/suites/0_stateless/20+_others/20_0000_describe_table.result index 10b43fa0575a..3d046a08a1bf 100644 --- a/tests/suites/0_stateless/20+_others/20_0000_describe_table.result +++ b/tests/suites/0_stateless/20+_others/20_0000_describe_table.result @@ -1,10 +1,10 @@ -a BIGINT YES NULL -b INT YES NULL -c VARCHAR YES NULL -d SMALLINT NO 0 -e DATE16 NO 0 -a BIGINT YES NULL -b INT YES NULL -c VARCHAR YES NULL -d SMALLINT NO 0 -e DATE16 NO 0 +a BIGINT YES NULL +b INT YES NULL +c VARCHAR YES NULL +d SMALLINT NO 0 +e DATE16 NO 0 +a BIGINT YES NULL +b INT YES NULL +c VARCHAR YES NULL +d SMALLINT NO 0 +e DATE16 NO 0 From fe8c5b388da3454c20aaf94f473ccdd815b12fad Mon Sep 17 00:00:00 2001 From: BohuTANG Date: Wed, 20 Apr 2022 20:23:31 +0800 Subject: [PATCH 2/2] chore(stateless): fix stateless test of desc after merge main branch --- .../05_ddl/05_0000_ddl_create_tables.result | 27 +++---------------- .../20+_others/20_0000_describe_table.result | 17 ++---------- 2 files changed, 5 insertions(+), 39 deletions(-) diff --git a/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result b/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result index ee23ca38f68c..a6389729fca5 100644 --- a/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result +++ b/tests/suites/0_stateless/05_ddl/05_0000_ddl_create_tables.result @@ -25,7 +25,6 @@ NULL ====END TEST CREATE TABLE AS SELECT STATEMENT==== ====TIMESTAMP==== ====CREATE ALL DATA TYPE TABLE==== -<<<<<<< HEAD tiny TINYINT NO 0 tiny_unsigned TINYINT UNSIGNED NO 0 smallint SMALLINT NO 0 @@ -36,31 +35,11 @@ bigint BIGINT NO 0 bigint_unsigned BIGINT UNSIGNED NO 0 float FLOAT NO 0 double DOUBLE NO 0 -date DATE16 NO 0 -datetime DATETIME32 NO 0 -ts DATETIME64(3) 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 ->>>>>>> main diff --git a/tests/suites/0_stateless/20+_others/20_0000_describe_table.result b/tests/suites/0_stateless/20+_others/20_0000_describe_table.result index 93fee09be565..f3c944c6cd51 100644 --- a/tests/suites/0_stateless/20+_others/20_0000_describe_table.result +++ b/tests/suites/0_stateless/20+_others/20_0000_describe_table.result @@ -1,23 +1,10 @@ -<<<<<<< HEAD a BIGINT YES NULL b INT YES NULL c VARCHAR YES NULL d SMALLINT NO 0 -e DATE16 NO 0 +e DATE NO 0 a BIGINT YES NULL b INT YES NULL c VARCHAR YES NULL d SMALLINT NO 0 -e DATE16 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 ->>>>>>> main +e DATE NO 0