Skip to content

Add CREATE TABLE AS support #206

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 12 commits into from
Jun 23, 2020
Merged

Add CREATE TABLE AS support #206

merged 12 commits into from
Jun 23, 2020

Conversation

Dandandan
Copy link
Contributor

@Dandandan Dandandan commented Jun 20, 2020

@coveralls
Copy link

coveralls commented Jun 20, 2020

Pull Request Test Coverage Report for Build 144885262

  • 49 of 51 (96.08%) changed or added relevant lines in 5 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.03%) to 91.837%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/mod.rs 13 14 92.86%
tests/sqlparser_postgres.rs 14 15 93.33%
Files with Coverage Reduction New Missed Lines %
src/ast/mod.rs 1 81.17%
Totals Coverage Status
Change from base Build 137652853: 0.03%
Covered Lines: 4174
Relevant Lines: 4545

💛 - Coveralls

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.

Thanks! I've added my thoughts inline.. Of those I'd like query.is_none() to be fixed before merging, for the rest I'm curious what you think.

@nickolay
Copy link
Contributor

I just realized that CREATE TABLE t (a INT) AS SELECT a from t2 is BigQuery-specific, while in both PostgreSQL and ANSI SQL a plain list of columns (without datatypes) in expected in the parentheses after the table name in a CTAS statement.

I also wanted to have a test for that and for CREATE TABLE t (CONSTRAINT positive CHECK (2 > 1)) you mentioned above, so I added a commit with those.

I think this is ready to be merged, what do you think?

@Dandandan
Copy link
Contributor Author

I think so too! Thanks a lot :)

@nickolay nickolay merged commit 15d5f71 into apache:master Jun 23, 2020
@nickolay
Copy link
Contributor

Excellent, thanks! I summarized the changes here in the commit message as follows:

We parse it as a regular CREATE TABLE statement followed by an AS <query>, which is how BigQuery works: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement

ANSI SQL and PostgreSQL only support a plain list of columns
after the table name in a CTAS CREATE TABLE t (a) AS SELECT a FROM foo
We currently only allow specifying a full schema with data types, or omitting it altogether.
https://www.postgresql.org/docs/12/sql-createtableas.html https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#as-subquery-clause

Finally, when no schema is specified, we print empty parens after a plain CREATE TABLE t (); as required by PostgreSQL, but skip them in a CTAS: CREATE TABLE t AS .... This affects serialization only, the parser allows omitting the schema in a regular CREATE TABLE too since the first release of the parser: https://github.com/benesch/sqlparser-rs/blame/7d27abdfb4ac4a0980d79b12d9d8ef156434e984/src/sqlparser.rs#L325-L332

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.

3 participants