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

lightning: fix panic due to row's column exceeds definition #33767

Merged
merged 13 commits into from
Apr 13, 2022
10 changes: 10 additions & 0 deletions br/pkg/lightning/backend/tidb/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,21 @@ func (enc *tidbEncoder) Encode(logger log.Logger, row []types.Datum, _ int64, co
// 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 {
// 1. if len(row) < enc.columnCnt: data in row cannot populate the insert statement, because
// there are enc.columnCnt elements to insert but fewer columns in row
logger.Error("column count mismatch", zap.Ints("column_permutation", columnPermutation),
zap.Array("data", kv.RowArrayMarshaler(row)))
return emptyTiDBRow, errors.Errorf("column count mismatch, expected %d, got %d", enc.columnCnt, len(row))
}

if len(row) > len(enc.columnIdx) {
// 2. if len(row) > len(columnIdx): raw row data has more columns than those
// in the table
logger.Error("column count mismatch", zap.Ints("column_count", enc.columnIdx),
zap.Array("data", kv.RowArrayMarshaler(row)))
return emptyTiDBRow, errors.Errorf("column count mismatch, at most %d but got %d", len(enc.columnIdx), len(row))
}

var encoded strings.Builder
encoded.Grow(8 * len(row))
encoded.WriteByte('(')
Expand Down
8 changes: 8 additions & 0 deletions br/pkg/lightning/backend/tidb/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,14 @@ func encodeRowsTiDB(t *testing.T, b backend.Backend, tbl table.Table) kv.Rows {
require.NoError(t, err)

row.ClassifyAndAppend(&dataRows, &dataChecksum, &indexRows, &indexChecksum)

rawRow := make([]types.Datum, 0)
Copy link
Member

Choose a reason for hiding this comment

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

Make slice cap with 15.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix it.

Copy link
Member

Choose a reason for hiding this comment

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

Make slice cap with 15.

for i := 0; i < 15; i++ {
rawRow = append(rawRow, types.NewIntDatum(0))
Copy link
Contributor

Choose a reason for hiding this comment

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

rawRow will have 29 items, but it's ok for test.

}
row, err = encoder.Encode(logger, rawRow, 1, []int{0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, "12.csv", 0)
require.NotNil(t, err)
require.Contains(t, err.Error(), "column count mismatch, at most")
return dataRows
}

Expand Down
2 changes: 0 additions & 2 deletions br/tests/lightning_routes/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ check_contains 'sum(x): 1296'
run_sql 'SHOW TABLES IN routes_a1;'
check_not_contains 'Tables_in_routes_a1: t2'

run_sql 'SHOW DATABASES;'
check_not_contains 'Database: routes_a0'