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
12 changes: 10 additions & 2 deletions go/sqltypes/bind_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ import (
querypb "vitess.io/vitess/go/vt/proto/query"
)

// NullBindVariable is a bindvar with NULL value.
var NullBindVariable = &querypb.BindVariable{Type: querypb.Type_NULL_TYPE}
var (
// BvSchemaName is bind variable to be sent down to vttablet for schema name.
BvSchemaName = "__vtschemaname"

// BvReplaceSchemaName is bind variable to be sent down to vttablet to replace schema name.
BvReplaceSchemaName = "__replacevtschemaname"

// NullBindVariable is a bindvar with NULL value.
NullBindVariable = &querypb.BindVariable{Type: querypb.Type_NULL_TYPE}
)

// ValueToProto converts Value to a *querypb.Value.
func ValueToProto(v Value) *querypb.Value {
Expand Down
4 changes: 4 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func TestInformationSchemaQuery(t *testing.T) {
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 = 'NONE'", 1000, true)
require.Nil(t, err)
assert.Empty(t, qr.Rows)
}

func TestOffsetAndLimitWithOLAP(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion go/vt/vtexplain/vtexplain_flaky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func TestJSONOutput(t *testing.T) {
{
"BindVars": {
"#maxLimit": "10001",
"__vtschemaname": "''",
"vtg1": "1"
},
"SQL": "select :vtg1 from user where id = :vtg1",
Expand Down
13 changes: 9 additions & 4 deletions go/vt/vtgate/engine/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ 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 @@ -386,14 +384,16 @@ func (route *Route) paramsSystemQuery(vcursor VCursor, bindVars map[string]*quer
}

var keyspace string
schemaExists := false
for _, expr := range route.SysTableKeyspaceExpr {
result, err := expr.Evaluate(env)
if err != nil {
return nil, nil, err
}
if keyspace == "" {
keyspace = result.Value().ToString()
bindVars[BvSchemaName] = sqltypes.StringBindVariable(keyspace)
bindVars[sqltypes.BvSchemaName] = sqltypes.StringBindVariable(keyspace)
schemaExists = true
} 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 @@ -404,7 +404,12 @@ func (route *Route) paramsSystemQuery(vcursor VCursor, bindVars map[string]*quer
}

destinations, _, err := vcursor.ResolveDestinations(keyspace, nil, []key.Destination{key.DestinationAnyShard{}})
if err != nil {
if err == nil {
// This is to indicate vttablet to replace the schema name.
if schemaExists {
bindVars[sqltypes.BvReplaceSchemaName] = sqltypes.Int64BindVariable(1)
}
} else {
// Check with assigned route keyspace.
destinations, _, err = vcursor.ResolveDestinations(route.Keyspace.Name, nil, []key.Destination{key.DestinationAnyShard{}})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion go/vt/vtgate/planbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"fmt"

"vitess.io/vitess/go/sqltypes"

"vitess.io/vitess/go/vt/key"

vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
Expand Down Expand Up @@ -257,7 +259,7 @@ func (r *rewriter) rewriteTableSchema(cursor *sqlparser.Cursor) bool {
return false
}
r.tableNameExpressions = append(r.tableNameExpressions, evalExpr)
parent.Right = sqlparser.NewArgument([]byte(":" + engine.BvSchemaName))
parent.Right = sqlparser.NewArgument([]byte(":" + sqltypes.BvSchemaName))
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions go/vt/vttablet/tabletserver/query_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func (qre *QueryExecutor) Execute() (reply *sqltypes.Result, err error) {
case planbuilder.PlanSelect, planbuilder.PlanSelectImpossible, planbuilder.PlanShowTables:
maxrows := qre.getSelectLimit()
qre.bindVars["#maxLimit"] = sqltypes.Int64BindVariable(maxrows + 1)
qre.bindVars["__vtschemaname"] = sqltypes.StringBindVariable(qre.tsv.config.DB.DBName)
if qre.bindVars[sqltypes.BvReplaceSchemaName] != nil {
qre.bindVars[sqltypes.BvSchemaName] = sqltypes.StringBindVariable(qre.tsv.config.DB.DBName)
}
qr, err := qre.execSelect()
if err != nil {
return nil, err
Expand Down Expand Up @@ -207,7 +209,9 @@ func (qre *QueryExecutor) txConnExec(conn *StatefulConnection) (*sqltypes.Result
case planbuilder.PlanSelect, planbuilder.PlanSelectLock, planbuilder.PlanSelectImpossible, planbuilder.PlanShowTables:
maxrows := qre.getSelectLimit()
qre.bindVars["#maxLimit"] = sqltypes.Int64BindVariable(maxrows + 1)
qre.bindVars["__vtschemaname"] = sqltypes.StringBindVariable(qre.tsv.config.DB.DBName)
if qre.bindVars[sqltypes.BvReplaceSchemaName] != nil {
qre.bindVars[sqltypes.BvSchemaName] = sqltypes.StringBindVariable(qre.tsv.config.DB.DBName)
}
qr, err := qre.txFetch(conn, false)
if err != nil {
return nil, err
Expand Down