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
// SPDX-License-Identifier: BUSL-1.1
//! Handler for `DocumentOp::Merge`: implements the MERGE statement execution.
//!
//! Execution model (mirroring SQL MERGE semantics):
//!
//! Phase 1: Build a join map from the source collection:
//! source_join_value → source_document
//!
//! Phase 2: Walk all target rows. For each target row:
//! - If the source map has a matching entry, evaluate WHEN MATCHED arms in
//! order; apply the first arm whose extra_predicate is satisfied.
//! - If no source row matches, evaluate WHEN NOT MATCHED BY SOURCE arms.
//!
//! Phase 3: Walk source rows that had no target match. Evaluate WHEN NOT
//! MATCHED arms in order; apply the first whose extra_predicate is satisfied.
use tracing::debug;
use super::merge_helpers::{
apply_action, apply_insert_action, build_merged, find_arm, json_to_str,
};
use crate::bridge::envelope::{ErrorCode, Response};
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::doc_format;
use crate::data::executor::response_codec::encode_json;
use crate::data::executor::task::ExecutionTask;
use nodedb_physical::physical_plan::document::merge_types::{
MergeClauseKind as MergeClauseKindOp, MergeClauseOp,
};
/// Parameters for `execute_merge`.
pub(in crate::data::executor) struct MergeParams<'a> {
pub target_collection: &'a str,
pub source_collection: &'a str,
pub source_alias: &'a str,
pub target_join_col: &'a str,
pub source_join_col: &'a str,
pub clauses: &'a [MergeClauseOp],
}
impl CoreLoop {
/// Execute a MERGE statement.
pub(in crate::data::executor) fn execute_merge(
&mut self,
task: &ExecutionTask,
tid: u64,
params: MergeParams<'_>,
) -> Response {
let MergeParams {
target_collection,
source_collection,
source_alias,
target_join_col,
source_join_col,
clauses,
} = params;
debug!(
core = self.core_id,
target = %target_collection,
source = %source_collection,
"merge"
);
// Phase 1: Build source join map.
let source_map = match self.build_merge_source_map(tid, source_collection, source_join_col)
{
Ok(m) => m,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
// Check strict schema for target.
let config_key = (
crate::types::TenantId::new(tid),
target_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
}
});
// Collect all target doc IDs and their documents.
let prefix = format!("{tid}:{target_collection}:");
let end = format!("{tid}:{target_collection}:\u{ffff}");
let target_docs: Vec<(String, Vec<u8>)> = {
let read_txn = match self
.sparse
.db()
.begin_read()
.map_err(|e| crate::Error::Storage {
engine: "sparse".into(),
detail: format!("read txn: {e}"),
}) {
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
let table = match read_txn
.open_table(crate::engine::sparse::btree::DOCUMENTS)
.map_err(|e| crate::Error::Storage {
engine: "sparse".into(),
detail: format!("open table: {e}"),
}) {
Ok(t) => t,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
let mut docs = Vec::new();
if let Ok(range) = table.range(prefix.as_str()..end.as_str()) {
for entry in range.flatten() {
let key = entry.0.value();
let bytes = entry.1.value().to_vec();
if let Some(doc_id) = key.strip_prefix(&prefix) {
docs.push((doc_id.to_string(), bytes));
}
}
}
docs
};
let mut affected = 0u64;
// Track which source keys were matched against a target row.
let mut matched_source_keys: std::collections::HashSet<String> =
std::collections::HashSet::new();
// Phase 2: process target rows.
for (doc_id, bytes) in &target_docs {
let target_doc = if let Some(ref schema) = strict_schema {
match super::super::strict_format::binary_tuple_to_json(bytes, schema) {
Some(v) => v,
None => continue,
}
} else {
match doc_format::decode_document(bytes) {
Some(v) => v,
None => continue,
}
};
let join_val = target_doc
.get(target_join_col)
.map(json_to_str)
.unwrap_or_default();
if let Some(source_doc) = source_map.get(&join_val) {
matched_source_keys.insert(join_val.clone());
// Build merged document for predicate / expression evaluation.
let merged = build_merged(&target_doc, source_doc, source_alias);
// Find first MATCHED arm whose predicate is satisfied.
if let Some(arm) = find_arm(clauses, MergeClauseKindOp::Matched, &merged) {
let db_id = task.request.database_id.as_u64();
match apply_action(
self,
db_id,
tid,
target_collection,
doc_id,
&target_doc,
source_doc,
source_alias,
arm,
&strict_schema,
) {
Ok(true) => affected += 1,
Ok(false) => {}
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
}
}
} else {
// No matching source row — check NOT MATCHED BY SOURCE arms.
let merged = target_doc.clone();
if let Some(arm) = find_arm(clauses, MergeClauseKindOp::NotMatchedBySource, &merged)
{
let db_id = task.request.database_id.as_u64();
match apply_action(
self,
db_id,
tid,
target_collection,
doc_id,
&target_doc,
&serde_json::Value::Null,
source_alias,
arm,
&strict_schema,
) {
Ok(true) => affected += 1,
Ok(false) => {}
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
}
}
}
}
// Phase 3: source rows without a matching target row.
for (src_key, src_doc) in &source_map {
if matched_source_keys.contains(src_key.as_str()) {
continue;
}
if let Some(arm) = find_arm(clauses, MergeClauseKindOp::NotMatched, src_doc) {
match apply_insert_action(
self,
tid,
target_collection,
src_doc,
arm,
&strict_schema,
) {
Ok(true) => affected += 1,
Ok(false) => {}
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
}
}
}
let result = serde_json::json!({ "affected": affected });
match encode_json(&result) {
Ok(payload) => self.response_with_payload(task, payload),
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
/// Scan source collection and build join map: `join_val → document`.
fn build_merge_source_map(
&self,
tid: u64,
collection: &str,
join_col: &str,
) -> crate::Result<std::collections::HashMap<String, serde_json::Value>> {
let prefix = format!("{tid}:{collection}:");
let end = format!("{tid}:{collection}:\u{ffff}");
let read_txn = self
.sparse
.db()
.begin_read()
.map_err(|e| crate::Error::Storage {
engine: "sparse".into(),
detail: format!("read txn for merge source: {e}"),
})?;
let table = read_txn
.open_table(crate::engine::sparse::btree::DOCUMENTS)
.map_err(|e| crate::Error::Storage {
engine: "sparse".into(),
detail: format!("open merge source table: {e}"),
})?;
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
}
});
let mut map = std::collections::HashMap::new();
if let Ok(range) = table.range(prefix.as_str()..end.as_str()) {
for entry in range.flatten() {
let value_bytes = entry.1.value();
let doc = if let Some(ref schema) = strict_schema {
match super::super::strict_format::binary_tuple_to_json(value_bytes, schema) {
Some(v) => v,
None => continue,
}
} else {
match doc_format::decode_document(value_bytes) {
Some(v) => v,
None => continue,
}
};
let key = doc.get(join_col).map(json_to_str).unwrap_or_default();
if !key.is_empty() {
map.insert(key, doc);
}
}
}
Ok(map)
}
}