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

Zero autoinc mode #6749

Merged
merged 3 commits into from
Sep 24, 2020
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
20 changes: 18 additions & 2 deletions go/vt/vtgate/engine/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ func (ins *Insert) execInsertSharded(vcursor VCursor, bindVars map[string]*query
return result, nil
}

// shouldGenerate determines if a sequence value should be generated for a given value
func shouldGenerate(v sqltypes.Value) bool {
if v.IsNull() {
return true
}

// Unless the NO_AUTO_VALUE_ON_ZERO sql mode is active in mysql, it also
// treats 0 as a value that should generate a new sequence.
n, err := evalengine.ToUint64(v)
if err == nil && n == 0 {
return true
}

return false
}

// processGenerate generates new values using a sequence if necessary.
// If no value was generated, it returns 0. Values are generated only
// for cases where none are supplied.
Expand All @@ -289,7 +305,7 @@ func (ins *Insert) processGenerate(vcursor VCursor, bindVars map[string]*querypb
}
count := int64(0)
for _, val := range resolved {
if val.IsNull() {
if shouldGenerate(val) {
count++
}
}
Expand Down Expand Up @@ -319,7 +335,7 @@ func (ins *Insert) processGenerate(vcursor VCursor, bindVars map[string]*querypb
// Fill the holes where no value was supplied.
cur := insertID
for i, v := range resolved {
if v.IsNull() {
if shouldGenerate(v) {
bindVars[SeqVarName+strconv.Itoa(i)] = sqltypes.Int64BindVariable(cur)
cur++
} else {
Expand Down
55 changes: 55 additions & 0 deletions go/vt/vtgate/engine/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,61 @@ func TestInsertUnshardedGenerate(t *testing.T) {
expectResult(t, "Execute", result, &sqltypes.Result{InsertID: 4})
}

func TestInsertUnshardedGenerate_Zeros(t *testing.T) {
ins := NewQueryInsert(
InsertUnsharded,
&vindexes.Keyspace{
Name: "ks",
Sharded: false,
},
"dummy_insert",
)
ins.Generate = &Generate{
Keyspace: &vindexes.Keyspace{
Name: "ks2",
Sharded: false,
},
Query: "dummy_generate",
Values: sqltypes.PlanValue{
Values: []sqltypes.PlanValue{
{Value: sqltypes.NewInt64(1)},
{Value: sqltypes.NewInt64(0)},
{Value: sqltypes.NewInt64(2)},
{Value: sqltypes.NewInt64(0)},
{Value: sqltypes.NewInt64(3)},
},
},
}

vc := newDMLTestVCursor("0")
vc.results = []*sqltypes.Result{
sqltypes.MakeTestResult(
sqltypes.MakeTestFields(
"nextval",
"int64",
),
"4",
),
{InsertID: 1},
}

result, err := ins.Execute(vc, map[string]*querypb.BindVariable{}, false)
if err != nil {
t.Fatal(err)
}
vc.ExpectLog(t, []string{
// Fetch two sequence value.
`ResolveDestinations ks2 [] Destinations:DestinationAnyShard()`,
`ExecuteStandalone dummy_generate n: type:INT64 value:"2" ks2 0`,
// Fill those values into the insert.
`ResolveDestinations ks [] Destinations:DestinationAllShards()`,
`ExecuteMultiShard ks.0: dummy_insert {__seq0: type:INT64 value:"1" __seq1: type:INT64 value:"4" __seq2: type:INT64 value:"2" __seq3: type:INT64 value:"5" __seq4: type:INT64 value:"3" } true true`,
})

// The insert id returned by ExecuteMultiShard should be overwritten by processGenerate.
expectResult(t, "Execute", result, &sqltypes.Result{InsertID: 4})
}

func TestInsertShardedSimple(t *testing.T) {
invschema := &vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{
Expand Down