-
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
planner/core: update missing virtual columns in update
and insert
#58401
base: master
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Hi @joechenrh. Thanks for your PR. PRs from untrusted users cannot be marked as trusted with I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #58401 +/- ##
================================================
- Coverage 73.5552% 73.2491% -0.3061%
================================================
Files 1680 1681 +1
Lines 463684 472266 +8582
================================================
+ Hits 341064 345931 +4867
- Misses 101817 105463 +3646
- Partials 20803 20872 +69
Flags with carried forward coverage won't be shown. Click here to find out more.
|
update
and insert
update
and insert
update
and insert
@joechenrh: once the present PR merges, I will cherry-pick it on top of release-7.1 in the new PR and assign it to you. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
for dep := range column.Dependences { | ||
if _, ok := onDups[dep]; ok { | ||
assign := &expression.Assignment{Col: colExpr, ColName: column.Name, Expr: expr} | ||
igc.OnDuplicates = append(igc.OnDuplicates, assign) | ||
// onDups use lower column name, see Insert.resolveOnDuplicate | ||
onDups[column.Name.L] = struct{}{} |
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.
Have we set the order of columns following the asc dependency?
for _, column := range columns
cuz we only set the onDups for the later coming columns, those prev ones couldn't see it any more.
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.
cuz we only set the onDups for the later coming columns, those prev ones couldn't see it any more.
Yes, you are right. After testing, I found that this situation may be possible to happen.
For CREATE TABLE
, a generated column can't depend on columns after it.
But for ALTER TABLE ADD COLUMN
, we can use AFTER
keyword to force the new generated column inserted before its dependent column and get a table like:
mysql> show create table t1;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int DEFAULT NULL,
`col2` int GENERATED ALWAYS AS (`col1` + 1) VIRTUAL,
`col1` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Let me think about how to modify it.
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.
There are something wrong in the previous comment.
In resolveGeneratedColumns
, we only process generated columns. Non-generated columns are already processed and stored in onDups
:
tidb/pkg/planner/core/planbuilder.go
Line 3859 in 43f2fb9
func (b *PlanBuilder) resolveGeneratedColumns(ctx context.Context, columns []*table.Column, onDups map[string]struct{}, mockPlan base.LogicalPlan) (igc InsertGeneratedColumns, err error) { |
Since generated columns can only depends on columns prior to it, we don't need to handle the order of columns.
mysql> alter table t1 add column col3 int generated always as (col2 + 1) virtual after id;
ERROR 3107 (HY000): Generated column can refer only to generated columns defined prior to it.
What problem does this PR solve?
Issue Number: close #58400
Problem Summary:
In #58494, we fixed a problem related to virtual columns. But we haven't completely fixed it yet. There still have problems with virtual columns.
For Update:
In
update
statements, virtual columns will be added intoUpdateLists
if their dependent columns have changed. #55829 has fixed nested generated column.tidb/pkg/planner/core/logical_plan_builder.go
Lines 5826 to 5837 in f2db9c4
However, we still ignore the columns with OnUpdateNow flag when checking dependency. Since these columns may be updated in the execution stage too.
For Insert:
For SQL like
INSERT INTO ... ON DUPLICATE KEY UPDATE ...
, only virtual columns that directly depend on the assignments afterUPDATE
will be updated. Take the table in #58400 as example:The dependencies of each column are listed below. Only
j2
and_V$_i1_0
are updated since they depends onj1
. We should handle it the same way as what we do in #55829.What changed and how does it work?
In summary, when extracting generated columns, we have to consider both chain dependencies and on-update-now flag too.
Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.