Skip to content

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

Merged
merged 4 commits into from
Nov 21, 2023

Conversation

CDThomas
Copy link
Contributor

@CDThomas CDThomas commented Nov 3, 2023

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 support DEFAULT 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 on Statement::Insert optional. This approach is based on the implementation in Postgres where both cols and selectStmt (which looks to be the Postgres AST's equivalent to source) are empty. I'm not completely sure if this is appropriate for all dialects, so I'm definitely interested in feedback here 🙏

@CDThomas CDThomas force-pushed the postgres-insert-default-values branch from d032c3c to 095d108 Compare November 3, 2023 05:18
@CDThomas CDThomas changed the title Support PostgreSQL INSERT INTO ... DEFAULT VALUES ... Support INSERT INTO ... DEFAULT VALUES ... Nov 3, 2023
@CDThomas CDThomas marked this pull request as ready for review November 3, 2023 05:57
write!(f, "{source}")?;
}

if source.is_none() && columns.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

@CDThomas CDThomas Nov 10, 2023

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?

@CDThomas
Copy link
Contributor Author

Just noticed that there's a bug in this implementation.

My additions don't play well with after_columns for Hive here. Looks like INSERT INTO test_table DEFAULT VALUES (some_column) doesn't get rejected.

I'll move this back to draft while I fix that up and add some test cases for invalid syntax as well.

@CDThomas CDThomas marked this pull request as draft November 10, 2023 06:44
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`.
@CDThomas
Copy link
Contributor Author

Just noticed that there's a bug in this implementation.

My additions don't play well with after_columns for Hive here. Looks like INSERT INTO test_table DEFAULT VALUES (some_column) doesn't get rejected.

I'll move this back to draft while I fix that up and add some test cases for invalid syntax as well.

Should be fixed by 4f334bd. This is ready for review again.

@CDThomas CDThomas marked this pull request as ready for review November 10, 2023 23:42
@coveralls
Copy link

coveralls commented Nov 20, 2023

Pull Request Test Coverage Report for Build 6935450177

  • 89 of 90 (98.89%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 87.729%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser/mod.rs 14 15 93.33%
Totals Coverage Status
Change from base Build 6935126371: 0.04%
Covered Lines: 17937
Relevant Lines: 20446

💛 - Coveralls

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

This looks good to me -- thank you @lovasoa for your review. @CDThomas can you possibly merge up from the main branch to get the CI passing on this PR and I'll merge it in?

@CDThomas
Copy link
Contributor Author

Thanks again @alamb and @lovasoa. I've merged in main and CI is green.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks again

@alamb alamb merged commit 3d2773a into apache:main Nov 21, 2023
panarch added a commit to gluesql/gluesql that referenced this pull request Apr 27, 2024
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.

4 participants