Skip to content

Commit

Permalink
fix: insert with negative value (#14244)
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
vitess-bot[bot] committed Oct 11, 2023
1 parent c074978 commit efcb6c9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
8 changes: 8 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import (
"github.com/stretchr/testify/require"
)

func TestInsertNeg(t *testing.T) {
conn, closer := start(t)
defer closer()

utils.Exec(t, conn, "insert ignore into t10(id, sharding_key, col1, col2, col3) values(10, 20, 'a', 1, 2), (20, -20, 'b', 3, 4), (30, -40, 'c', 6, 7), (40, 60, 'd', 4, 10)")
utils.Exec(t, conn, "insert ignore into t10(id, sharding_key, col1, col2, col3) values(1, 2, 'a', 1, 2), (2, -2, 'b', -3, 4), (3, -4, 'c', 6, -7), (4, 6, 'd', 4, -10)")
}

func TestSelectNull(t *testing.T) {
conn, closer := start(t)
defer closer()
Expand Down
3 changes: 3 additions & 0 deletions go/test/endtoend/vtgate/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ create table t10
(
id bigint,
sharding_key bigint,
col1 varchar(50),
col2 int,
col3 int,
primary key (id)
) Engine = InnoDB;

Expand Down
2 changes: 2 additions & 0 deletions go/vt/vterrors/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
VT03023 = errorWithoutState("VT03023", vtrpcpb.Code_INVALID_ARGUMENT, "INSERT not supported when targeting a key range: %s", "When targeting a range of shards, Vitess does not know which shard to send the INSERT to.")
VT03024 = errorWithoutState("VT03024", vtrpcpb.Code_INVALID_ARGUMENT, "'%s' user defined variable does not exists", "The query cannot be prepared using the user defined variable as it does not exists for this session.")
VT03025 = errorWithState("VT03025", vtrpcpb.Code_INVALID_ARGUMENT, WrongArguments, "Incorrect arguments to %s", "The execute statement have wrong number of arguments")
VT03026 = errorWithoutState("VT03024", vtrpcpb.Code_INVALID_ARGUMENT, "'%s' bind variable does not exists", "The query cannot be executed as missing the bind variable.")

VT05001 = errorWithState("VT05001", vtrpcpb.Code_NOT_FOUND, DbDropExists, "cannot drop database '%s'; database does not exists", "The given database does not exist; Vitess cannot drop it.")
VT05002 = errorWithState("VT05002", vtrpcpb.Code_NOT_FOUND, BadDb, "cannot alter database '%s'; unknown database", "The given database does not exist; Vitess cannot alter it.")
Expand Down Expand Up @@ -119,6 +120,7 @@ var (
VT03023,
VT03024,
VT03025,
VT03026,
VT05001,
VT05002,
VT05003,
Expand Down
14 changes: 12 additions & 2 deletions go/vt/vtgate/engine/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,18 @@ 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]
err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
if arg, ok := node.(*sqlparser.Argument); ok {
bv, exists := bindVars[arg.Name]
if !exists {
return false, vterrors.VT03026(arg.Name)
}
shardBindVars[arg.Name] = bv
}
return true, nil
}, expr, nil)
if err != nil {
return nil, nil, err
}
}
}
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 @@ -332,8 +332,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 efcb6c9

Please sign in to comment.