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

ddl: support pre-split index regions before creating index #57553

Merged
merged 17 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
support parsing simple expression
  • Loading branch information
tangenta committed Nov 21, 2024
commit 56628be903d41664d1e99e574dbff31cad2f9372
58 changes: 25 additions & 33 deletions pkg/ddl/index_presplit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ import (
"github.com/pingcap/tidb/pkg/expression/exprctx"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/table/tables"
"github.com/pingcap/tidb/pkg/tablecodec"
"github.com/pingcap/tidb/pkg/types"
driver "github.com/pingcap/tidb/pkg/types/parser_driver"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/chunk"
contextutil "github.com/pingcap/tidb/pkg/util/context"
Expand All @@ -58,10 +56,9 @@ func presplitIndexRegions(
if err != nil {
return errors.Trace(err)
}
evalCtx := exprCtx.GetEvalCtx()
for i, idxInfo := range allIndexInfos {
idxArg := args.IndexArgs[i]
splitArgs, err := evalSplitDatumFromArgs(evalCtx, tblInfo, idxInfo, idxArg)
splitArgs, err := evalSplitDatumFromArgs(exprCtx, tblInfo, idxInfo, idxArg)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -269,7 +266,7 @@ func splitIndexRegionAndWait(
}

func evalSplitDatumFromArgs(
evalCtx exprctx.EvalContext,
buildCtx exprctx.BuildContext,
tblInfo *model.TableInfo,
idxInfo *model.IndexInfo,
idxArg *model.IndexArg,
Expand All @@ -284,7 +281,7 @@ func evalSplitDatumFromArgs(
if len(valueList) > len(idxInfo.Columns) {
return nil, plannererrors.ErrWrongValueCountOnRow.GenWithStackByArgs(i + 1)
}
values, err := evalConstExprNodes(evalCtx, valueList, tblInfo, idxInfo)
values, err := evalConstExprNodes(buildCtx, valueList, tblInfo, idxInfo)
if err != nil {
return nil, err
}
Expand All @@ -301,7 +298,7 @@ func evalSplitDatumFromArgs(
if len(valuesItem) > len(idxInfo.Columns) {
return nil, errors.Errorf("Split index `%v` region column count doesn't match value count at %v", idxInfo.Name, name)
}
return evalConstExprNodes(evalCtx, valuesItem, tblInfo, idxInfo)
return evalConstExprNodes(buildCtx, valuesItem, tblInfo, idxInfo)
}
lowerValues, err := checkLowerUpperValue(opt.Lower, "lower")
if err != nil {
Expand All @@ -327,7 +324,7 @@ func evalSplitDatumFromArgs(
}

func evalConstExprNodes(
evalCtx exprctx.EvalContext,
buildCtx exprctx.BuildContext,
valueList []string,
tblInfo *model.TableInfo,
idxInfo *model.IndexInfo,
Expand All @@ -336,35 +333,30 @@ func evalConstExprNodes(
for j, value := range valueList {
colOffset := idxInfo.Columns[j].Offset
col := tblInfo.Columns[colOffset]
valExpr := ast.NewValueExpr(value, "", "")
switch x := valExpr.(type) {
case *driver.ValueExpr:
constant := &expression.Constant{
Value: x.Datum,
RetType: &x.Type,
}
value, err := constant.Eval(evalCtx, chunk.Row{})
if err != nil {
exp, err := expression.ParseSimpleExpr(buildCtx, value)
if err != nil {
return nil, err
}
evalCtx := buildCtx.GetEvalCtx()
value, err := exp.Eval(evalCtx, chunk.Row{})
tangenta marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

d, err := value.ConvertTo(evalCtx.TypeCtx(), &col.FieldType)
if err != nil {
if !types.ErrTruncated.Equal(err) &&
!types.ErrTruncatedWrongVal.Equal(err) &&
!types.ErrBadNumber.Equal(err) {
return nil, err
}

d, err := value.ConvertTo(evalCtx.TypeCtx(), &col.FieldType)
if err != nil {
if !types.ErrTruncated.Equal(err) &&
!types.ErrTruncatedWrongVal.Equal(err) &&
!types.ErrBadNumber.Equal(err) {
return nil, err
}
valStr, err1 := value.ToString()
if err1 != nil {
return nil, err
}
return nil, types.ErrTruncated.GenWithStack("Incorrect value: '%-.128s' for column '%.192s'", valStr, col.Name.O)
valStr, err1 := value.ToString()
if err1 != nil {
return nil, err
}
values = append(values, d)
default:
return nil, errors.New("Expect constant values")
return nil, types.ErrTruncated.GenWithStack("Incorrect value: '%-.128s' for column '%.192s'", valStr, col.Name.O)
}
values = append(values, d)
}
return values, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3180,8 +3180,8 @@ func TestDDL(t *testing.T) {
{"ALTER TABLE t ADD INDEX IF NOT EXISTS (a) USING BTREE COMMENT 'a'", true, "ALTER TABLE `t` ADD INDEX IF NOT EXISTS(`a`) USING BTREE COMMENT 'a'"},
{"ALTER TABLE t ADD INDEX (a) USING RTREE COMMENT 'a'", true, "ALTER TABLE `t` ADD INDEX(`a`) USING RTREE COMMENT 'a'"},
{"ALTER TABLE t ADD INDEX (a) PRE_SPLIT_REGIONS = 4", true, "ALTER TABLE `t` ADD INDEX(`a`) PRE_SPLIT_REGIONS = 4"},
D3Hunter marked this conversation as resolved.
Show resolved Hide resolved
{"ALTER TABLE t ADD INDEX (a) PRE_SPLIT_REGIONS 4", true, "ALTER TABLE `t` ADD INDEX(`a`) PRE_SPLIT_REGIONS = 4"},
{"ALTER TABLE t ADD INDEX (a) PRE_SPLIT_REGIONS = 'a'", false, ""},
{"ALTER TABLE t ADD INDEX (a) PRE_SPLIT_REGIONS 4", false, ""},
{"ALTER TABLE t ADD PRIMARY KEY (a) CLUSTERED PRE_SPLIT_REGIONS = 4", true, "ALTER TABLE `t` ADD PRIMARY KEY(`a`) CLUSTERED PRE_SPLIT_REGIONS = 4"},
{"ALTER TABLE t ADD PRIMARY KEY (a) PRE_SPLIT_REGIONS = 4 NONCLUSTERED", true, "ALTER TABLE `t` ADD PRIMARY KEY(`a`) NONCLUSTERED PRE_SPLIT_REGIONS = 4"},
{"ALTER TABLE t ADD INDEX (a) PRE_SPLIT_REGIONS = (between (1, 'a') and (2, 'b') regions 4);", true, "ALTER TABLE `t` ADD INDEX(`a`) PRE_SPLIT_REGIONS = (BETWEEN (1,_UTF8MB4'a') AND (2,_UTF8MB4'b') REGIONS 4)"},
Expand Down
4 changes: 2 additions & 2 deletions tests/realtikvtest/addindextest3/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestAddIndexPresplitIndexRegions(t *testing.T) {
}
require.Equal(t, 3, idxRegionCnt)

tk.MustExec("alter table t add index idx2(c) pre_split_regions = (between (0) and (100000) regions 3);")
tk.MustExec("alter table t add index idx2(c) pre_split_regions = (between (0) and (10 * 10000) regions 3);")
retRows = tk.MustQuery("show table t regions;").Rows()
idxRegionCnt = 0
for _, r := range retRows {
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestAddIndexPresplitIndexRegions(t *testing.T) {
}
}
require.Equal(t, 9, idxRegionCnt)
tk.MustExec("alter table t add index idx2(c) pre_split_regions = (between (0) and (100000) regions 3);")
tk.MustExec("alter table t add index idx2(c) pre_split_regions = (between (0) and (10 * 10000) regions 3);")
retRows = tk.MustQuery("show table t regions;").Rows()
idxRegionCnt = 0
for _, r := range retRows {
Expand Down