-
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
fix alter add index on virtual column bug #7575
Merged
crazycs520
merged 23 commits into
pingcap:master
from
crazycs520:add-index-on-vir-col-bug
Oct 10, 2018
Merged
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0112b14
fix alter add index on virtual column bug
crazycs520 e19e34c
address comment
crazycs520 177a35d
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 0e1cd5a
fix column generate expr no cast to target field type bug
crazycs520 7ef6521
refactor code
crazycs520 880bd06
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 2c3be20
add row decoder
crazycs520 7f5b7cc
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 bc00f25
refine code
crazycs520 9645b21
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 9c624ff
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 3ac1fb2
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 4e44d06
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 de0ae54
address comment
crazycs520 732c952
address comment
crazycs520 02c24f9
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 c2a3ecf
add more test and fix timestamp bugs
crazycs520 a9d653b
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 9c36df4
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 2818aab
address comment
crazycs520 012ea29
fix add index on multi generated column bugs and add test
crazycs520 2916122
Merge branch 'master' of /~https://github.com/pingcap/tidb into add-ind…
crazycs520 1da045a
Merge branch 'master' into add-index-on-vir-col-bug
crazycs520 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
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package decoder | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/table" | ||
"github.com/pingcap/tidb/tablecodec" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Column contains the info and generated expr of column. | ||
type Column struct { | ||
Info *model.ColumnInfo | ||
GenExpr expression.Expression | ||
} | ||
|
||
// RowDecoder decodes a byte slice into datums and eval the generated column value. | ||
type RowDecoder struct { | ||
mutRow chunk.MutRow | ||
columns map[int64]Column | ||
colTypes map[int64]*types.FieldType | ||
haveGenColumn bool | ||
} | ||
|
||
// NewRowDecoder returns a new RowDecoder. | ||
func NewRowDecoder(cols []*table.Column, decodeColMap map[int64]Column) RowDecoder { | ||
colFieldMap := make(map[int64]*types.FieldType, len(decodeColMap)) | ||
haveGenCol := false | ||
for id, col := range decodeColMap { | ||
colFieldMap[id] = &col.Info.FieldType | ||
if col.GenExpr != nil { | ||
haveGenCol = true | ||
} | ||
} | ||
if !haveGenCol { | ||
return RowDecoder{ | ||
colTypes: colFieldMap, | ||
} | ||
} | ||
|
||
tps := make([]*types.FieldType, len(cols)) | ||
for _, col := range cols { | ||
tps[col.Offset] = &col.FieldType | ||
} | ||
return RowDecoder{ | ||
mutRow: chunk.MutRowFromTypes(tps), | ||
columns: decodeColMap, | ||
colTypes: colFieldMap, | ||
haveGenColumn: haveGenCol, | ||
} | ||
} | ||
|
||
// DecodeAndEvalRowWithMap decodes a byte slice into datums and evaluates the generated column value. | ||
func (rd RowDecoder) DecodeAndEvalRowWithMap(ctx sessionctx.Context, b []byte, loc *time.Location, row map[int64]types.Datum) (map[int64]types.Datum, error) { | ||
_, err := tablecodec.DecodeRowWithMap(b, rd.colTypes, loc, row) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
if !rd.haveGenColumn { | ||
return row, nil | ||
} | ||
|
||
for id, v := range row { | ||
rd.mutRow.SetValue(rd.columns[id].Info.Offset, v.GetValue()) | ||
} | ||
for id, col := range rd.columns { | ||
if col.GenExpr == nil { | ||
continue | ||
} | ||
// Eval the column value | ||
val, err := col.GenExpr.Eval(rd.mutRow.ToRow()) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
val, err = table.CastValue(ctx, val, col.Info) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
row[id] = val | ||
} | ||
return row, 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.
Please add more test cases.
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.
@winkyao done.