Skip to main content

uqa_execution/row_locks/
physical_changes.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Physical tuple-successor tracking for rows that move between partitions.
8
9use super::{
10    epoch_is_after, lock_strengths_conflict, table_hash, CommittedRowChange,
11    CommittedRowChangeKind, CrossAttachment, DocId, LockStrength, RowChangeBaseline, RowLockKey,
12    RowLockManager, SQLError,
13};
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum PhysicalRowChangeTarget {
17    Unchanged,
18    Present { table_hash: u64, doc_id: DocId },
19    Deleted,
20}
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub(super) enum LocalPhysicalRowChangeTarget {
24    Unchanged,
25    Present(RowLockKey),
26    Deleted,
27}
28
29impl RowLockManager {
30    /// Follow committed rewrites while retaining the physical relation as well as the document id. Partition movement changes both halves of the tuple identity, so DML rechecks must not collapse the successor back onto the source leaf.
31    pub fn physical_row_successor_after(
32        &self,
33        table: &str,
34        doc_id: DocId,
35        baseline: &RowChangeBaseline,
36    ) -> Result<PhysicalRowChangeTarget, SQLError> {
37        let key = RowLockKey {
38            table: self.table_key(table),
39            doc_id,
40        };
41        if let Some(CrossAttachment::Active(coordinator)) = self.cross.as_ref() {
42            return coordinator
43                .physical_change_target_after(
44                    table_hash(table.as_bytes()),
45                    doc_id,
46                    baseline.cross_sequence,
47                    LockStrength::ForUpdate,
48                )
49                .map_err(SQLError::Internal);
50        }
51        Ok(
52            match resolve_local_physical_change_target(
53                &self.state.lock().changes,
54                key,
55                baseline.epoch,
56                LockStrength::ForUpdate,
57            ) {
58                LocalPhysicalRowChangeTarget::Unchanged => PhysicalRowChangeTarget::Unchanged,
59                LocalPhysicalRowChangeTarget::Deleted => PhysicalRowChangeTarget::Deleted,
60                LocalPhysicalRowChangeTarget::Present(target) => PhysicalRowChangeTarget::Present {
61                    table_hash: table_hash(&self.relation_bytes(target.table)),
62                    doc_id: target.doc_id,
63                },
64            },
65        )
66    }
67}
68
69pub(super) fn resolve_local_physical_change_target(
70    changes: &[CommittedRowChange],
71    key: RowLockKey,
72    baseline: u64,
73    wanted: LockStrength,
74) -> LocalPhysicalRowChangeTarget {
75    let mut current = key;
76    let mut requires_recheck = false;
77    for change in changes {
78        if !epoch_is_after(change.epoch, baseline) || change.key != current {
79            continue;
80        }
81        match change.kind {
82            CommittedRowChangeKind::Update => {
83                requires_recheck |= lock_strengths_conflict(change.strength, wanted);
84            }
85            CommittedRowChangeKind::Delete => {
86                if lock_strengths_conflict(change.strength, wanted) {
87                    return LocalPhysicalRowChangeTarget::Deleted;
88                }
89            }
90            CommittedRowChangeKind::Rewrite(successor) => {
91                if lock_strengths_conflict(change.strength, wanted) {
92                    requires_recheck = true;
93                    current = successor;
94                }
95            }
96        }
97    }
98    if requires_recheck {
99        LocalPhysicalRowChangeTarget::Present(current)
100    } else {
101        LocalPhysicalRowChangeTarget::Unchanged
102    }
103}