Skip to content

Commit

Permalink
fix: canonicalize name of create database
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Mar 14, 2024
1 parent d79384d commit 877d2c3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
58 changes: 56 additions & 2 deletions src/sql/src/parsers/create_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'a> ParserContext<'a> {
expected: "a database name",
actual: self.peek_token_as_string(),
})?;

let database_name = Self::canonicalize_object_name(database_name);
Ok(Statement::CreateDatabase(CreateDatabase {
name: database_name,
if_not_exists,
Expand Down Expand Up @@ -722,7 +722,7 @@ mod tests {
use common_catalog::consts::FILE_ENGINE;
use common_error::ext::ErrorExt;
use sqlparser::ast::ColumnOption::NotNull;
use sqlparser::ast::{BinaryOperator, Value};
use sqlparser::ast::{BinaryOperator, ObjectName, Value};

use super::*;
use crate::dialect::GreptimeDbDialect;
Expand Down Expand Up @@ -916,6 +916,60 @@ mod tests {
}
_ => unreachable!(),
}

let sql = "CREATE DATABASE my_catalog.foo";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::CreateDatabase(CreateDatabase::new(
ObjectName(vec![Ident::new("my_catalog"), Ident::new("foo")]),
false
))
);

let sql = "CREATE DATABASE MY_CATALOG.FOO";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::CreateDatabase(CreateDatabase::new(
ObjectName(vec![Ident::new("my_catalog"), Ident::new("foo")]),
false
))
);

let sql = "CREATE DATABASE `My_cAtAlOg`.`fOo`";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::CreateDatabase(CreateDatabase::new(
ObjectName(vec![
Ident::with_quote('`', "My_cAtAlOg"),
Ident::with_quote('`', "fOo"),
]),
false
))
);

let sql = "CREATE DATABASE `My_cAtAlOg`.foo";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let mut stmts = result.unwrap();
assert_eq!(
stmts.pop().unwrap(),
Statement::CreateDatabase(CreateDatabase::new(
ObjectName(vec![
Ident::with_quote('`', "My_cAtAlOg"),
Ident::new("foo"),
]),
false
))
);
}

#[test]
Expand Down
10 changes: 10 additions & 0 deletions src/sql/src/statements/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ pub struct CreateDatabase {
pub if_not_exists: bool,
}

impl CreateDatabase {
/// Creates a statement for `CREATE DATABASE`
pub fn new(name: ObjectName, if_not_exists: bool) -> Self {
Self {
name,
if_not_exists,
}
}
}

#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut)]
pub struct CreateExternalTable {
/// Table name
Expand Down

0 comments on commit 877d2c3

Please sign in to comment.