1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: BUSL-1.1
//! Document collection scan handler.
use tracing::{debug, warn};
use super::decode::{decode_scanned_document, decode_scanned_document_msgpack};
use super::fetch::{DocFetchParams, DocScanMode};
use super::projection::{apply_projection, apply_projection_msgpack};
use crate::bridge::envelope::{ErrorCode, Response};
use crate::bridge::scan_filter::ScanFilter;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::doc_format;
use crate::data::executor::handlers::document::sort;
use crate::data::executor::response_codec::DocumentRow;
use crate::data::executor::strict_format;
use crate::data::executor::task::ExecutionTask;
/// Parameters for [`CoreLoop::execute_document_scan`].
pub(in crate::data::executor) struct DocumentScanParams<'a> {
pub tid: u64,
pub collection: &'a str,
pub limit: usize,
pub offset: usize,
pub sort_keys: &'a [(String, bool)],
pub filters: &'a [u8],
pub distinct: bool,
pub projection: &'a [String],
pub computed_columns_bytes: &'a [u8],
pub window_functions_bytes: &'a [u8],
pub mode: DocScanMode,
pub prefilter: Option<&'a nodedb_types::SurrogateBitmap>,
}
impl CoreLoop {
pub(in crate::data::executor) fn execute_document_scan(
&mut self,
task: &ExecutionTask,
params: DocumentScanParams<'_>,
) -> Response {
let DocumentScanParams {
tid,
collection,
limit,
offset,
sort_keys,
filters,
distinct,
projection,
computed_columns_bytes,
window_functions_bytes,
mode,
prefilter,
} = params;
debug!(
core = self.core_id,
%collection,
limit,
offset,
sort_fields = sort_keys.len(),
"document scan"
);
let _scan_guard = match self.acquire_scan_guard(task, tid, collection) {
Ok(g) => g,
Err(resp) => return resp,
};
let window_specs: Vec<crate::bridge::window_func::WindowFuncSpec> =
if window_functions_bytes.is_empty() {
Vec::new()
} else {
zerompk::from_msgpack(window_functions_bytes).unwrap_or_default()
};
let computed_cols: Vec<crate::bridge::expr_eval::ComputedColumn> =
if computed_columns_bytes.is_empty() {
Vec::new()
} else {
zerompk::from_msgpack(computed_columns_bytes).unwrap_or_default()
};
let scan_budget_bytes = self.query_tuning.max_scan_result_bytes;
let filter_predicates: Vec<ScanFilter> = if filters.is_empty() {
Vec::new()
} else {
match zerompk::from_msgpack(filters) {
Ok(f) => f,
Err(e) => {
warn!(core = self.core_id, error = %e, "failed to parse scan filters");
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("malformed scan filters: {e}"),
},
);
}
}
};
let config_key = (
task.request.database_id,
crate::types::TenantId::new(tid),
collection.to_string(),
);
let strict_schema = self.doc_configs.get(&config_key).and_then(|c| {
if let nodedb_physical::physical_plan::StorageMode::Strict { ref schema } =
c.storage_mode
{
Some(schema.clone())
} else {
None
}
});
// Fetch stage: the ONLY part that differs between a current-time read
// and a bitemporal `AS OF` / all-versions audit read. It returns the
// raw rows plus the schema the downstream should decode them with
// (`None` for temporal reads, whose bodies are already normalized to
// MessagePack with any synthetic `_ts_*` columns injected). Everything
// below runs identically for every mode, giving `AS OF` reads full
// ORDER BY / computed-column / window-function / DISTINCT parity.
let fetched = self.document_scan_fetch(
task,
tid,
DocFetchParams {
collection,
mode: &mode,
limit,
offset,
filter_predicates: &filter_predicates,
strict_schema: strict_schema.as_ref(),
},
);
match fetched {
Ok(fetched) => {
let mut filtered = fetched.rows;
let effective_schema = fetched.effective_schema;
if let Some(ref m) = self.metrics {
m.record_document_read();
}
// Read-your-own-writes for scans: fold this transaction's
// staging overlay onto the base result before any budget /
// sort / projection / limit stage, so staged inserts count
// against the budget and flow through sort+limit unchanged.
// Only current-version reads merge staged writes — temporal
// (`AS OF` / all-versions) reads never see the overlay, whose
// staged bodies are current-version only.
if mode.is_current()
&& let Some(txn_id) = task.request.txn_id
{
let coll_key = (
task.request.database_id,
crate::types::TenantId::new(tid),
collection.to_string(),
);
let matches = |value: &[u8]| -> bool {
if filter_predicates.is_empty() {
return true;
}
crate::data::executor::core_loop::filter_match::matches_with_resolved_schema(
effective_schema.as_ref(),
&filter_predicates,
value,
)
};
self.merge_overlay_into_scan(txn_id, &coll_key, &mut filtered, &matches);
}
// Bound an unbounded (no-LIMIT) scan by the memory budget. If
// the materialized result exceeds `max_scan_result_bytes`,
// surface a deterministic error instead of silently dropping
// rows. Only enforced for unbounded scans — an explicit
// `LIMIT n` is already row-bounded by the planner.
if limit == usize::MAX
&& crate::data::executor::handlers::scan_budget::scan_bytes_exceeded(
&filtered,
scan_budget_bytes,
)
{
return self.response_error(task, ErrorCode::ResourcesExhausted);
}
if let Some(pf) = prefilter {
filtered.retain(|(doc_id, _)| {
if let Ok(n) = u32::from_str_radix(doc_id, 16) {
pf.contains(nodedb_types::Surrogate::new(n))
} else {
false
}
});
}
// Strict collections may store binary tuples. Sort and projection
// operate on msgpack, so normalize binary tuples here.
let filtered = if !sort_keys.is_empty() || !projection.is_empty() {
if let Some(ref schema) = effective_schema {
filtered
.into_iter()
.map(|(id, bytes)| {
match strict_format::binary_tuple_to_msgpack(&bytes, schema) {
Some(mp) => (id, mp),
None => (id, bytes),
}
})
.collect()
} else {
filtered
}
} else {
filtered
};
let sorted = if sort_keys.is_empty() {
filtered
} else if filtered.len() <= self.query_tuning.sort_run_size {
let mut v = filtered;
if let Err(e) = sort::sort_rows(&mut v, sort_keys) {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("in-memory sort failed: {e}"),
},
);
}
v
} else {
match self.external_sort(filtered, sort_keys, limit.saturating_add(offset)) {
Ok(merged) => merged,
Err(e) => {
warn!(core = self.core_id, error = %e, "external sort failed");
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("external sort failed: {e}"),
},
);
}
}
};
let stream_chunk_size = self.query_tuning.stream_chunk_size;
if let Some(ref schema) = effective_schema
&& window_specs.is_empty()
{
// SQL DISTINCT semantics require deduplication on the
// *projected* row, not the raw document bytes — two rows
// with the same `category` but different ids/payload are
// distinct as documents but the same under
// `SELECT DISTINCT category`. Project first, then dedupe.
let projected_rows: Vec<_> = sorted
.into_iter()
.map(|(doc_id, val)| {
let mp = decode_scanned_document_msgpack(&val, Some(schema));
let projected =
apply_projection_msgpack(&mp, &computed_cols, projection);
(doc_id, projected)
})
.collect();
let deduped = if distinct {
let mut seen = std::collections::HashSet::new();
projected_rows
.into_iter()
.filter(|(_, value)| seen.insert(value.clone()))
.collect::<Vec<_>>()
} else {
projected_rows
};
let result: Vec<_> = deduped.into_iter().skip(offset).take(limit).collect();
return self.send_document_rows_raw(task, &result, stream_chunk_size);
}
if !window_specs.is_empty() {
let mut decoded_rows: Vec<(String, serde_json::Value)> = sorted
.into_iter()
.map(|(id, val)| {
let doc = decode_scanned_document(&val, effective_schema.as_ref());
(id, doc)
})
.collect();
crate::bridge::window_func::evaluate_window_functions(
&mut decoded_rows,
&window_specs,
);
// Project first, then dedupe on the projected JSON value
// so `SELECT DISTINCT col` honours SQL semantics.
let projected_rows: Vec<_> = decoded_rows
.into_iter()
.map(|(doc_id, data)| {
let projected = apply_projection(data, &computed_cols, projection);
DocumentRow {
id: doc_id,
data: projected,
}
})
.collect();
let deduped: Vec<_> = if distinct {
let mut seen = std::collections::HashSet::new();
projected_rows
.into_iter()
.filter(|row| seen.insert(row.data.to_string()))
.collect()
} else {
projected_rows
};
let result: Vec<_> = deduped.into_iter().skip(offset).take(limit).collect();
self.send_document_rows_transformed(task, &result, stream_chunk_size)
} else {
let needs_transform = !computed_cols.is_empty() || !projection.is_empty();
if needs_transform {
// Project first so DISTINCT acts on the projected
// row, not the raw document.
let projected_rows: Vec<_> = sorted
.into_iter()
.map(|(doc_id, value)| {
let mp = doc_format::json_to_msgpack(&value);
let projected =
apply_projection_msgpack(&mp, &computed_cols, projection);
(doc_id, projected)
})
.collect();
let deduped = if distinct {
let mut seen = std::collections::HashSet::new();
projected_rows
.into_iter()
.filter(|(_, value)| seen.insert(value.clone()))
.collect()
} else {
projected_rows
};
let result: Vec<_> = deduped.into_iter().skip(offset).take(limit).collect();
self.send_document_rows_raw(task, &result, stream_chunk_size)
} else {
// No projection — `SELECT DISTINCT *` semantics dedupe
// on the entire raw value, which is what the
// pre-existing path does.
let deduped = if distinct {
let mut seen = std::collections::HashSet::new();
sorted
.into_iter()
.filter(|(_, value)| seen.insert(value.clone()))
.collect()
} else {
sorted
};
let rows: Vec<_> = deduped.into_iter().skip(offset).take(limit).collect();
self.send_document_rows_raw(task, &rows, stream_chunk_size)
}
}
}
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
}