Skip to content

Commit

Permalink
Support Keyword DATABASE synonym SCHEMA
Browse files Browse the repository at this point in the history
Link to ISSUE #4854
  • Loading branch information
TCeason committed Apr 14, 2022
1 parent 1cfee51 commit e3fef6d
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ Create a database.
## Syntax

```sql
CREATE DATABASE <database_name>
CREATE { DATABASE | SCHEMA } [IF NOT EXISTS] <database_name>
```

`CREATE SCHEMA` is a synonym for `CREATE DATABASE`.

## Examples

```sql title='msyql>'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ Drop a database.
## Syntax

```sql
DROP DATABASE [IF EXISTS] <database_name>
DROP { DATABASE | SCHEMA } [IF EXISTS] <database_name>
```

`DROP SCHEMA` is a synonym for `DROP DATABASE`.

## Examples

```sql title='mysql>'
Expand Down
24 changes: 24 additions & 0 deletions docs/doc/30-reference/30-sql/40-show/show-create-database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: SHOW CREATE DATABASE
---

Shows the CREATE DATABASE statement that creates the named database.

## Syntax

```
SHOW CREATE { DATABASE | SCHEMA } database_name
```

`SHOW CREATE SCHEMA` is a synonym for `SHOW CREATE DATABASE`.

## Examples

```sql
mysql> SHOW CREATE DATABASE default;
+----------+---------------------------+
| Database | Create Database |
+----------+---------------------------+
| default | CREATE DATABASE `default` |
+----------+---------------------------+
```
6 changes: 3 additions & 3 deletions query/src/sql/sql_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'a> DfParser<'a> {
//TODO:make stage to sql parser keyword
match w.keyword {
Keyword::TABLE => self.parse_create_table(),
Keyword::DATABASE => self.parse_create_database(),
Keyword::DATABASE | Keyword::SCHEMA => self.parse_create_database(),
Keyword::USER => self.parse_create_user(),
Keyword::ROLE => self.parse_create_role(),
Keyword::FUNCTION => self.parse_create_udf(),
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<'a> DfParser<'a> {
fn parse_drop(&mut self) -> Result<DfStatement<'a>, ParserError> {
match self.parser.next_token() {
Token::Word(w) => match w.keyword {
Keyword::DATABASE => self.parse_drop_database(),
Keyword::DATABASE | Keyword::SCHEMA => self.parse_drop_database(),
Keyword::TABLE => self.parse_drop_table(),
Keyword::USER => self.parse_drop_user(),
Keyword::ROLE => self.parse_drop_role(),
Expand Down Expand Up @@ -359,7 +359,7 @@ impl<'a> DfParser<'a> {
match self.parser.next_token() {
Token::Word(w) => match w.keyword {
Keyword::TABLE => self.parse_show_create_table(),
Keyword::DATABASE => self.parse_show_create_database(),
Keyword::DATABASE | Keyword::SCHEMA => self.parse_show_create_database(),
_ => self.expected("show create statement", Token::Word(w)),
},
unexpected => self.expected("show create statement", unexpected),
Expand Down
13 changes: 13 additions & 0 deletions query/tests/it/sql/parsers/parser_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn create_database() -> Result<()> {
});
expect_parse_ok(sql, expected)?;
}
expect_synonym_parse_eq("CREATE DATABASE db1", "CREATE SCHEMA db1")?;

{
let sql = "CREATE DATABASE db1 engine = github";
Expand All @@ -48,6 +49,10 @@ fn create_database() -> Result<()> {
});
expect_parse_ok(sql, expected)?;
}
expect_synonym_parse_eq(
"CREATE DATABASE db1 engine = github",
"CREATE SCHEMA db1 engine = github",
)?;

{
let sql = "CREATE DATABASE IF NOT EXISTS db1";
Expand All @@ -60,6 +65,10 @@ fn create_database() -> Result<()> {
});
expect_parse_ok(sql, expected)?;
}
expect_synonym_parse_eq(
"CREATE DATABASE IF NOT EXISTS db1",
"CREATE SCHEMA IF NOT EXISTS db1",
)?;

Ok(())
}
Expand All @@ -74,6 +83,8 @@ fn drop_database() -> Result<()> {
});
expect_parse_ok(sql, expected)?;
}
expect_synonym_parse_eq("DROP DATABASE db1", "DROP SCHEMA db1")?;

{
let sql = "DROP DATABASE IF EXISTS db1";
let expected = DfStatement::DropDatabase(DfDropDatabase {
Expand All @@ -82,6 +93,7 @@ fn drop_database() -> Result<()> {
});
expect_parse_ok(sql, expected)?;
}
expect_synonym_parse_eq("DROP DATABASE IF EXISTS db1", "DROP SCHEMA IF EXISTS db1")?;

Ok(())
}
Expand All @@ -94,6 +106,7 @@ fn show_create_database_test() -> Result<()> {
name: ObjectName(vec![Ident::new("test")]),
}),
)?;
expect_synonym_parse_eq("SHOW CREATE DATABASE test", "SHOW CREATE SCHEMA test")?;

Ok(())
}
11 changes: 11 additions & 0 deletions query/tests/it/sql/parsers/parser_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,43 @@ fn show_databases_test() -> Result<()> {
"SHOW DATABASES",
DfStatement::ShowDatabases(DfShowDatabases::create(DfShowKind::All)),
)?;
expect_synonym_parse_eq("SHOW DATABASES", "SHOW SCHEMAS")?;

