@@ -20,6 +20,7 @@ import (
2020 "fmt"
2121 "time"
2222
23+ "github.com/cockroachdb/cockroach/pkg/sql/privilege"
2324 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2425 "github.com/cockroachdb/cockroach/pkg/sql/sem/types"
2526 "github.com/cockroachdb/cockroach/pkg/util/treeprinter"
@@ -36,6 +37,93 @@ type ColumnName string
3637// index, even if it meant adding a hidden unique rowid column.
3738const PrimaryIndex = 0
3839
40+ // Catalog is an interface to a database catalog, exposing only the information
41+ // needed by the query optimizer.
42+ type Catalog interface {
43+ // ResolveDataSource locates a data source with the given name and returns it.
44+ // If no such data source exists, then ResolveDataSource returns an error. As
45+ // a side effect, the name parameter is updated to be fully qualified if it
46+ // was not before (i.e. to include catalog and schema names).
47+ ResolveDataSource (ctx context.Context , name * tree.TableName ) (DataSource , error )
48+
49+ // ResolveDataSourceByID is similar to ResolveDataSource, except that it
50+ // locates a data source by its unique identifier in the database. This id
51+ // is stable as long as the data source exists.
52+ ResolveDataSourceByID (ctx context.Context , dataSourceID int64 ) (DataSource , error )
53+ }
54+
55+ // DataSource is an interface to a database object that provides rows, like a
56+ // table, a view, or a sequence.
57+ type DataSource interface {
58+ // Name returns the fully normalized, fully qualified, and fully resolved
59+ // name of the data source. The ExplicitCatalog and ExplicitSchema fields
60+ // will always be true, since all parts of the name are always specified.
61+ Name () * tree.TableName
62+
63+ // CheckPrivilege verifies that the current user has the given privilege on
64+ // this data source. If not, then CheckPrivilege returns an error.
65+ CheckPrivilege (ctx context.Context , priv privilege.Kind ) error
66+ }
67+
68+ // Table is an interface to a database table, exposing only the information
69+ // needed by the query optimizer.
70+ type Table interface {
71+ DataSource
72+
73+ // IsVirtualTable returns true if this table is a special system table that
74+ // constructs its rows "on the fly" when it's queried. An example is the
75+ // information_schema tables.
76+ IsVirtualTable () bool
77+
78+ // ColumnCount returns the number of columns in the table.
79+ ColumnCount () int
80+
81+ // Column returns a Column interface to the column at the ith ordinal
82+ // position within the table, where i < ColumnCount.
83+ Column (i int ) Column
84+
85+ // LookupColumnOrdinal returns the ordinal of the column with the given ID.
86+ // Note that this takes the internal column ID, and has no relation to
87+ // ColumnIDs in the optimizer.
88+ LookupColumnOrdinal (colID uint32 ) (int , error )
89+
90+ // IndexCount returns the number of indexes defined on this table. This
91+ // includes the primary index, so the count is always >= 1.
92+ IndexCount () int
93+
94+ // Index returns the ith index, where i < IndexCount. The table's primary
95+ // index is always the 0th index, and is always present (use the
96+ // opt.PrimaryIndex to select it). The primary index corresponds to the
97+ // table's primary key. If a primary key was not explicitly specified, then
98+ // the system implicitly creates one based on a hidden rowid column.
99+ Index (i int ) Index
100+
101+ // StatisticCount returns the number of statistics available for the table.
102+ StatisticCount () int
103+
104+ // Statistic returns the ith statistic, where i < StatisticCount.
105+ Statistic (i int ) TableStatistic
106+ }
107+
108+ // View is an interface to a database view, exposing only the information needed
109+ // by the query optimizer.
110+ type View interface {
111+ DataSource
112+
113+ // Query returns the SQL text that specifies the SELECT query that constitutes
114+ // this view.
115+ Query () string
116+
117+ // ColumnNameCount returns the number of column names specified in the view.
118+ // If zero, then the columns are not aliased. Otherwise, it will match the
119+ // number of columns in the view.
120+ ColumnNameCount () int
121+
122+ // ColumnNames returns the name of the column at the ith ordinal position
123+ // within the view, where i < ColumnNameCount.
124+ ColumnName (i int ) tree.Name
125+ }
126+
39127// Column is an interface to a table column, exposing only the information
40128// needed by the query optimizer.
41129type Column interface {
@@ -175,65 +263,10 @@ type TableStatistic interface {
175263 // TODO(radu): add Histogram().
176264}
177265
178- // Table is an interface to a database table, exposing only the information
179- // needed by the query optimizer.
180- type Table interface {
181- // TabName returns the fully normalized, fully qualified, and fully resolved
182- // name of the table. The ExplicitCatalog and ExplicitSchema fields will
183- // always be true, since both names are always specified.
184- TabName () * tree.TableName
185-
186- // IsVirtualTable returns true if this table is a special system table that
187- // constructs its rows "on the fly" when it's queried. An example is the
188- // information_schema tables.
189- IsVirtualTable () bool
190-
191- // ColumnCount returns the number of columns in the table.
192- ColumnCount () int
193-
194- // Column returns a Column interface to the column at the ith ordinal
195- // position within the table, where i < ColumnCount.
196- Column (i int ) Column
197-
198- // LookupColumnOrdinal returns the ordinal of the column with the given ID.
199- // Note that this takes the internal column ID, and has no relation to
200- // ColumnIDs in the optimizer.
201- LookupColumnOrdinal (colID uint32 ) (int , error )
202-
203- // IndexCount returns the number of indexes defined on this table. This
204- // includes the primary index, so the count is always >= 1.
205- IndexCount () int
206-
207- // Index returns the ith index, where i < IndexCount. The table's primary
208- // index is always the 0th index, and is always present (use the
209- // opt.PrimaryIndex to select it). The primary index corresponds to the
210- // table's primary key. If a primary key was not explicitly specified, then
211- // the system implicitly creates one based on a hidden rowid column.
212- Index (i int ) Index
213-
214- // StatisticCount returns the number of statistics available for the table.
215- StatisticCount () int
216-
217- // Statistic returns the ith statistic, where i < StatisticCount.
218- Statistic (i int ) TableStatistic
219- }
220-
221- // Catalog is an interface to a database catalog, exposing only the information
222- // needed by the query optimizer.
223- type Catalog interface {
224- // FindTable returns a Table interface for the database table matching the
225- // given table name. Returns an error if the table does not exist.
226- FindTable (ctx context.Context , name * tree.TableName ) (Table , error )
227-
228- // FindTableByID returns a Table interface for the database table
229- // matching the given table ID. Returns an error if the table does not exist.
230- FindTableByID (ctx context.Context , tableID int64 ) (Table , error )
231- }
232-
233266// FormatCatalogTable nicely formats a catalog table using a treeprinter for
234267// debugging and testing.
235268func FormatCatalogTable (tab Table , tp treeprinter.Node ) {
236- child := tp .Childf ("TABLE %s" , tab .TabName ().TableName )
269+ child := tp .Childf ("TABLE %s" , tab .Name ().TableName )
237270
238271 var buf bytes.Buffer
239272 for i := 0 ; i < tab .ColumnCount (); i ++ {
@@ -289,3 +322,23 @@ func formatColumn(col Column, buf *bytes.Buffer) {
289322 fmt .Fprintf (buf , " (hidden)" )
290323 }
291324}
325+
326+ // FormatCatalogView nicely formats a catalog view using a treeprinter for
327+ // debugging and testing.
328+ func FormatCatalogView (view View , tp treeprinter.Node ) {
329+ var buf bytes.Buffer
330+ if view .ColumnNameCount () > 0 {
331+ buf .WriteString (" (" )
332+ for i := 0 ; i < view .ColumnNameCount (); i ++ {
333+ if i != 0 {
334+ buf .WriteString (", " )
335+ }
336+ buf .WriteString (string (view .ColumnName (i )))
337+ }
338+ buf .WriteString (")" )
339+ }
340+
341+ child := tp .Childf ("VIEW %s%s" , view .Name ().TableName , buf .String ())
342+
343+ child .Child (view .Query ())
344+ }
0 commit comments