Skip to content

Commit

Permalink
sql/schemachanger: added tests for the ALTER PRIMARY KEY vanilla case
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
Xiang-Gu committed Jul 15, 2022
1 parent 1a14770 commit ea59185
Show file tree
Hide file tree
Showing 6 changed files with 1,225 additions and 4 deletions.
30 changes: 30 additions & 0 deletions pkg/sql/schemachanger/scbuild/testdata/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,33 @@ ALTER TABLE defaultdb.bar ADD COLUMN b INT;
{columnId: 3, isNullable: true, tableId: 105, type: {family: IntFamily, oid: 20, width: 64}}
- [[IndexColumn:{DescID: 105, ColumnID: 3, IndexID: 1}, PUBLIC], ABSENT]
{columnId: 3, indexId: 1, kind: STORED, ordinalInKind: 1, tableId: 105}

setup
CREATE TABLE t (i INT PRIMARY KEY, j INT NOT NULL)
----

build
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (j)
----
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 1}, ABSENT], PUBLIC]
{columnId: 1, indexId: 1, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 1}, ABSENT], PUBLIC]
{columnId: 2, indexId: 1, kind: STORED, tableId: 106}
- [[PrimaryIndex:{DescID: 106, IndexID: 1, ConstraintID: 1}, ABSENT], PUBLIC]
{constraintId: 1, indexId: 1, isUnique: true, tableId: 106}
- [[IndexName:{DescID: 106, Name: t_pkey, IndexID: 1}, ABSENT], PUBLIC]
{indexId: 1, name: t_pkey, tableId: 106}
- [[PrimaryIndex:{DescID: 106, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], ABSENT]
{constraintId: 1, indexId: 2, isUnique: true, sourceIndexId: 1, tableId: 106, temporaryIndexId: 3}
- [[TemporaryIndex:{DescID: 106, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], ABSENT]
{constraintId: 1, indexId: 3, isUnique: true, sourceIndexId: 1, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 2}, PUBLIC], ABSENT]
{columnId: 2, indexId: 2, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 3}, PUBLIC], ABSENT]
{columnId: 2, indexId: 3, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 2}, PUBLIC], ABSENT]
{columnId: 1, indexId: 2, kind: STORED, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 3}, PUBLIC], ABSENT]
{columnId: 1, indexId: 3, kind: STORED, tableId: 106}
- [[IndexName:{DescID: 106, Name: t_pkey, IndexID: 2}, PUBLIC], ABSENT]
{indexId: 2, name: t_pkey, tableId: 106}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ unimplemented
ALTER TABLE defaultdb.foo ALTER COLUMN i SET DATA TYPE STRING
----

unimplemented
ALTER TABLE defaultdb.foo ALTER PRIMARY KEY USING COLUMNS (i)
----

unimplemented
ALTER TABLE defaultdb.foo DROP COLUMN i
----
Expand Down
218 changes: 218 additions & 0 deletions pkg/sql/schemachanger/scplan/testdata/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,221 @@ PostCommitPhase stage 2 of 2 with 7 MutationType ops
*scop.UpdateSchemaChangerJob
IsNonCancelable: true
JobID: 1

setup
CREATE TABLE t (i INT PRIMARY KEY, j INT NOT NULL)
----

