Skip to content

Fix mt_update and mt_upsert WHERE clauses for partitioned tables#4236

Merged
jeremydmiller merged 1 commit intomasterfrom
fix/4223-tag-type-auto-discovery
Apr 6, 2026
Merged

Fix mt_update and mt_upsert WHERE clauses for partitioned tables#4236
jeremydmiller merged 1 commit intomasterfrom
fix/4223-tag-type-auto-discovery

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

  • Fix mt_update_* function to include partition columns in WHERE clause alongside id = docId
  • Fix SELECT mt_version statements in both mt_update_* and mt_upsert_* to include partition columns
  • Previously, updating a document with the same ID but different partition key would target all rows matching the ID, causing duplicate key violations

Closes #4223

What Changed

  • UpsertFunction.cs — build _andPartitionWhereClause from partition columns, append to all WHERE/SELECT clauses
  • UpdateFunction.cs — append _andPartitionWhereClause to UPDATE WHERE and SELECT WHERE clauses

Example

For a table with PK (id, date):

-- Before (broken):
UPDATE mt_doc_snapshot SET ... WHERE id = docId;

-- After (fixed):
UPDATE mt_doc_snapshot SET ... WHERE id = docId AND date = arg_date;

Test plan

  • 2 tests: update and upsert with same id + different partition key both correctly target single rows

🤖 Generated with Claude Code

When tables have partition columns in their composite PK (e.g., id + date),
the generated mt_update_* function's WHERE clause only used "id = docId",
ignoring the partition column. This caused updates to target all rows with
the same id regardless of partition key, leading to duplicate key errors.

Fix: build _andPartitionWhereClause from partition columns and their
corresponding function arguments, then append to all WHERE clauses in
both UpdateFunction and UpsertFunction (UPDATE, SELECT mt_version).

The UpsertFunction's ON CONFLICT clause was already correct (uses
_primaryKeyFields), but its SELECT statements had the same issue.

Closes #4223

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit 648b6df into master Apr 6, 2026
5 of 6 checks passed
@jeremydmiller jeremydmiller deleted the fix/4223-tag-type-auto-discovery branch April 6, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistency between mt_update_xxx and mt_upsert_xxx for partitioned tables: update uses only id, upsert uses composite PK (id, date)

1 participant