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

Route explain tab plan to the proper Keyspace #10027

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,13 @@ func TestFilterAfterLeftJoin(t *testing.T) {
query := "select /*vt+ PLANNER=gen4 */ A.id1, A.id2 from t1 as A left join t1 as B on A.id1 = B.id2 WHERE B.id1 IS NULL"
utils.AssertMatches(t, conn, query, `[[INT64(1) INT64(10)]]`)
}

func TestDescribeVindex(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

utils.AssertContainsError(t, conn, "describe hash", "'vt_ks.hash' doesn't exist")
}
9 changes: 9 additions & 0 deletions go/vt/vtgate/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,15 @@ func TestExecutorShowVitessMigrations(t *testing.T) {
assert.Contains(t, sbc2.StringQueries(), "SELECT * FROM _vt.schema_migrations")
}

func TestExecutorDescHash(t *testing.T) {
executor, _, _, _ := createExecutorEnv()
showQuery := "desc hash_index"
session := NewSafeSession(&vtgatepb.Session{TargetString: "TestExecutor"})
ctx := context.Background()
_, err := executor.Execute(ctx, "", session, showQuery, nil)
require.NoError(t, err)
}

func exec(executor *Executor, session *SafeSession, sql string) (*sqltypes.Result, error) {
return executor.Execute(context.Background(), "TestExecute", session, sql, nil)
}
Expand Down
12 changes: 10 additions & 2 deletions go/vt/vtgate/planbuilder/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func buildExplainPlan(stmt sqlparser.Explain, reservedVars *sqlparser.ReservedVa
}

func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema) (engine.Primitive, error) {
table, _, _, _, destination, err := vschema.FindTableOrVindex(explain.Table)
_, _, ks, _, destination, err := vschema.FindTableOrVindex(explain.Table)
if err != nil {
return nil, err
}
Expand All @@ -55,8 +55,16 @@ func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema)
destination = key.DestinationAnyShard{}
}

keyspace, err := vschema.FindKeyspace(ks)
if err != nil {
return nil, err
}
if keyspace == nil {
return nil, vterrors.Errorf(vtrpcpb.Code_UNAVAILABLE, "Cannot find keyspace for: %s", ks)
}

return &engine.Send{
Keyspace: table.Keyspace,
Keyspace: keyspace,
TargetDestination: destination,
Query: sqlparser.String(explain),
SingleShardOnly: true,
Expand Down
11 changes: 11 additions & 0 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ func (vw *vschemaWrapper) AllKeyspace() ([]*vindexes.Keyspace, error) {
return []*vindexes.Keyspace{vw.keyspace}, nil
}

// FindKeyspace implements the VSchema interface
func (vw *vschemaWrapper) FindKeyspace(keyspace string) (*vindexes.Keyspace, error) {
if vw.keyspace == nil {
return nil, errors.New("keyspace not available")
}
if vw.keyspace.Name == keyspace {
return vw.keyspace, nil
}
return nil, nil
}

func (vw *vschemaWrapper) Planner() plancontext.PlannerVersion {
return vw.version
}
Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/planbuilder/plancontext/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type VSchema interface {
SysVarSetEnabled() bool
KeyspaceExists(keyspace string) bool
AllKeyspace() ([]*vindexes.Keyspace, error)
FindKeyspace(keyspace string) (*vindexes.Keyspace, error)
GetSemTable() *semantics.SemTable
Planner() PlannerVersion
SetPlannerVersion(pv PlannerVersion)
Expand Down
17 changes: 15 additions & 2 deletions go/vt/vtgate/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type iExecute interface {
VSchema() *vindexes.VSchema
}

//VSchemaOperator is an interface to Vschema Operations
// VSchemaOperator is an interface to Vschema Operations
type VSchemaOperator interface {
GetCurrentSrvVschema() *vschemapb.SrvVSchema
UpdateVSchema(ctx context.Context, ksName string, vschema *vschemapb.SrvVSchema) error
Expand Down Expand Up @@ -378,6 +378,19 @@ func (vc *vcursorImpl) AllKeyspace() ([]*vindexes.Keyspace, error) {
return kss, nil
}

// FindKeyspace implements the VSchema interface
func (vc *vcursorImpl) FindKeyspace(keyspace string) (*vindexes.Keyspace, error) {
if len(vc.vschema.Keyspaces) == 0 {
return nil, errNoDbAvailable
}
for _, ks := range vc.vschema.Keyspaces {
if ks.Keyspace.Name == keyspace {
return ks.Keyspace, nil
}
}
return nil, nil
}

// Planner implements the ContextVSchema interface
func (vc *vcursorImpl) Planner() plancontext.PlannerVersion {
if vc.safeSession.Options != nil &&
Expand Down Expand Up @@ -608,7 +621,7 @@ func (vc *vcursorImpl) SetSysVar(name string, expr string) {
vc.safeSession.SetSystemVariable(name, expr)
}

//NeedsReservedConn implements the SessionActions interface
// NeedsReservedConn implements the SessionActions interface
func (vc *vcursorImpl) NeedsReservedConn() {
vc.safeSession.SetReservedConn(true)
}
Expand Down