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

CREATE TABLE AS — define a new table from the results of a query #301

Open
Tracked by #262
tconbeer opened this issue Nov 2, 2022 · 2 comments
Open
Tracked by #262
Labels
DDL-DML enhancement New feature or request

Comments

@tconbeer
Copy link
Owner

tconbeer commented Nov 2, 2022

https://www.postgresql.org/docs/current/sql-createtableas.html

@tconbeer tconbeer added the enhancement New feature or request label Nov 2, 2022
@tconbeer tconbeer added this to the v0.14.0 milestone Nov 10, 2022
@tconbeer tconbeer modified the milestones: v0.14.0, v0.15.0 Nov 30, 2022
@tconbeer tconbeer removed this from the v0.15.0 milestone Jan 18, 2023
@mlemainque
Copy link

mlemainque commented Nov 27, 2023

Hello, I'm using sqlfmt as it is the best SQL formatter I found, to apply it on the Snowflake SQL dialect, but unfortunately a lot of commands such as this CREATE TABLE AS ... seems not supported yet.

Do you think I can help you solving this? And in the meantime, would you recommend a workaround to make sqlfmt still format the rest of the query?

Thanks a lot 🙏🏻

@tconbeer
Copy link
Owner Author

tconbeer commented Dec 4, 2023

@mlemainque Thanks for your feedback and offer to contribute.

Unfortunately adding new commands like this requires changing lexing rules, which gets complex fast. I can point you to this PR, which implements (snowflake's) create ... clone statement, which is quite a bit simpler than CTAS.

I think it's likely that the implementation for CTAS will be closely related to supporting all create table statements (#300), and I don't know when I'll get around to that -- to be transparent this isn't my highest priority at the moment.

As for a workaround, for safety reasons, sqlfmt interprets create table as a FMT_OFF token, identical to the tokens we use to lex --fmt: off comments. This means you can use a --fmt: on comment to reactivate formatting. In other words this:

create table foo as --fmt: on
    select 1,
    2, 3

Will actually get formatted to this:

create table foo as --fmt: on
select 1, 2, 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DDL-DML enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants