Skip to content

Commit

Permalink
sql: add the crdb_internal.create_type_statements table
Browse files Browse the repository at this point in the history
This PR adds the `crdb_internal.create_type_statments` table, which
contains create statements for user defined types. This will be used
when generating create statments for `cockroach dump` in #47765.

This PR additionally refactors and deduplicates some code that iterates
over all user defined type descriptors in other virtual tables.

Release note (sql change): Add the
`crdb_internal.create_type_statements` virtual table. It holds create
statements for user defined types.
  • Loading branch information
rohany committed Jun 2, 2020
1 parent 187036c commit 0bcddba
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 173 deletions.
1 change: 1 addition & 0 deletions pkg/cli/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ table_name NOT IN (
'backward_dependencies',
'builtin_functions',
'create_statements',
'create_type_statements',
'forward_dependencies',
'index_columns',
'table_columns',
Expand Down
52 changes: 52 additions & 0 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var crdbInternal = virtualSchema{
sqlbase.CrdbInternalClusterSessionsTableID: crdbInternalClusterSessionsTable,
sqlbase.CrdbInternalClusterSettingsTableID: crdbInternalClusterSettingsTable,
sqlbase.CrdbInternalCreateStmtsTableID: crdbInternalCreateStmtsTable,
sqlbase.CrdbInternalCreateTypeStmtsTableID: crdbInternalCreateTypeStmtsTable,
sqlbase.CrdbInternalFeatureUsageID: crdbInternalFeatureUsage,
sqlbase.CrdbInternalForwardDependenciesTableID: crdbInternalForwardDependenciesTable,
sqlbase.CrdbInternalGossipNodesTableID: crdbInternalGossipNodesTable,
Expand Down Expand Up @@ -1346,6 +1347,57 @@ CREATE TABLE crdb_internal.builtin_functions (
},
}

var crdbInternalCreateTypeStmtsTable = virtualSchemaTable{
comment: "CREATE statements for all user defined types accessible by the current user in current database (KV scan)",
schema: `
CREATE TABLE crdb_internal.create_type_statements (
database_id INT,
database_name STRING,
schema_name STRING,
descriptor_id INT,
descriptor_name STRING,
create_statement STRING,
INDEX (descriptor_id)
)
`,
populate: func(ctx context.Context, p *planner, db *DatabaseDescriptor, addRow func(...tree.Datum) error) error {
return forEachTypeDesc(ctx, p, db, func(db *DatabaseDescriptor, sc string, typeDesc *TypeDescriptor) error {
switch typeDesc.Kind {
case sqlbase.TypeDescriptor_ENUM:
var enumLabels []string
for i := range typeDesc.EnumMembers {
enumLabels = append(enumLabels, typeDesc.EnumMembers[i].LogicalRepresentation)
}
name, err := tree.NewUnresolvedObjectName(3, [3]string{typeDesc.Name, sc, db.Name}, 0)
if err != nil {
return err
}
node := &tree.CreateType{
Variety: tree.Enum,
TypeName: name,
EnumLabels: enumLabels,
}
if err := addRow(
tree.NewDInt(tree.DInt(db.ID)), // database_id
tree.NewDString(db.Name), // database_name
tree.NewDString(sc), // schema_name
tree.NewDInt(tree.DInt(typeDesc.ID)), // descriptor_id
tree.NewDString(typeDesc.Name), // descriptor_name
tree.NewDString(tree.AsString(node)), // create_statement
); err != nil {
return err
}
case sqlbase.TypeDescriptor_ALIAS:
// Alias types are created implicitly, so we don't have create
// statements for them.
default:
return errors.AssertionFailedf("unknown type descriptor kind %s", typeDesc.Kind.String())
}
return nil
})
},
}

// Prepare the row populate function.
var typeView = tree.NewDString("view")
var typeTable = tree.NewDString("table")
Expand Down
35 changes: 35 additions & 0 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,41 @@ func forEachDatabaseDesc(
return nil
}

// forEachTypeDesc calls a function for each TypeDescriptor. If dbContext is
// not nil, then the function is called for only TypeDescriptors within the
// given database.
func forEachTypeDesc(
ctx context.Context,
p *planner,
dbContext *DatabaseDescriptor,
fn func(db *DatabaseDescriptor, sc string, typ *TypeDescriptor) error,
) error {
descs, err := p.Tables().GetAllDescriptors(ctx, p.txn)
if err != nil {
return err
}
schemaNames, err := getSchemaNames(ctx, p, dbContext)
if err != nil {
return err
}
lCtx := newInternalLookupCtx(descs, dbContext)
for _, id := range lCtx.typIDs {
typ := lCtx.typDescs[id]
dbDesc, parentExists := lCtx.dbDescs[typ.ParentID]
if !parentExists {
continue
}
scName, ok := schemaNames[typ.GetParentSchemaID()]
if !ok {
return errors.AssertionFailedf("schema id %d not found", typ.GetParentSchemaID())
}
if err := fn(dbDesc, scName, typ); err != nil {
return err
}
}
return nil
}

// forEachTableDesc retrieves all table descriptors from the current
// database and all system databases and iterates through them. For
// each table, the function will call fn with its respective database
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ crdb_internal cluster_sessions table
crdb_internal cluster_settings table
crdb_internal cluster_transactions table
crdb_internal create_statements table
crdb_internal create_type_statements table
crdb_internal feature_usage table
crdb_internal forward_dependencies table
crdb_internal gossip_alerts table
Expand Down Expand Up @@ -618,3 +619,19 @@ query B
SELECT crdb_internal.is_admin()
----
false

user root

# Test the crdb_internal.create_type_statements table.
statement ok
SET experimental_enable_enums=true;
CREATE TYPE enum1 AS ENUM ('hello', 'hi');
CREATE TYPE enum2 AS ENUM ()

query ITTITT
SELECT * FROM crdb_internal.create_type_statements
----
52 test public 61 enum1 CREATE TYPE test.public.enum1 AS ENUM ('hello', 'hi')
52 test public 63 enum2 CREATE TYPE test.public.enum2 AS ENUM ()

# Test the virtual index as well.
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/grant_table
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ test crdb_internal cluster_sessions public S
test crdb_internal cluster_settings public SELECT
test crdb_internal cluster_transactions public SELECT
test crdb_internal create_statements public SELECT
test crdb_internal create_type_statements public SELECT
test crdb_internal feature_usage public SELECT
test crdb_internal forward_dependencies public SELECT
test crdb_internal gossip_alerts public SELECT
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ crdb_internal cluster_sessions
crdb_internal cluster_settings
crdb_internal cluster_transactions
crdb_internal create_statements
crdb_internal create_type_statements
crdb_internal feature_usage
crdb_internal forward_dependencies
crdb_internal gossip_alerts
Expand Down Expand Up @@ -370,6 +371,7 @@ cluster_sessions
cluster_settings
cluster_transactions
create_statements
create_type_statements
feature_usage
forward_dependencies
gossip_alerts
Expand Down Expand Up @@ -524,6 +526,7 @@ system crdb_internal cluster_sessions SYSTEM VIE
system crdb_internal cluster_settings SYSTEM VIEW NO 1
system crdb_internal cluster_transactions SYSTEM VIEW NO 1
system crdb_internal create_statements SYSTEM VIEW NO 1
system crdb_internal create_type_statements SYSTEM VIEW NO 1
system crdb_internal feature_usage SYSTEM VIEW NO 1
system crdb_internal forward_dependencies SYSTEM VIEW NO 1
system crdb_internal gossip_alerts SYSTEM VIEW NO 1
Expand Down Expand Up @@ -1561,6 +1564,7 @@ NULL public system crdb_internal cluster_sessions
NULL public system crdb_internal cluster_settings SELECT NULL YES
NULL public system crdb_internal cluster_transactions SELECT NULL YES
NULL public system crdb_internal create_statements SELECT NULL YES
NULL public system crdb_internal create_type_statements SELECT NULL YES
NULL public system crdb_internal feature_usage SELECT NULL YES
NULL public system crdb_internal forward_dependencies SELECT NULL YES
NULL public system crdb_internal gossip_alerts SELECT NULL YES
Expand Down Expand Up @@ -1913,6 +1917,7 @@ NULL public system crdb_internal cluster_sessions
NULL public system crdb_internal cluster_settings SELECT NULL YES
NULL public system crdb_internal cluster_transactions SELECT NULL YES
NULL public system crdb_internal create_statements SELECT NULL YES
NULL public system crdb_internal create_type_statements SELECT NULL YES
NULL public system crdb_internal feature_usage SELECT NULL YES
NULL public system crdb_internal forward_dependencies SELECT NULL YES
NULL public system crdb_internal gossip_alerts SELECT NULL YES
Expand Down
Loading

0 comments on commit 0bcddba

Please sign in to comment.