-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add foreign key support for insert on duplicate key update #14638
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fae6169
feat: added upsert engine primitive
harshit-gangal 00962ee
sizegen update
harshit-gangal 6d5c612
insert on duplicate key update plan for single row insert
harshit-gangal 59813b5
disallow autocommit for insert in upsert plan
harshit-gangal 7e7ad99
feat: update upsert engine to report correct affected rows, added uni…
harshit-gangal 2cec09e
feat: update upsert engine to support multiple upserts
harshit-gangal 213e423
feat: plan insert on duplicate key update with multiple rows
harshit-gangal bc51d7e
feat: add +1 to rowaffected in upsert engine only if update returns a…
harshit-gangal 4bf4926
Merge remote-tracking branch 'upstream/main' into fk-insert-dup
harshit-gangal b3b4edd
simplified the if condition check
harshit-gangal 50bd269
addressed review comments
harshit-gangal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package engine | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"vitess.io/vitess/go/sqltypes" | ||
querypb "vitess.io/vitess/go/vt/proto/query" | ||
topodatapb "vitess.io/vitess/go/vt/proto/topodata" | ||
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc" | ||
"vitess.io/vitess/go/vt/vterrors" | ||
) | ||
|
||
var _ Primitive = (*Upsert)(nil) | ||
|
||
// Upsert Primitive will execute the insert primitive first and | ||
// if there is `Duplicate Key` error, it executes the update primitive. | ||
type Upsert struct { | ||
Upserts []upsert | ||
|
||
txNeeded | ||
} | ||
|
||
type upsert struct { | ||
Insert Primitive | ||
Update Primitive | ||
} | ||
|
||
// AddUpsert appends to the Upsert Primitive. | ||
func (u *Upsert) AddUpsert(ins, upd Primitive) { | ||
u.Upserts = append(u.Upserts, upsert{ | ||
Insert: ins, | ||
Update: upd, | ||
}) | ||
} | ||
|
||
// RouteType implements Primitive interface type. | ||
func (u *Upsert) RouteType() string { | ||
return "UPSERT" | ||
} | ||
|
||
// GetKeyspaceName implements Primitive interface type. | ||
func (u *Upsert) GetKeyspaceName() string { | ||
if len(u.Upserts) > 0 { | ||
return u.Upserts[0].Insert.GetKeyspaceName() | ||
} | ||
return "" | ||
} | ||
|
||
// GetTableName implements Primitive interface type. | ||
func (u *Upsert) GetTableName() string { | ||
if len(u.Upserts) > 0 { | ||
return u.Upserts[0].Insert.GetTableName() | ||
} | ||
return "" | ||
} | ||
|
||
// GetFields implements Primitive interface type. | ||
func (u *Upsert) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) { | ||
return nil, vterrors.VT13001("unexpected to receive GetFields call for insert on duplicate key update query") | ||
} | ||
|
||
// TryExecute implements Primitive interface type. | ||
func (u *Upsert) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) { | ||
result := &sqltypes.Result{} | ||
for _, up := range u.Upserts { | ||
qr, err := execOne(ctx, vcursor, bindVars, wantfields, up) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result.RowsAffected += qr.RowsAffected | ||
} | ||
return result, nil | ||
} | ||
|
||
func execOne(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, up upsert) (*sqltypes.Result, error) { | ||
insQr, err := vcursor.ExecutePrimitive(ctx, up.Insert, bindVars, wantfields) | ||
if err == nil { | ||
return insQr, nil | ||
} | ||
if vterrors.Code(err) != vtrpcpb.Code_ALREADY_EXISTS { | ||
return nil, err | ||
} | ||
updQr, err := vcursor.ExecutePrimitive(ctx, up.Update, bindVars, wantfields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// To match mysql, need to report +1 on rows affected if there is any change. | ||
if updQr.RowsAffected > 0 { | ||
updQr.RowsAffected += 1 | ||
} | ||
return updQr, nil | ||
} | ||
|
||
// TryStreamExecute implements Primitive interface type. | ||
func (u *Upsert) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error { | ||
qr, err := u.TryExecute(ctx, vcursor, bindVars, wantfields) | ||
if err != nil { | ||
return err | ||
} | ||
return callback(qr) | ||
} | ||
|
||
// Inputs implements Primitive interface type. | ||
func (u *Upsert) Inputs() ([]Primitive, []map[string]any) { | ||
var inputs []Primitive | ||
var inputsMap []map[string]any | ||
for i, up := range u.Upserts { | ||
inputs = append(inputs, up.Insert, up.Update) | ||
inputsMap = append(inputsMap, | ||
map[string]any{inputName: fmt.Sprintf("Insert-%d", i+1)}, | ||
map[string]any{inputName: fmt.Sprintf("Update-%d", i+1)}) | ||
} | ||
return inputs, inputsMap | ||
} | ||
|
||
func (u *Upsert) description() PrimitiveDescription { | ||
return PrimitiveDescription{ | ||
OperatorType: "Upsert", | ||
TargetTabletType: topodatapb.TabletType_PRIMARY, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that MySQL has this behaviour, but for the life of me, I can't understand why 😆