Skip to content

create if not exists for all dialect #163

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

Merged
merged 3 commits into from
Apr 21, 2020

Conversation

alex-dukhno
Copy link
Contributor

This PR attempts to implement CREATE TABLE IF NOT EXISTS table_name for all dialects.
However, I am not sure that it is a standard SQL statement - please correct me if I am wrong.

Copy link
Contributor

@nickolay nickolay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not in the standard, but at least PostgreSQL has it.

Thanks for the PR! Please see the comments inline. And please run cargo fmt and rebase on master, otherwise the CI fails.

src/parser.rs Outdated
@@ -932,6 +933,37 @@ impl Parser {
}

pub fn parse_create_table(&mut self) -> Result<Statement, ParserError> {
let if_keyword = self.parse_keyword("IF");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simply use let if_not_exists = self.parse_keywords(vec!["IF", "NOT", "EXISTS"]);, why not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I use let if_not_exists = self.parse_keywords(vec!["IF", "NOT", "EXISTS"]); I wouldn't be able to say if user typed in just CREATE TABLE or missed one of keyword because self.parse_keywords returns bool

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I fully understand what you're saying.. When parse_keywords returns false the current token is left unchanged. So CREATE TABLE if (...) would be accepted (which seems more appropriate for the generic parser at least; I don't know if Postgres rejects that), and missing some of the keywords like CREATE TABLE IF NOT ... would fail with an error like "Expected end of statement, found: NOT" instead of more specific error message, which seems acceptable too.

Listing all possible errors for multiple dialects doesn't seem maintainable, and we already use parse_keywords in other places.

@alex-dukhno alex-dukhno force-pushed the create-table-if-not-exists branch from eb59356 to 112234d Compare April 20, 2020 08:37
@alex-dukhno
Copy link
Contributor Author

@nickolay thank you for review, I've rebased over master and moved tests to only PostgreSQL dialect module, but I have general question how do I make sure that CREATE TABLE IF NOT EXISTS applies only to Postgres dialect?

Copy link
Contributor

@nickolay nickolay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do I make sure that CREATE TABLE IF NOT EXISTS applies only to Postgres dialect?

You don't; the dialects only affect tokenizing at this point.. Just running the test for pg_and_generic() dialects is fine.

src/parser.rs Outdated
@@ -932,6 +933,37 @@ impl Parser {
}

pub fn parse_create_table(&mut self) -> Result<Statement, ParserError> {
let if_keyword = self.parse_keyword("IF");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I fully understand what you're saying.. When parse_keywords returns false the current token is left unchanged. So CREATE TABLE if (...) would be accepted (which seems more appropriate for the generic parser at least; I don't know if Postgres rejects that), and missing some of the keywords like CREATE TABLE IF NOT ... would fail with an error like "Expected end of statement, found: NOT" instead of more specific error message, which seems acceptable too.

Listing all possible errors for multiple dialects doesn't seem maintainable, and we already use parse_keywords in other places.

@nickolay
Copy link
Contributor

nickolay commented Apr 20, 2020

And please run cargo fmt

Note that CI still thinks you didn't run rustfmt before comitting: https://travis-ci.org/github/andygrove/sqlparser-rs/builds/677142460#L641 - have you?

@nickolay nickolay merged commit 5ad578e into apache:master Apr 21, 2020
@nickolay
Copy link
Contributor

Thank you!

@alex-dukhno alex-dukhno deleted the create-table-if-not-exists branch April 21, 2020 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants