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

plan: fix insert onduplicate key update #7406

Merged
merged 6 commits into from
Aug 27, 2018

Conversation

zz-jason
Copy link
Member

What problem does this PR solve?

The schema we built for Insert.Schema4OnDuplicate was incorrect, thus the data record inserted by of insert ... select ... on duplicate key update ... statement based on this schema was incorrect, too. Let's take the following queries as an example:

drop table if exists t;
create table t(k1 bigint, k2 bigint, val bigint, primary key(k1, k2));
insert into t (val, k1, k2) values (3, 1, 2);
insert into t (val, k1, k2) select c, a, b from (select 1 as a, 2 as b, 4 as c) tmp on duplicate key update val = tmp.c;

After executing the above queries, the data record inside in table t is not as expected:

TiDB(localhost:4000) > select * from t;
+----+----+------+
| k1 | k2 | val  |
+----+----+------+
|  1 |  2 |    1 |
+----+----+------+
1 row in set (0.00 sec)

This PR fixes this issue, and the correct result is:

TiDB(localhost:4000) > select * from t;
+----+----+------+
| k1 | k2 | val  |
+----+----+------+
|  1 |  2 |    4 |
+----+----+------+
1 row in set (0.00 sec)

What is changed and how it works?

construct the Insert.Schema4OnDuplicate based on the schema of table to be inserted.

Check List

Tests

  • Integration test

Related changes

  • Need to be included in the release note

@zz-jason zz-jason added type/bugfix This PR fixes a bug. sig/planner SIG: Planner release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 15, 2018
@zz-jason
Copy link
Member Author

/run-all-tests

@zz-jason
Copy link
Member Author

/run-all-tests

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it affectedValuesCols?

Copy link
Member Author

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

Copy link
Contributor

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.

insertCols = tableCols
}

schema4NewRow := expression.NewSchema(make([]*expression.Column, len(tableCols))...)
Copy link
Contributor

Choose a reason for hiding this comment

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

We need a comment for this variable.

@zz-jason
Copy link
Member Author

@XuHuaiyu @lamxTyler PTAL

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason added the status/LGT2 Indicates that a PR has LGTM 2. label Aug 27, 2018
@zz-jason zz-jason merged commit 2d1fae8 into pingcap:master Aug 27, 2018
@zz-jason zz-jason deleted the dev/fix-insert-on-dup branch August 27, 2018 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants