Skip to content

CREATE and CTAS SORTED BY and PARTITIONED BY#1103

Draft
Alex-Monahan wants to merge 2 commits into
duckdb:mainfrom
Alex-Monahan:ctas-sorted-partitioned
Draft

CREATE and CTAS SORTED BY and PARTITIONED BY#1103
Alex-Monahan wants to merge 2 commits into
duckdb:mainfrom
Alex-Monahan:ctas-sorted-partitioned

Conversation

@Alex-Monahan
Copy link
Copy Markdown
Contributor

@Alex-Monahan Alex-Monahan commented May 1, 2026

This PR adds support for the syntax from duckdb/duckdb#20431 and duckdb/duckdb#20728 :

CREATE TABLE woot 
  PARTITIONED BY (i) 
  SORTED BY (j)
  AS 
  FROM RANGE(9001) t1(i) 
  SELECT i, i+1 as j 
  ;

plus the non CTAS CREATE TABLE equivalent.

One open issue is that the syntax added to DuckDB in the above PRs does not support adding sort modifiers (ASC / DESC) to the sort, so I'll open up a bug for that here shortly if there isn't one already (added a FIXME in the code once that is unblocked).

The key value of this PR is to make dbt work smoother with DuckLake. By default, everything in dbt is a CTAS. So this allows partitioning and sorting in a much smoother way in dbt (without a workaround).

This was built with Claude, but I did make a number of adjustments to its initial solution to try and refactor it (and I trimmed down the ridiculously long comments...). Let me know if there is anything you would like to see changed!

CC @nicku33

@Alex-Monahan
Copy link
Copy Markdown
Contributor Author

This is the related DuckDB PR to unblock ASC/DESC on the SORTED BY clause for CREATE and CTAS: duckdb/duckdb#22411

@Alex-Monahan
Copy link
Copy Markdown
Contributor Author

Alex-Monahan commented May 1, 2026

Happy to target 1.5 if it would be helpful! [Edit - I made another PR for that in case it is helpful - see below!]

@Alex-Monahan
Copy link
Copy Markdown
Contributor Author

Marking this as draft - I think 1.5 is the better branch to target for now? (See #1107 )

@Alex-Monahan Alex-Monahan marked this pull request as draft May 5, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants