-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
pkg: Allow user defined primary key in CREATE TABLE ... AS #38589
Conversation
c0d2b21
to
f390742
Compare
f390742
to
83e361d
Compare
RFAL now that the core BulkRowWriter is merged in. |
} | ||
|
||
ct := &createTableNode{n: n, dbDesc: dbDesc, sourcePlan: sourcePlan} | ||
ct.run.synthRowID = synthRowID | ||
ct.run.fromHeuristicPlanner = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you comment this / where used below (explaining the why in addition to the what)?
pkg/sql/parser/sql.y
Outdated
@@ -802,7 +802,7 @@ func newNameFromStr(s string) *tree.Name { | |||
%type <tree.IsolationLevel> iso_level | |||
%type <tree.UserPriority> user_priority | |||
|
|||
%type <tree.TableDefs> opt_table_elem_list table_elem_list | |||
%type <tree.TableDefs> opt_table_elem_list table_elem_list ctas_opt_col_list ctas_table_elem_list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here/thoughout: for someone else coming into this code or trying to read the diagrams, I think "ctas" could be more descriptive. maybe "create_as" ?
{ | ||
name := $7.unresolvedObjectName().ToTableName() | ||
$$.val = &tree.CreateTable{ | ||
Table: name, | ||
IfNotExists: true, | ||
Interleave: nil, | ||
Defs: nil, | ||
Defs: $8.tblDefs(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you expand the commit message (or better yet, a comment on the AST node fields) about what's going in Defs
in an AS stmt? I'm having a bit of trouble grokking this part.
pkg/sql/create_table.go
Outdated
parentID, id, | ||
creationTime, | ||
privileges, | ||
affected, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is any point in plumbing this as an arg just to pass nil. might as well pass nil here, right?
affected
is only for resolving FK defs which we don't expect (or support) here.
pkg/sql/parser/sql.y
Outdated
} | ||
|
||
ctas_table_elem: | ||
ctas_column_def |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another catch from @rolandcrosby: in additional to column def (indeed, more important than being able to put a qualifier on one of the names in fact) we also want the ability to say PRIMARY KEY(a, b)
as to define a new multi-col PK (that's where this really gets interesting in repartitioning). I think we could allow it as an alternative _elem case if we wanted, and then handle it or a qualifier. Alternatively, that could be the only syntax for specifying the PK (instead of the qualifier) which eliminated conflicts. I don't have a preference so I'll let you and @rolandcrosby figure that out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for me, supporting multi-col PK at all is a higher priority than supporting multiple syntaxes to define PKs, but otherwise I don't have a strong opinion on whether it's worth supporting this via both inline qualifiers and the PRIMARY KEY (a, b)
syntax.
83e361d
to
9c3757c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @dt and @lucy-zhang)
pkg/sql/create_table.go, line 98 at r2 (raw file):
Previously, dt (David Taylor) wrote…
can you comment this / where used below (explaining the why in addition to the what)?
Done.
pkg/sql/create_table.go, line 1030 at r2 (raw file):
Previously, dt (David Taylor) wrote…
I don't think there is any point in plumbing this as an arg just to pass nil. might as well pass nil here, right?
affected
is only for resolving FK defs which we don't expect (or support) here.
Done.
pkg/sql/parser/sql.y, line 805 at r2 (raw file):
Previously, dt (David Taylor) wrote…
here/thoughout: for someone else coming into this code or trying to read the diagrams, I think "ctas" could be more descriptive. maybe "create_as" ?
Done.
pkg/sql/parser/sql.y, line 4040 at r2 (raw file):
Previously, dt (David Taylor) wrote…
can you expand the commit message (or better yet, a comment on the AST node fields) about what's going in
Defs
in an AS stmt? I'm having a bit of trouble grokking this part.
Done.
8e26c79
to
48442c7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (waiting on @dt and @lucy-zhang)
pkg/sql/parser/sql.y, line 4446 at r2 (raw file):
Previously, rolandcrosby (Roland Crosby) wrote…
for me, supporting multi-col PK at all is a higher priority than supporting multiple syntaxes to define PKs, but otherwise I don't have a strong opinion on whether it's worth supporting this via both inline qualifiers and the
PRIMARY KEY (a, b)
syntax.
Done.
This change adds grammar rules to support users specifying the primary key columns in a CREATE TABLE...AS query. It allows for both column level qualification, as well as constraint style declaration of PKs. eg: CREATE TABLE a (id PRIMARY KEY) AS SELECT * FROM b CREATE TABLE a (id, PRIMARY KEY(id)) AS SELECT * FROM b CREATE TABLE a (id, idtwo, PRIMARY KEY(id, idtwo)) AS SELECT * FROM b Due to CREATE TABLE and CREATE TABLE ... AS having an identical syntax for declaring PKs, the logic resolving qualifiers and/or constraints had to be pushed to after a column_name without a type is encountered. This avoids reduce/reduce conflicts, but introduces a restriction that a PRIMARY KEY constraint must be preceded by at least one column name. Release note (sql change): Allows user defined PK in CTAS statements.
This change removes the need for a separate `AsColumnNames` field in the CreateTable node, and uses `ColumnTableDefs` instead. This is similar to a normal CREATE TABLE query, thereby allowing us to piggyback on most of the existing logic. It ensures `row_id` key generation only occurs if no PK is explicitly specified. Some special handling is required in pretty printing, as column types are not populated at the time of parsing. Release note: None
48442c7
to
f8333e7
Compare
@dt ready for a final look. I addressed your comments, added the modified grammar, and a few more logic tests. |
TFTR! |
38589: pkg: Allow user defined primary key in CREATE TABLE ... AS r=adityamaru27 a=adityamaru27 This change allows users to specify which column in the newly created table should serve as the primary key (#20940). If no such constraint is specified, we generate a unique row_id to serve as the primary key. Co-authored-by: Aditya Maru <[email protected]>
Build succeeded |
Parameter updates Diagram updates Examples now use MovR Code PRs: cockroachdb/cockroach#38589 cockroachdb/cockroach#38904 Fixes #5190
Parameter updates Diagram updates Examples now use MovR Code PRs: cockroachdb/cockroach#38589 cockroachdb/cockroach#38904 Fixes #5190
Parameter updates Diagram updates Examples now use MovR Code PRs: cockroachdb/cockroach#38589 cockroachdb/cockroach#38904 Fixes #5190
Parameter updates Diagram updates Examples now use MovR New primary key example for partitioning Code PRs: cockroachdb/cockroach#38589 cockroachdb/cockroach#38904 Fixes #5190
Parameter updates Diagram updates Examples now use MovR New primary key example for partitioning Code PRs: cockroachdb/cockroach#38589 cockroachdb/cockroach#38904 Fixes #5190
This change allows users to specify which column in the newly created table should serve as the primary key (#20940). If no such constraint is specified, we generate a unique row_id to serve as the primary key.