-
Notifications
You must be signed in to change notification settings - Fork 608
Support INSERT INTO ... DEFAULT VALUES ...
#1036
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
Support INSERT INTO ... DEFAULT VALUES ...
#1036
Conversation
d032c3c
to
095d108
Compare
INSERT INTO ... DEFAULT VALUES ...
INSERT INTO ... DEFAULT VALUES ...
write!(f, "{source}")?; | ||
} | ||
|
||
if source.is_none() && columns.is_empty() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if source.is_none() && columns.is_empty() { | |
if source.is_none() { |
I think we should never print INSERT INTO my_table(my_column);
, which is syntactically invalid.
INSERT INTO my_table(my_column) DEFAULT VALUES;
should successfully round-trip through sqlparser, since it's a logical error, but not a syntax error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @lovasoa.
INSERT INTO my_table(my_column) DEFAULT VALUES; should successfully round-trip through sqlparser, since it's a logical error, but not a syntax error.
I thought about this a bit and double checked the SQL grammar and implementation in Postgres and both seem to make INSERT INTO my_table(my_column) DEFAULT VALUES;
invalid at the syntax level. I think it'd make sense for sqlparser to make this scenario a syntax error as well, but I could be missing something.
I also tested a statement out with Postgres in a DB fiddle and confirmed that it raises a syntax error:
-- error: syntax error at or near "DEFAULT"
INSERT INTO test_table (test_column) DEFAULT VALUES;
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right about postgres, but it's not a syntax error in sqlite.
And insert into t(x);
is a syntax error everywhere anyway, so I think we should never emit that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah interesting, I was not aware of the behaviour in SQLite.
I added some coverage for syntax errors with DEFAULT VALUES
here. Do you think that behaviour looks reasonable?
Just noticed that there's a bug in this implementation. My additions don't play well with I'll move this back to draft while I fix that up and add some test cases for invalid syntax as well. |
Fix a bug where parsing `DEFAULT VALUES` in inserts didn't consider Hive syntax for partitions and columns. Also add tests for syntax errors when using `DEFAULT VALUES`.
Should be fixed by 4f334bd. This is ready for review again. |
Pull Request Test Coverage Report for Build 6935450177
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apache#1037 removed the extra whitespace, so we need to remove it here as well now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again
This change adds support for
DEFAULT VALUES
in insert statements.I'm specifically after Postgres support, but it looks like
DEFAULT VALUES
is part of the SQL grammar. It looks to be supported by SQLite and MSSQL at least as well (I haven't tested with these, though). I took a quick look at the MySQL and BigQuery docs and they don't seem to supportDEFAULT VALUES
(and there are probably others that I haven't checked as well).This change includes a breaking change to the AST that makes the
source
field onStatement::Insert
optional. This approach is based on the implementation in Postgres where bothcols
andselectStmt
(which looks to be the Postgres AST's equivalent tosource
) are empty. I'm not completely sure if this is appropriate for all dialects, so I'm definitely interested in feedback here 🙏