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 Aug 8, 2022
1 parent a79d11b commit dfc168c
Show file tree
Hide file tree
Showing 20 changed files with 3,785 additions and 4 deletions.
44 changes: 44 additions & 0 deletions pkg/sql/schemachanger/scbuild/testdata/alter_table_add_column
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,47 @@ 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}
- [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 2}, PUBLIC], ABSENT]
{columnId: 2, indexId: 2, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 2}, PUBLIC], ABSENT]
{columnId: 1, indexId: 2, kind: STORED, 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}
- [[IndexName:{DescID: 106, Name: t_pkey, IndexID: 2}, PUBLIC], ABSENT]
{indexId: 2, name: t_pkey, tableId: 106}
- [[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: 3}, PUBLIC], ABSENT]
{columnId: 2, indexId: 3, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 3}, PUBLIC], ABSENT]
{columnId: 1, indexId: 3, kind: STORED, tableId: 106}
- [[SecondaryIndex:{DescID: 106, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], ABSENT]
{indexId: 4, isUnique: true, sourceIndexId: 1, tableId: 106, temporaryIndexId: 5}
- [[TemporaryIndex:{DescID: 106, IndexID: 5, SourceIndexID: 1}, TRANSIENT_ABSENT], ABSENT]
{indexId: 5, isUnique: true, isUsingSecondaryEncoding: true, sourceIndexId: 1, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 4}, PUBLIC], ABSENT]
{columnId: 1, indexId: 4, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 1, IndexID: 5}, PUBLIC], ABSENT]
{columnId: 1, indexId: 5, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 4}, PUBLIC], ABSENT]
{columnId: 2, indexId: 4, kind: KEY_SUFFIX, tableId: 106}
- [[IndexColumn:{DescID: 106, ColumnID: 2, IndexID: 5}, PUBLIC], ABSENT]
{columnId: 2, indexId: 5, kind: KEY_SUFFIX, tableId: 106}
- [[IndexName:{DescID: 106, Name: t_i_key, IndexID: 4}, PUBLIC], ABSENT]
{indexId: 4, name: t_i_key, tableId: 106}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,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 k
----
Expand Down
318 changes: 318 additions & 0 deletions pkg/sql/schemachanger/scplan/testdata/alter_table_add_column
Original file line number Diff line number Diff line change
Expand Up @@ -1241,3 +1241,321 @@ 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 12 MutationType ops
transitions:
[[IndexColumn:{DescID: 108, ColumnID: 2, IndexID: 2}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 2}, PUBLIC], ABSENT] -> PUBLIC
[[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: 3}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 3}, PUBLIC], ABSENT] -> PUBLIC
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], ABSENT] -> BACKFILL_ONLY
[[TemporaryIndex:{DescID: 108, IndexID: 5, SourceIndexID: 1}, TRANSIENT_ABSENT], ABSENT] -> DELETE_ONLY
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 4}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 1, IndexID: 5}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 2, IndexID: 4}, PUBLIC], ABSENT] -> PUBLIC
[[IndexColumn:{DescID: 108, ColumnID: 2, IndexID: 5}, 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: 3
TableID: 108
*scop.AddColumnToIndex
ColumnID: 1
IndexID: 3
Kind: 2
TableID: 108
*scop.MakeAddedIndexBackfilling
Index:
IndexID: 4
IsUnique: true
SourceIndexID: 1
TableID: 108
TemporaryIndexID: 5
IsSecondaryIndex: true
*scop.MakeAddedTempIndexDeleteOnly
Index:
IndexID: 5
IsUnique: true
SourceIndexID: 1
TableID: 108
IsSecondaryIndex: true
*scop.AddColumnToIndex
ColumnID: 1
IndexID: 4
TableID: 108
*scop.AddColumnToIndex
ColumnID: 1
IndexID: 5
TableID: 108
*scop.AddColumnToIndex
ColumnID: 2
IndexID: 4
Kind: 1
TableID: 108
*scop.AddColumnToIndex
ColumnID: 2
IndexID: 5
Kind: 1
TableID: 108
*scop.AddColumnToIndex
ColumnID: 2
IndexID: 2
TableID: 108
*scop.AddColumnToIndex
ColumnID: 1
IndexID: 2
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 7 with 2 MutationType ops 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 7 with 4 MutationType ops
transitions:
[[TemporaryIndex:{DescID: 108, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], DELETE_ONLY] -> WRITE_ONLY
[[TemporaryIndex:{DescID: 108, IndexID: 5, SourceIndexID: 1}, TRANSIENT_ABSENT], DELETE_ONLY] -> WRITE_ONLY
ops:
*scop.MakeAddedIndexDeleteAndWriteOnly
IndexID: 3
TableID: 108
*scop.MakeAddedIndexDeleteAndWriteOnly
IndexID: 5
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 2 of 7 with 2 BackfillType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], BACKFILL_ONLY] -> BACKFILLED
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], BACKFILL_ONLY] -> BACKFILLED
ops:
*scop.BackfillIndex
IndexID: 2
SourceIndexID: 1
TableID: 108
*scop.BackfillIndex
IndexID: 4
SourceIndexID: 1
TableID: 108
PostCommitPhase stage 3 of 7 with 4 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], BACKFILLED] -> DELETE_ONLY
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], BACKFILLED] -> DELETE_ONLY
ops:
*scop.MakeBackfillingIndexDeleteOnly
IndexID: 2
TableID: 108
*scop.MakeBackfillingIndexDeleteOnly
IndexID: 4
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 4 of 7 with 4 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], DELETE_ONLY] -> MERGE_ONLY
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], DELETE_ONLY] -> MERGE_ONLY
ops:
*scop.MakeBackfilledIndexMerging
IndexID: 2
TableID: 108
*scop.MakeBackfilledIndexMerging
IndexID: 4
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 5 of 7 with 2 BackfillType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], MERGE_ONLY] -> MERGED
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], MERGE_ONLY] -> MERGED
ops:
*scop.MergeIndex
BackfilledIndexID: 2
TableID: 108
TemporaryIndexID: 3
*scop.MergeIndex
BackfilledIndexID: 4
TableID: 108
TemporaryIndexID: 5
PostCommitPhase stage 6 of 7 with 4 MutationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], MERGED] -> WRITE_ONLY
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], MERGED] -> WRITE_ONLY
ops:
*scop.MakeMergedIndexWriteOnly
IndexID: 2
TableID: 108
*scop.MakeMergedIndexWriteOnly
IndexID: 4
TableID: 108
*scop.SetJobStateOnDescriptor
DescriptorID: 108
*scop.UpdateSchemaChangerJob
JobID: 1
PostCommitPhase stage 7 of 7 with 2 ValidationType ops
transitions:
[[PrimaryIndex:{DescID: 108, IndexID: 2, ConstraintID: 1, TemporaryIndexID: 3, SourceIndexID: 1}, PUBLIC], WRITE_ONLY] -> VALIDATED
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], WRITE_ONLY] -> VALIDATED
ops:
*scop.ValidateUniqueIndex
IndexID: 2
TableID: 108
*scop.ValidateUniqueIndex
IndexID: 4
TableID: 108
PostCommitNonRevertiblePhase stage 1 of 3 with 12 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
[[IndexName:{DescID: 108, Name: t_pkey, IndexID: 2}, PUBLIC], ABSENT] -> PUBLIC
[[TemporaryIndex:{DescID: 108, IndexID: 3, SourceIndexID: 1}, TRANSIENT_ABSENT], WRITE_ONLY] -> TRANSIENT_DELETE_ONLY
[[SecondaryIndex:{DescID: 108, IndexID: 4, ConstraintID: 0, TemporaryIndexID: 5, SourceIndexID: 1}, PUBLIC], VALIDATED] -> PUBLIC
[[TemporaryIndex:{DescID: 108, IndexID: 5, SourceIndexID: 1}, TRANSIENT_ABSENT], WRITE_ONLY] -> TRANSIENT_DELETE_ONLY
[[IndexName:{DescID: 108, Name: t_i_key, IndexID: 4}, PUBLIC], ABSENT] -> PUBLIC
ops:
*scop.MakeDroppedPrimaryIndexDeleteAndWriteOnly
IndexID: 1
TableID: 108
*scop.SetIndexName
IndexID: 1
Name: crdb_internal_index_1_name_placeholder
TableID: 108
*scop.SetIndexName
IndexID: 2
Name: t_pkey
TableID: 108
*scop.MakeDroppedIndexDeleteOnly
IndexID: 3
TableID: 108
*scop.MakeDroppedIndexDeleteOnly
IndexID: 5
TableID: 108
*scop.SetIndexName
IndexID: 4
Name: t_i_key
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.MakeAddedSecondaryIndexPublic
IndexID: 4
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 8 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
[[TemporaryIndex:{DescID: 108, IndexID: 5, 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: 5
TableID: 108
*scop.MakeIndexAbsent
IndexID: 5
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 dfc168c

Please sign in to comment.