Skip to content

[fix] don't use index if it has all PK columns - #22053

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
aunjgr:fix_index
Jun 25, 2025
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
aunjgr:fix_index

Conversation

@aunjgr

@aunjgr aunjgr commented Jun 25, 2025 •

Copy link
Copy Markdown
Contributor

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue https://github.com/matrixorigin/matrixflow/issues/6758

What this PR does / why we need it:

to fix the bug that if user defines a secondary index on the primary key column, delete doesn't work


PR Type

Bug fix


Description

  • Fix table existence inconsistency in DDL operations

  • Filter out indexes covering all primary key columns

  • Consolidate index validation logic across DML operations

  • Remove redundant index existence checks


Changes walkthrough 📝

Relevant files
Bug fix
bind_delete.go
Consolidate index validation in delete operations               

pkg/sql/plan/bind_delete.go

  • Add getValidIndexes call to filter valid indexes
  • Remove redundant TableExist and IndexAlgo checks
  • Consolidate index validation logic
  • +6/-8     
    bind_insert.go
    Add index filtering logic for insert operations                   

    pkg/sql/plan/bind_insert.go

  • Add getValidIndexes function to filter indexes covering all PK columns
  • Remove scattered TableExist and IndexAlgo validation checks
  • Apply consistent index filtering across insert operations
  • +50/-18 
    bind_replace.go
    Consolidate index validation in replace operations             

    pkg/sql/plan/bind_replace.go

  • Add getValidIndexes call for index validation
  • Remove redundant TableExist checks throughout replace logic
  • Simplify index iteration by removing existence checks
  • +8/-24   
    bind_update.go
    Consolidate index validation in update operations               

    pkg/sql/plan/bind_update.go

  • Add getValidIndexes call to filter valid indexes
  • Remove vecAndTextIndexCols tracking and related checks
  • Simplify index validation by removing TableExist checks
  • +10/-27 

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @qodo-code-review

    Copy link
    Copy Markdown

    You are nearing your monthly Qodo Merge usage quota. For more information, please visit here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    5763 - PR Code Verified

    Compliant requirements:

    • Fix the bug where secondary indexes on primary key columns cause delete operations to fail
    • Resolve table existence inconsistency in DDL operations

    Requires further human verification:

    • Verification that CREATE/DROP TABLE operations now work consistently
    • Testing that delete operations work properly with secondary indexes on primary key columns

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Logic Complexity

    The getValidIndexes function has complex logic for filtering indexes that cover all primary key columns. The nested loops and map operations should be carefully reviewed for correctness, especially the logic that determines if an index covers all PK columns.

    func getValidIndexes(tableDef *plan.TableDef) (indexes []*plan.IndexDef, hasIrregularIndex bool) {
    	if tableDef == nil || len(tableDef.Indexes) == 0 {
    		return
    	}
    
    	for _, idxDef := range tableDef.Indexes {
    		if !catalog.IsRegularIndexAlgo(idxDef.IndexAlgo) {
    			hasIrregularIndex = true
    			continue
    		}
    
    		if !idxDef.TableExist {
    			continue
    		}
    
    		colMap := make(map[string]bool)
    		for _, part := range idxDef.Parts {
    			colMap[part] = true
    		}
    
    		notCoverPk := false
    		for _, part := range tableDef.Pkey.Names {
    			if !colMap[part] {
    				notCoverPk = true
    				break
    			}
    		}
    
    		if notCoverPk {
    			indexes = append(indexes, idxDef)
    		}
    	}
    
    	return
    }
    Potential Bug

    The condition if !idxDef.Unique at line 964 may skip non-unique indexes inappropriately in the context where it's checking skipUniqueIdx. This logic change from the original condition needs validation.

    if !idxDef.Unique {
    	continue
    }
    Error Handling

    The getValidIndexes function is called but the returned validIndexes directly replaces tableDef.Indexes without preserving original state. If an error occurs later, the table definition may be left in an inconsistent state.

    validIndexes, hasIrregularIndex := getValidIndexes(tableDef)
    if hasIrregularIndex {
    	return 0, moerr.NewUnsupportedDML(builder.GetContext(), "have vector index table")
    }
    tableDef.Indexes = validIndexes

    @mergify mergify Bot added the kind/bug Something isn't working label Jun 25, 2025
    @qodo-code-review

    qodo-code-review Bot commented Jun 25, 2025 •

    Copy link
    Copy Markdown

    You are nearing your monthly Qodo Merge usage quota. For more information, please visit here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add nil check for primary key

    The function doesn't handle the case where tableDef.Pkey is nil, which could
    cause a panic when accessing tableDef.Pkey.Names. Add a nil check for the
    primary key before iterating over its names.

    pkg/sql/plan/bind_insert.go [74-95]

     func getValidIndexes(tableDef *plan.TableDef) (indexes []*plan.IndexDef, hasIrregularIndex bool) {
    -    if tableDef == nil || len(tableDef.Indexes) == 0 {
    +    if tableDef == nil || len(tableDef.Indexes) == 0 || tableDef.Pkey == nil {
             return
         }
         ...
         for _, part := range tableDef.Pkey.Names {

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies a potential nil pointer dereference. The code at line 95 accesses tableDef.Pkey.Names without first checking if tableDef.Pkey is nil. Adding this check prevents a potential panic for tables without a primary key, which is a critical correctness issue.

    Medium
    • Update

    @mergify
    mergify Bot merged commit b0884fc into matrixorigin:main Jun 25, 2025
    @aunjgr
    aunjgr deleted the fix_index branch June 25, 2025 13:25
    fengttt pushed a commit to fengttt/matrixone that referenced this pull request May 7, 2026
    to fix the bug that if user defines a secondary index on the primary key column, delete doesn't work
    
    Approved by: @ouyuanning

    This branch was previously deployed

    1 inactive deployment
    ci — 70f26c9a Deployed Jun 25, 2025 by mergify[bot]
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    kind/bug Something isn't working Review effort 4/5 size/M Denotes a PR that changes [100,499] lines

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    3 participants