Skip to content

Commit

Permalink
Add Logical Column ID field to ColumnDescriptor
Browse files Browse the repository at this point in the history
Release note (<category, see below>): <what> <show> <why>
  • Loading branch information
RichardJCai committed Apr 3, 2020
1 parent 1c27a9f commit fed431e
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 429 deletions.
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.LogicalColumnID)), // ordinal_position
dStringPtrOrNull(column.DefaultExpr), // column_default
yesOrNoDatum(column.Nullable), // is_nullable
tree.NewDString(column.Type.InformationSchemaName()), // data_type
Expand Down
42 changes: 20 additions & 22 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,7 @@ CREATE TABLE pg_catalog.pg_attrdef (
h := makeOidHasher()
return forEachTableDesc(ctx, p, dbContext, virtualMany,
func(db *sqlbase.DatabaseDescriptor, scName string, table *sqlbase.TableDescriptor) error {
colNum := 0
return forEachColumnInTable(table, func(column *sqlbase.ColumnDescriptor) error {
colNum++
if column.DefaultExpr == nil {
// pg_attrdef only expects rows for columns with default values.
return nil
Expand All @@ -408,11 +406,11 @@ CREATE TABLE pg_catalog.pg_attrdef (
defSrc = tree.NewDString(ctx.String())
}
return addRow(
h.ColumnOid(table.ID, column.ID), // oid
defaultOid(table.ID), // adrelid
tree.NewDInt(tree.DInt(colNum)), // adnum
defSrc, // adbin
defSrc, // adsrc
h.ColumnOid(table.ID, column.ID), // oid
defaultOid(table.ID), // adrelid
tree.NewDInt(tree.DInt(column.LogicalColumnID)), // adnum
defSrc, // adbin
defSrc, // adsrc
)
})
})
Expand Down Expand Up @@ -450,7 +448,7 @@ CREATE TABLE pg_catalog.pg_attribute (
h := makeOidHasher()
return forEachTableDesc(ctx, p, dbContext, virtualMany, func(db *sqlbase.DatabaseDescriptor, scName string, table *sqlbase.TableDescriptor) error {
// addColumn adds adds either a table or a index column to the pg_attribute table.
addColumn := func(column *sqlbase.ColumnDescriptor, attRelID tree.Datum, colID sqlbase.ColumnID) error {
addColumn := func(column *sqlbase.ColumnDescriptor, attRelID tree.Datum, logicalColID sqlbase.ColumnID) error {
colTyp := &column.Type
attTypMod := int32(-1)
if width := colTyp.Width(); width != 0 {
Expand All @@ -470,18 +468,18 @@ CREATE TABLE pg_catalog.pg_attribute (
}
}
return addRow(
attRelID, // attrelid
tree.NewDName(column.Name), // attname
typOid(colTyp), // atttypid
zeroVal, // attstattarget
typLen(colTyp), // attlen
tree.NewDInt(tree.DInt(colID)), // attnum
zeroVal, // attndims
negOneVal, // attcacheoff
tree.NewDInt(tree.DInt(attTypMod)), // atttypmod
tree.DNull, // attbyval (see pg_type.typbyval)
tree.DNull, // attstorage
tree.DNull, // attalign
attRelID, // attrelid
tree.NewDName(column.Name), // attname
typOid(colTyp), // atttypid
zeroVal, // attstattarget
typLen(colTyp), // attlen
tree.NewDInt(tree.DInt(logicalColID)), // attnum
zeroVal, // attndims
negOneVal, // attcacheoff
tree.NewDInt(tree.DInt(attTypMod)), // atttypmod
tree.DNull, // attbyval (see pg_type.typbyval)
tree.DNull, // attstorage
tree.DNull, // attalign
tree.MakeDBool(tree.DBool(!column.Nullable)), // attnotnull
tree.MakeDBool(tree.DBool(column.DefaultExpr != nil)), // atthasdef
tree.DBoolFalse, // attisdropped
Expand All @@ -497,7 +495,7 @@ CREATE TABLE pg_catalog.pg_attribute (
// Columns for table.
if err := forEachColumnInTable(table, func(column *sqlbase.ColumnDescriptor) error {
tableID := defaultOid(table.ID)
return addColumn(column, tableID, column.ID)
return addColumn(column, tableID, column.LogicalColumnID)
}); err != nil {
return err
}
Expand All @@ -507,7 +505,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.LogicalColumnID)
},
)
})
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ func (desc *MutableTableDescriptor) MaybeFillColumnID(
}
columnNames[c.Name] = columnID
c.ID = columnID
c.LogicalColumnID = columnID
}

// AllocateIDs allocates column, family, and index ids for any column, family,
Expand Down
Loading

0 comments on commit fed431e

Please sign in to comment.