Skip to main content

radixdb_executor/expression/
execution_context.rs

1use rustc_hash::FxHashMap;
2
3use crate::context::StoredFunctionInvoker;
4use crate::operator::RowRef;
5use radixdb_core::{CompactArc, Row, Value};
6use radixdb_storage::DeferredRow;
7
8/// A read-only row view accepted by the expression VM.
9#[derive(Clone, Copy)]
10pub(super) enum RowView<'a> {
11    Owned(&'a Row),
12    Deferred(&'a RowRef),
13    PortableDeferred(&'a DeferredRow),
14}
15
16impl<'a> RowView<'a> {
17    #[inline]
18    pub(super) fn get(self, index: usize) -> Option<&'a Value> {
19        match self {
20            Self::Owned(row) => row.get(index),
21            Self::Deferred(row) => row.get(index),
22            Self::PortableDeferred(row) => row.get(index),
23        }
24    }
25
26    #[cfg(test)]
27    pub(super) fn len(self) -> usize {
28        match self {
29            Self::Owned(row) => row.len(),
30            Self::Deferred(row) => row.len(),
31            Self::PortableDeferred(row) => row.len(),
32        }
33    }
34}
35
36/// Execution context for the expression VM.
37///
38/// Holds row views, correlated context, parameters and the request-local
39/// durable function dispatcher. It owns no query or transaction semantics.
40pub struct ExecuteContext<'a> {
41    /// Primary row data.
42    pub(super) row: RowView<'a>,
43    /// Second row for joins.
44    pub(super) row2: Option<RowView<'a>>,
45    /// Outer row context for correlated subqueries.
46    pub(super) outer_row: Option<&'a FxHashMap<CompactArc<str>, Value>>,
47    /// Positional parameters.
48    pub(super) params: &'a [Value],
49    /// Named parameters.
50    pub(super) named_params: Option<&'a FxHashMap<String, Value>>,
51    /// Current transaction ID.
52    pub(super) transaction_id: Option<u64>,
53    /// Request-local durable function dispatcher.
54    pub(crate) stored_function_invoker: Option<&'a std::sync::Arc<dyn StoredFunctionInvoker>>,
55}
56
57impl<'a> ExecuteContext<'a> {
58    #[inline]
59    pub fn new(row: &'a Row) -> Self {
60        Self {
61            row: RowView::Owned(row),
62            row2: None,
63            outer_row: None,
64            params: &[],
65            named_params: None,
66            transaction_id: None,
67            stored_function_invoker: None,
68        }
69    }
70
71    #[inline]
72    pub fn with_common_params(
73        row: &'a Row,
74        params: &'a [Value],
75        named_params: Option<&'a FxHashMap<String, Value>>,
76        transaction_id: Option<u64>,
77    ) -> Self {
78        Self {
79            row: RowView::Owned(row),
80            row2: None,
81            outer_row: None,
82            params,
83            named_params,
84            transaction_id,
85            stored_function_invoker: None,
86        }
87    }
88
89    pub fn for_join(row1: &'a Row, row2: &'a Row) -> Self {
90        Self {
91            row: RowView::Owned(row1),
92            row2: Some(RowView::Owned(row2)),
93            outer_row: None,
94            params: &[],
95            named_params: None,
96            transaction_id: None,
97            stored_function_invoker: None,
98        }
99    }
100
101    pub fn for_join_ref(row1: &'a RowRef, row2: &'a Row) -> Self {
102        Self {
103            row: RowView::Deferred(row1),
104            row2: Some(RowView::Owned(row2)),
105            outer_row: None,
106            params: &[],
107            named_params: None,
108            transaction_id: None,
109            stored_function_invoker: None,
110        }
111    }
112
113    pub fn for_join_refs(row1: &'a RowRef, row2: &'a RowRef) -> Self {
114        Self {
115            row: RowView::Deferred(row1),
116            row2: Some(RowView::Deferred(row2)),
117            outer_row: None,
118            params: &[],
119            named_params: None,
120            transaction_id: None,
121            stored_function_invoker: None,
122        }
123    }
124
125    pub fn for_deferred(row: &'a DeferredRow) -> Self {
126        Self {
127            row: RowView::PortableDeferred(row),
128            row2: None,
129            outer_row: None,
130            params: &[],
131            named_params: None,
132            transaction_id: None,
133            stored_function_invoker: None,
134        }
135    }
136
137    pub fn for_row_ref(row: &'a RowRef) -> Self {
138        Self {
139            row: RowView::Deferred(row),
140            row2: None,
141            outer_row: None,
142            params: &[],
143            named_params: None,
144            transaction_id: None,
145            stored_function_invoker: None,
146        }
147    }
148
149    pub fn with_transaction_id(mut self, transaction_id: Option<u64>) -> Self {
150        self.transaction_id = transaction_id;
151        self
152    }
153
154    pub(crate) fn with_stored_function_invoker(
155        mut self,
156        invoker: Option<&'a std::sync::Arc<dyn StoredFunctionInvoker>>,
157    ) -> Self {
158        self.stored_function_invoker = invoker;
159        self
160    }
161
162    pub fn with_params(mut self, params: &'a [Value]) -> Self {
163        self.params = params;
164        self
165    }
166
167    pub fn with_named_params(mut self, named_params: &'a FxHashMap<String, Value>) -> Self {
168        self.named_params = Some(named_params);
169        self
170    }
171
172    pub fn with_outer_row(mut self, outer_row: &'a FxHashMap<CompactArc<str>, Value>) -> Self {
173        self.outer_row = Some(outer_row);
174        self
175    }
176}