ops
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (j)
----
StatementPhase stage 1 of 1 with 6 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], ABSENT] -> BACKFILL_ONLY
[[TemporaryIndex:{DescID: 108, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], ABSENT] -> DELETE_ONLY
[[IndexColumn:{DescID: 108, ColumnID: 2, IndexID: 2}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 2, IndexID: 3}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 2}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 3}, PUBLIC], ABSENT] -> PUBLIC
ops:
*scop.MakeAddedIndexBackfilling
Index:
ConstraintID: 1
IndexID: 2
IsUnique: true
SourceIndexID: 1
TableID: 108
TemporaryIndexID: 3
*scop.MakeAddedTempIndexDeleteOnly
Index:
ConstraintID: 1
IndexID: 3
IsUnique: true
SourceIndexID: 1
TableID: 108
*scop.AddColumnToIndex
ColumnID: 2
IndexID: 2
TableID: 108
*scop.AddColumnToIndex
ColumnID: 2
IndexID: 3
TableID: 108
*scop.AddColumnToIndex
ColumnID: 1
IndexID: 2
Kind: 2
TableID: 108
*scop.AddColumnToIndex
ColumnID: 1
IndexID: 3
Kind: 2
TableID: 108
PreCommitPhase stage 1 of 1 with 2 MutationType ops
transitions:
ops:
*scop.SetJobStateOnDescriptor
DescriptorID: 108
Initialize: true
*scop.CreateSchemaChangerJob
Authorization:
UserName: root
DescriptorIDs:
- 108
JobID: 1
RunningStatus: PostCommitPhase stage 1 of 6 with 1 MutationType op pending
Statements:
- statement: ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (j)
redactedstatement: ALTER TABLE ‹defaultdb›.public.‹t› ALTER PRIMARY KEY USING COLUMNS
(‹j›)
statementtag: ALTER TABLE
PostCommitPhase stage 1 of 6 with 3 MutationType ops
transitions:
[[TemporaryIndex:{DescID: 108, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], DELETE_ONLY] -> WRITE_ONLY
ops:
*scop.MakeAddedIndexDeleteAndWriteOnly
IndexID: 3
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 2 of 6 with 1 BackfillType op
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], BACKFILL_ONLY] -> BACKFILLED
ops:
*scop.BackfillIndex
IndexID: 2
SourceIndexID: 1
TableID: 108
PostCommitPhase stage 3 of 6 with 3 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], BACKFILLED] -> DELETE_ONLY
ops:
*scop.MakeBackfillingIndexDeleteOnly
IndexID: 2
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 4 of 6 with 3 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], DELETE_ONLY] -> MERGE_ONLY
ops:
*scop.MakeAddedIndexDeleteAndWriteOnly
IndexID: 2
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 5 of 6 with 1 BackfillType op
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], MERGE_ONLY] -> MERGED
ops:
*scop.MergeIndex
BackfilledIndexID: 2
TableID: 108
TemporaryIndexID: 3
PostCommitPhase stage 6 of 6 with 1 ValidationType op
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], MERGED] -> VALIDATED
ops:
*scop.ValidateUniqueIndex
IndexID: 2
TableID: 108
PostCommitNonRevertiblePhase stage 1 of 3 with 9 MutationType ops
transitions:
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 1}, ABSENT], PUBLIC] -> ABSENT
[[IndexColumn:{DescID: 108, ColumnID: 2, IndexID: 1}, ABSENT], PUBLIC] -> ABSENT
[[PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}, ABSENT], PUBLIC] -> WRITE_ONLY
[[IndexName:{DescID: 108, Name: t_pkey, IndexID: 1}, ABSENT], PUBLIC] -> ABSENT
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], VALIDATED] -> PUBLIC
[[TemporaryIndex:{DescID: 108, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], WRITE_ONLY] -> TRANSIENT_DELETE_ONLY
[[IndexName:{DescID: 108, Name: t_pkey, IndexID: 2}, PUBLIC], ABSENT] -> PUBLIC
ops:
*scop.MakeDroppedPrimaryIndexDeleteAndWriteOnly
IndexID: 1
TableID: 108
*scop.SetIndexName
IndexID: 1
Name: crdb_internal_index_1_name_placeholder
TableID: 108
*scop.MakeDroppedIndexDeleteOnly
IndexID: 3
TableID: 108
*scop.SetIndexName
IndexID: 2
Name: t_pkey
TableID: 108
*scop.RemoveColumnFromIndex
ColumnID: 1
IndexID: 1
TableID: 108
*scop.RemoveColumnFromIndex
ColumnID: 2
IndexID: 1
Kind: 2
TableID: 108
*scop.MakeAddedPrimaryIndexPublic
EventBase:
Authorization:
UserName: root
Statement: ALTER TABLE ‹defaultdb›.public.‹t› ALTER PRIMARY KEY USING COLUMNS (‹j›)
StatementTag: ALTER TABLE
TargetMetadata:
SourceElementID: 1
SubWorkID: 1
IndexID: 2
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
IsNonCancelable: true
JobID: 1
PostCommitNonRevertiblePhase stage 2 of 3 with 3 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}, ABSENT], WRITE_ONLY] -> DELETE_ONLY
ops:
*scop.MakeDroppedIndexDeleteOnly
IndexID: 1
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
IsNonCancelable: true
JobID: 1
PostCommitNonRevertiblePhase stage 3 of 3 with 6 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 1, ConstraintID: 1}, ABSENT], DELETE_ONLY] -> ABSENT
[[TemporaryIndex:{DescID: 108, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], TRANSIENT_DELETE_ONLY] -> TRANSIENT_ABSENT
ops:
*scop.CreateGcJobForIndex
IndexID: 3
TableID: 108
*scop.MakeIndexAbsent
IndexID: 3
TableID: 108
*scop.CreateGcJobForIndex
IndexID: 1
StatementForDropJob:
Statement: ALTER TABLE defaultdb.public.t ALTER PRIMARY KEY USING COLUMNS (j)
TableID: 108
*scop.MakeIndexAbsent
EventBase:
Authorization:
UserName: root
Statement: ALTER TABLE ‹defaultdb›.public.‹t› ALTER PRIMARY KEY USING COLUMNS (‹j›)
StatementTag: ALTER TABLE
TargetMetadata:
SourceElementID: 1
SubWorkID: 1
IndexID: 1
TableID: 108
*scop.RemoveJobStateFromDescriptor
DescriptorID: 108
JobID: 1
*scop.UpdateSchemaChangerJob
IsNonCancelable: true
JobID: 1
Loading

0 comments on commit ea59185

Please sign in to comment.