Skip to content
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
134 changes: 0 additions & 134 deletions go/test/endtoend/vtgate/dbnameoverride/tablet_master_test.go

This file was deleted.

31 changes: 31 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,37 @@ func TestShowTablesWithWhereClause(t *testing.T) {
assertMatches(t, conn, "show tables from ks where Tables_in_ks='t3'", `[[VARCHAR("t3")]]`)
}

func TestDbNameOverride(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.Nil(t, err)
defer conn.Close()
qr, err := conn.ExecuteFetch("SELECT distinct database() FROM information_schema.tables WHERE table_schema = database()", 1000, true)

require.Nil(t, err)
assert.Equal(t, 1, len(qr.Rows), "did not get enough rows back")
assert.Equal(t, "vt_ks", qr.Rows[0][0].ToString())
}

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

qr, err := conn.ExecuteFetch("SELECT distinct table_schema FROM information_schema.tables WHERE table_schema = 'ks'", 1000, true)
require.Nil(t, err)
assert.Equal(t, 1, len(qr.Rows), "did not get enough rows back")
assert.Equal(t, "vt_ks", qr.Rows[0][0].ToString())

qr, err = conn.ExecuteFetch("SELECT distinct table_schema FROM information_schema.tables WHERE table_schema = 'vt_ks'", 1000, true)
require.Nil(t, err)
assert.Equal(t, 1, len(qr.Rows), "did not get enough rows back")
assert.Equal(t, "vt_ks", qr.Rows[0][0].ToString())
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := exec(t, conn, query)
Expand Down
9 changes: 8 additions & 1 deletion go/vt/vtgate/engine/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ var routeName = map[RouteOpcode]string{

var (
partialSuccessScatterQueries = stats.NewCounter("PartialSuccessScatterQueries", "Count of partially successful scatter queries")
// BvSchemaName is bind variable to be sent down to vttablet for schema name.
BvSchemaName = "__vtschemaname"
)

// MarshalJSON serializes the RouteOpcode as a JSON string.
Expand Down Expand Up @@ -391,6 +393,7 @@ func (route *Route) paramsSystemQuery(vcursor VCursor, bindVars map[string]*quer
}
if keyspace == "" {
keyspace = result.Value().ToString()
bindVars[BvSchemaName] = sqltypes.StringBindVariable(keyspace)
} else if other := result.Value().ToString(); keyspace != other {
return nil, nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "can't use more than one keyspace per system table query - found both '%s' and '%s'", keyspace, other)
}
Expand All @@ -402,7 +405,11 @@ func (route *Route) paramsSystemQuery(vcursor VCursor, bindVars map[string]*quer

destinations, _, err := vcursor.ResolveDestinations(keyspace, nil, []key.Destination{key.DestinationAnyShard{}})
if err != nil {
return nil, nil, vterrors.Wrapf(err, "failed to find information about keyspace `%s`", keyspace)
// Check with assigned route keyspace.
destinations, _, err = vcursor.ResolveDestinations(route.Keyspace.Name, nil, []key.Destination{key.DestinationAnyShard{}})
if err != nil {
return nil, nil, vterrors.Wrapf(err, "failed to find information about keyspace `%s`", keyspace)
}
}
return destinations, []map[string]*querypb.BindVariable{bindVars}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (r *rewriter) rewriteTableSchema(cursor *sqlparser.Cursor) bool {
return false
}
r.tableNameExpressions = append(r.tableNameExpressions, evalExpr)
parent.Right = sqlparser.NewArgument([]byte(":__vtschemaname"))
parent.Right = sqlparser.NewArgument([]byte(":" + engine.BvSchemaName))
}
}
}
Expand Down