Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cubestore): add CREATE DATABASE and SHOW DATABASES aliases for CREATE SCHEMA and SHOW SCHEMAS #1451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion rust/cubestore/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl SqlService for SqlServiceImpl {
match ast {
CubeStoreStatement::Statement(Statement::ShowVariable { variable }) => {
match variable.value.to_lowercase() {
s if s == "schemas" => Ok(DataFrame::from(self.db.get_schemas().await?)),
s if ["schemas", "databases"].contains(&s.as_str()) => Ok(DataFrame::from(self.db.get_schemas().await?)),
s if s == "tables" => Ok(DataFrame::from(self.db.get_tables().await?)),
s if s == "chunks" => Ok(DataFrame::from(self.db.chunks_table().all_rows().await?)),
s if s == "indexes" => Ok(DataFrame::from(self.db.index_table().all_rows().await?)),
Expand Down Expand Up @@ -487,6 +487,25 @@ mod tests {
('LastName 1', 23, 'FirstName 1', 'Address 1', 'City 1'), ('LastName 2', 22, 'FirstName 2', 'Address 2', 'City 2');").await.unwrap();
}).await;
}
#[tokio::test]
async fn create_database_test() {
Config::run_test("select", async move |services| {
let service = services.sql_service;
let dbs = ["foo", "bar"];

for db in dbs.iter() {
let _ = service.exec_query(&format!("CREATE DATABASE {}", &db)).await.unwrap();
}

let result = service.exec_query("SHOW DATABASES").await.unwrap();
let mut index = 0;
for db in dbs.iter() {
assert_eq!(result.get_rows()[index], Row::new(vec![TableValue::Int((index + 1) as i64),
TableValue::String(db.to_string())]));
index += 1;
}
}).await;
}

#[tokio::test]
async fn select_test() {
Expand Down
15 changes: 13 additions & 2 deletions rust/cubestore/src/sql/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ impl CubeStoreParser {
})
}

/// Parse an aribtrary string token
pub fn parse_token_str(&mut self, token_str: &str) -> bool {
match self.parser.peek_token() {
Token::Word(w) if token_str.to_lowercase() == w.value.to_lowercase() => {
self.parser.next_token();
true
}
_ => false,
}
}

pub fn parse_statement(&mut self) -> Result<Statement, ParserError> {
match self.parser.peek_token() {
Token::Word(w) => match w.keyword {
Expand All @@ -59,7 +70,7 @@ impl CubeStoreParser {
}

pub fn parse_create(&mut self) -> Result<Statement, ParserError> {
if self.parser.parse_keyword(Keyword::SCHEMA) {
if self.parser.parse_keyword(Keyword::SCHEMA) || self.parse_token_str("database") {
self.parse_create_schema()
} else if self.parser.parse_keyword(Keyword::TABLE) {
self.parse_create_table()
Expand Down Expand Up @@ -114,4 +125,4 @@ impl CubeStoreParser {
if_not_exists
})
}
}
}