Skip to main content

uqa_execution/mutation/overlay/
expressions.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Evaluate physical tuple keys before borrowing command frames; retain them with their row version.
8
9use super::{
10    resource_error, Arc, BudgetedVec, CommandMutationOverlay, CommandStoredDocument, DocId,
11    Document, DocumentChanges, DocumentMetadata, KeyKind, RetainedDocumentFields, SQLError,
12    StorageBackendResult, StorageReadControl, Value,
13};
14use crate::catalog::index::physical::PhysicalIndexDefinitions;
15use crate::mutation::constraints::index_keys::IndexExpressionContext;
16
17impl CommandStoredDocument {
18    /// The expressions run before the caller locks its frame stack, so callbacks can read the original command view.
19    pub fn indexed(
20        fields: Arc<Document>,
21        metadata: DocumentMetadata,
22        table: &str,
23        indexes: &PhysicalIndexDefinitions,
24        expressions: IndexExpressionContext<'_>,
25        control: &StorageReadControl,
26    ) -> Result<Self, SQLError> {
27        let values = indexes.command_expression_values(expressions, table, &fields)?;
28        Self::new(fields, metadata, control)
29            .map_err(resource_error)?
30            .with_index_values(values, control)
31            .map_err(resource_error)
32    }
33
34    pub(super) fn with_index_values(
35        mut self,
36        values: Document,
37        control: &StorageReadControl,
38    ) -> StorageBackendResult<Self> {
39        if !values.is_empty() {
40            self.index_values = Some(RetainedDocumentFields::new(Arc::new(values), control)?);
41        }
42        Ok(self)
43    }
44}
45
46/// Exact physical tuple matches and the same command view that masks older stored keys. Transaction-private stored rows are already represented by the mutation index reader.
47pub struct CommandIndexProbe {
48    pub matches: BudgetedVec<DocId>,
49    pub changes: DocumentChanges,
50}
51
52impl CommandMutationOverlay {
53    pub fn column_matches(
54        overlays: &mut [Self],
55        table: &str,
56        fields: &[String],
57        values: &[Value],
58        control: &StorageReadControl,
59    ) -> Result<CommandIndexProbe, SQLError> {
60        Self::column_matches_with_catalog(overlays, table, fields, values, control, None)
61    }
62
63    pub fn column_matches_with_catalog(
64        overlays: &mut [Self],
65        table: &str,
66        fields: &[String],
67        values: &[Value],
68        control: &StorageReadControl,
69        catalog: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
70    ) -> Result<CommandIndexProbe, SQLError> {
71        let matches =
72            Self::matches_with_catalog(overlays, table, fields, values, control, catalog)?;
73        let changes = Self::changes(overlays, table, DocumentChanges::default(), control)?;
74        Ok(CommandIndexProbe { matches, changes })
75    }
76
77    pub fn expression_matches(
78        overlays: &mut [Self],
79        table: &str,
80        physical_key: &str,
81        values: &[Value],
82        control: &StorageReadControl,
83    ) -> Result<CommandIndexProbe, SQLError> {
84        Self::expression_matches_with_catalog(overlays, table, physical_key, values, control, None)
85    }
86
87    pub fn expression_matches_with_catalog(
88        overlays: &mut [Self],
89        table: &str,
90        physical_key: &str,
91        values: &[Value],
92        control: &StorageReadControl,
93        catalog: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
94    ) -> Result<CommandIndexProbe, SQLError> {
95        let matches = Self::matches_keys(
96            overlays,
97            table,
98            &[physical_key.to_owned()],
99            &[Value::Row(values.to_vec().into())],
100            KeyKind::Expressions,
101            control,
102            catalog,
103        )?;
104        let changes = Self::changes(overlays, table, DocumentChanges::default(), control)?;
105        Ok(CommandIndexProbe { matches, changes })
106    }
107}