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

pkg: Allow user defined primary key in CREATE TABLE ... AS #38589

Merged
merged 2 commits into from
Jul 16, 2019

Conversation

adityamaru27
Copy link
Contributor

@adityamaru27 adityamaru27 commented Jul 1, 2019

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.

@adityamaru27 adityamaru27 requested review from dt, thoszhang and a team July 1, 2019 18:46
@adityamaru27 adityamaru27 requested a review from a team as a code owner July 1, 2019 18:46
@adityamaru27 adityamaru27 requested review from a team July 1, 2019 18:46
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@adityamaru27 adityamaru27 force-pushed the ctas-user-setting-pk branch 4 times, most recently from c0d2b21 to f390742 Compare July 3, 2019 03:05
@adityamaru27 adityamaru27 force-pushed the ctas-user-setting-pk branch from f390742 to 83e361d Compare July 9, 2019 13:43
@adityamaru27
Copy link
Contributor Author

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
Copy link
Member

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)?

@@ -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
Copy link
Member

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(),
Copy link
Member

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.

parentID, id,
creationTime,
privileges,
affected,
Copy link
Member

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.

}

ctas_table_elem:
ctas_column_def
Copy link
Member

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

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.

@adityamaru27 adityamaru27 force-pushed the ctas-user-setting-pk branch from 83e361d to 9c3757c Compare July 15, 2019 15:59
Copy link
Contributor Author

@adityamaru27 adityamaru27 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: 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.

@adityamaru27 adityamaru27 force-pushed the ctas-user-setting-pk branch 2 times, most recently from 8e26c79 to 48442c7 Compare July 16, 2019 02:49
Copy link
Contributor Author

@adityamaru27 adityamaru27 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: 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
@adityamaru27 adityamaru27 force-pushed the ctas-user-setting-pk branch from 48442c7 to f8333e7 Compare July 16, 2019 02:57
@adityamaru27
Copy link
Contributor Author

@dt ready for a final look. I addressed your comments, added the modified grammar, and a few more logic tests.

@adityamaru27
Copy link
Contributor Author

TFTR!
bors r+

craig bot pushed a commit that referenced this pull request Jul 16, 2019
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]>
@craig
Copy link
Contributor

craig bot commented Jul 16, 2019

Build succeeded

@craig craig bot merged commit f8333e7 into cockroachdb:master Jul 16, 2019
ericharmeling added a commit to cockroachdb/docs that referenced this pull request Aug 21, 2019
Parameter updates
Diagram updates
Examples now use MovR

Code PRs:
cockroachdb/cockroach#38589
cockroachdb/cockroach#38904

Fixes #5190
ericharmeling added a commit to cockroachdb/docs that referenced this pull request Aug 21, 2019
Parameter updates
Diagram updates
Examples now use MovR

Code PRs:
cockroachdb/cockroach#38589
cockroachdb/cockroach#38904

Fixes #5190
ericharmeling added a commit to cockroachdb/docs that referenced this pull request Aug 26, 2019
Parameter updates
Diagram updates
Examples now use MovR

Code PRs:
cockroachdb/cockroach#38589
cockroachdb/cockroach#38904

Fixes #5190
ericharmeling added a commit to cockroachdb/docs that referenced this pull request Aug 26, 2019
Parameter updates
Diagram updates
Examples now use MovR
New primary key example for partitioning

Code PRs:
cockroachdb/cockroach#38589
cockroachdb/cockroach#38904

Fixes #5190
ericharmeling added a commit to cockroachdb/docs that referenced this pull request Aug 28, 2019
Parameter updates
Diagram updates
Examples now use MovR
New primary key example for partitioning

Code PRs:
cockroachdb/cockroach#38589
cockroachdb/cockroach#38904

Fixes #5190
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.

5 participants