-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
planner: fix panic when prepare and execute the insert on duplicate #37924
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
28f9545
planner: fix panic when prepare and execute the insert on duplicate
Reminiscent 5dbf5cc
Merge branch 'master' of https://github.com/pingcap/tidb into issue37187
Reminiscent 95765cb
address comments
Reminiscent e78f0e2
Merge branch 'master' of https://github.com/pingcap/tidb into issue37187
Reminiscent 7b26599
address comments
Reminiscent 7af05a6
Merge branch 'master' of https://github.com/pingcap/tidb into issue37187
Reminiscent 50b7a69
Merge branch 'master' into issue37187
ti-chi-bot 891aa00
Merge branch 'master' into issue37187
ti-chi-bot 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
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 |
---|---|---|
|
@@ -3821,13 +3821,13 @@ func (b *PlanBuilder) buildSelectPlanOfInsert(ctx context.Context, insert *ast.I | |
if err != nil { | ||
return err | ||
} | ||
actualColLen := -1 | ||
actualColLen := insert.ColLen | ||
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. How about adding some comments like |
||
// For MYSQL, it handles the case that the columns in ON DUPLICATE UPDATE is not the project column of the SELECT clause | ||
// but just in the table occurs in the SELECT CLAUSE. | ||
// e.g. insert into a select x from b ON DUPLICATE KEY UPDATE a.x=b.y; the `y` is not a column of select's output. | ||
// MySQL won't throw error and will execute this SQL successfully. | ||
// To make compatible with this strange feature, we add the variable `actualColLen` and the following IF branch. | ||
if len(insert.OnDuplicate) > 0 { | ||
if len(insert.OnDuplicate) > 0 && actualColLen == 0 { | ||
// If the select has aggregation, it cannot see the columns not in the select field. | ||
// e.g. insert into a select x from b ON DUPLICATE KEY UPDATE a.x=b.y; can be executed successfully. | ||
// insert into a select x from b group by x ON DUPLICATE KEY UPDATE a.x=b.y; will report b.y not found. | ||
|
@@ -3874,7 +3874,7 @@ func (b *PlanBuilder) buildSelectPlanOfInsert(ctx context.Context, insert *ast.I | |
} | ||
|
||
// Check to guarantee that the length of the row returned by select is equal to that of affectedValuesCols. | ||
if (actualColLen == -1 && selectPlan.Schema().Len() != len(affectedValuesCols)) || (actualColLen != -1 && actualColLen != len(affectedValuesCols)) { | ||
if (actualColLen == 0 && selectPlan.Schema().Len() != len(affectedValuesCols)) || (actualColLen != 0 && actualColLen != len(affectedValuesCols)) { | ||
return ErrWrongValueCountOnRow.GenWithStackByArgs(1) | ||
} | ||
|
||
|
@@ -3897,9 +3897,12 @@ func (b *PlanBuilder) buildSelectPlanOfInsert(ctx context.Context, insert *ast.I | |
return err | ||
} | ||
|
||
if actualColLen == -1 { | ||
if actualColLen == 0 { | ||
actualColLen = selectPlan.Schema().Len() | ||
} | ||
if insert.ColLen == 0 { | ||
insert.ColLen = actualColLen | ||
} | ||
insertPlan.RowLen = actualColLen | ||
// schema4NewRow is the schema for the newly created data record based on | ||
// the result of the select statement. | ||
|
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.
Because the initialization value for
insert.ColLen
is 0. And it's impossible for the variableactualColLen
's end value be zero(I guess. In code, it will be assigned toactualColLen = selectPlan.Schema().Len()
). So we change theactualColLen
's initiation value to 0.