Skip to content

Commit

Permalink
fix: walk the expression and add all the arguments to the bindvars
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Oct 11, 2023
1 parent 5348254 commit 606ee30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
9 changes: 6 additions & 3 deletions go/vt/vtgate/engine/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,12 @@ func (ins *Insert) getInsertShardedRoute(
if keyspaceIDs[index] != nil {
mids = append(mids, sqlparser.String(ins.Mid[index]))
for _, expr := range ins.Mid[index] {
if arg, ok := expr.(*sqlparser.Argument); ok {
shardBindVars[arg.Name] = bindVars[arg.Name]
}
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
if arg, ok := node.(*sqlparser.Argument); ok {
shardBindVars[arg.Name] = bindVars[arg.Name]
}
return true, nil
}, expr, nil)
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions go/vt/vtgate/executor_dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,42 @@ func TestInsertSharded(t *testing.T) {
testQueryLog(t, executor, logChan, "TestExecute", "INSERT", "insert into `user`(id, v, `name`) values (:vtg1 /* INT64 */, :vtg2 /* INT64 */, _binary :vtg3 /* VARCHAR */)", 1)
}

func TestInsertNegativeValue(t *testing.T) {
executor, sbc1, sbc2, sbclookup, ctx := createExecutorEnv(t)
executor.normalize = true

logChan := executor.queryLogger.Subscribe("Test")
defer executor.queryLogger.Unsubscribe(logChan)

session := &vtgatepb.Session{
TargetString: "@primary",
}
_, err := executorExec(ctx, executor, session, "insert into user(id, v, name) values (1, -2, 'myname')", nil)
require.NoError(t, err)
wantQueries := []*querypb.BoundQuery{{
Sql: "insert into `user`(id, v, `name`) values (:_Id_0, -:vtg2 /* INT64 */, :_name_0)",
BindVariables: map[string]*querypb.BindVariable{
"_Id_0": sqltypes.Int64BindVariable(1),
"vtg2": sqltypes.Int64BindVariable(2),
"_name_0": sqltypes.StringBindVariable("myname"),
},
}}
assertQueries(t, sbc1, wantQueries)
assertQueries(t, sbc2, nil)
wantQueries = []*querypb.BoundQuery{{
Sql: "insert into name_user_map(`name`, user_id) values (:name_0, :user_id_0)",
BindVariables: map[string]*querypb.BindVariable{
"name_0": sqltypes.StringBindVariable("myname"),
"user_id_0": sqltypes.Uint64BindVariable(1),
},
}}
assertQueries(t, sbclookup, wantQueries)

testQueryLog(t, executor, logChan, "MarkSavepoint", "SAVEPOINT", "savepoint x", 0)
testQueryLog(t, executor, logChan, "VindexCreate", "INSERT", "insert into name_user_map(`name`, user_id) values (:name_0, :user_id_0)", 1)
testQueryLog(t, executor, logChan, "TestExecute", "INSERT", "insert into `user`(id, v, `name`) values (:vtg1 /* INT64 */, -:vtg2 /* INT64 */, :vtg3 /* VARCHAR */)", 1)
}

func TestInsertShardedKeyrange(t *testing.T) {
executor, _, _, _, ctx := createExecutorEnv(t)

Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/executor_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ func assertQueries(t *testing.T, sbc *sandboxconn.SandboxConn, wantQueries []*qu
}
got := query.Sql
expected := wantQueries[idx].Sql
assert.Equal(t, expected, got)
assert.Equal(t, wantQueries[idx].BindVariables, query.BindVariables)
utils.MustMatch(t, expected, got)
utils.MustMatch(t, wantQueries[idx].BindVariables, query.BindVariables)
idx++
}
}
Expand Down

0 comments on commit 606ee30

Please sign in to comment.