Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
79998: sql: fix show create table after altering col type of col with comment r=chengxiong-ruan a=chengxiong-ruan

Previously `SHOW CREATE TABLE` use `sub_id` from `system.comments`
as a column id to find a column. However we actually use `PGAttributeNum`
when creating a column comment. So things will break if a column's id
has changed such as when type is altered.

Release note (sql change): Previously, if a column in a table has a comment,
`SHOW CREATE TABLE` would fail after the column type is changed.
Now this is fixed.

80159: Revert "sqlsmith: skip crdb_internal.reset_multi_region_zone_configs_for_database" r=rytaft a=rytaft

This reverts commit c1a959a.
That commit was unnecessary because
`crdb_internal.reset_multi_region_zone_configs_for_database` already
existed in the list of functions to skip.

Release note: None

Co-authored-by: Chengxiong Ruan <[email protected]>
Co-authored-by: Rebecca Taft <[email protected]>
  • Loading branch information
3 people committed Apr 19, 2022
3 parents 4837e3a + 1fde0df + 624882e commit 996f52c
Show file tree
Hide file tree
Showing 36 changed files with 207 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/generated/redact_safe.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pkg/sql/sem/catid/ids.go | `ConstraintID`
pkg/sql/sem/catid/ids.go | `DescID`
pkg/sql/sem/catid/ids.go | `FamilyID`
pkg/sql/sem/catid/ids.go | `IndexID`
pkg/sql/sem/catid/ids.go | `PGAttributeNum`
pkg/sql/sqlliveness/sqlliveness.go | `SessionID`
pkg/storage/enginepb/mvcc.go | `TxnEpoch`
pkg/storage/enginepb/mvcc.go | `TxnSeq`
Expand Down
7 changes: 7 additions & 0 deletions pkg/ccl/schemachangerccl/testdata/decomp/multiregion
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ ElementState:
Status: PUBLIC
- Column:
columnId: 1
pgAttributeNum: 1
tableId: 110
Status: PUBLIC
- Column:
columnId: 2
isHidden: true
pgAttributeNum: 2
tableId: 110
Status: PUBLIC
- PrimaryIndex:
Expand Down Expand Up @@ -189,11 +191,13 @@ ElementState:
Status: PUBLIC
- Column:
columnId: 1
pgAttributeNum: 1
tableId: 109
Status: PUBLIC
- Column:
columnId: 2
isHidden: true
pgAttributeNum: 2
tableId: 109
Status: PUBLIC
- PrimaryIndex:
Expand Down Expand Up @@ -311,15 +315,18 @@ ElementState:
Status: PUBLIC
- Column:
columnId: 1
pgAttributeNum: 1
tableId: 108
Status: PUBLIC
- Column:
columnId: 2
pgAttributeNum: 2
tableId: 108
Status: PUBLIC
- Column:
columnId: 3
isHidden: true
pgAttributeNum: 3
tableId: 108
Status: PUBLIC
- PrimaryIndex:
Expand Down
5 changes: 5 additions & 0 deletions pkg/ccl/schemachangerccl/testdata/decomp/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ ElementState:
Status: PUBLIC
- Column:
columnId: 1
pgAttributeNum: 1
tableId: 104
Status: PUBLIC
- Column:
columnId: 2
pgAttributeNum: 2
tableId: 104
Status: PUBLIC
- Column:
columnId: 3
pgAttributeNum: 3
tableId: 104
Status: PUBLIC
- PrimaryIndex:
Expand Down Expand Up @@ -217,10 +220,12 @@ ElementState:
Status: PUBLIC
- Column:
columnId: 1
pgAttributeNum: 1
tableId: 105
Status: PUBLIC
- Column:
columnId: 2
pgAttributeNum: 2
tableId: 105
Status: PUBLIC
- PrimaryIndex:
Expand Down
6 changes: 6 additions & 0 deletions pkg/ccl/schemachangerccl/testdata/end_to_end/drop_multiregion
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ upsert descriptor #108
+ - elementProto:
+ column:
+ columnId: 1
+ pgAttributeNum: 1
+ tableId: 108
+ metadata:
+ sourceElementId: 1
Expand Down Expand Up @@ -238,6 +239,7 @@ upsert descriptor #108
+ column:
+ columnId: 2
+ isHidden: true
+ pgAttributeNum: 2
+ tableId: 108
+ metadata:
+ sourceElementId: 1
Expand Down Expand Up @@ -537,6 +539,7 @@ upsert descriptor #108
- - elementProto:
- column:
- columnId: 1
- pgAttributeNum: 1
- tableId: 108
- metadata:
- sourceElementId: 1
Expand Down Expand Up @@ -578,6 +581,7 @@ upsert descriptor #108
- column:
- columnId: 2
- isHidden: true
- pgAttributeNum: 2
- tableId: 108
- metadata:
- sourceElementId: 1
Expand Down Expand Up @@ -873,6 +877,7 @@ upsert descriptor #109
+ - elementProto:
+ column:
+ columnId: 1
+ pgAttributeNum: 1
+ tableId: 109
+ metadata:
+ sourceElementId: 1
Expand Down Expand Up @@ -1106,6 +1111,7 @@ upsert descriptor #109
- - elementProto:
- column:
- columnId: 1
- pgAttributeNum: 1
- tableId: 109
- metadata:
- sourceElementId: 1
Expand Down
1 change: 0 additions & 1 deletion pkg/internal/sqlsmith/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ var functions = func() map[tree.FunctionClass]map[oid.Oid][]function {
"crdb_internal.start_replication_stream",
"crdb_internal.replication_stream_progress",
"crdb_internal.complete_replication_stream",
"crdb_internal.reset_multi_region_zone_configs_for_database",
} {
skip = skip || strings.Contains(def.Name, substr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/colinfo/result_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ResultColumnsFromColDescs(
Typ: typ,
Hidden: colDesc.Hidden,
TableID: tableID,
PGAttributeNum: colDesc.GetPGAttributeNum(),
PGAttributeNum: uint32(colDesc.GetPGAttributeNum()),
}
}
return cols
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/catalog/descpb/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func (desc *ColumnDescriptor) CheckCanBeInboundFKRef() error {
// GetPGAttributeNum returns the PGAttributeNum of the ColumnDescriptor
// if the PGAttributeNum is set (non-zero). Returns the ID of the
// ColumnDescriptor if the PGAttributeNum is not set.
func (desc ColumnDescriptor) GetPGAttributeNum() uint32 {
func (desc ColumnDescriptor) GetPGAttributeNum() PGAttributeNum {
if desc.PGAttributeNum != 0 {
return desc.PGAttributeNum
}

return uint32(desc.ID)
return PGAttributeNum(desc.ID)
}

// SQLStringNotHumanReadable returns the SQL statement describing the column.
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/catalog/descpb/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ const (
LatestIndexDescriptorVersion = PrimaryIndexWithStoredColumnsVersion
)

// PGAttributeNum is a custom type for ColumnDescriptor's PGAttributeNum field.
type PGAttributeNum = catid.PGAttributeNum

// ColumnID is a custom type for ColumnDescriptor IDs.
type ColumnID = catid.ColumnID

Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/catalog/descpb/structured.proto
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ message ColumnDescriptor {
// This only differs from ID when the Column order is swapped or
// the ColumnDescriptor must be remade while remaining visual ordering.
// This does not exist in TableDescriptors pre 20.2.
optional uint32 pg_attribute_num = 13 [(gogoproto.nullable) = false, (gogoproto.customname) = "PGAttributeNum"];
optional uint32 pg_attribute_num = 13 [(gogoproto.nullable) = false,
(gogoproto.customname) = "PGAttributeNum",
(gogoproto.casttype) = "PGAttributeNum"];
// Used to indicate column is used and dropped for ALTER COLUMN TYPE mutation.
optional bool alter_column_type_in_progress = 14 [(gogoproto.nullable) = false];

Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/catalog/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ type TableDescriptor interface {
// provided target ID, in the canonical order.
// If no column is found then an error is also returned.
FindColumnWithID(id descpb.ColumnID) (Column, error)

// FindColumnWithPGAttributeNum returns the first column found whose
// PGAttributeNum (if set, otherwise ID) matches the provider id.
// Error is returned if no column is found.
FindColumnWithPGAttributeNum(id descpb.PGAttributeNum) (Column, error)

// FindColumnWithName returns the first column found whose name matches the
// provided target name, in the canonical order.
// If no column is found then an error is also returned.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/table_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ type Column interface {
// GetPGAttributeNum returns the PGAttributeNum of the column descriptor
// if the PGAttributeNum is set (non-zero). Returns the ID of the
// column descriptor if the PGAttributeNum is not set.
GetPGAttributeNum() uint32
GetPGAttributeNum() descpb.PGAttributeNum

// IsSystemColumn returns true iff the column is a system column.
IsSystemColumn() bool
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/tabledesc/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (w column) CheckCanBeOutboundFKRef() error {
// GetPGAttributeNum returns the PGAttributeNum of the column descriptor
// if the PGAttributeNum is set (non-zero). Returns the ID of the
// column descriptor if the PGAttributeNum is not set.
func (w column) GetPGAttributeNum() uint32 {
func (w column) GetPGAttributeNum() descpb.PGAttributeNum {
return w.desc.GetPGAttributeNum()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/catalog/tabledesc/structured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ func TestLogicalColumnID(t *testing.T) {
actual := tests[i].desc.Columns[0].GetPGAttributeNum()
expected := tests[i].expected

if expected != actual {
if expected != uint32(actual) {
t.Fatalf("Expected PGAttributeNum to be %d, got %d.", expected, actual)
}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/catalog/tabledesc/table_desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,19 @@ func (desc *wrapper) FindColumnWithID(id descpb.ColumnID) (catalog.Column, error
return nil, pgerror.Newf(pgcode.UndefinedColumn, "column-id \"%d\" does not exist", id)
}

// FindColumnWithPGAttributeNum implements the TableDescriptor interface.
func (desc *wrapper) FindColumnWithPGAttributeNum(
id descpb.PGAttributeNum,
) (catalog.Column, error) {
for _, col := range desc.AllColumns() {
if col.GetPGAttributeNum() == id {
return col, nil
}
}

return nil, pgerror.Newf(pgcode.UndefinedColumn, "column with logical order %q does not exist", id)
}

// FindColumnWithName implements the TableDescriptor interface.
func (desc *wrapper) FindColumnWithName(name tree.Name) (catalog.Column, error) {
for _, col := range desc.AllColumns() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func newCopyMachine(
Name: col.GetName(),
Typ: col.GetType(),
TableID: tableDesc.GetID(),
PGAttributeNum: col.GetPGAttributeNum(),
PGAttributeNum: uint32(col.GetPGAttributeNum()),
}
}
// If there are no column specifiers and we expect non-visible columns
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/indexbackfiller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ INSERT INTO foo VALUES (1), (10), (100);
Nullable: false,
DefaultExpr: proto.String("42"),
Hidden: false,
PGAttributeNum: uint32(mut.NextColumnID),
PGAttributeNum: descpb.PGAttributeNum(mut.NextColumnID),
}
mut.NextColumnID++
mut.AddColumnMutation(&columnWithDefault, descpb.DescriptorMutation_ADD)
Expand All @@ -311,7 +311,7 @@ INSERT INTO foo VALUES (1), (10), (100);
Nullable: false,
ComputeExpr: proto.String("i + def"),
Hidden: false,
PGAttributeNum: uint32(mut.NextColumnID),
PGAttributeNum: descpb.PGAttributeNum(mut.NextColumnID),
}
mut.NextColumnID++
mut.AddColumnMutation(&computedColumnNotInPrimaryIndex, descpb.DescriptorMutation_ADD)
Expand Down
34 changes: 34 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/show_create
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,37 @@ c CREATE TABLE public.c (
COMMENT ON TABLE public.c IS 'table';
COMMENT ON COLUMN public.c.a IS 'column';
COMMENT ON INDEX public.c@c_a_b_idx IS 'index'

subtest alter_column_type_not_break_show_create

statement ok
SET enable_experimental_alter_column_type_general = true;

statement ok
CREATE TABLE t (c INT);

statement ok
COMMENT ON COLUMN t.c IS 'first comment';

query T
SELECT @2 FROM [SHOW CREATE TABLE t];
----
CREATE TABLE public.t (
c INT8 NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (rowid ASC)
);
COMMENT ON COLUMN public.t.c IS 'first comment'

statement ok
ALTER TABLE t ALTER COLUMN c TYPE character varying;

query T
SELECT @2 FROM [SHOW CREATE TABLE t];
----
CREATE TABLE public.t (
c VARCHAR NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT t_pkey PRIMARY KEY (rowid ASC)
);
COMMENT ON COLUMN public.t.c IS 'first comment'
Loading

0 comments on commit 996f52c

Please sign in to comment.