From 4bad8e5a7f8c42158cba2a762f77bbe049bd7495 Mon Sep 17 00:00:00 2001 From: Manan Gupta Date: Thu, 1 Aug 2024 10:07:30 +0530 Subject: [PATCH] feat: rebuild global tables after schema has augmented the vschema Signed-off-by: Manan Gupta --- go/vt/vtgate/vindexes/vschema.go | 11 ++++++++--- go/vt/vtgate/vschema_manager.go | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/go/vt/vtgate/vindexes/vschema.go b/go/vt/vtgate/vindexes/vschema.go index eba3ac49969..86b2ccca42e 100644 --- a/go/vt/vtgate/vindexes/vschema.go +++ b/go/vt/vtgate/vindexes/vschema.go @@ -331,9 +331,9 @@ func BuildVSchema(source *vschemapb.SrvVSchema, parser *sqlparser.Parser) (vsche created: time.Now(), } buildKeyspaces(source, vschema, parser) - // buildGlobalTables before buildReferences so that buildReferences can + // BuildGlobalTables before buildReferences so that buildReferences can // resolve sources which reference global tables. - buildGlobalTables(source, vschema) + BuildGlobalTables(source, vschema) buildReferences(source, vschema) buildRoutingRule(source, vschema, parser) buildShardRoutingRule(source, vschema) @@ -437,7 +437,12 @@ func (vschema *VSchema) AddUDF(ksname, udfName string) error { return nil } -func buildGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) { +// BuildGlobalTables rebuilds the global tables map that is used for global routing. +// If a table name is unique across all keyspaces, then Vitess allows the users to use the table +// without needing to select a database first. +func BuildGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) { + // Reinitialize the global Tables map to restart the building process. + vschema.globalTables = make(map[string]*Table) for ksname, ks := range source.Keyspaces { ksvschema := vschema.Keyspaces[ksname] // If the keyspace requires explicit routing, don't include any of diff --git a/go/vt/vtgate/vschema_manager.go b/go/vt/vtgate/vschema_manager.go index 2b6761f4a8e..2e8b2231c77 100644 --- a/go/vt/vtgate/vschema_manager.go +++ b/go/vt/vtgate/vschema_manager.go @@ -196,6 +196,8 @@ func (vm *VSchemaManager) buildAndEnhanceVSchema(v *vschemapb.SrvVSchema) *vinde // We mark the keyspaces that have foreign key management in Vitess and have cyclic foreign keys // to have an error. This makes all queries against them to fail. markErrorIfCyclesInFk(vschema) + // We build the global routing table again because schema tracking might have added more tables to the vschema. + vindexes.BuildGlobalTables(v, vschema) } return vschema }