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 uuid panic when expression index enabled #44516

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion br/pkg/lightning/backend/kv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ go_test(
embed = [":kv"],
flaky = True,
race = "on",
shard_count = 15,
shard_count = 16,
deps = [
"//br/pkg/lightning/backend/encode",
"//br/pkg/lightning/common",
Expand All @@ -68,6 +68,7 @@ go_test(
"//parser/ast",
"//parser/model",
"//parser/mysql",
"//planner/core",
"//sessionctx",
"//table",
"//table/tables",
Expand Down
9 changes: 9 additions & 0 deletions br/pkg/lightning/backend/kv/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -266,6 +267,14 @@ func (e *BaseKVEncoder) getActualDatum(col *table.Column, rowID int64, inputDatu
case isBadNullValue:
err = col.HandleBadNull(&value, e.SessionCtx.Vars.StmtCtx, 0)
default:
// copy from the following GetColDefaultValue function, when this is true it will use getColDefaultExprValue
if col.DefaultIsExpr {
// the expression rewriter requires a non-nil TxnCtx.
e.SessionCtx.Vars.TxnCtx = new(variable.TransactionContext)
defer func() {
e.SessionCtx.Vars.TxnCtx = nil
}()
}
value, err = table.GetColDefaultValue(e.SessionCtx, col.ToInfo())
}
return value, err
Expand Down
35 changes: 35 additions & 0 deletions br/pkg/lightning/backend/kv/sql2kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
_ "github.com/pingcap/tidb/planner/core" // to setup expression.EvalAstExpr. Otherwise we cannot parse the default value
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
Expand Down Expand Up @@ -455,6 +456,40 @@ func TestEncodeMissingAutoValue(t *testing.T) {
}
}

func TestEncodeExpressionColumn(t *testing.T) {
tblInfo := mockTableInfo(t, "create table t (id varchar(40) not null DEFAULT uuid(), unique key `u_id` (`id`));")
tbl, err := tables.TableFromMeta(lkv.NewPanickingAllocators(0), tblInfo)
require.NoError(t, err)

encoder, err := lkv.NewTableKVEncoder(&encode.EncodingConfig{
Table: tbl,
SessionOptions: encode.SessionOptions{
SQLMode: mysql.ModeStrictAllTables,
SysVars: map[string]string{
"tidb_row_format_version": "2",
},
},
Logger: log.L(),
}, nil)
require.NoError(t, err)

strDatumForID := types.NewStringDatum("1")
actualDatum, err := lkv.GetActualDatum(encoder, tbl.Cols()[0], 70, &strDatumForID)
require.NoError(t, err)
require.Equal(t, strDatumForID, actualDatum)

actualDatum, err = lkv.GetActualDatum(encoder, tbl.Cols()[0], 70, nil)
require.NoError(t, err)
require.Equal(t, types.KindString, actualDatum.Kind())
require.Len(t, actualDatum.GetString(), 36) // uuid length

actualDatum2, err := lkv.GetActualDatum(encoder, tbl.Cols()[0], 70, nil)
require.NoError(t, err)
require.Equal(t, types.KindString, actualDatum2.Kind())
require.Len(t, actualDatum2.GetString(), 36)
require.NotEqual(t, actualDatum.GetString(), actualDatum2.GetString()) // check different uuid
}

func mockTableInfo(t *testing.T, createSQL string) *model.TableInfo {
parser := parser.New()
node, err := parser.ParseOneStmt(createSQL, "", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE `uuid` (
`a` bigint(30) NOT NULL,
`b` varchar(40) NOT NULL DEFAULT uuid(),
UNIQUE KEY `c` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
insert into uuid (a) values (1), (10), (100), (1000);
4 changes: 4 additions & 0 deletions br/tests/lightning_generated_columns/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ for BACKEND in 'local' 'tidb'; do
check_contains 'id: 2'
check_contains 'a: ABC'
check_contains 'b: CDSFDS'

run_sql 'ADMIN CHECK TABLE gencol.uuid'
run_sql 'SELECT /*+ use_index(gencol.uuid, c) */ COUNT(DISTINCT(b)) FROM gencol.uuid'
check_contains '4'
done