Skip to content

Commit ea8c9b0

Browse files
authored
add view column information to information_schema.columns (#2695)
1 parent eeb68a8 commit ea8c9b0

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

enginetest/queries/information_schema_queries.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,18 @@ FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'mydb'`,
11581158
SetUpScript: []string{
11591159
"USE foo",
11601160
"drop table othertable",
1161-
"CREATE TABLE t (i int)",
1162-
"CREATE VIEW v as select * from t",
1161+
"CREATE TABLE t (i int primary key, j int)",
1162+
"CREATE VIEW v as select i + 1, j * 2, mod(i, j) from t",
11631163
},
11641164
Assertions: []ScriptTestAssertion{
11651165
{
11661166
Query: "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'foo'",
11671167
Expected: []sql.Row{
1168-
{"def", "foo", "t", "i", uint32(1), nil, "YES", "int", nil, nil, int64(10), int64(0), nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1169-
{"def", "foo", "v", "", uint32(0), nil, "", nil, nil, nil, nil, nil, nil, "", "", "", "", "", "select", "", "", nil},
1168+
{"def", "foo", "t", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1169+
{"def", "foo", "t", "j", uint32(2), nil, "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1170+
{"def", "foo", "v", "i + 1", uint32(1), nil, "NO", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1171+
{"def", "foo", "v", "j * 2", uint32(2), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1172+
{"def", "foo", "v", "mod(i, j)", uint32(3), nil, "YES", "decimal", nil, nil, 10, 0, nil, nil, nil, "decimal(10,0)", "", "", "insert,references,select,update", "", "", nil},
11701173
},
11711174
},
11721175
},

sql/information_schema/columns_table.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828

2929
"github.com/dolthub/go-mysql-server/sql"
3030
"github.com/dolthub/go-mysql-server/sql/mysql_db"
31+
"github.com/dolthub/go-mysql-server/sql/plan"
32+
"github.com/dolthub/go-mysql-server/sql/planbuilder"
3133
"github.com/dolthub/go-mysql-server/sql/transform"
3234
"github.com/dolthub/go-mysql-server/sql/types"
3335
)
@@ -235,7 +237,7 @@ func columnsRowIter(ctx *sql.Context, catalog sql.Catalog, allColsWithDefaultVal
235237
}
236238
rows = append(rows, rs...)
237239

238-
rs, err = getRowsFromViews(ctx, db)
240+
rs, err = getRowsFromViews(ctx, catalog, db, privSet, globalPrivSetMap)
239241
if err != nil {
240242
return nil, err
241243
}
@@ -364,41 +366,30 @@ func getRowsFromTable(ctx *sql.Context, db DbWithNames, t sql.Table, privSetDb s
364366
}
365367

366368
// getRowsFromViews returns array or rows for columns for all views for given database.
367-
func getRowsFromViews(ctx *sql.Context, db DbWithNames) ([]sql.Row, error) {
369+
func getRowsFromViews(ctx *sql.Context, catalog sql.Catalog, db DbWithNames, privSet sql.PrivilegeSet, privSetMap map[string]struct{}) ([]sql.Row, error) {
368370
var rows []sql.Row
369-
// TODO: View Definition is lacking information to properly fill out these table
370-
// TODO: Should somehow get reference to table(s) view is referencing
371-
// TODO: Each column that view references should also show up as unique entries as well
372371
views, err := ViewsInDatabase(ctx, db.Database)
373372
if err != nil {
374373
return nil, err
375374
}
376-
375+
privSetDb := privSet.Database(db.Database.Name())
377376
for _, view := range views {
378-
rows = append(rows, sql.Row{
379-
db.CatalogName, // table_catalog
380-
db.SchemaName, // table_schema
381-
view.Name, // table_name
382-
"", // column_name
383-
uint32(0), // ordinal_position
384-
nil, // column_default
385-
"", // is_nullable
386-
nil, // data_type
387-
nil, // character_maximum_length
388-
nil, // character_octet_length
389-
nil, // numeric_precision
390-
nil, // numeric_scale
391-
nil, // datetime_precision
392-
"", // character_set_name
393-
"", // collation_name
394-
"", // column_type
395-
"", // column_key
396-
"", // extra
397-
"select", // privileges
398-
"", // column_comment
399-
"", // generation_expression
400-
nil, // srs_id
401-
})
377+
node, _, err := planbuilder.Parse(ctx, catalog, view.CreateViewStatement)
378+
if err != nil {
379+
continue // sometimes views contains views from other databases
380+
}
381+
createViewNode, ok := node.(*plan.CreateView)
382+
if !ok {
383+
continue
384+
}
385+
privSetTbl := privSetDb.Table(view.Name)
386+
curPrivSetMap := getCurrentPrivSetMapForColumn(privSetDb.ToSlice(), privSetMap)
387+
for i, col := range createViewNode.Definition.Schema() {
388+
r := getRowFromColumn(ctx, i, col, db.CatalogName, db.SchemaName, view.Name, "", privSetTbl, curPrivSetMap)
389+
if r != nil {
390+
rows = append(rows, r)
391+
}
392+
}
402393
}
403394

404395
return rows, nil

0 commit comments

Comments
 (0)