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

sql: Support for selecting from views #9355

Merged
merged 2 commits into from
Sep 15, 2016
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
102 changes: 82 additions & 20 deletions sql/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,28 +237,10 @@ func (p *planner) getDataSource(
if foundVirtual {
return ds, nil
}

// This name designates a real table.
scan := p.Scan()
if err := scan.initTable(p, tn, hints, scanVisibility); err != nil {
return planDataSource{}, err
}

return planDataSource{
info: newSourceInfoForSingleTable(*tn, scan.Columns()),
plan: scan,
}, nil
return p.getTableScanOrViewPlan(tn, hints, scanVisibility)

case *parser.Subquery:
// We have a subquery (this includes a simple "VALUES").
plan, err := p.newPlan(t.Select, nil, false)
if err != nil {
return planDataSource{}, err
}
return planDataSource{
info: newSourceInfoForSingleTable(parser.TableName{}, plan.Columns()),
plan: plan,
}, nil
return p.getSubqueryPlan(t.Select, nil)

case *parser.JoinTableExpr:
// Joins: two sources.
Expand Down Expand Up @@ -324,6 +306,86 @@ func (p *planner) getDataSource(
}
}

// getTableScanOrViewPlan builds a planDataSource from a single data source
// clause (either a table or a view) in a SelectClause, expanding views out
// into subqueries.
func (p *planner) getTableScanOrViewPlan(
tn *parser.TableName,
hints *parser.IndexHints,
scanVisibility scanVisibility,
) (planDataSource, error) {
descFunc := p.getTableLease
if p.asOf {
// AS OF SYSTEM TIME queries need to fetch the table descriptor at the
// specified time, and never lease anything. The proto transaction already
// has its timestamps set correctly so mustGetTableDesc will fetch with the
// correct timestamp.
descFunc = p.mustGetTableDesc
}
desc, err := descFunc(tn)
if err != nil {
return planDataSource{}, err
}

if desc.IsView() {
return p.getViewPlan(tn, desc)
} else if !desc.IsTable() {
return planDataSource{},
errors.Errorf("unexpected table descriptor of type %s for %q", desc.TypeName(), tn)
}

// This name designates a real table.
scan := p.Scan()
if err := scan.initTable(p, desc, hints, scanVisibility); err != nil {
return planDataSource{}, err
}

return planDataSource{
info: newSourceInfoForSingleTable(*tn, scan.Columns()),
plan: scan,
}, nil
}

// getViewPlan builds a planDataSource for the view specified by the
// table name and descriptor, expanding out its subquery plan.
func (p *planner) getViewPlan(
tn *parser.TableName, desc *sqlbase.TableDescriptor,
) (planDataSource, error) {
// Parse the query as Traditional syntax because we know the query was
// saved in the descriptor by printing it with parser.Format.
stmt, err := parser.ParseOneTraditional(desc.ViewQuery)
if err != nil {
return planDataSource{}, errors.Wrapf(err, "failed to parse underlying query from view %q", tn)
}
sel, ok := stmt.(*parser.Select)
if !ok {
return planDataSource{},
errors.Errorf("failed to parse underlying query from view %q as a select", tn)
}
// TODO(a-robinson): Support ORDER BY and LIMIT in views. Is it as simple as
// just passing the entire select here or will inserting an ORDER BY in the
// middle of a query plan break things?
return p.getSubqueryPlan(sel.Select, makeResultColumns(desc.Columns))
}

// getSubqueryPlan builds a planDataSource for a select statement, including
// for simple VALUES statements.
func (p *planner) getSubqueryPlan(
sel parser.SelectStatement, cols ResultColumns,
) (planDataSource, error) {
plan, err := p.newPlan(sel, nil, false)
if err != nil {
return planDataSource{}, err
}
if len(cols) == 0 {
cols = plan.Columns()
}
return planDataSource{
info: newSourceInfoForSingleTable(parser.TableName{}, cols),
plan: plan,
}, nil
}

// expandStar returns the array of column metadata and name
// expressions that correspond to the expansion of a star.
func (src *dataSourceInfo) expandStar(
Expand Down
14 changes: 7 additions & 7 deletions sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ CREATE TABLE information_schema.columns (
DATETIME_PRECISION INT
);
`,
desc: sqlbase.TableDescriptor{Name: "columns", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "TABLE_CATALOG", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "COLUMN_NAME", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "ORDINAL_POSITION", ID: 0x5, Type: sqlbase.ColumnType{Kind: 1}, DefaultExpr: &zeroStr}, {Name: "COLUMN_DEFAULT", ID: 0x6, Type: sqlbase.ColumnType{Kind: 7}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "IS_NULLABLE", ID: 0x7, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "DATA_TYPE", ID: 0x8, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CHARACTER_MAXIMUM_LENGTH", ID: 0x9, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "CHARACTER_OCTET_LENGTH", ID: 0xa, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "NUMERIC_PRECISION", ID: 0xb, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "NUMERIC_SCALE", ID: 0xc, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "DATETIME_PRECISION", ID: 0xd, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}}, NextColumnID: 0xe, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x1, Unique: false, ColumnNames: []string(nil), ColumnDirections: []sqlbase.IndexDescriptor_Direction(nil), StoreColumnNames: []string(nil), ColumnIDs: []sqlbase.ColumnID(nil), ImplicitColumnIDs: []sqlbase.ColumnID(nil), Interleave: sqlbase.InterleaveDescriptor{Ancestors: []sqlbase.InterleaveDescriptor_Ancestor(nil)}, InterleavedBy: []sqlbase.ForeignKeyReference(nil)}, Indexes: []sqlbase.IndexDescriptor(nil), NextIndexID: 0x2, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
desc: sqlbase.TableDescriptor{Name: "columns", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "TABLE_CATALOG", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "COLUMN_NAME", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "ORDINAL_POSITION", ID: 0x5, Type: sqlbase.ColumnType{Kind: 1}, DefaultExpr: &zeroStr}, {Name: "COLUMN_DEFAULT", ID: 0x6, Type: sqlbase.ColumnType{Kind: 7}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "IS_NULLABLE", ID: 0x7, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "DATA_TYPE", ID: 0x8, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CHARACTER_MAXIMUM_LENGTH", ID: 0x9, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "CHARACTER_OCTET_LENGTH", ID: 0xa, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "NUMERIC_PRECISION", ID: 0xb, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "NUMERIC_SCALE", ID: 0xc, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}, {Name: "DATETIME_PRECISION", ID: 0xd, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}}, NextColumnID: 0xe, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x0, Unique: false, ColumnNames: []string(nil), ColumnDirections: []sqlbase.IndexDescriptor_Direction(nil), StoreColumnNames: []string(nil), ColumnIDs: []sqlbase.ColumnID(nil), ImplicitColumnIDs: []sqlbase.ColumnID(nil), Interleave: sqlbase.InterleaveDescriptor{Ancestors: []sqlbase.InterleaveDescriptor_Ancestor(nil)}, InterleavedBy: []sqlbase.ForeignKeyReference(nil)}, Indexes: []sqlbase.IndexDescriptor(nil), NextIndexID: 0x0, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
Expand Down Expand Up @@ -168,7 +168,7 @@ CREATE TABLE information_schema.table_privileges (
WITH_HIERARCHY BOOL NOT NULL DEFAULT FALSE
);
`,
desc: sqlbase.TableDescriptor{Name: "table_privileges", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "GRANTOR", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "GRANTEE", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_CATALOG", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x5, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "PRIVILEGE_TYPE", ID: 0x6, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "IS_GRANTABLE", ID: 0x7, Type: sqlbase.ColumnType{Kind: 0}, DefaultExpr: &falseStr}, {Name: "WITH_HIERARCHY", ID: 0x8, Type: sqlbase.ColumnType{Kind: 0}, DefaultExpr: &falseStr}}, NextColumnID: 0x9, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x1}, NextIndexID: 0x2, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
desc: sqlbase.TableDescriptor{Name: "table_privileges", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "GRANTOR", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "GRANTEE", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_CATALOG", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x5, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "PRIVILEGE_TYPE", ID: 0x6, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "IS_GRANTABLE", ID: 0x7, Type: sqlbase.ColumnType{Kind: 0}, DefaultExpr: &falseStr}, {Name: "WITH_HIERARCHY", ID: 0x8, Type: sqlbase.ColumnType{Kind: 0}, DefaultExpr: &falseStr}}, NextColumnID: 0x9, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x0}, NextIndexID: 0x0, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
Expand Down Expand Up @@ -202,7 +202,7 @@ CREATE TABLE information_schema.schemata (
DEFAULT_CHARACTER_SET_NAME STRING NOT NULL DEFAULT '',
SQL_PATH STRING
);`,
desc: sqlbase.TableDescriptor{Name: "schemata", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "CATALOG_NAME", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "SCHEMA_NAME", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "DEFAULT_CHARACTER_SET_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "SQL_PATH", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, Nullable: true, DefaultExpr: (*string)(nil)}}, NextColumnID: 0x5, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x1}, NextIndexID: 0x2, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
desc: sqlbase.TableDescriptor{Name: "schemata", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "CATALOG_NAME", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "SCHEMA_NAME", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "DEFAULT_CHARACTER_SET_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "SQL_PATH", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, Nullable: true, DefaultExpr: (*string)(nil)}}, NextColumnID: 0x5, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x0}, NextIndexID: 0x0, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachDatabaseDesc(p, func(db *sqlbase.DatabaseDescriptor) error {
return addRow(
Expand All @@ -225,7 +225,7 @@ CREATE TABLE information_schema.schema_privileges (
IS_GRANTABLE BOOL NOT NULL DEFAULT FALSE
);
`,
desc: sqlbase.TableDescriptor{Name: "schema_privileges", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "GRANTEE", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_CATALOG", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "PRIVILEGE_TYPE", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "IS_GRANTABLE", ID: 0x5, Type: sqlbase.ColumnType{Kind: 0}, DefaultExpr: &falseStr}}, NextColumnID: 0x6, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x1}, NextIndexID: 0x2, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
desc: sqlbase.TableDescriptor{Name: "schema_privileges", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "GRANTEE", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_CATALOG", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "PRIVILEGE_TYPE", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "IS_GRANTABLE", ID: 0x5, Type: sqlbase.ColumnType{Kind: 0}, DefaultExpr: &falseStr}}, NextColumnID: 0x6, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x0}, NextIndexID: 0x0, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachDatabaseDesc(p, func(db *sqlbase.DatabaseDescriptor) error {
for _, u := range db.Privileges.Show() {
Expand Down Expand Up @@ -264,7 +264,7 @@ CREATE TABLE information_schema.table_constraints (
TABLE_NAME STRING NOT NULL DEFAULT '',
CONSTRAINT_TYPE STRING NOT NULL DEFAULT ''
);`,
desc: sqlbase.TableDescriptor{Name: "table_constraints", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "CONSTRAINT_CATALOG", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CONSTRAINT_SCHEMA", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CONSTRAINT_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x5, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CONSTRAINT_TYPE", ID: 0x6, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}}, NextColumnID: 0x7, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x1}, NextIndexID: 0x2, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
desc: sqlbase.TableDescriptor{Name: "table_constraints", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "CONSTRAINT_CATALOG", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CONSTRAINT_SCHEMA", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CONSTRAINT_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x5, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "CONSTRAINT_TYPE", ID: 0x6, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}}, NextColumnID: 0x7, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x0}, NextIndexID: 0x0, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
Expand All @@ -273,7 +273,7 @@ CREATE TABLE information_schema.table_constraints (
typ parser.Datum
}
var constraints []constraint
if table.HasPrimaryKey() {
if table.IsPhysicalTable() {
constraints = append(constraints, constraint{
name: table.PrimaryIndex.Name,
typ: constraintTypePrimaryKey,
Expand Down Expand Up @@ -329,7 +329,7 @@ CREATE TABLE information_schema.tables (
TABLE_TYPE STRING NOT NULL DEFAULT '',
VERSION INT
);`,
desc: sqlbase.TableDescriptor{Name: "tables", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "TABLE_CATALOG", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_TYPE", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "VERSION", ID: 0x5, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}}, NextColumnID: 0x6, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x1}, NextIndexID: 0x2, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
desc: sqlbase.TableDescriptor{Name: "tables", ID: 0xffffffff, Version: 0x1, Columns: []sqlbase.ColumnDescriptor{{Name: "TABLE_CATALOG", ID: 0x1, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_SCHEMA", ID: 0x2, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_NAME", ID: 0x3, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "TABLE_TYPE", ID: 0x4, Type: sqlbase.ColumnType{Kind: 7}, DefaultExpr: &emptyStr}, {Name: "VERSION", ID: 0x5, Type: sqlbase.ColumnType{Kind: 1}, Nullable: true, DefaultExpr: (*string)(nil)}}, NextColumnID: 0x6, PrimaryIndex: sqlbase.IndexDescriptor{Name: "", ID: 0x0}, NextIndexID: 0x0, Privileges: emptyPrivileges, NextMutationID: 0x1, FormatVersion: 0x3},
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
Expand Down
5 changes: 0 additions & 5 deletions sql/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ func (s LeaseStore) Acquire(
if tableDesc == nil {
return nil, errors.Errorf("ID %d is not a table", tableID)
}
// TODO(a-robinson): Allow taking leases on view descriptors once this
// method's callers know how to handle views.
if !tableDesc.IsTable() {
return nil, errors.Errorf("ID %d is a %s, not a table", tableID, tableDesc.TypeName())
}
if err := filterTableState(tableDesc); err != nil {
return nil, err
}
Expand Down
17 changes: 2 additions & 15 deletions sql/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,26 +251,13 @@ func (n *scanNode) ExplainTypes(regTypes func(string, string)) {
}
}

// Initializes a scanNode with a tableName. Returns the table or index name that can be used for
// fully-qualified columns if an alias is not specified.
// Initializes a scanNode with a table descriptor.
func (n *scanNode) initTable(
p *planner,
tableName *parser.TableName,
desc *sqlbase.TableDescriptor,
indexHints *parser.IndexHints,
scanVisibility scanVisibility,
) error {
descFunc := p.getTableLease
if p.asOf {
// AS OF SYSTEM TIME queries need to fetch the table descriptor at the
// specified time, and never lease anything. The proto transaction already
// has its timestamps set correctly so mustGetTableDesc will fetch with the
// correct timestamp.
descFunc = p.mustGetTableDesc
}
desc, err := descFunc(tableName)
if err != nil {
return err
}
n.desc = *desc

if err := p.checkPrivilege(&n.desc, privilege.SELECT); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion sql/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p *planner) ShowCreateTable(n *parser.ShowCreateTable) (planNode, error) {
if col.DefaultExpr != nil {
fmt.Fprintf(&buf, " DEFAULT %s", *col.DefaultExpr)
}
if desc.HasPrimaryKey() && desc.PrimaryIndex.ColumnIDs[0] == col.ID {
if desc.IsPhysicalTable() && desc.PrimaryIndex.ColumnIDs[0] == col.ID {
// Only set primary if the primary key is on a visible column (not rowid).
primary = fmt.Sprintf(",\n\tCONSTRAINT %s PRIMARY KEY (%s)",
quoteNames(desc.PrimaryIndex.Name),
Expand Down
Loading