Skip to content

Commit

Permalink
Merge pull request #10288 from mtMabo/information_schema
Browse files Browse the repository at this point in the history
sql: Add information_schema.views
  • Loading branch information
nvanbenschoten authored Nov 2, 2016
2 parents 0b3d611 + 2330221 commit 7915024
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var informationSchema = virtualSchema{
informationSchemaTableConstraintTable,
informationSchemaTablePrivileges,
informationSchemaTablesTable,
informationSchemaViewsTable,
},
}

Expand Down Expand Up @@ -473,6 +474,51 @@ CREATE TABLE information_schema.tables (
},
}

var informationSchemaViewsTable = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.views (
TABLE_CATALOG STRING NOT NULL DEFAULT '',
TABLE_SCHEMA STRING NOT NULL DEFAULT '',
TABLE_NAME STRING NOT NULL DEFAULT '',
VIEW_DEFINITION STRING NOT NULL DEFAULT '',
CHECK_OPTION STRING NOT NULL DEFAULT '',
IS_UPDATABLE BOOL NOT NULL DEFAULT FALSE,
IS_INSERTABLE_INTO BOOL NOT NULL DEFAULT FALSE,
IS_TRIGGER_UPDATABLE BOOL NOT NULL DEFAULT FALSE,
IS_TRIGGER_DELETABLE BOOL NOT NULL DEFAULT FALSE,
IS_TRIGGER_INSERTABLE_INTO BOOL NOT NULL DEFAULT FALSE
);`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
if !table.IsView() {
return nil
}
// Note that the view query printed will not include any column aliases
// specified outside the initial view query into the definition returned,
// unlike Postgres. For example, for the view created via
// `CREATE VIEW (a) AS SELECT b FROM foo`
// we'll only print `SELECT b FROM foo` as the view definition here,
// while Postgres would more accurately print `SELECT b AS a FROM foo`.
// TODO(a-robinson): Insert column aliases into view query once we
// have a semantic query representation to work with (#10083).
return addRow(
defString, // table_catalog
parser.NewDString(db.Name), // table_schema
parser.NewDString(table.Name), // table_name
parser.NewDString(table.ViewQuery), // view_definition
parser.DNull, // check_option
parser.DNull, // is_updatable
parser.DNull, // is_insertable_into
parser.DNull, // is_trigger_updatable
parser.DNull, // is_trigger_deletable
parser.DNull, // is_trigger_insertable_into
)
},
)
},
}

type sortedDBDescs []*sqlbase.DatabaseDescriptor

// sortedDBDescs implements sort.Interface. It sorts a slice of DatabaseDescriptors
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/testdata/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ statistics
table_constraints
table_privileges
tables
views

query TT colnames
SHOW CREATE TABLE information_schema.tables
Expand Down Expand Up @@ -342,6 +343,7 @@ statistics
table_constraints
table_privileges
tables
views
abc
xyz
pg_am
Expand Down Expand Up @@ -370,6 +372,7 @@ SELECT table_name FROM information_schema.tables WHERE table_name > 'n' ORDER BY
----
zones
xyz
views
users
ui
tables
Expand Down Expand Up @@ -405,6 +408,7 @@ def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
def information_schema views SYSTEM VIEW 1
def other_db abc VIEW 1
def other_db xyz BASE TABLE 2
def pg_catalog pg_am SYSTEM VIEW 1
Expand Down Expand Up @@ -451,6 +455,7 @@ def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
def information_schema views SYSTEM VIEW 1
def pg_catalog pg_am SYSTEM VIEW 1
def pg_catalog pg_attrdef SYSTEM VIEW 1
def pg_catalog pg_attribute SYSTEM VIEW 1
Expand Down Expand Up @@ -483,6 +488,7 @@ def information_schema statistics SYSTEM VIEW 1
def information_schema table_constraints SYSTEM VIEW 1
def information_schema table_privileges SYSTEM VIEW 1
def information_schema tables SYSTEM VIEW 1
def information_schema views SYSTEM VIEW 1
def other_db xyz BASE TABLE 6
def pg_catalog pg_am SYSTEM VIEW 1
def pg_catalog pg_attrdef SYSTEM VIEW 1
Expand Down Expand Up @@ -700,5 +706,28 @@ def other_db teststatis true other_db idx_cd 1
def other_db teststatis true other_db idx_cd 2 d NULL NULL ASC false
def other_db teststatis true other_db primary 1 id NULL NULL ASC false

# Verify information_schema.views
statement ok
CREATE VIEW other_db.v_xyz AS SELECT i FROM other_db.xyz

query TTTTT colnames
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION, CHECK_OPTION
FROM information_schema.views
WHERE TABLE_NAME='v_xyz'
----
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME VIEW_DEFINITION CHECK_OPTION
def other_db v_xyz SELECT i FROM other_db.xyz NULL

query BBBBB colnames
SELECT IS_UPDATABLE, IS_INSERTABLE_INTO, IS_TRIGGER_UPDATABLE, IS_TRIGGER_DELETABLE, IS_TRIGGER_INSERTABLE_INTO
FROM information_schema.views
WHERE TABLE_NAME='v_xyz'
----
IS_UPDATABLE IS_INSERTABLE_INTO IS_TRIGGER_UPDATABLE IS_TRIGGER_DELETABLE IS_TRIGGER_INSERTABLE_INTO
NULL NULL NULL NULL NULL


statement ok
DROP DATABASE other_db


0 comments on commit 7915024

Please sign in to comment.