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
// SPDX-License-Identifier: BUSL-1.1
//! Recursive CTE handler: iterative fixed-point execution.
//!
//! Executes the base query once to seed the working table, then
//! repeatedly joins the collection against the working table via
//! the `join_link` until no new rows are produced (fixed point)
//! or `max_iterations` is reached.
use std::collections::HashSet;
use crate::bridge::envelope::{ErrorCode, Response};
use crate::bridge::scan_filter::ScanFilter;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::task::ExecutionTask;
impl CoreLoop {
/// Execute a recursive CTE scan.
///
/// Algorithm:
/// 1. Seed: scan collection with base_filters → working_table
/// 2. Loop: for each row in collection, check if `join_link.0` value
/// matches any `join_link.1` value in the working_table → new matches
/// 3. New matches become the working table for the next iteration
/// 4. Accumulate all results
/// 5. Repeat until no new rows or max_iterations reached
#[allow(clippy::too_many_arguments)]
pub(in crate::data::executor) fn execute_recursive_scan(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
base_filters: &[u8],
recursive_filters: &[u8],
join_link: Option<&(String, String)>,
max_iterations: usize,
distinct: bool,
limit: usize,
) -> Response {
// Scan-quiesce gate.
let _scan_guard = match self.acquire_scan_guard(task, tid, collection) {
Ok(g) => g,
Err(resp) => return resp,
};
let scan_limit = self.query_tuning.aggregate_scan_cap;
// Parse filter predicates.
let base_preds: Vec<ScanFilter> = if base_filters.is_empty() {
Vec::new()
} else {
match zerompk::from_msgpack(base_filters) {
Ok(p) => p,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("base filter deserialization failed: {e}"),
},
);
}
}
};
let recursive_preds: Vec<ScanFilter> = if recursive_filters.is_empty() {
Vec::new()
} else {
match zerompk::from_msgpack(recursive_filters) {
Ok(p) => p,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("recursive filter deserialization failed: {e}"),
},
);
}
}
};
// Check if the collection uses strict (Binary Tuple) encoding.
let config_key = (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
}
});
// Scan all documents once (used for both base and recursive steps).
let all_docs = match self.sparse.scan_documents(tid, collection, scan_limit) {
Ok(d) => d,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("recursive scan failed: {e}"),
},
);
}
};
// Convert raw bytes to msgpack. For strict docs, this requires the schema.
let to_msgpack = |value: &[u8]| -> Option<Vec<u8>> {
if let Some(ref schema) = strict_schema {
super::super::strict_format::binary_tuple_to_msgpack(value, schema)
} else {
Some(super::super::doc_format::json_to_msgpack(value))
}
};
// Step 1: Seed working table with base query results.
let mut results: Vec<Vec<u8>> = Vec::new();
let mut seen_keys: HashSet<String> = HashSet::new();
tracing::debug!(
core = self.core_id,
%collection,
all_docs = all_docs.len(),
base_preds = base_preds.len(),
strict = strict_schema.is_some(),
?join_link,
"recursive CTE: starting seed"
);
for (_doc_id, value) in &all_docs {
let mp = match to_msgpack(value) {
Some(m) => m,
None => {
tracing::debug!(core = self.core_id, "to_msgpack returned None");
continue;
}
};
if !base_preds.iter().all(|f| f.matches_binary(&mp)) {
continue;
}
let key = if distinct {
nodedb_types::msgpack_to_json_string(&mp).unwrap_or_default()
} else {
String::new()
};
if !distinct || seen_keys.insert(key) {
results.push(mp);
}
}
tracing::debug!(
core = self.core_id,
seed_count = results.len(),
"recursive CTE: seed complete"
);
// Step 2: Iterate recursive step until fixed point.
if let Some((collection_field, working_field)) = join_link {
// Working-table hash-join: each iteration finds collection rows
// where `collection_field` matches a `working_field` value from
// the previous iteration's new rows.
let mut frontier = results.clone();
for iteration in 0..max_iterations {
if results.len() >= limit || frontier.is_empty() {
break;
}
// Build hash set of working_field values from the frontier.
let frontier_values: HashSet<String> = frontier
.iter()
.filter_map(|row| extract_field_string(row, working_field))
.collect();
if frontier_values.is_empty() {
break;
}
let mut new_rows = Vec::new();
for (_doc_id, value) in &all_docs {
if results.len() + new_rows.len() >= limit {
break;
}
let mp = match to_msgpack(value) {
Some(m) => m,
None => continue,
};
// Apply recursive filters (WHERE clause from recursive branch).
if !recursive_preds.iter().all(|f| f.matches_binary(&mp)) {
continue;
}
// Check join link: collection_field value must be in frontier.
let field_val = match extract_field_string(&mp, collection_field) {
Some(v) => v,
None => continue,
};
if !frontier_values.contains(&field_val) {
continue;
}
let key = if distinct {
nodedb_types::msgpack_to_json_string(&mp).unwrap_or_default()
} else {
String::new()
};
if !distinct || seen_keys.insert(key) {
new_rows.push(mp);
}
}
if new_rows.is_empty() {
break;
}
tracing::debug!(
core = self.core_id,
iteration,
new_rows = new_rows.len(),
total = results.len() + new_rows.len(),
"recursive CTE iteration (join-link)"
);
frontier = new_rows.clone();
results.extend(new_rows);
}
} else {
// No join link — fall back to filter-only iteration (original behavior).
let mut prev_count = 0;
for iteration in 0..max_iterations {
if results.len() >= limit || results.len() == prev_count {
break;
}
prev_count = results.len();
let mut new_rows = Vec::new();
for (doc_id, value) in &all_docs {
if results.len() + new_rows.len() >= limit {
break;
}
let mp = match to_msgpack(value) {
Some(m) => m,
None => continue,
};
if !recursive_preds.iter().all(|f| f.matches_binary(&mp)) {
continue;
}
let key = if distinct {
nodedb_types::msgpack_to_json_string(&mp).unwrap_or_default()
} else {
doc_id.clone()
};
if !distinct || seen_keys.insert(key) {
new_rows.push(mp);
}
}
if new_rows.is_empty() {
break;
}
tracing::debug!(
core = self.core_id,
iteration,
new_rows = new_rows.len(),
total = results.len() + new_rows.len(),
"recursive CTE iteration (filter-only)"
);
results.extend(new_rows);
}
}
tracing::debug!(
core = self.core_id,
total = results.len(),
"recursive CTE: iteration complete"
);
// Truncate to limit.
results.truncate(limit);
// Build raw msgpack array from collected msgpack rows.
let mut payload = Vec::with_capacity(results.iter().map(|r| r.len()).sum::<usize>() + 8);
nodedb_query::msgpack_scan::write_array_header(&mut payload, results.len());
for row in &results {
payload.extend_from_slice(row);
}
self.response_with_payload(task, payload)
}
}
/// Extract a field value from a msgpack document as a string for hash lookup.
fn extract_field_string(msgpack_doc: &[u8], field_name: &str) -> Option<String> {
let value = nodedb_types::value_from_msgpack(msgpack_doc).ok()?;
match &value {
nodedb_types::Value::Object(map) => {
let v = map.get(field_name)?;
match v {
nodedb_types::Value::String(s) => Some(s.clone()),
nodedb_types::Value::Integer(i) => Some(i.to_string()),
nodedb_types::Value::Float(f) => Some(f.to_string()),
nodedb_types::Value::Bool(b) => Some(b.to_string()),
_ => Some(format!("{v:?}")),
}
}
_ => None,
}
}