Skip to content

Conversation

tmercieca
Copy link

Tables can have partitioning applied to them, however this functionality was not available through the CREATE TABLE AS syntax. This commit updates the grammar to support PARTITION BY to the CREATE TABLE AS syntax, and the related
functionality.

Fixes: #20178

Release note (sql change): add support for PARTITION BY on CREATE TABLE AS

@tmercieca tmercieca requested a review from a team as a code owner May 10, 2023 21:12
@blathers-crl
Copy link

blathers-crl bot commented May 10, 2023

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added the O-community Originated from the community label May 10, 2023
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@tmercieca
Copy link
Author

I would like to point out that the syntax adopted in this PR is CREATE TABLE ... PARTITION BY ... AS .... The opt_partition_by_table rule is placed before opt_table_with production rule to be in line with the syntax adopted by create_table_stmt.

@tmercieca
Copy link
Author

tmercieca commented May 10, 2023

While working on this PR, I noticed a strange interaction with the testing framework which executes the create_table test cases. When marking a test case which can be parsed successfully (does not output error) with error, the test passes. I am expecting the test to fail as the statement did not error. For example, consider the following test case which can be parsed successfully. Despite labeling it with error and not producing any error, the test passes. Actually, the test passes with both error and parse tags, which seems counterintuitive.

error
CREATE TABLE IF NOT EXISTS a (b) PARTITION BY RANGE (b) (
   PARTITION p1 VALUES FROM (MINVALUE) TO (1),
   PARTITION p2 VALUES FROM (2, MAXVALUE) TO (4, 4),
   PARTITION p3 VALUES FROM (4, 4) TO (MAXVALUE))
AS
SELECT 1
----

Should I open an issue or is this intended?

@tmercieca tmercieca force-pushed the create-table-as-partition-by branch from 1db2d92 to 832e9b8 Compare May 11, 2023 20:20
@blathers-crl
Copy link

blathers-crl bot commented May 11, 2023

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@jayshrivastava jayshrivastava added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 19, 2023
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

thanks for your contribution! can you also add a test case for this in pkg/sql/logictest/testdata/logic_test/create_table?

@tmercieca tmercieca force-pushed the create-table-as-partition-by branch 2 times, most recently from a7cd99d to 55d862e Compare May 25, 2025 22:19
@tmercieca
Copy link
Author

@rafiss I have just added test cases in pkg/sql/logictest/testdata/logic_test/create_table as per your suggestion. Due to your input I discovered an additional change needed in pkg/sql/sem/tree/create.go! 👍 ... thank you :)
Apologies for the delay in actioning this change.
Could I kindly request another review?

@tmercieca tmercieca force-pushed the create-table-as-partition-by branch from 55d862e to 6fbc0e2 Compare September 24, 2025 00:32
Tables can have partitioning applied to them, however this
functionality was not available through the CREATE TABLE AS
syntax. This commit updates the grammar to support PARTITION
BY to the CREATE TABLE AS syntax, and the related
functionality.

Fixes: cockroachdb#20178

Release note (sql change): add support for PARTITION BY on
CREATE TABLE AS

Co-authored-by: Jonathan Camilleri <jon.d.cam@gmail.com>
@tmercieca tmercieca force-pushed the create-table-as-partition-by branch from 6fbc0e2 to f2d0c1b Compare September 24, 2025 01:29
@tmercieca tmercieca requested a review from rafiss September 24, 2025 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: support CREATE TABLE ... AS ... PARTITION BY

4 participants