Skip to content

Commit

Permalink
add view as table to vschema, if exists then update the type to view
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Jan 9, 2025
1 parent 192b16c commit 43718a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const (
TypeTable = ""
TypeSequence = "sequence"
TypeReference = "reference"
TypeView = "view"
)

// VSchema represents the denormalized version of SrvVSchema,
Expand Down Expand Up @@ -439,7 +440,7 @@ func (vschema *VSchema) AddView(ksname, viewName, query string, parser *sqlparse
}
ks.Views[viewName] = selectStmt
t := &Table{
Type: "View",
Type: TypeView,
Name: sqlparser.NewIdentifierCS(viewName),
Keyspace: ks.Keyspace,
ColumnListAuthoritative: true,
Expand Down
23 changes: 18 additions & 5 deletions go/vt/vtgate/vschema_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,34 @@ func (vm *VSchemaManager) buildAndEnhanceVSchema(v *vschemapb.SrvVSchema) *vinde

func (vm *VSchemaManager) updateFromSchema(vschema *vindexes.VSchema) {
for ksName, ks := range vschema.Keyspaces {
vm.updateTableInfo(vschema, ks, ksName)
vm.updateViewInfo(ks, ksName)
vm.updateTableInfo(vschema, ks, ksName)
vm.updateUDFsInfo(ks, ksName)
}
}

func (vm *VSchemaManager) updateViewInfo(ks *vindexes.KeyspaceSchema, ksName string) {
views := vm.schema.Views(ksName)
if views != nil {
ks.Views = make(map[string]sqlparser.SelectStatement, len(views))
for name, def := range views {
ks.Views[name] = sqlparser.Clone(def)
if views == nil {
return
}
ks.Views = make(map[string]sqlparser.SelectStatement, len(views))
for name, def := range views {
ks.Views[name] = sqlparser.Clone(def)
vTbl, ok := ks.Tables[name]
if ok {
vTbl.Type = vindexes.TypeView
} else {
// Adding view to the VSchema as a table.
ks.Tables[name] = &vindexes.Table{
Type: vindexes.TypeView,
Name: sqlparser.NewIdentifierCS(name),
Keyspace: ks.Keyspace,
}
}
}
}

func (vm *VSchemaManager) updateTableInfo(vschema *vindexes.VSchema, ks *vindexes.KeyspaceSchema, ksName string) {
m := vm.schema.Tables(ksName)
// Before we add the foreign key definitions in the tables, we need to make sure that all the tables
Expand Down

0 comments on commit 43718a4

Please sign in to comment.