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: improve error message for typo in IF NOT EXISTS #3817

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Changes from 2 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
46 changes: 34 additions & 12 deletions src/sql/src/parsers/create_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use table::requests::validate_table_option;
use crate::ast::{ColumnDef, Ident, TableConstraint};
use crate::error::{
self, InvalidColumnOptionSnafu, InvalidTableOptionSnafu, InvalidTimeIndexSnafu,
MissingTimeIndexSnafu, Result, SyntaxSnafu,
MissingTimeIndexSnafu, Result, SyntaxSnafu, UnexpectedSnafu, UnsupportedSnafu,
};
use crate::parser::ParserContext;
use crate::statements::create::{
Expand Down Expand Up @@ -63,9 +63,7 @@ impl<'a> ParserContext<'a> {
self.parser
.expect_keyword(Keyword::TABLE)
.context(SyntaxSnafu)?;
let if_not_exists =
self.parser
.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let if_not_exists = self.parse_if_not_exist()?;
let table_name = self.intern_parse_table_name()?;
let (columns, constraints) = self.parse_columns()?;
if !columns.is_empty() {
Expand All @@ -86,11 +84,7 @@ impl<'a> ParserContext<'a> {

fn parse_create_database(&mut self) -> Result<Statement> {
let _ = self.parser.next_token();

let if_not_exists =
self.parser
.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);

let if_not_exists = self.parse_if_not_exist()?;
let database_name = self.parse_object_name().context(error::UnexpectedSnafu {
sql: self.sql,
expected: "a database name",
Expand All @@ -105,9 +99,8 @@ impl<'a> ParserContext<'a> {

fn parse_create_table(&mut self) -> Result<Statement> {
let _ = self.parser.next_token();
let if_not_exists =
self.parser
.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);

let if_not_exists = self.parse_if_not_exist()?;

let table_name = self.intern_parse_table_name()?;

Expand Down Expand Up @@ -144,6 +137,35 @@ impl<'a> ParserContext<'a> {
Ok(Statement::CreateTable(create_table))
}

fn parse_if_not_exist(&mut self) -> Result<bool> {
match self.parser.peek_token().token {
Token::Word(w) if Keyword::IF != w.keyword => return Ok(false),
_ => {}
}

if self.parser.parse_keywords(&[Keyword::IF, Keyword::NOT]) {
return self
.parser
.expect_keyword(Keyword::EXISTS)
.map(|_| true)
.context(UnexpectedSnafu {
sql: self.sql,
expected: "EXISTS",
actual: self.peek_token_as_string(),
});
}

if self.parser.parse_keywords(&[Keyword::IF, Keyword::EXISTS]) {
return UnsupportedSnafu {
sql: self.sql,
tisonkun marked this conversation as resolved.
Show resolved Hide resolved
keyword: "EXISTS",
}
.fail();
}

Ok(false)
}

fn parse_create_table_options(&mut self) -> Result<OptionMap> {
let options = self
.parser
Expand Down
Loading