Skip to main content

uqa_execution/catalog/
sequence.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use uqa_sql::ast::SequenceDataType;
8use uqa_storage::SequenceOwner;
9
10mod decode;
11mod definition;
12
13pub use definition::altered_sequence_state;
14
15/// Mutable state of a single SQL sequence.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
17pub struct SequenceState {
18    pub start: i64,
19    pub increment: i64,
20    pub current: i64,
21    /// Whether `current` has already been returned by `nextval`.  Keeping this
22    /// bit avoids the lossy `start - increment` sentinel at BIGINT boundaries.
23    #[serde(default = "sequence_state_called_default")]
24    pub called: bool,
25    #[serde(default)]
26    pub log_count: i64,
27    pub data_type: SequenceDataType,
28    pub min_value: i64,
29    pub max_value: i64,
30    pub cycle: bool,
31    #[serde(default = "sequence_cache_size_default")]
32    pub cache_size: i64,
33    #[serde(default)]
34    pub definition_generation: [u8; 16],
35    #[serde(default)]
36    pub owner: Option<SequenceOwner>,
37}
38
39const fn sequence_state_called_default() -> bool {
40    // Legacy serialized states used `current = start - increment`; treating
41    // that value as called preserves their next allocation semantics.
42    true
43}
44
45const fn sequence_cache_size_default() -> i64 {
46    1
47}
48
49impl SequenceState {
50    /// The key of this allocation generation's position.
51    #[must_use]
52    pub const fn position_key(&self, object_id: [u8; 16]) -> crate::row_locks::SequencePositionKey {
53        crate::row_locks::SequencePositionKey {
54            object: object_id,
55            definition: self.definition_generation,
56        }
57    }
58
59    /// The state with the value state of the durable record its generation's latest commit holds, when there is one.
60    #[must_use]
61    pub fn with_record(mut self, record: Option<&uqa_storage::SequenceValuePosition>) -> Self {
62        if let Some(record) = record {
63            self.current = record.current;
64            self.called = record.called;
65            self.log_count = record.log_count;
66        }
67        self
68    }
69
70    /// The state at the exact position recorded for it. The value state held here is the durable record's, which runs ahead of the values handed out while a position is recorded.
71    #[must_use]
72    pub fn at_position(
73        mut self,
74        recorded: Option<crate::row_locks::RecordedSequencePosition>,
75    ) -> Self {
76        if let Some(position) =
77            recorded.and_then(|recorded| recorded.continuing((self.current, self.called)))
78        {
79            self.current = position.current;
80            self.called = position.called;
81            self.log_count = position.log_count;
82        }
83        self
84    }
85}
86
87use super::security::BoundSequenceSecurity;
88use uqa_core::RelationIdentity;
89use uqa_storage::{SequenceOptions, SequenceRow, StorageBackendError, StorageBackendResult};
90
91pub fn sequence_row(
92    name: &str,
93    object_id: [u8; 16],
94    state: SequenceState,
95    persistence: uqa_sql::ast::RelationPersistence,
96    security: &BoundSequenceSecurity,
97) -> StorageBackendResult<SequenceRow> {
98    Ok(SequenceRow {
99        relation: RelationIdentity::from_legacy_name(name).map_err(StorageBackendError::Other)?,
100        security: security.row().into(),
101        object_id,
102        definition_generation: state.definition_generation,
103        start: state.start,
104        increment: state.increment,
105        current: state.current,
106        called: state.called,
107        log_count: state.log_count,
108        persistence: persistence.catalog_code().into(),
109        owner: state.owner,
110        options: SequenceOptions {
111            data_type: state.data_type.sql_name().into(),
112            min_value: Some(state.min_value),
113            max_value: Some(state.max_value),
114            cycle: state.cycle,
115            cache_size: state.cache_size,
116        },
117    })
118}
119
120pub mod catalog_oids;
121pub mod latest_values;
122pub mod restoration;
123
124pub mod session;
125pub mod snapshot;
126pub mod values;