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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: BUSL-1.1
//! PointUpdate: read-modify-write field-level changes to a single document.
//!
//! Each assignment is either a pre-encoded literal (fast binary merge when
//! possible) or a `SqlExpr` that must be evaluated against the *current* row —
//! the evaluator is `nodedb_query::expr::SqlExpr::eval`, shared with
//! computed-column, window, and typeguard paths.
use tracing::debug;
use crate::bridge::envelope::{ErrorCode, Response, WriteSetEntry};
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::doc_format;
use crate::data::executor::handlers::returning_rows;
use crate::data::executor::task::ExecutionTask;
use crate::engine::document::store::surrogate_to_doc_id;
use nodedb_physical::physical_plan::{ReturningSpec, UpdateValue};
use nodedb_types::Surrogate;
/// Parameters for `execute_point_update`.
pub(in crate::data::executor) struct PointUpdateParams<'a> {
pub tid: u64,
pub collection: &'a str,
pub document_id: &'a str,
pub surrogate: Surrogate,
pub updates: &'a [(String, UpdateValue)],
pub returning: Option<&'a ReturningSpec>,
}
impl CoreLoop {
pub(in crate::data::executor) fn execute_point_update(
&mut self,
task: &ExecutionTask,
params: PointUpdateParams<'_>,
) -> Response {
let PointUpdateParams {
tid,
collection,
document_id,
surrogate,
updates,
returning,
} = params;
let row_key = surrogate_to_doc_id(surrogate);
let row_key = row_key.as_str();
debug!(
core = self.core_id,
%collection,
%document_id,
fields = updates.len(),
has_returning = returning.is_some(),
"point update"
);
let config_key = (
task.request.database_id,
crate::types::TenantId::new(tid),
collection.to_string(),
);
let is_strict = self.doc_configs.get(&config_key).is_some_and(|c| {
matches!(
c.storage_mode,
nodedb_physical::physical_plan::StorageMode::Strict { .. }
)
});
// Reject direct updates to generated columns.
if let Some(config) = self.doc_configs.get(&config_key)
&& let Err(e) = super::super::generated::check_generated_readonly(
updates,
&config.enforcement.generated_columns,
)
{
return self.response_error(task, e);
}
// Any non-literal assignment forces the slow decode→eval→re-encode path,
// because we need the current document to evaluate against.
let has_expr = updates
.iter()
.any(|(_, v)| matches!(v, UpdateValue::Expr(_)));
let bitemporal = self.is_bitemporal(task.request.database_id.as_u64(), tid, collection);
let sys_from_for_encode = if bitemporal {
self.bitemporal_now_ms()
} else {
0
};
let database_id = task.request.database_id.as_u64();
let get_result = if bitemporal {
self.sparse
.versioned_get_current(database_id, tid, collection, row_key)
} else {
self.sparse.get(database_id, tid, collection, row_key)
};
match get_result {
Ok(Some(current_bytes)) => {
let has_generated = self.doc_configs.get(&config_key).is_some_and(|c| {
!c.enforcement.generated_columns.is_empty()
&& super::super::generated::needs_recomputation(
updates,
&c.enforcement.generated_columns,
)
});
// Fast path: non-strict, no generated columns, all literal — merge at binary level.
let updated_bytes = if !is_strict && !has_generated && !has_expr {
let base_mp = doc_format::json_to_msgpack(¤t_bytes);
let update_pairs: Vec<(&str, &[u8])> = updates
.iter()
.filter_map(|(field, v)| match v {
UpdateValue::Literal(bytes) => Some((field.as_str(), bytes.as_slice())),
UpdateValue::Expr(_) => None,
})
.collect();
nodedb_query::msgpack_scan::merge_fields(&base_mp, &update_pairs)
} else {
// Strict, generated, or expression RHS: decode → mutate → re-encode.
let mut doc = if is_strict {
if let Some(config) = self.doc_configs.get(&config_key)
&& let nodedb_physical::physical_plan::StorageMode::Strict {
ref schema,
} = config.storage_mode
{
match super::super::super::strict_format::binary_tuple_to_json(
¤t_bytes,
schema,
) {
Some(v) => v,
None => {
return self.response_error(
task,
ErrorCode::Internal {
detail: "failed to decode Binary Tuple for update"
.into(),
},
);
}
}
} else {
return self.response_error(
task,
ErrorCode::Internal {
detail: "strict config missing during update".into(),
},
);
}
} else {
match doc_format::decode_document(¤t_bytes) {
Some(v) => v,
None => {
return self.response_error(
task,
ErrorCode::Internal {
detail: "failed to parse document for update".into(),
},
);
}
}
};
// Apply field-level updates. Expressions are evaluated
// against the current-row snapshot, so a later assignment
// observing a column updated earlier in the same statement
// still sees the pre-update value — matches PostgreSQL.
let eval_doc: nodedb_types::Value = doc.clone().into();
if let Some(obj) = doc.as_object_mut() {
for (field, update_val) in updates {
let val = match update_val {
UpdateValue::Literal(bytes) => {
match nodedb_types::json_from_msgpack(bytes) {
Ok(v) => v,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!(
"update field '{field}': msgpack decode: {e}"
),
},
);
}
}
}
UpdateValue::Expr(expr) => {
let result: nodedb_types::Value = expr.eval(&eval_doc);
// Convert nodedb_types::Value → serde_json::Value so the
// downstream re-encode path (strict or msgpack) can proceed
// through its existing json-based branches unchanged.
let json: serde_json::Value = result.into();
json
}
};
obj.insert(field.clone(), val);
}
}
// Recompute generated columns.
if has_generated
&& let Some(config) = self.doc_configs.get(&config_key)
&& let Err(e) = super::super::generated::evaluate_generated_columns(
&mut doc,
&config.enforcement.generated_columns,
)
{
return self.response_error(task, e);
}
// Re-encode.
if is_strict {
if let Some(config) = self.doc_configs.get(&config_key)
&& let nodedb_physical::physical_plan::StorageMode::Strict {
ref schema,
} = config.storage_mode
{
let ndb_val: nodedb_types::Value = doc.clone().into();
let result = if bitemporal && schema.bitemporal {
super::super::super::strict_format::value_to_binary_tuple_bitemporal(
&ndb_val,
schema,
sys_from_for_encode,
i64::MIN,
i64::MAX,
)
} else {
super::super::super::strict_format::value_to_binary_tuple(
&ndb_val, schema,
)
};
match result {
Ok(bytes) => bytes,
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("strict re-encode: {e}"),
},
);
}
}
} else {
return self.response_error(
task,
ErrorCode::Internal {
detail: "strict config missing during re-encode".into(),
},
);
}
} else {
doc_format::encode_to_msgpack(&doc)
}
};
// The plain `INDEXES` secondary-index paths for this collection.
// The non-bitemporal write must reconcile these atomically with
// the primary body so a changed value can't leave a stale index
// entry pointing at the old value.
let index_paths = self
.doc_configs
.get(&config_key)
.map(|c| c.index_paths.clone())
.unwrap_or_default();
let write_result = if bitemporal {
// Bitemporal collections keep secondary-index entries in the
// versioned index only; the update must tombstone values it
// dropped and assert current values, atomically with the new
// body. Decode old/new docs (storage-mode-aware) so the
// reindex sees the real indexed values for strict + schemaless.
let index_paths = self
.doc_configs
.get(&config_key)
.map(|c| c.index_paths.clone())
.unwrap_or_default();
let old_doc = self
.doc_configs
.get(&config_key)
.and_then(|c| self.decode_stored_document(c, ¤t_bytes));
let new_doc = self
.doc_configs
.get(&config_key)
.and_then(|c| self.decode_stored_document(c, &updated_bytes));
match new_doc {
Some(new_doc) => self
.bitemporal_update_reindex(
super::update_reindex::BitemporalUpdateReindex {
database_id,
tid,
collection,
doc_id: row_key,
sys_from_ms: sys_from_for_encode,
valid_from_ms: i64::MIN,
valid_until_ms: i64::MAX,
new_body: &updated_bytes,
index_paths: &index_paths,
old_doc: old_doc.as_ref(),
new_doc: &new_doc,
wal_lsn: task.wal_lsn(),
},
)
.map(|()| None::<Vec<u8>>),
None => self
.sparse
.versioned_put(crate::engine::sparse::btree_versioned::VersionedPut {
database_id,
tenant: tid,
coll: collection,
doc_id: row_key,
sys_from_ms: sys_from_for_encode,
valid_from_ms: i64::MIN,
valid_until_ms: i64::MAX,
body: &updated_bytes,
})
.map(|()| None::<Vec<u8>>),
}
} else if index_paths.is_empty() {
// No secondary index to maintain — nothing to diff, so the
// self-committing put is sufficient and avoids a redundant
// decode of both document images.
self.sparse
.put(database_id, tid, collection, row_key, &updated_bytes)
} else {
// Reconcile the plain secondary index atomically with the
// primary body. Decode old/new (storage-mode-aware) so the
// SET diff drops values the update removed and asserts the
// new ones in the same redb transaction — otherwise a later
// lookup on the new value misses the row and a lookup on the
// old value wrongly returns it. Mirrors the bitemporal branch.
let (old_doc, new_doc) = match self.doc_configs.get(&config_key) {
Some(cfg) => (
self.decode_stored_document(cfg, ¤t_bytes),
self.decode_stored_document(cfg, &updated_bytes),
),
None => (None, None),
};
match (old_doc, new_doc) {
(Some(old_doc), Some(new_doc)) => self
.nonbitemporal_update_reindex(
super::update_reindex::NonbitemporalUpdateReindex {
database_id,
tid,
collection,
doc_id: row_key,
new_body: &updated_bytes,
index_paths: &index_paths,
old_doc: &old_doc,
new_doc: &new_doc,
wal_lsn: task.wal_lsn(),
},
)
.map(|()| None::<Vec<u8>>),
_ => {
// Unreachable for well-formed data: both images are
// documents we just read / re-encoded. If one ever
// fails to decode we cannot compute the secondary-index
// diff, so we must NOT write the primary alone — that
// would silently desync the index (the very bug this
// path fixes). Fail loud instead.
Err(crate::Error::Storage {
engine: "sparse".into(),
detail: format!(
"non-bitemporal update: document failed to decode for \
secondary-index diff (collection {collection}, id {row_key})"
),
})
}
}
};
match write_result {
Ok(_prior) => {
self.doc_cache.put(
task.request.database_id.as_u64(),
tid,
collection,
row_key,
&updated_bytes,
);
// Maintain the secondary HNSW vector index. The body
// rewrite above (sparse.put / bitemporal_update_reindex)
// reconciled storage + the secondary btree/FTS/graph
// overlays, but never the vector index — re-index the
// surrogate's vectors from the new body so KNN search
// reflects an embedding change in the same process.
// No-op when the collection has no vector index.
let has_vectors = self.collection_has_vectors(database_id, tid, collection);
self.update_reindex_vector_indexes(
super::update_reindex_vector::UpdateVectorReindex {
database_id,
tid,
collection,
row_key,
surrogate,
new_body: &updated_bytes,
is_strict,
has_vectors,
},
);
// Maintain the sparse inverted index the same way: the
// body rewrite never touched it, so re-index the row's
// sparse literal from the new body. No-op when the
// collection declares no `SparseVector` column.
let has_sparse = self.collection_has_sparse(database_id, tid, collection);
self.update_reindex_sparse_indexes(
super::update_reindex_sparse::UpdateSparseReindex {
database_id,
tid,
collection,
row_key,
new_body: &updated_bytes,
is_strict,
has_sparse,
},
);
// Emit update event to Event Plane. `current_bytes`
// is the pre-update row already read above; the
// helper derives `WriteOp::Update` from the Some
// prior + Some new pair and handles strict→msgpack
// conversion on both sides.
self.emit_put_event(
task,
tid,
collection,
row_key,
&updated_bytes,
Some(¤t_bytes),
);
// Build the response for both the RETURNING and
// non-RETURNING branches first, then — only when the
// collection carries a secondary vector index — carry the
// surrogate + post-image back in the write-set so the
// Control Plane can mint a post-apply `Put` redo record.
// The autocommit WAL path mints none for a PointUpdate, so
// without this a WAL-only restart rebuilds the HNSW from the
// pre-update body and resurrects the old embedding.
// `updated_bytes` is moved in as its last use.
let mut response = if let Some(spec) = returning {
// Build the post-update document with id injected.
let with_id = nodedb_query::msgpack_scan::inject_str_field(
&updated_bytes,
"id",
document_id,
);
let doc = match doc_format::decode_document(&with_id) {
Some(v) => v,
None => serde_json::json!({"id": document_id}),
};
match returning_rows::build_rows_payload(spec, &[doc]) {
Ok(payload) => self.response_with_payload(task, payload),
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: format!("RETURNING encode: {e}"),
},
);
}
}
} else {
let mut payload = Vec::with_capacity(16);
nodedb_query::msgpack_scan::write_map_header(&mut payload, 1);
nodedb_query::msgpack_scan::write_kv_i64(&mut payload, "affected", 1);
self.response_with_payload(task, payload)
};
if has_vectors {
response.write_set = vec![WriteSetEntry {
surrogate: surrogate.as_u32(),
is_delete: false,
value: updated_bytes,
}];
}
response
}
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
Ok(None) => {
let mut payload = Vec::with_capacity(16);
nodedb_query::msgpack_scan::write_map_header(&mut payload, 1);
nodedb_query::msgpack_scan::write_kv_i64(&mut payload, "affected", 0);
self.response_with_payload(task, payload)
}
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
),
}
}
}