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

systemschema: stop running PostDeserializationChanges when building tables #105444

Merged
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
2 changes: 2 additions & 0 deletions pkg/sql/catalog/bootstrap/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ func addSystemDescriptorsToSchema(target *MetadataSchema) {
// If adding a call to AddDescriptor or AddDescriptorForSystemTenant, please
// bump the value of NumSystemTablesForSystemTenant below. This constant is
// just used for testing purposes.
// Also add it to the list generated by systemschema.MakeSystemTables(), which
// is also only used for testing purposes.
}

// NumSystemTablesForSystemTenant is the number of system tables defined on
Expand Down
68 changes: 50 additions & 18 deletions pkg/sql/catalog/systemschema/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,31 @@ func systemTable(
// assigned before the primary index.
tbl.PrimaryIndex.ConstraintID = tbl.NextConstraintID
tbl.NextConstraintID++

// Make sure all primary indexes have the correct encoding and version,
// which also requires the stored columns to be set.
tbl.PrimaryIndex.Version = descpb.PrimaryIndexWithStoredColumnsVersion
tbl.PrimaryIndex.EncodingType = catenumpb.PrimaryIndexEncoding
storedColumnIDs := make([]descpb.ColumnID, 0, len(tbl.Columns))
storedColumnNames := make([]string, 0, len(tbl.Columns))
keyColIDs := catalog.TableColSet{}
for _, colID := range tbl.PrimaryIndex.KeyColumnIDs {
keyColIDs.Add(colID)
}
for _, col := range tbl.Columns {
if keyColIDs.Contains(col.ID) || col.Virtual {
continue
}
storedColumnIDs = append(storedColumnIDs, col.ID)
storedColumnNames = append(storedColumnNames, col.Name)
}
if len(storedColumnIDs) == 0 {
storedColumnIDs = nil
storedColumnNames = nil
}
tbl.PrimaryIndex.StoreColumnIDs = storedColumnIDs
tbl.PrimaryIndex.StoreColumnNames = storedColumnNames

return tbl
}

Expand Down Expand Up @@ -1130,12 +1155,6 @@ func makeSystemTable(
fn(&tbl)
}
b := tabledesc.NewBuilder(&tbl)
if err := b.RunPostDeserializationChanges(); err != nil {
log.Fatalf(
ctx, "system table %q cannot be registered, error during RunPostDeserializationChanges: %+v",
tbl.Name, err,
)
}
return SystemTable{
Schema: createTableStmt,
TableDescriptor: b.BuildImmutableTable(),
Expand Down Expand Up @@ -1164,6 +1183,7 @@ func MakeSystemTables() []SystemTable {
RangeEventTable,
UITable,
JobsTable,
SystemJobInfoTable,
WebSessionsTable,
TableStatisticsTable,
LocationsTable,
Expand Down Expand Up @@ -2269,10 +2289,12 @@ var (
),
func(tbl *descpb.TableDescriptor) {
tbl.Checks = []*descpb.TableDescriptor_CheckConstraint{{
Name: "check_singleton",
Expr: "singleton",
ColumnIDs: []descpb.ColumnID{1},
Name: "check_singleton",
Expr: "singleton",
ColumnIDs: []descpb.ColumnID{1},
ConstraintID: tbl.NextConstraintID,
}}
tbl.NextConstraintID++
},
)

Expand Down Expand Up @@ -2412,10 +2434,12 @@ var (
),
func(tbl *descpb.TableDescriptor) {
tbl.Checks = []*descpb.TableDescriptor_CheckConstraint{{
Name: "check_sampling_probability",
Expr: "sampling_probability BETWEEN 0.0:::FLOAT8 AND 1.0:::FLOAT8",
ColumnIDs: []descpb.ColumnID{8},
Name: "check_sampling_probability",
Expr: "sampling_probability BETWEEN 0.0:::FLOAT8 AND 1.0:::FLOAT8",
ColumnIDs: []descpb.ColumnID{8},
ConstraintID: tbl.NextConstraintID,
}}
tbl.NextConstraintID++
},
)

Expand Down Expand Up @@ -2851,7 +2875,9 @@ var (
ColumnIDs: []descpb.ColumnID{11},
IsNonNullConstraint: false,
FromHashShardedColumn: true,
ConstraintID: tbl.NextConstraintID,
}}
tbl.NextConstraintID++
},
)

Expand Down Expand Up @@ -3073,7 +3099,9 @@ var (
ColumnIDs: []descpb.ColumnID{8},
IsNonNullConstraint: false,
FromHashShardedColumn: true,
ConstraintID: tbl.NextConstraintID,
}}
tbl.NextConstraintID++
},
)

Expand Down Expand Up @@ -3606,10 +3634,12 @@ var (
),
func(tbl *descpb.TableDescriptor) {
tbl.Checks = []*descpb.TableDescriptor_CheckConstraint{{
Name: "check_bounds",
Expr: "start_key < end_key",
ColumnIDs: []descpb.ColumnID{1, 2},
Name: "check_bounds",
Expr: "start_key < end_key",
ColumnIDs: []descpb.ColumnID{1, 2},
ConstraintID: tbl.NextConstraintID,
}}
tbl.NextConstraintID++
},
)

Expand Down Expand Up @@ -3673,10 +3703,12 @@ var (
),
func(tbl *descpb.TableDescriptor) {
tbl.Checks = []*descpb.TableDescriptor_CheckConstraint{{
Name: "single_row",
Expr: "singleton",
ColumnIDs: []descpb.ColumnID{1},
Name: "single_row",
Expr: "singleton",
ColumnIDs: []descpb.ColumnID{1},
ConstraintID: tbl.NextConstraintID,
}}
tbl.NextConstraintID++
},
)

Expand Down
9 changes: 6 additions & 3 deletions pkg/sql/tests/system_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ func TestSystemTableLiterals(t *testing.T) {
}
}

const expectedNumberOfSystemTables = bootstrap.NumSystemTablesForSystemTenant
// Add one for the system.span_count table, which is currently the only
// non-system tenant table.
const expectedNumberOfSystemTables = bootstrap.NumSystemTablesForSystemTenant + 1
require.Equal(t, expectedNumberOfSystemTables, len(testcases))

runTest := func(name string, test testcase) {
runTest := func(t *testing.T, name string, test testcase) {
t.Helper()
privs := *test.pkg.GetPrivileges()
desc := test.pkg
// Allocate an ID to dynamically allocated system tables.
Expand Down Expand Up @@ -247,7 +250,7 @@ func TestSystemTableLiterals(t *testing.T) {

for name, test := range testcases {
t.Run(name, func(t *testing.T) {
runTest(name, test)
runTest(t, name, test)
})
}
}