@@ -78,6 +78,75 @@ import (
7878 "go.uber.org/zap"
7979)
8080
81+ type indexPresplitOpt struct {
82+ Lower []string
83+ Upper []string
84+ Num int64
85+ ValueLists [][]string
86+ }
87+
88+ func buildIndexPresplitOpt (indexOpt * ast.IndexOption ) (* indexPresplitOpt , error ) {
89+ if indexOpt == nil {
90+ return nil , nil
91+ }
92+ opt := indexOpt .SplitOpt
93+ if opt == nil {
94+ return nil , nil
95+ }
96+ if len (opt .ValueLists ) > 0 {
97+ valLists := make ([][]string , 0 , len (opt .ValueLists ))
98+ for _ , lst := range opt .ValueLists {
99+ values := make ([]string , 0 , len (lst ))
100+ for _ , exp := range lst {
101+ var sb strings.Builder
102+ rCtx := format .NewRestoreCtx (format .DefaultRestoreFlags , & sb )
103+ err := exp .Restore (rCtx )
104+ if err != nil {
105+ return nil , errors .Trace (err )
106+ }
107+ values = append (values , sb .String ())
108+ }
109+ valLists = append (valLists , values )
110+ }
111+ return & indexPresplitOpt {
112+ Num : opt .Num ,
113+ ValueLists : valLists ,
114+ }, nil
115+ }
116+
117+ lowers := make ([]string , 0 , len (opt .Lower ))
118+ for _ , expL := range opt .Lower {
119+ var sb strings.Builder
120+ rCtx := format .NewRestoreCtx (format .DefaultRestoreFlags , & sb )
121+ err := expL .Restore (rCtx )
122+ if err != nil {
123+ return nil , errors .Trace (err )
124+ }
125+ lowers = append (lowers , sb .String ())
126+ }
127+ uppers := make ([]string , 0 , len (opt .Upper ))
128+ for _ , expU := range opt .Upper {
129+ var sb strings.Builder
130+ rCtx := format .NewRestoreCtx (format .DefaultRestoreFlags , & sb )
131+ err := expU .Restore (rCtx )
132+ if err != nil {
133+ return nil , errors .Trace (err )
134+ }
135+ uppers = append (uppers , sb .String ())
136+ }
137+ maxSplitRegionNum := int64 (config .GetGlobalConfig ().SplitRegionMaxNum )
138+ if opt .Num > maxSplitRegionNum {
139+ return nil , errors .Errorf ("Split index region num exceeded the limit %v" , maxSplitRegionNum )
140+ } else if opt .Num < 1 {
141+ return nil , errors .Errorf ("Split index region num should be greater than 0" )
142+ }
143+ return & indexPresplitOpt {
144+ Lower : lowers ,
145+ Upper : uppers ,
146+ Num : opt .Num ,
147+ }, nil
148+ }
149+
81150const (
82151 expressionIndexPrefix = "_V$"
83152 changingColumnPrefix = "_Col$_"
@@ -7472,6 +7541,11 @@ func (d *ddl) CreatePrimaryKey(ctx sessionctx.Context, ti ast.Ident, indexName m
74727541 }
74737542 }
74747543
7544+ splitOpt , err := buildIndexPresplitOpt (indexOption )
7545+ if err != nil {
7546+ return errors .Trace (err )
7547+ }
7548+
74757549 unique := true
74767550 sqlMode := ctx .GetSessionVars ().SQLMode
74777551 job := & model.Job {
@@ -7482,7 +7556,7 @@ func (d *ddl) CreatePrimaryKey(ctx sessionctx.Context, ti ast.Ident, indexName m
74827556 Type : model .ActionAddPrimaryKey ,
74837557 BinlogInfo : & model.HistoryInfo {},
74847558 ReorgMeta : nil ,
7485- Args : []any {unique , indexName , indexPartSpecifications , indexOption , sqlMode , nil , global },
7559+ Args : []any {unique , indexName , indexPartSpecifications , indexOption , sqlMode , nil , global , splitOpt },
74867560 Priority : ctx .GetSessionVars ().DDLReorgPriority ,
74877561 CDCWriteSource : ctx .GetSessionVars ().CDCWriteSource ,
74887562 SQLMode : ctx .GetSessionVars ().SQLMode ,
@@ -7725,6 +7799,11 @@ func (d *ddl) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde
77257799 }
77267800 }
77277801
7802+ splitOpt , err := buildIndexPresplitOpt (indexOption )
7803+ if err != nil {
7804+ return errors .Trace (err )
7805+ }
7806+
77287807 if indexOption != nil && indexOption .Tp == model .IndexTypeHypo { // for hypo-index
77297808 indexInfo , err := BuildIndexInfo (ctx , tblInfo .Columns , indexName , false , unique , global ,
77307809 indexPartSpecifications , indexOption , model .StatePublic )
@@ -7743,7 +7822,7 @@ func (d *ddl) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast.Inde
77437822 Type : model .ActionAddIndex ,
77447823 BinlogInfo : & model.HistoryInfo {},
77457824 ReorgMeta : nil ,
7746- Args : []any {unique , indexName , indexPartSpecifications , indexOption , hiddenCols , global },
7825+ Args : []any {unique , indexName , indexPartSpecifications , indexOption , hiddenCols , global , splitOpt },
77477826 Priority : ctx .GetSessionVars ().DDLReorgPriority ,
77487827 Charset : chs ,
77497828 Collate : coll ,
0 commit comments