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

fix: insert with negative value #14244

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we check if the bindVars has the value or not? here we would not fail on missing arguments, we would just put in a null value

Copy link
Member Author

@harshit-gangal harshit-gangal Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should ideally never happen.
Not sure what should be the correct behaviour.
May be failing as internal error would be better

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could also be a user error when using grpc for the case when the user did not provide the bind var.

}
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
Loading