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

fix alter add index on virtual column bug #7575

Merged
merged 23 commits into from
Oct 10, 2018

Conversation

crazycs520
Copy link
Contributor

@crazycs520 crazycs520 commented Aug 31, 2018

What problem does this PR solve?

create table test ( b json , c int as (JSON_EXTRACT(b,'$.d')));
insert into test set b='{"d": 100}';
alter table test add index idxd(c);
admin check table test;

When alter add index on a virtual generated column, we shoud work out the val by colGenExpr.
The old TiDB will use test virtual generated column default value to fill back to index record when alter add index.

What is changed and how it works?

If index.column is a virtual generated column, get the generatedExpr, and also, we need to decode the column of virtual column dependent on.
after decode from raw data, we have to use generatedExpr to eval the virtual column data.

Check List

Tests

  • Unit test

  • Increased code complexity

Related changes

  • Need to cherry-pick to the release branch
  • Need to update the documentation
  • Need to update the tidb-ansible repository
  • Need to be included in the release note

@crazycs520
Copy link
Contributor Author

@winkyao @zimulala @ciscoxll PTAL

ddl/index.go Outdated
closed bool
priority int

// The following attributes are used to reduce memory allocation.
defaultVals []types.Datum
idxRecords []*indexRecord
rowMap map[int64]types.Datum
rowSlice []types.Datum
Copy link
Member

Choose a reason for hiding this comment

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

colSlice instead of row?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

colSlice is just a buffer, main purpose is getting row for expression eval.

Copy link
Member

Choose a reason for hiding this comment

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

I think rowBuffer is a more clear name.

ddl/index.go Outdated
w.rowSlice[col.Offset] = ri
}
}
w.row = chunk.MutRowFromDatums(w.rowSlice).ToRow()
Copy link
Member

Choose a reason for hiding this comment

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

Since rowSlice is only used here, and the check are both if len(colExprMap) != 0. So let it be a local variable instead of one in struct.

Copy link
Contributor

Choose a reason for hiding this comment

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

put it in the struct is used to reduce memory allocations. @winoros

@winkyao
Copy link
Contributor

winkyao commented Sep 4, 2018

@crazycs520 Please pick the proper items in Related changes, in the pr description.

ddl/index.go Outdated
@@ -843,14 +890,28 @@ func (w *addIndexWorker) run(d *ddlCtx) {
log.Infof("[ddl-reorg] worker[%v] exit", w.id)
}

func makeupIndexColFieldMap(t table.Table, indexInfo *model.IndexInfo) map[int64]*types.FieldType {
func makeupIndexColFieldMapAndColExpr(sessCtx sessionctx.Context, t table.Table, indexInfo *model.IndexInfo) (map[int64]*types.FieldType, map[int64]expression.Expression, error) {
Copy link
Contributor

@winkyao winkyao Sep 4, 2018

Choose a reason for hiding this comment

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

You can add a struct like

type SomeStruct struct {
    tp *types.FieldType
    genExpr expression.Expression
}

then return map like map[int64]*SomeStruct to avoid using two map.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I respectively need 2 map, colFieldMap use to decode row, and colExprMap use to expression to eval.

Copy link
Contributor

@winkyao winkyao Sep 4, 2018

Choose a reason for hiding this comment

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

You can achieve decoding row and eval an expression by using just one map.

@crazycs520
Copy link
Contributor Author

@winkyao PTAL

@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520
Copy link
Contributor Author

/run-common-test

@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520
Copy link
Contributor Author

/run-all-tests

@ciscoxll
Copy link
Contributor

ciscoxll commented Sep 12, 2018

@crazycs520 Please fix conflicts.

Copy link
Contributor

@ciscoxll ciscoxll left a comment

Choose a reason for hiding this comment

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

reset LGTM

@@ -273,6 +273,12 @@ func (s *testSuite) TestAdmin(c *C) {
tk.MustExec("ALTER TABLE t1 ADD INDEX idx3 (c4);")
tk.MustExec("admin check table t1;")

// For add index on virtual column
tk.MustExec("drop table if exists t1;")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add more test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@winkyao done.

@crazycs520
Copy link
Contributor Author

/run-all-tests

Copy link
Contributor

@winkyao winkyao left a comment

Choose a reason for hiding this comment

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

LGTM

zhexuany
zhexuany previously approved these changes Sep 29, 2018
Copy link
Contributor

@zhexuany zhexuany left a comment

Choose a reason for hiding this comment

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

LGTM

package decoder

import (
"github.com/pingcap/tidb/mysql"
Copy link
Contributor

Choose a reason for hiding this comment

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

Put it to line 20.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. @zimulala PTAL

tk.MustExec("alter table t1 add index idx_g(g);")
tk.MustExec("alter table t1 add index idx_h(h);")
tk.MustExec("alter table t1 add index idx_j(j);")
tk.MustExec("alter table t1 add index idx_i(i);")
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we going to add some indexes for multiple columns?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. @zimulala PTAL

@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520 crazycs520 added the status/LGT2 Indicates that a PR has LGTM 2. label Oct 10, 2018
Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

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

LGTM

@zimulala
Copy link
Contributor

/run-all-tests

@zimulala zimulala added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. labels Oct 10, 2018
@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520 crazycs520 merged commit fd71236 into pingcap:master Oct 10, 2018
crazycs520 added a commit to crazycs520/tidb that referenced this pull request Dec 11, 2018
@you06 you06 added the sig/sql-infra SIG: SQL Infra label Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/sql-infra SIG: SQL Infra status/LGT3 The PR has already had 3 LGTM. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants