surrealdb-expr 4.0.0

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
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
#![deny(clippy::wildcard_enum_match_arm)]

use std::cmp::Ordering;
use std::sync::Arc;

use revision::{DeserializeRevisioned, Revisioned, SerializeRevisioned};
use storekey::{BorrowDecode, Encode};
use surrealdb_types::{SqlFormat, ToSql, write_sql};

use crate::expr::variables::Variables;
use crate::expr::{ClosureExpr, Expr, Kind, Param};

/// An engine-provided closure implementation, opaque to the value layer.
///
/// The value layer holds and clones it but cannot invoke, compare, render,
/// or persist it. The execution layer downcasts via [`Self::as_any`] to the
/// concrete type it constructed in order to invoke the closure.
pub trait BuiltinClosureObj: Send + Sync + 'static {
	fn as_any(&self) -> &dyn std::any::Any;

	/// The values this closure captured by value, exposed so the value-depth
	/// guard can walk them. A closure that captures a value deep-clones it, so a
	/// closure capturing a closure nests exactly as a `Closure::Expr` does and
	/// must be counted the same way, or an accumulator that wraps itself in a
	/// closure each turn escapes the cap and overflows the stack on its own
	/// recursive clone or drop. A closure that captures nothing by value (it
	/// wraps a function pointer, say) returns an empty list, the default.
	fn captured_values(&self) -> Vec<&crate::val::Value> {
		Vec::new()
	}

	/// Render the closure as SurrealQL. An implementation that keeps its
	/// source renders that; the default is an opaque placeholder for a
	/// closure that has none.
	fn fmt_sql(&self, f: &mut String, _fmt: SqlFormat) {
		f.push_str(r#"|| THROW "builtin""#);
	}

	/// Whether `other` denotes the same closure as this one.
	///
	/// Consulted by [`Closure`]'s equality once identity has been ruled out:
	/// an implementation that keeps its source compares it (and whatever it
	/// captured) so two separately created values of the same closure are
	/// equal, as two `Closure::Expr` values are. The default holds that two
	/// distinct objects are distinct closures.
	fn dyn_eq(&self, _other: &dyn BuiltinClosureObj) -> bool {
		false
	}
}

#[derive(Clone)]
pub enum Closure {
	Expr {
		args: Vec<(Param, Kind)>,
		returns: Option<Kind>,
		body: Expr,
		captures: Variables,
	},
	Builtin(Arc<dyn BuiltinClosureObj>),
}

impl std::fmt::Debug for Closure {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Closure::Expr {
				args,
				returns,
				body,
				captures,
			} => f
				.debug_struct("Closure::Expr")
				.field("args", args)
				.field("returns", returns)
				.field("body", body)
				.field("captures", captures)
				.finish(),
			Closure::Builtin(_) => {
				f.debug_tuple("Closure::Builtin")
					// .field("_")
					.finish()
			}
		}
	}
}

impl PartialEq for Closure {
	fn eq(&self, other: &Self) -> bool {
		match (self, other) {
			(
				Closure::Expr {
					args: args1,
					returns: returns1,
					body: body1,
					captures: captures1,
				},
				Closure::Expr {
					args: args2,
					returns: returns2,
					body: body2,
					captures: captures2,
				},
			) => args1 == args2 && returns1 == returns2 && body1 == body2 && captures1 == captures2,
			// The same object is the same closure; otherwise the implementation
			// decides, so a closure value is at least equal to itself and a
			// compiled closure compares structurally.
			(Closure::Builtin(a), Closure::Builtin(b)) => Arc::ptr_eq(a, b) || a.dyn_eq(b.as_ref()),
			_ => false,
		}
	}
}

impl Eq for Closure {}

impl std::hash::Hash for Closure {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		match self {
			Closure::Expr {
				args,
				returns,
				body,
				captures,
			} => {
				0u8.hash(state); // discriminant
				args.hash(state);
				returns.hash(state);
				body.hash(state);
				captures.hash(state);
			}
			Closure::Builtin(_) => {
				1u8.hash(state); // discriminant
				// Note: logic is not hashed as function pointers cannot be hashed
			}
		}
	}
}

impl PartialOrd for Closure {
	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
		Some(self.cmp(other))
	}
}

impl Ord for Closure {
	fn cmp(&self, other: &Self) -> Ordering {
		if self == other {
			return Ordering::Equal;
		}
		match (self, other) {
			(
				Closure::Expr {
					args: args1,
					returns: returns1,
					body: body1,
					captures: captures1,
				},
				Closure::Expr {
					args: args2,
					returns: returns2,
					body: body2,
					captures: captures2,
				},
			) => self
				.to_sql()
				.cmp(&other.to_sql())
				.then_with(|| captures1.0.cmp(&captures2.0))
				.then_with(|| args1.len().cmp(&args2.len()))
				.then_with(|| {
					for ((p1, k1), (p2, k2)) in args1.iter().zip(args2.iter()) {
						let ord = p1
							.as_str()
							.cmp(p2.as_str())
							.then_with(|| k1.to_sql().cmp(&k2.to_sql()));
						if ord != Ordering::Equal {
							return ord;
						}
					}
					Ordering::Equal
				})
				.then_with(|| {
					returns1.as_ref().map(ToSql::to_sql).cmp(&returns2.as_ref().map(ToSql::to_sql))
				})
				.then_with(|| body1.to_sql().cmp(&body2.to_sql()))
				.then_with(|| format!("{self:?}").cmp(&format!("{other:?}"))),
			(
				Closure::Expr {
					..
				},
				Closure::Builtin(_),
			) => Ordering::Less,
			(
				Closure::Builtin(_),
				Closure::Expr {
					..
				},
			) => Ordering::Greater,
			(Closure::Builtin(a), Closure::Builtin(b)) => self
				.to_sql()
				.cmp(&other.to_sql())
				.then_with(|| a.captured_values().cmp(&b.captured_values()))
				.then_with(|| Arc::as_ptr(a).cast::<()>().cmp(&Arc::as_ptr(b).cast::<()>())),
		}
	}
}

impl Closure {
	pub fn into_expr(self) -> Expr {
		match self {
			Closure::Expr {
				args,
				returns,
				body,
				..
			} => Expr::Closure(Box::new(ClosureExpr {
				args,
				returns,
				body,
			})),
			Closure::Builtin {
				..
			} => Expr::Closure(Box::new(ClosureExpr {
				args: vec![],
				returns: None,
				body: Expr::Block(Box::default()),
			})),
		}
	}
}

impl ToSql for Closure {
	fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
		match self {
			Closure::Expr {
				args,
				returns,
				body,
				..
			} => {
				write_sql!(f, sql_fmt, "|");
				for (i, (name, kind)) in args.iter().enumerate() {
					if i > 0 {
						write_sql!(f, sql_fmt, ", ");
					}
					write_sql!(f, sql_fmt, "{name}: ");
					if matches!(kind, Kind::Either(_)) {
						write_sql!(f, sql_fmt, "<{kind}>");
					} else {
						write_sql!(f, sql_fmt, "{kind}");
					}
				}
				write_sql!(f, sql_fmt, "|");
				if let Some(returns) = &returns {
					write_sql!(f, sql_fmt, " -> {returns}");
				}
				write_sql!(f, sql_fmt, " {}", body);
			}
			Closure::Builtin(obj) => obj.fmt_sql(f, sql_fmt),
		}
	}
}

impl<F> Encode<F> for Closure {
	fn encode<W: std::io::Write>(
		&self,
		_: &mut storekey::Writer<W>,
	) -> Result<(), storekey::EncodeError> {
		Err(storekey::EncodeError::message("Closure cannot be encoded"))
	}
}

impl<'de, F> BorrowDecode<'de, F> for Closure {
	fn borrow_decode(_: &mut storekey::BorrowReader<'de>) -> Result<Self, storekey::DecodeError> {
		Err(storekey::DecodeError::message("Closure cannot be decoded"))
	}
}

std::thread_local! {
	static SPILL_SERIALIZATION_DEPTH: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

const MAX_SPILLED_CLOSURES: usize = 65_536;

struct SpillClosureTable {
	next_id: u64,
	entries: std::collections::VecDeque<(u64, Closure)>,
}

static SPILL_CLOSURES: std::sync::LazyLock<std::sync::Mutex<SpillClosureTable>> =
	std::sync::LazyLock::new(|| {
		std::sync::Mutex::new(SpillClosureTable {
			next_id: 1,
			entries: std::collections::VecDeque::new(),
		})
	});

/// Run `f` with temporary spill serialization enabled for [`Closure`] values on
/// the current thread.
///
/// Outside this scope, [`Closure`]'s `SerializeRevisioned` and
/// `DeserializeRevisioned` implementations refuse to encode or decode closures
/// so they can never be persisted to the datastore.
pub fn with_spill_serialization<R>(f: impl FnOnce() -> R) -> R {
	struct Guard;
	impl Drop for Guard {
		fn drop(&mut self) {
			SPILL_SERIALIZATION_DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
		}
	}
	SPILL_SERIALIZATION_DEPTH.with(|d| d.set(d.get() + 1));
	let _guard = Guard;
	f()
}

fn is_spill_serialization_active() -> bool {
	SPILL_SERIALIZATION_DEPTH.with(std::cell::Cell::get) > 0
}

fn stash_spilled_closure(closure: &Closure) -> u64 {
	let mut table = SPILL_CLOSURES.lock().unwrap_or_else(|e| e.into_inner());
	if let Some((id, existing)) = table.entries.back()
		&& existing == closure
	{
		return *id;
	}
	let id = table.next_id;
	table.next_id = table.next_id.wrapping_add(1);
	if table.entries.len() >= MAX_SPILLED_CLOSURES {
		table.entries.pop_front();
	}
	table.entries.push_back((id, closure.clone()));
	id
}

fn take_spilled_closure(id: u64) -> Option<Closure> {
	let table = SPILL_CLOSURES.lock().unwrap_or_else(|e| e.into_inner());
	let first = table.entries.front()?.0;
	let offset = usize::try_from(id.checked_sub(first)?).ok()?;
	let (found_id, closure) = table.entries.get(offset)?;
	(*found_id == id).then(|| closure.clone())
}

impl Revisioned for Closure {
	fn revision() -> u16 {
		1
	}
}

impl SerializeRevisioned for Closure {
	fn serialize_revisioned<W: std::io::Write>(
		&self,
		writer: &mut W,
	) -> Result<(), revision::Error> {
		if is_spill_serialization_active() {
			let id = stash_spilled_closure(self);
			return id.serialize_revisioned(writer);
		}
		Err(revision::Error::Conversion("Closures cannot be stored on disk".to_string()))
	}
}

impl DeserializeRevisioned for Closure {
	fn deserialize_revisioned<R: std::io::Read>(reader: &mut R) -> Result<Self, revision::Error> {
		if is_spill_serialization_active() {
			let id = u64::deserialize_revisioned(reader)?;
			return take_spilled_closure(id).ok_or_else(|| {
				revision::Error::Conversion("Spilled closure handle not found".to_string())
			});
		}
		Err(revision::Error::Conversion("Closures cannot be deserialized from disk".to_string()))
	}
}

impl revision::SkipRevisioned for Closure {
	fn skip_revisioned<R: std::io::Read>(reader: &mut R) -> Result<(), revision::Error> {
		if is_spill_serialization_active() {
			return u64::skip_revisioned(reader);
		}
		Err(revision::Error::Conversion("Closures cannot be skipped on the wire".to_string()))
	}

	#[inline]
	fn skip_revisioned_slice(
		reader: &mut revision::SliceReader<'_>,
	) -> Result<(), revision::Error> {
		if is_spill_serialization_active() {
			return u64::skip_revisioned_slice(reader);
		}
		Err(revision::Error::Conversion("Closures cannot be skipped on the wire".to_string()))
	}
}

impl revision::WalkRevisioned for Closure {
	type Walker<'r, R: revision::BorrowedReader + 'r> = revision::LeafWalker<'r, Closure, R>;

	fn walk_revisioned<'r, R: revision::BorrowedReader>(
		reader: &'r mut R,
	) -> Result<Self::Walker<'r, R>, revision::Error> {
		// Closures never appear on the wire; expose a leaf walker whose
		// `decode`/`skip` invariably error so callers walking from outer
		// types fail fast rather than silently advancing.
		Ok(revision::LeafWalker::new(reader))
	}
}

#[cfg(test)]
mod tests {
	use std::collections::BTreeSet;

	use super::*;
	use crate::expr::Literal;
	use crate::val::Value;

	fn expr_closure(param: &str, lit: i64) -> Closure {
		Closure::Expr {
			args: vec![(Param::from(param.to_string()), Kind::Any)],
			returns: None,
			body: Expr::Literal(Literal::Integer(lit)),
			captures: Variables::new(),
		}
	}

	#[test]
	fn closure_ord_is_consistent_with_eq() {
		let c1 = expr_closure("x", 1);
		let c1_dup = expr_closure("x", 1);
		let c2 = expr_closure("x", 2);
		let c3 = expr_closure("y", 1);

		assert_eq!(c1, c1_dup);
		assert_eq!(c1.cmp(&c1_dup), Ordering::Equal);

		assert_ne!(c1, c2);
		assert_ne!(c1.cmp(&c2), Ordering::Equal);
		assert_eq!(c1.cmp(&c2), c2.cmp(&c1).reverse());

		assert_ne!(c1, c3);
		assert_ne!(c1.cmp(&c3), Ordering::Equal);
		assert_eq!(c1.cmp(&c3), c3.cmp(&c1).reverse());

		#[expect(clippy::mutable_key_type, reason = "a closure value is ordered by its definition")]
		let mut set = BTreeSet::new();
		set.insert(Value::Closure(Box::new(c1)));
		set.insert(Value::Closure(Box::new(c1_dup)));
		set.insert(Value::Closure(Box::new(c2)));
		set.insert(Value::Closure(Box::new(c3)));
		assert_eq!(set.len(), 3);
	}

	#[test]
	fn closure_serialization_refused_outside_spill_and_roundtrips_inside_spill() {
		let c = expr_closure("x", 42);
		let mut buf = Vec::new();
		assert!(c.serialize_revisioned(&mut buf).is_err());

		with_spill_serialization(|| {
			c.serialize_revisioned(&mut buf).expect("spill serialization succeeds");
			let mut slice = buf.as_slice();
			let decoded = Closure::deserialize_revisioned(&mut slice)
				.expect("spill deserialization succeeds");
			assert_eq!(decoded, c);
		});

		let mut slice = buf.as_slice();
		assert!(Closure::deserialize_revisioned(&mut slice).is_err());
	}
}