expect_parse_ok(
"SHOW DATABASES;",
DfStatement::ShowDatabases(DfShowDatabases::create(DfShowKind::All)),
)?;
expect_synonym_parse_eq("SHOW DATABASES;", "SHOW SCHEMAS;")?;

expect_parse_ok(
"SHOW DATABASES WHERE Database = 'ss'",
DfStatement::ShowDatabases(DfShowDatabases::create(DfShowKind::Where(
parse_sql_to_expr("Database = 'ss'"),
))),
)?;
expect_synonym_parse_eq(
"SHOW DATABASES WHERE Database = 'ss'",
"SHOW SCHEMAS WHERE Database = 'ss'",
)?;

expect_parse_ok(
"SHOW DATABASES WHERE Database Like 'ss%'",
DfStatement::ShowDatabases(DfShowDatabases::create(DfShowKind::Where(
parse_sql_to_expr("Database Like 'ss%'"),
))),
)?;
expect_synonym_parse_eq(
"SHOW DATABASES WHERE Database Like 'ss%'",
"SHOW SCHEMAS WHERE Database Like 'ss%'",
)?;

expect_parse_ok(
"SHOW DATABASES LIKE 'ss%'",
DfStatement::ShowDatabases(DfShowDatabases::create(DfShowKind::Like(
Ident::with_quote('\'', "ss%"),
))),
)?;
expect_synonym_parse_eq("SHOW DATABASES LIKE 'ss%'", "SHOW SCHEMAS LIKE 'ss%'")?;

Ok(())
}
Expand Down
17 changes: 17 additions & 0 deletions query/tests/it/sql/sql_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ pub fn expect_parse_ok(sql: &str, expected: DfStatement) -> Result<()> {
Ok(())
}

pub fn expect_synonym_parse_eq(sql: &str, sql2: &str) -> Result<()> {
let (statements, _) = DfParser::parse_sql(sql)?;
assert_eq!(
statements.len(),
1,
"Expected to parse exactly one statement"
);
let (statements2, _) = DfParser::parse_sql(sql2)?;
assert_eq!(
statements2.len(),
1,
"Expected to parse exactly one statement"
);
assert_eq!(statements[0], statements2[0]);
Ok(())
}

pub fn expect_parse_err(sql: &str, expected: String) -> Result<()> {
let result = DfParser::parse_sql(sql);
assert!(result.is_err(), "'{}' SHOULD BE '{}'", sql, expected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
1
1
16 changes: 16 additions & 0 deletions tests/suites/0_stateless/05_ddl/05_0001_ddl_create_database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ CREATE DATABASE system; -- {ErrorCode 2301}
DROP DATABASE system; -- {ErrorCode 1002}

CREATE DATABASE db.t; -- {ErrorCode 1005}

DROP SCHEMA IF EXISTS db;

CREATE SCHEMA db;
CREATE TABLE db.t(c1 int) ENGINE = Null;
SELECT COUNT(1) from system.tables where name = 't' and database = 'db';

CREATE SCHEMA IF NOT EXISTS db;
CREATE SCHEMA db; -- {ErrorCode 2301}

DROP SCHEMA IF EXISTS db;

CREATE SCHEMA system; -- {ErrorCode 2301}
DROP SCHEMA system; -- {ErrorCode 1002}

CREATE SCHEMA db.t; -- {ErrorCode 1005}
9 changes: 9 additions & 0 deletions tests/suites/0_stateless/05_ddl/05_0001_ddl_drop_database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ DROP DATABASE db;
DROP DATABASE IF EXISTS db;

DROP DATABASE db; -- {ErrorCode 1003}

DROP SCHEMA IF EXISTS db;

CREATE SCHEMA db;
DROP SCHEMA db;

DROP SCHEMA IF EXISTS db;

DROP SCHEMA db; -- {ErrorCode 1003}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ss
ss1
ss2
ss
ss1
ss2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE DATABASE ss1;
CREATE DATABASE ss2;

SHOW DATABASES like 'ss%';
SHOW SCHEMAS like 'ss%';

DROP DATABASE IF EXISTS ss;
DROP DATABASE IF EXISTS ss1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
system CREATE DATABASE `system` ENGINE=SYSTEM
test CREATE DATABASE `test`
datafuselabs CREATE DATABASE `datafuselabs` ENGINE=GITHUB(token='xxx')
system CREATE DATABASE `system` ENGINE=SYSTEM
test CREATE DATABASE `test`
datafuselabs CREATE DATABASE `datafuselabs` ENGINE=GITHUB(token='xxx')
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ DROP DATABASE `test`;
CREATE DATABASE `datafuselabs` ENGINE=github(token='xxx'); -- {ErrorCode 1073}
SHOW CREATE DATABASE `datafuselabs`;
DROP DATABASE `datafuselabs`;
SHOW CREATE SCHEMA `system`;
DROP SCHEMA IF EXISTS `test`;
CREATE SCHEMA `test`;
SHOW CREATE SCHEMA `test`;
DROP SCHEMA `test`;
CREATE SCHEMA `datafuselabs` ENGINE=github(token='xxx'); -- {ErrorCode 1073}
SHOW CREATE SCHEMA `datafuselabs`;
DROP SCHEMA `datafuselabs`;

0 comments on commit e3fef6d

Please sign in to comment.