Skip to content

Commit

Permalink
sql: add index check for virtualTables
Browse files Browse the repository at this point in the history
Check that the indexes in the schema for the virtualTable matches the
number of index array elements.

Release note: None
  • Loading branch information
otan committed Dec 9, 2021
1 parent 48a0695 commit 36d38d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ go_test(
"//pkg/sql/catalog/descs",
"//pkg/sql/catalog/lease",
"//pkg/sql/catalog/multiregion",
"//pkg/sql/catalog/schemadesc",
"//pkg/sql/catalog/systemschema",
"//pkg/sql/catalog/tabledesc",
"//pkg/sql/catalog/typedesc",
Expand Down
31 changes: 29 additions & 2 deletions pkg/sql/virtual_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ package sql

import (
"bufio"
"context"
"flag"
"fmt"
"os"
"strings"
"testing"

"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/stretchr/testify/require"
)

var rewriteTables = flag.Bool(
Expand All @@ -49,7 +53,9 @@ func TestVirtualSchemas(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

for _, schema := range virtualSchemas {
ctx := context.Background()

for schemaID, schema := range virtualSchemas {
_, isSchemaFixable := fixableSchemas[schema.name]
if len(schema.undefinedTables) == 0 || !isSchemaFixable {
continue
Expand All @@ -64,7 +70,7 @@ func TestVirtualSchemas(t *testing.T) {
rewriteSchema(schema.name, unimplementedTables)
} else {
t.Run(fmt.Sprintf("VirtualSchemaTest/%s", schema.name), func(t *testing.T) {
for _, virtualTable := range schema.tableDefs {
for tableID, virtualTable := range schema.tableDefs {
tableName, err := getTableNameFromCreateTable(virtualTable.getSchema())
if err != nil {
t.Fatal(err)
Expand All @@ -76,6 +82,27 @@ func TestVirtualSchemas(t *testing.T) {
tableName,
)
}

// Sanity check indexes are all defined.
sc, ok := schemadesc.GetVirtualSchemaByID(schemaID)
require.True(t, ok)
d, err := virtualTable.initVirtualTableDesc(
ctx,
cluster.MakeClusterSettings(),
sc,
tableID,
)
require.NoError(t, err)
switch virtualTable := virtualTable.(type) {
case *virtualSchemaTable:
require.Equalf(
t,
len(d.GetIndexes()),
len(virtualTable.indexes),
"number of indexes in description must match number of indexes defined for table %s",
d.GetName(),
)
}
}
})
}
Expand Down

0 comments on commit 36d38d9

Please sign in to comment.