Skip to content

Commit

Permalink
Merge #53562
Browse files Browse the repository at this point in the history
53562: sql: add the `tableoid` system column r=rohany a=rohany

This commit adds the `tableoid` system column. While doing so, it fixes
some bugs and ergonomics around creating new system columns. In
particular, the existing strategy for producing system columns during
scans was deficient. It would pass to the processor the set of system
columns that was desired, while the processor itself would be set with a
series of column ordinals to produce from the scan. These ordinals
didn't line up well with the set of desired system columns when the
desired system columns were more than just the set of the mvcc_timestamp
column. This commit changes the strategy to just mark when system
columns are desired, and add all registered system columns to the
candidate set of columns used to resolve ordinals.

This commit also makes adding system columns a bit more automatic,
rather than requiring users to add cases in multiple functions.

Release justification: bug fixes and low-risk updates to new
functionality
Release note (sql change): Add the `tableoid` system column.

Co-authored-by: Rohan Yadav <[email protected]>
  • Loading branch information
craig[bot] and rohany committed Aug 28, 2020
2 parents 123ca07 + 89b73d2 commit 2e4df33
Show file tree
Hide file tree
Showing 36 changed files with 1,072 additions and 1,083 deletions.
12 changes: 12 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ TABLE ok1
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down Expand Up @@ -467,6 +468,7 @@ TABLE ok2
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down Expand Up @@ -510,6 +512,7 @@ TABLE ok3
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down Expand Up @@ -556,6 +559,7 @@ TABLE ok4
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down Expand Up @@ -595,6 +599,7 @@ TABLE ok5
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down Expand Up @@ -659,6 +664,7 @@ TABLE ok6
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
└── b int not null
Expand Down Expand Up @@ -695,6 +701,7 @@ TABLE ok7
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
└── b int not null
Expand Down Expand Up @@ -737,6 +744,7 @@ TABLE ok8
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
└── b int not null
Expand Down Expand Up @@ -781,6 +789,7 @@ TABLE ok9
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
└── b int not null
Expand Down Expand Up @@ -827,6 +836,7 @@ TABLE ok10
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
└── b int not null
Expand Down Expand Up @@ -882,6 +892,7 @@ TABLE ok11
├── b int not null
├── c int not null
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down Expand Up @@ -928,6 +939,7 @@ TABLE ok12
├── b int not null
├── c int
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
└── INDEX primary
├── a int not null
├── b int not null
Expand Down
6 changes: 6 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/zone
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ TABLE t
├── k int not null
├── v string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
│ ├── k int not null
Expand Down Expand Up @@ -86,6 +87,7 @@ TABLE t
├── k int not null
├── v string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
│ ├── k int not null
Expand Down Expand Up @@ -152,6 +154,7 @@ TABLE t
├── k int not null
├── v string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
│ ├── k int not null
Expand Down Expand Up @@ -259,6 +262,7 @@ TABLE t
├── k int not null
├── v string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
│ ├── k int not null
Expand Down Expand Up @@ -314,6 +318,7 @@ TABLE t
├── k int not null
├── v string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
│ ├── k int not null
Expand Down Expand Up @@ -420,6 +425,7 @@ TABLE t
├── k int not null
├── v string
├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
├── tableoid oid [hidden] [system]
├── FAMILY fam_0_k_v (k, v)
├── INDEX primary
│ ├── k int not null
Expand Down
11 changes: 7 additions & 4 deletions pkg/internal/sqlsmith/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,13 @@ ORDER BY
return fmt.Errorf("zero columns for %s.%s", lastCatalog, lastName)
}
// All non virtual tables contain implicit system columns.
currentCols = append(currentCols, &tree.ColumnTableDef{
Name: colinfo.MVCCTimestampColumnName,
Type: colinfo.MVCCTimestampColumnType,
})
for i := range colinfo.AllSystemColumnDescs {
col := &colinfo.AllSystemColumnDescs[i]
currentCols = append(currentCols, &tree.ColumnTableDef{
Name: tree.Name(col.Name),
Type: col.Type,
})
}
tables = append(tables, &tableRef{
TableName: tree.NewTableName(lastCatalog, lastName),
Columns: currentCols,
Expand Down
111 changes: 43 additions & 68 deletions pkg/sql/catalog/colinfo/system_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@ import (
// column ID that is counting down from math.MaxUint32. This is done so that
// each system column ID won't conflict with existing column ID's and also
// will be stable across all changes to the table.
// * MVCC Timestamp: contains a representation of the row's HLC timestamp.
// * crdb_internal_mvcc_timestamp: contains a representation of the row's
// HLC timestamp.
// * tableoid: A Postgres system column that contains the ID of the table
// that a particular row came from.

// MVCCTimestampColumnName is the name of the MVCC timestamp system column.
const MVCCTimestampColumnName = "crdb_internal_mvcc_timestamp"

// MVCCTimestampColumnType is the type of the MVCC timestamp system column.
var MVCCTimestampColumnType = types.Decimal
// AllSystemColumnDescs contains all registered system columns.
var AllSystemColumnDescs = []descpb.ColumnDescriptor{
MVCCTimestampColumnDesc,
TableOIDColumnDesc,
}

// MVCCTimestampColumnID is the ColumnID of the MVCC timesatmp column. Future
// system columns will have ID's that decrement from this value.
const MVCCTimestampColumnID = math.MaxUint32

// TableOIDColumnID is the ID of the tableoid system column.
const TableOIDColumnID = MVCCTimestampColumnID - 1

// MVCCTimestampColumnDesc is a column descriptor for the MVCC system column.
var MVCCTimestampColumnDesc = descpb.ColumnDescriptor{
Name: MVCCTimestampColumnName,
Expand All @@ -49,87 +55,56 @@ var MVCCTimestampColumnDesc = descpb.ColumnDescriptor{
ID: MVCCTimestampColumnID,
}

// MVCCTimestampColumnName is the name of the MVCC timestamp system column.
const MVCCTimestampColumnName = "crdb_internal_mvcc_timestamp"

// MVCCTimestampColumnType is the type of the MVCC timestamp system column.
var MVCCTimestampColumnType = types.Decimal

// TableOIDColumnDesc is a column descriptor for the tableoid column.
var TableOIDColumnDesc = descpb.ColumnDescriptor{
Name: "tableoid",
Type: types.Oid,
Hidden: true,
Nullable: true,
SystemColumnKind: descpb.SystemColumnKind_TABLEOID,
ID: TableOIDColumnID,
}

// IsColIDSystemColumn returns whether a column ID refers to a system column.
func IsColIDSystemColumn(colID descpb.ColumnID) bool {
switch colID {
case MVCCTimestampColumnID:
return true
default:
return false
}
return GetSystemColumnKindFromColumnID(colID) != descpb.SystemColumnKind_NONE
}

// GetSystemColumnDescriptorFromID returns a column descriptor corresponding
// to the system column referred to by the input column ID.
func GetSystemColumnDescriptorFromID(colID descpb.ColumnID) (*descpb.ColumnDescriptor, error) {
switch colID {
case MVCCTimestampColumnID:
return &MVCCTimestampColumnDesc, nil
default:
return nil, errors.AssertionFailedf("unsupported system column ID %d", colID)
for i := range AllSystemColumnDescs {
if AllSystemColumnDescs[i].ID == colID {
return &AllSystemColumnDescs[i], nil
}
}
return nil, errors.AssertionFailedf("unsupported system column ID %d", colID)
}

// GetSystemColumnKindFromColumnID returns the kind of system column that colID
// refers to.
func GetSystemColumnKindFromColumnID(colID descpb.ColumnID) descpb.SystemColumnKind {
switch colID {
case MVCCTimestampColumnID:
return descpb.SystemColumnKind_MVCCTIMESTAMP
default:
return descpb.SystemColumnKind_NONE
}
}

// GetSystemColumnIDByKind returns the column ID of the desired system column.
func GetSystemColumnIDByKind(kind descpb.SystemColumnKind) (descpb.ColumnID, error) {
switch kind {
case descpb.SystemColumnKind_MVCCTIMESTAMP:
return MVCCTimestampColumnID, nil
default:
return 0, errors.Newf("invalid system column kind %s", kind.String())
}
}

// GetSystemColumnTypeForKind returns the types.T of the input system column.
func GetSystemColumnTypeForKind(kind descpb.SystemColumnKind) *types.T {
switch kind {
case descpb.SystemColumnKind_MVCCTIMESTAMP:
return MVCCTimestampColumnType
default:
return nil
for i := range AllSystemColumnDescs {
if AllSystemColumnDescs[i].ID == colID {
return AllSystemColumnDescs[i].SystemColumnKind
}
}
return descpb.SystemColumnKind_NONE
}

// IsSystemColumnName returns whether or not a name is a reserved system
// column name.
func IsSystemColumnName(name string) bool {
switch name {
case MVCCTimestampColumnName:
return true
default:
return false
}
}

// GetSystemColumnTypesAndDescriptors is a utility method to construct a set of
// types and column descriptors from an input list of system column kinds.
func GetSystemColumnTypesAndDescriptors(
kinds []descpb.SystemColumnKind,
) ([]*types.T, []descpb.ColumnDescriptor, error) {
resTypes := make([]*types.T, len(kinds))
resDescs := make([]descpb.ColumnDescriptor, len(kinds))
for i, k := range kinds {
resTypes[i] = GetSystemColumnTypeForKind(k)
colID, err := GetSystemColumnIDByKind(k)
if err != nil {
return nil, nil, err
}
colDesc, err := GetSystemColumnDescriptorFromID(colID)
if err != nil {
return nil, nil, err
for i := range AllSystemColumnDescs {
if AllSystemColumnDescs[i].Name == name {
return true
}
resDescs[i] = *colDesc
}
return resTypes, resDescs, nil
return false
}
Loading

0 comments on commit 2e4df33

Please sign in to comment.