This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
restore: check row value count to avoid unexpected encode result #528
Merged
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9b63146
check row value count to avoid unexpected encode result
glorv 324954a
check the '_tidb_row_id' field
glorv 8c78d98
Merge branch 'master' into check-read-row
glorv df51e9d
resolve comments
glorv 47240af
Merge branch 'check-read-row' of https://github.com/pingcap/tidb-ligh…
glorv 27d7448
Merge branch 'master' into check-read-row
glorv 72de282
Merge branch 'master' of https://github.com/pingcap/tidb-lightning in…
glorv e104101
fix issue related to '_tidb_rowid' and move column count to tidb encoder
glorv cbb6242
Merge branch 'master' of https://github.com/pingcap/tidb-lightning in…
glorv f24b6fa
add tidb_opt_write_row_id session var
glorv 76b2023
fix test
glorv 551ce93
resolve comments
glorv 2e6f2c7
update tidb to apply tidb#22062
glorv 34ea98c
Merge branch 'master' into check-read-row
glorv ddad685
fix test
glorv 5c4f9f7
Merge branch 'check-read-row' of https://github.com/pingcap/tidb-ligh…
glorv 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -38,6 +38,14 @@ import ( | |||||
"github.com/pingcap/tidb-lightning/lightning/verification" | ||||||
) | ||||||
|
||||||
var ( | ||||||
extraHandleTableColumn = &table.Column{ | ||||||
ColumnInfo: extraHandleColumnInfo, | ||||||
GeneratedExpr: nil, | ||||||
DefaultExpr: nil, | ||||||
} | ||||||
) | ||||||
|
||||||
type tidbRow string | ||||||
|
||||||
type tidbRows []tidbRow | ||||||
|
@@ -51,10 +59,13 @@ func (row tidbRows) MarshalLogArray(encoder zapcore.ArrayEncoder) error { | |||||
} | ||||||
|
||||||
type tidbEncoder struct { | ||||||
mode mysql.SQLMode | ||||||
tbl table.Table | ||||||
se *session | ||||||
mode mysql.SQLMode | ||||||
tbl table.Table | ||||||
se *session | ||||||
// the index of table columns for each data field. | ||||||
// index == len(table.columns) means this field is `_tidb_rowid` | ||||||
columnIdx []int | ||||||
columnCnt int | ||||||
} | ||||||
|
||||||
type tidbBackend struct { | ||||||
|
@@ -228,17 +239,37 @@ func (enc *tidbEncoder) appendSQL(sb *strings.Builder, datum *types.Datum, col * | |||||
|
||||||
func (*tidbEncoder) Close() {} | ||||||
|
||||||
func getColumnByIndex(cols []*table.Column, index int) *table.Column { | ||||||
if index == len(cols) { | ||||||
return extraHandleTableColumn | ||||||
} else { | ||||||
return cols[index] | ||||||
} | ||||||
} | ||||||
|
||||||
func (enc *tidbEncoder) Encode(logger log.Logger, row []types.Datum, _ int64, columnPermutation []int) (Row, error) { | ||||||
cols := enc.tbl.Cols() | ||||||
|
||||||
if len(enc.columnIdx) == 0 { | ||||||
columnCount := 0 | ||||||
columnIdx := make([]int, len(columnPermutation)) | ||||||
for i, idx := range columnPermutation { | ||||||
if idx >= 0 { | ||||||
columnIdx[idx] = i | ||||||
columnCount++ | ||||||
} | ||||||
} | ||||||
enc.columnIdx = columnIdx | ||||||
enc.columnCnt = columnCount | ||||||
} | ||||||
|
||||||
// TODO: since the column count doesn't exactly reflect the real column names, we only check the upper bound currently. | ||||||
// See: tests/generated_columns/data/gencol.various_types.0.sql this sql has no columns, so encodeLoop will fill the | ||||||
// column permutation with default, thus enc.columnCnt > len(row). | ||||||
if len(row) > enc.columnCnt { | ||||||
log.L().Error("column count mismatch", zap.Ints("column_permutation", columnPermutation), | ||||||
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.
Suggested change
|
||||||
zap.Array("data", rowArrayMarshaler(row))) | ||||||
return nil, errors.Errorf("column count mismatch, expected %d, got %d", enc.columnCnt, len(row)) | ||||||
lance6716 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
var encoded strings.Builder | ||||||
|
@@ -248,7 +279,7 @@ func (enc *tidbEncoder) Encode(logger log.Logger, row []types.Datum, _ int64, co | |||||
if i != 0 { | ||||||
encoded.WriteByte(',') | ||||||
} | ||||||
if err := enc.appendSQL(&encoded, &field, cols[enc.columnIdx[i]]); err != nil { | ||||||
if err := enc.appendSQL(&encoded, &field, getColumnByIndex(cols, enc.columnIdx[i])); err != nil { | ||||||
logger.Error("tidb encode failed", | ||||||
zap.Array("original", rowArrayMarshaler(row)), | ||||||
zap.Int("originalCol", i), | ||||||
|
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
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
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.
strange that it is not linted