Commit b4d831f
fix(codegen): provenance view uses ROW_NUMBER for latest-per-entity (#82)
* fix(codegen): temporal current-version UNIQUE; valid_to CHECK
Closes #41.
`verisimdb_temporal_versions` had a non-unique partial index on
`(entity_id, table_name) WHERE valid_to IS NULL`. Two concurrent
writers could both successfully insert a row with `valid_to=NULL` for
the same entity, leaving two "current" versions and breaking the
point-in-time query semantics. There was also no constraint that
`valid_to` (when set) couldn't precede `valid_from`.
- Promote the partial index to `CREATE UNIQUE INDEX` so the storage
layer enforces "at most one current version per (entity, table)".
- Add `CHECK (valid_to IS NULL OR valid_to >= valid_from)` so a
version interval can't be backwards.
Test `test_temporal_table_has_unique_partial_index_and_valid_to_check`
asserts both clauses appear in the emitted DDL with temporal enabled.
`cargo clippy --all-targets -- -D warnings` clean; 37 unit tests pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* fix(codegen): provenance view uses ROW_NUMBER for latest-per-entity
Closes #40.
`generate_provenance_view` emitted a correlated MAX(timestamp) subquery
whose outer reference `verisimdb_provenance_log.entity_id` was
ambiguous: the same table appears in multiple nested scopes, and
depending on planner choices the correlation either bound to the wrong
scope (returning all rows for the entity) or worked accidentally. The
view did not reliably return exactly one "latest" row per entity.
Replace with a window-function pattern:
ROW_NUMBER() OVER (PARTITION BY entity_id ORDER BY timestamp DESC)
picked out as `_rn = 1`. No correlation, no scoping ambiguity, one row
per entity by construction. Supported on PostgreSQL (always) and SQLite
≥ 3.25.
Test `test_provenance_view_uses_window_function_for_latest_per_entity`
asserts the new pattern is present and the old `MAX(p2.timestamp)`
pattern is gone.
`cargo clippy --all-targets -- -D warnings` clean; 38 unit tests pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent a4dc26e commit b4d831f
1 file changed
Lines changed: 43 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
184 | 190 | | |
185 | 191 | | |
186 | 192 | | |
| |||
193 | 199 | | |
194 | 200 | | |
195 | 201 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
204 | 209 | | |
205 | 210 | | |
206 | 211 | | |
| |||
398 | 403 | | |
399 | 404 | | |
400 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
401 | 435 | | |
402 | 436 | | |
403 | 437 | | |
| |||
0 commit comments