-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
plan: fix insert onduplicate key update #7406
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5520c6a
plan: fix insert onduplicate key update
zz-jason 4f392d5
fix panic
zz-jason 213650a
Merge branch 'master' into dev/fix-insert-on-dup
zz-jason f911f5e
Merge branch 'master' into dev/fix-insert-on-dup
zz-jason 8d555d1
address comment
zz-jason a063dae
Merge branch 'master' into dev/fix-insert-on-dup
alivxxx 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
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 |
---|---|---|
|
@@ -1187,7 +1187,40 @@ func (b *planBuilder) buildSelectPlanOfInsert(insert *ast.InsertStmt, insertPlan | |
return errors.Trace(err) | ||
} | ||
|
||
insertPlan.Schema4OnDuplicate = expression.MergeSchema(insertPlan.tableSchema, insertPlan.SelectPlan.Schema()) | ||
tableCols := insertPlan.Table.Cols() | ||
var insertCols []*table.Column | ||
|
||
if len(insertPlan.Columns) > 0 { | ||
colNames := make([]string, 0, len(tableCols)) | ||
for _, col := range insertPlan.Columns { | ||
colNames = append(colNames, col.Name.O) | ||
} | ||
for _, col := range insertPlan.GenCols.Columns { | ||
colNames = append(colNames, col.Name.O) | ||
} | ||
insertCols, err = table.FindCols(tableCols, colNames, insertPlan.Table.Meta().PKIsHandle) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} else { | ||
insertCols = tableCols | ||
} | ||
|
||
schema4NewRow := expression.NewSchema(make([]*expression.Column, len(tableCols))...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a comment for this variable. |
||
for i, selCol := range insertPlan.SelectPlan.Schema().Columns { | ||
ordinal := insertCols[i].Offset | ||
schema4NewRow.Columns[ordinal] = &expression.Column{} | ||
*schema4NewRow.Columns[ordinal] = *selCol | ||
|
||
schema4NewRow.Columns[ordinal].RetType = &types.FieldType{} | ||
*schema4NewRow.Columns[ordinal].RetType = insertCols[i].FieldType | ||
} | ||
for i := range schema4NewRow.Columns { | ||
if schema4NewRow.Columns[i] == nil { | ||
schema4NewRow.Columns[i] = &expression.Column{UniqueID: insertPlan.ctx.GetSessionVars().AllocPlanColumnID()} | ||
} | ||
} | ||
insertPlan.Schema4OnDuplicate = expression.MergeSchema(insertPlan.tableSchema, schema4NewRow) | ||
return nil | ||
} | ||
|
||
|
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.
Is it
affectedValuesCols
?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.
Yeah, this name is better
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.
And we already have it at line 1158.