Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Views global routing #17633

Merged
merged 11 commits into from
Feb 5, 2025
Prev Previous commit
Next Next commit
add views to global tables for sharded keyspace
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Jan 29, 2025
commit aedb3e796ae243fd648e4ff2c0b9d089dc343e7e
27 changes: 16 additions & 11 deletions go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,13 @@ func AddAdditionalGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) {
// Collect valid uniquely named tables from unsharded keyspaces.
for ksname, ks := range source.Keyspaces {
ksvschema := vschema.Keyspaces[ksname]
// Ignore sharded keyspaces and those flagged for explicit routing.
if ks.RequireExplicitRouting || ks.Sharded {
// Ignore keyspaces marked for explicit routing.
if ks.RequireExplicitRouting {
continue
}
for tname, table := range ksvschema.Tables {
// Views does not require routing, so this can be added to global tables
// for sharded keyspace as well
for tname, table := range ksvschema.Views {
// Ignore tables already global (i.e. if specified in the vschema of an unsharded keyspace) or ambiguous.
if _, found := vschema.globalTables[tname]; !found {
_, ok := newTables[tname]
Expand All @@ -505,7 +507,11 @@ func AddAdditionalGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) {
}
}
}
for tname, table := range ksvschema.Views {
// Sharded tables needs vindex information, adding to global table will not help.
if ks.Sharded {
continue
}
for tname, table := range ksvschema.Tables {
// Ignore tables already global (i.e. if specified in the vschema of an unsharded keyspace) or ambiguous.
if _, found := vschema.globalTables[tname]; !found {
_, ok := newTables[tname]
Expand Down Expand Up @@ -1346,25 +1352,24 @@ func (vschema *VSchema) FindTable(keyspace, tablename string) (*BaseTable, error
//
// - constructUnshardedIfNotFound is not requested, than no table is returned.
// - constructUnshardedIfNotFound is requested, and there is only one keyspace,
// and that keyspace is unsharded, then a *Table representing that table is
// and that keyspace is unsharded, then a BaseTable representing that table is
// returned.
func (vschema *VSchema) findGlobalTable(
tablename string,
constructUnshardedIfNotFound bool,
) (Table, error) {
table, ok := vschema.globalTables[tablename]
if table != nil {
return table, nil
}

if len(vschema.Keyspaces) == 1 {
for _, ks := range vschema.Keyspaces {
table := ks.findTable(tablename, constructUnshardedIfNotFound)
return table, nil
}
}

table, ok := vschema.globalTables[tablename]

if table != nil {
return table, nil
}

if ok {
return nil, vterrors.Errorf(
vtrpcpb.Code_FAILED_PRECONDITION,
Expand Down
38 changes: 37 additions & 1 deletion go/vt/vtgate/vschema_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vtgate
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/test/utils"
Expand Down Expand Up @@ -515,6 +516,40 @@ func TestVSchemaUDFsUpdate(t *testing.T) {
utils.MustMatch(t, vs, vm.currentVschema, "currentVschema does not match Vschema")
}

// TestVSchemaViewsUpdate tests that the views are updated in the VSchema.
func TestVSchemaViewsUpdate(t *testing.T) {
vm := &VSchemaManager{}
var vs *vindexes.VSchema
vm.subscriber = func(vschema *vindexes.VSchema, _ *VSchemaStats) {
vs = vschema
vs.ResetCreated()
}
vm.schema = &fakeSchema{v: map[string]sqlparser.TableStatement{
"v1": &sqlparser.Select{
From: sqlparser.TableExprs{sqlparser.NewAliasedTableExpr(sqlparser.NewTableName("t1"), "")},
SelectExprs: sqlparser.SelectExprs{sqlparser.NewAliasedExpr(sqlparser.NewIntLiteral("1"), "")},
},
"v2": &sqlparser.Select{
From: sqlparser.TableExprs{sqlparser.NewAliasedTableExpr(sqlparser.NewTableName("t2"), "")},
SelectExprs: sqlparser.SelectExprs{sqlparser.NewAliasedExpr(sqlparser.NewIntLiteral("2"), "")},
},
}}
vm.VSchemaUpdate(&vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{
"ks": {Sharded: true},
},
}, nil)

// find in views map
assert.NotNil(t, vs.FindView("ks", "v1"))
assert.NotNil(t, vs.FindView("ks", "v2"))
// find in global table
assert.NotNil(t, vs.FindView("", "v1"))
assert.NotNil(t, vs.FindView("", "v2"))

utils.MustMatch(t, vs, vm.currentVschema, "currentVschema does not match Vschema")
}

func TestMarkErrorIfCyclesInFk(t *testing.T) {
ksName := "ks"
keyspace := &vindexes.Keyspace{
Expand Down Expand Up @@ -893,6 +928,7 @@ func makeTestSrvVSchema(ks string, sharded bool, tbls map[string]*vschemapb.Tabl

type fakeSchema struct {
t map[string]*vindexes.TableInfo
v map[string]sqlparser.TableStatement
udfs []string
}

Expand All @@ -901,7 +937,7 @@ func (f *fakeSchema) Tables(string) map[string]*vindexes.TableInfo {
}

func (f *fakeSchema) Views(string) map[string]sqlparser.TableStatement {
return nil
return f.v
}
func (f *fakeSchema) UDFs(string) []string { return f.udfs }

Expand Down