Repository navigation
perf(onduplicatekey): replace O(N²) linear scan with O(1) hash map - #24069
Conversation
…r intra-batch conflict detection The OnDuplicateKey operator previously used a linear scan (checkConflict function) to detect uniqueness conflicts within the same INSERT batch. For each new row, it copied values into every row of checkConflictBatch and evaluated expressions on all rows, resulting in O(N²) time complexity and unbounded memory growth. This commit replaces the linear scan with hash map-based conflict detection: - Extract unique key column indices from UniqueColCheckExpr at init time - Serialize unique key values via vector.GetRawBytesAt() into length-prefixed byte strings for use as hash map keys - Use map[string]int per unique constraint for O(1) conflict lookups - Eliminate the checkConflictBatch (2x memory overhead) from the hot path - Properly handle NULL values (never conflict per SQL semantics) - Update hash maps after in-place row modifications Key new functions: - extractColIndicesFromExpr: walks expression tree to get column positions - serializeUniqueKey: length-prefixed binary serialization of key columns - findConflictByHashMap: O(1) conflict detection replacing O(N) scan - addToConflictMaps: maintains hash maps as rows are inserted Performance impact: INSERT...ON DUPLICATE KEY UPDATE with N rows in a single batch improves from O(N²) to O(N) for intra-batch conflict detection. Co-authored-by: Copilot <[email protected]>
Review Summary by QodoReplace O(N²) linear scan with O(1) hash map conflict detection
WalkthroughsDescription• Replace O(N²) linear scan with O(1) hash map for intra-batch conflict detection • Extract unique key column indices at initialization time • Serialize unique key values using length-prefixed binary format • Eliminate checkConflictBatch memory overhead from hot path • Properly handle NULL values per SQL semantics • Update hash maps after in-place row modifications Diagramflowchart LR
A["Linear Scan O(N²)"] -->|"Replace with"| B["Hash Map O(1)"]
B -->|"Extract indices"| C["uniqueKeyColIndices"]
B -->|"Serialize keys"| D["conflictMaps"]
C -->|"Used by"| E["findConflictByHashMap"]
D -->|"Updated by"| F["addToConflictMaps"]
E -->|"Eliminates"| G["checkConflictBatch"]
File Changes1. pkg/sql/colexec/onduplicatekey/on_duplicate_key.go
|
Code Review by Qodo
1. Conflict map overwrites earliest
|
… extraction - Add defensive validation: error out if extractColIndicesFromExpr returns empty indices instead of silently disabling conflict detection - Add extractColRefFromExpr to recursively unwrap cast/function expressions when extracting column references, for future-proofing - Replace per-call bytes.Buffer allocation in serializeUniqueKey with a reusable keyBuf stored in the container struct - Use clear() instead of make(map[string]int) to reuse map storage and reduce GC pressure (Go 1.21+) Co-authored-by: Copilot <[email protected]>
Match SQL '=' semantics for float types in hash-based conflict detection: - float32 with Scale>0: apply same rounding as func_compare.go equalFn - float32/float64: normalize -0.0 to +0.0 (IEEE 754 -0 has different bytes but SQL treats -0 == +0) This ensures the hash map agrees with the expression-based '=' operator for all float value edge cases. Co-authored-by: Copilot <[email protected]>
fixed by 057678b |
Merge Queue Status
This pull request spent 59 seconds in the queue, with no time running CI. Required conditions to merge
ReasonThe pull request #24069 has been manually updated HintIf you want to requeue this pull request, you can post a |
Merge Queue Status
This pull request spent 45 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #24030
What this PR does / why we need it:
The OnDuplicateKey operator previously used a linear scan (checkConflict function) to detect uniqueness conflicts within the same INSERT batch. For each new row, it copied values into every row of checkConflictBatch and evaluated expressions on all rows, resulting in O(N²) time complexity and unbounded memory growth.
This commit replaces the linear scan with hash map-based conflict detection:
Key new functions:
Performance impact: INSERT...ON DUPLICATE KEY UPDATE with N rows in a single batch improves from O(N²) to O(N) for intra-batch conflict detection.