From b14599f163a3f096d0db8df27508d1b4c0d475d4 Mon Sep 17 00:00:00 2001 From: WenyXu Date: Thu, 14 Mar 2024 12:33:07 +0000 Subject: [PATCH] fix: canonicalize name of create database --- src/sql/src/parsers/create_parser.rs | 16 ++++++++++++++-- src/sql/src/statements/create.rs | 10 ++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/sql/src/parsers/create_parser.rs b/src/sql/src/parsers/create_parser.rs index b84c965aa5f7..ec61a20d5378 100644 --- a/src/sql/src/parsers/create_parser.rs +++ b/src/sql/src/parsers/create_parser.rs @@ -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, @@ -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; @@ -916,6 +916,18 @@ mod tests { } _ => unreachable!(), } + + let sql = "CREATE DATABASE `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('`', "fOo"),]), + false + )) + ); } #[test] diff --git a/src/sql/src/statements/create.rs b/src/sql/src/statements/create.rs index e665ef257750..cfcbd8d68242 100644 --- a/src/sql/src/statements/create.rs +++ b/src/sql/src/statements/create.rs @@ -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