Skip to content

Commit 4f4e5cb

Browse files
authored
check parts updated in PreInsertFullTextIndex (#22681)
skip insert when columns in index parts not updated. Approved by: @ouyuanning
1 parent 10e29e4 commit 4f4e5cb

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

pkg/sql/plan/build_dml_util.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func buildInsertPlans(
182182
}
183183
return buildInsertPlansWithRelatedHiddenTable(stmt, ctx, builder, insertBindCtx, objRef, tableDef,
184184
updateColLength, sourceStep, addAffectedRows, isFkRecursionCall, updatePkCol, pkFilterExpr,
185-
newPartitionExpr, ifExistAutoPkCol, ifNeedCheckPkDup, indexSourceColTypes, fuzzymessage, insertWithoutUniqueKeyMap, ifInsertFromUniqueColMap)
185+
newPartitionExpr, ifExistAutoPkCol, ifNeedCheckPkDup, indexSourceColTypes, fuzzymessage, insertWithoutUniqueKeyMap, ifInsertFromUniqueColMap, nil)
186186
}
187187

188188
// buildUpdatePlans build update plan.
@@ -266,7 +266,8 @@ func buildUpdatePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
266266
var fuzzymessage *OriginTableMessageForFuzzy
267267
return buildInsertPlansWithRelatedHiddenTable(nil, ctx, builder, insertBindCtx, updatePlanCtx.objRef, updatePlanCtx.tableDef,
268268
updatePlanCtx.updateColLength, sourceStep, addAffectedRows, updatePlanCtx.isFkRecursionCall, updatePlanCtx.updatePkCol,
269-
updatePlanCtx.pkFilterExprs, partitionExpr, ifExistAutoPkCol, ifNeedCheckPkDup, indexSourceColTypes, fuzzymessage, nil, nil)
269+
updatePlanCtx.pkFilterExprs, partitionExpr, ifExistAutoPkCol, ifNeedCheckPkDup, indexSourceColTypes, fuzzymessage, nil, nil,
270+
updatePlanCtx.updateColPosMap)
270271
}
271272

272273
func getStepByNodeId(builder *QueryBuilder, nodeId int32) int {
@@ -854,6 +855,7 @@ func buildInsertPlansWithRelatedHiddenTable(
854855
updatePkCol bool, pkFilterExprs []*Expr, partitionExpr *Expr, ifExistAutoPkCol bool,
855856
checkInsertPkDupForHiddenIndexTable bool, indexSourceColTypes []*plan.Type, fuzzymessage *OriginTableMessageForFuzzy,
856857
insertWithoutUniqueKeyMap map[string]bool, ifInsertFromUniqueColMap map[string]bool,
858+
updateColPosMap map[string]int,
857859
) error {
858860
//var lastNodeId int32
859861
var err error
@@ -912,7 +914,7 @@ func buildInsertPlansWithRelatedHiddenTable(
912914

913915
// TODO: choose either PostInsertFullTextIndex or PreInsertFullTextIndex
914916
if !postdml_flag && indexdef.TableExist && catalog.IsFullTextIndexAlgo(indexdef.IndexAlgo) {
915-
err = buildPreInsertFullTextIndex(stmt, ctx, builder, bindCtx, objRef, tableDef, updateColLength, sourceStep, ifInsertFromUniqueColMap, indexdef, idx)
917+
err = buildPreInsertFullTextIndex(stmt, ctx, builder, bindCtx, objRef, tableDef, updateColLength, sourceStep, ifInsertFromUniqueColMap, indexdef, idx, updateColPosMap)
916918
if err != nil {
917919
return err
918920
}
@@ -4297,10 +4299,36 @@ func buildDeleteIndexPlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *
42974299
// For INSERT, create INSERT plan with prePreInsertFullTextIndex()
42984300
// For UPDATE, create DELETE plan with prePreDeleteFullTextIndex() and then create INSERT plan with preInsertFullTextIndex().
42994301
// i.e. delete old rows and then insert new values
4300-
func buildPreInsertFullTextIndex(stmt *tree.Insert, ctx CompilerContext, builder *QueryBuilder, bindCtx *BindContext, objRef *ObjectRef, tableDef *TableDef,
4301-
updateColLength int, sourceStep int32, ifInsertFromUniqueColMap map[string]bool, indexdef *plan.IndexDef, idx int) error {
4302+
func buildPreInsertFullTextIndex(stmt *tree.Insert, ctx CompilerContext, builder *QueryBuilder, bindCtx *BindContext, objRef *ObjectRef,
4303+
tableDef *TableDef, updateColLength int, sourceStep int32, ifInsertFromUniqueColMap map[string]bool, indexdef *plan.IndexDef,
4304+
idx int, updateColPosMap map[string]int) error {
4305+
4306+
// Check if secondary key is being updated.
4307+
isSecondaryKeyUpdated := func() bool {
4308+
posMap := make(map[string]int)
4309+
for idx, col := range tableDef.Cols {
4310+
posMap[col.Name] = idx
4311+
}
4312+
4313+
for _, colName := range indexdef.Parts {
4314+
resolvedColName := catalog.ResolveAlias(colName)
4315+
if colIdx, ok := posMap[resolvedColName]; ok {
4316+
col := tableDef.Cols[colIdx]
4317+
if _, exists := updateColPosMap[resolvedColName]; exists || col.OnUpdate != nil {
4318+
return true
4319+
}
4320+
}
4321+
}
4322+
return false
4323+
}
43024324

43034325
isUpdate := (updateColLength > 0)
4326+
if isUpdate {
4327+
if updateColPosMap != nil && !isSecondaryKeyUpdated() {
4328+
// index parts not updated and skip
4329+
return nil
4330+
}
4331+
}
43044332

43054333
lastNodeId := appendSinkScanNode(builder, bindCtx, sourceStep)
43064334

0 commit comments

Comments
 (0)