From 2330221d2968a3974b6b683b6e03c898a73aea01 Mon Sep 17 00:00:00 2001 From: MaBo Date: Tue, 1 Nov 2016 19:27:40 +0800 Subject: [PATCH] sql: Add information_schema.views --- pkg/sql/information_schema.go | 46 +++++++++++++++++++++++++++++ pkg/sql/testdata/information_schema | 29 ++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/pkg/sql/information_schema.go b/pkg/sql/information_schema.go index d740ac043eb6..464a5743012d 100644 --- a/pkg/sql/information_schema.go +++ b/pkg/sql/information_schema.go @@ -35,6 +35,7 @@ var informationSchema = virtualSchema{ informationSchemaTableConstraintTable, informationSchemaTablePrivileges, informationSchemaTablesTable, + informationSchemaViewsTable, }, } @@ -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 diff --git a/pkg/sql/testdata/information_schema b/pkg/sql/testdata/information_schema index 5d1eca65da9f..d2cbbbea540e 100644 --- a/pkg/sql/testdata/information_schema +++ b/pkg/sql/testdata/information_schema @@ -112,6 +112,7 @@ statistics table_constraints table_privileges tables +views query TT colnames SHOW CREATE TABLE information_schema.tables @@ -342,6 +343,7 @@ statistics table_constraints table_privileges tables +views abc xyz pg_am @@ -370,6 +372,7 @@ SELECT table_name FROM information_schema.tables WHERE table_name > 'n' ORDER BY ---- zones xyz +views users ui tables @@ -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 @@ -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 @@ -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 @@ -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 + +