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

sql: Add Logical Column ID field to ColumnDescriptor #46992

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ https://www.postgresql.org/docs/9.5/infoschema-columns.html`,
collationName = tree.NewDString(locale)
}
return addRow(
dbNameStr, // table_catalog
scNameStr, // table_schema
tree.NewDString(table.Name), // table_name
tree.NewDString(column.Name), // column_name
tree.NewDInt(tree.DInt(column.ID)), // ordinal_position
dbNameStr, // table_catalog
scNameStr, // table_schema
tree.NewDString(table.Name), // table_name
tree.NewDString(column.Name), // column_name
tree.NewDInt(tree.DInt(column.GetLogicalColumnID())), // ordinal_position
dStringPtrOrNull(column.DefaultExpr), // column_default
yesOrNoDatum(column.Nullable), // is_nullable
tree.NewDString(column.Type.InformationSchemaName()), // data_type
Expand Down
23 changes: 23 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -2279,3 +2279,26 @@ indexname indexdef
primary CREATE UNIQUE INDEX "primary" ON test.public.geospatial_table USING btree (id ASC)
idxa CREATE INDEX idxa ON test.public.geospatial_table USING gin (a ASC)
idxb CREATE INDEX idxb ON test.public.geospatial_table USING gin (b ASC)

subtest regression_46799
statement ok
create table t(x INT DEFAULT 1, y INT DEFAULT 1);

query I
select adnum from pg_attrdef WHERE adrelid = 85
----
1
2
3

statement ok
alter table t drop column y;
alter table t add column y int default 1;

# make sure after adding and dropping the same column, the adnum for the re-added column increases.
query I
select adnum from pg_attrdef WHERE adrelid = 85
----
1
3
4
14 changes: 7 additions & 7 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,11 @@ CREATE TABLE pg_catalog.pg_attrdef (
defSrc = tree.NewDString(ctx.String())
}
return addRow(
h.ColumnOid(table.ID, column.ID), // oid
tableOid(table.ID), // adrelid
tree.NewDInt(tree.DInt(colNum)), // adnum
defSrc, // adbin
defSrc, // adsrc
h.ColumnOid(table.ID, column.ID), // oid
tableOid(table.ID), // adrelid
tree.NewDInt(tree.DInt(column.GetLogicalColumnID())), // adnum
defSrc, // adbin
defSrc, // adsrc
)
})
})
Expand Down Expand Up @@ -502,7 +502,7 @@ CREATE TABLE pg_catalog.pg_attribute (
// Columns for table.
if err := forEachColumnInTable(table, func(column *sqlbase.ColumnDescriptor) error {
tableID := tableOid(table.ID)
return addColumn(column, tableID, column.ID)
return addColumn(column, tableID, column.GetLogicalColumnID())
}); err != nil {
return err
}
Expand All @@ -512,7 +512,7 @@ CREATE TABLE pg_catalog.pg_attribute (
return forEachColumnInIndex(table, index,
func(column *sqlbase.ColumnDescriptor) error {
idxID := h.IndexOid(table.ID, index.ID)
return addColumn(column, idxID, column.ID)
return addColumn(column, idxID, column.GetLogicalColumnID())
},
)
})
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -4130,3 +4130,14 @@ func GenerateUniqueConstraintName(prefix string, nameExistsFunc func(name string
}
return name
}

// GetLogicalColumnID returns the LogicalColumnID of the ColumnDescriptor
// if the LogicalColumnID is set (non-zero). Returns the ID of the
// ColumnDescriptor if the LogicalColumnID is not set.
func (desc ColumnDescriptor) GetLogicalColumnID() ColumnID {
if desc.LogicalColumnID != 0 {
return desc.LogicalColumnID
}

return desc.ID
}
Loading