surrealdb-expr 3.3.1

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
use std::hash::{Hash, Hasher};

use rust_decimal::Decimal;
use surrealdb_strand::Strand;
use surrealdb_types::{SqlFormat, ToSql};

use crate::expr::{Expr, RecordIdLit};
use crate::val::{Bytes, Datetime, Duration, File, Geometry, Regex, Uuid};

// Note: Literal uses `Strand` (not `String`) for the inner value of
// `Literal::String` so that converting between `Value` and `Expr` via
// `into_literal()` is a zero-cost move rather than a heap allocation.
// `Literal` is not revisioned, so there is no on-disk compatibility
// concern with changing the inner type.

/// A literal value, should be computed to get an actual value.
///
/// # Note regarding equality.
/// A literal is equal to an other literal if it is the exact same byte
/// representation, so normal float rules regarding equality do not apply, i.e.
/// if `a != b` then `Literal::Float(a)` could still be equal
/// to `Literal::Float(b)` in the case of `NaN` floats for example. Also
/// surrealql rules regarding number equality are not observed, 1f != 1dec.

#[derive(Clone, Debug)]
pub enum Literal {
	None,
	Null,
	// An unbounded range, i.e. `..` without any start or end bound.
	UnboundedRange,
	Bool(bool),
	Float(f64),
	Integer(i64),
	Decimal(Decimal),
	String(Strand),
	Bytes(Bytes),
	Regex(Regex),
	RecordId(RecordIdLit),
	Array(Vec<Expr>),
	Set(Vec<Expr>),
	Object(Vec<ObjectEntry>),
	Duration(Duration),
	Datetime(Datetime),
	Uuid(Uuid),
	Geometry(Geometry),
	File(File),
}

impl Literal {
	pub fn is_static(&self) -> bool {
		match self {
			Literal::None
			| Literal::Null
			| Literal::UnboundedRange
			| Literal::Bool(_)
			| Literal::Float(_)
			| Literal::Integer(_)
			| Literal::Decimal(_)
			| Literal::String(_)
			| Literal::Bytes(_)
			| Literal::Regex(_)
			| Literal::Duration(_)
			| Literal::Datetime(_)
			| Literal::Uuid(_)
			| Literal::File(_)
			| Literal::Geometry(_) => true,
			Literal::RecordId(record_id_lit) => record_id_lit.is_static(),
			Literal::Array(exprs) => exprs.iter().all(|x| x.is_static()),
			Literal::Set(exprs) => exprs.iter().all(|x| x.is_static()),
			Literal::Object(items) => items.iter().all(|x| x.value.is_static()),
		}
	}

	/// Whether evaluating this literal can modify data. Composite literals
	/// (arrays, sets, objects, record ids) evaluate their element expressions
	/// in place, so those must be inspected; scalars cannot write.
	pub fn read_only(&self) -> bool {
		match self {
			Literal::None
			| Literal::Null
			| Literal::UnboundedRange
			| Literal::Bool(_)
			| Literal::Float(_)
			| Literal::Integer(_)
			| Literal::Decimal(_)
			| Literal::String(_)
			| Literal::Bytes(_)
			| Literal::Regex(_)
			| Literal::Duration(_)
			| Literal::Datetime(_)
			| Literal::Uuid(_)
			| Literal::File(_)
			| Literal::Geometry(_) => true,
			Literal::RecordId(record_id_lit) => record_id_lit.read_only(),
			Literal::Array(exprs) => exprs.iter().all(|x| x.read_only()),
			Literal::Set(exprs) => exprs.iter().all(|x| x.read_only()),
			Literal::Object(items) => items.iter().all(|x| x.value.read_only()),
		}
	}

	/// The value this literal denotes, when it denotes one on its own.
	///
	/// `Some` when no part of the literal needs the evaluator: the scalars, and
	/// the collections whose every element is itself directly convertible. The
	/// result is required to equal what evaluating the literal produces, so a
	/// caller may use either interchangeably. In particular an object's entries
	/// are ordered and a repeated key resolves to its last occurrence, matching
	/// evaluation.
	///
	/// `None` for anything whose value is not a property of the literal alone:
	/// a record id with a generated key (the key is allocated when the literal
	/// is evaluated), and any collection containing such an element. `None` is
	/// always a safe answer, so a caller must treat it as "ask the evaluator"
	/// rather than as "not a value".
	///
	/// This is the shape a `.surql` import consists of almost entirely, and
	/// answering it without the evaluator is what keeps import off the async
	/// stack: see the callers in the legacy literal evaluator and in the
	/// planner.
	pub fn as_static_value(&self) -> Option<crate::val::Value> {
		use crate::val::Value;

		let value = match self {
			Literal::None => Value::None,
			Literal::Null => Value::Null,
			Literal::UnboundedRange => Value::Range(Box::new(crate::val::Range::unbounded())),
			Literal::Bool(x) => Value::Bool(*x),
			Literal::Float(x) => Value::Number(crate::val::Number::Float(*x)),
			Literal::Integer(x) => Value::Number(crate::val::Number::Int(*x)),
			Literal::Decimal(x) => Value::Number(crate::val::Number::Decimal(*x)),
			Literal::String(x) => Value::String(x.clone()),
			Literal::Bytes(x) => Value::Bytes(x.clone()),
			Literal::Regex(x) => Value::Regex(x.clone()),
			Literal::Duration(x) => Value::Duration(*x),
			Literal::Datetime(x) => Value::Datetime(*x),
			Literal::Uuid(x) => Value::Uuid(*x),
			Literal::Geometry(x) => Value::Geometry(x.clone()),
			Literal::File(x) => Value::File(x.clone()),
			// Only the key forms that are values in their own right. A
			// generated key is allocated during evaluation, and the compound
			// keys hold expressions.
			Literal::RecordId(rid) => {
				use crate::expr::RecordIdKeyLit;
				let key = match &rid.key {
					RecordIdKeyLit::Number(x) => crate::val::RecordIdKey::Number(*x),
					RecordIdKeyLit::String(x) => crate::val::RecordIdKey::String(x.clone()),
					RecordIdKeyLit::Uuid(x) => crate::val::RecordIdKey::Uuid(*x),
					_ => return None,
				};
				Value::RecordId(crate::val::RecordId::new(rid.table.clone(), key))
			}
			Literal::Array(exprs) => {
				let mut values = Vec::with_capacity(exprs.len());
				for e in exprs {
					values.push(e.as_static_value()?);
				}
				Value::Array(crate::val::Array(values))
			}
			Literal::Set(exprs) => {
				let mut set = crate::val::Set::new();
				for e in exprs {
					set.insert(e.as_static_value()?);
				}
				Value::Set(set)
			}
			// Built through a sorted map, exactly as evaluation builds it, so
			// key ordering and duplicate-key resolution cannot drift apart.
			Literal::Object(items) => {
				let mut map = std::collections::BTreeMap::new();
				for i in items {
					map.insert(i.key.clone(), i.value.as_static_value()?);
				}
				Value::Object(crate::val::Object::from(map))
			}
		};
		Some(value)
	}
}

impl PartialEq for Literal {
	fn eq(&self, other: &Self) -> bool {
		match (self, other) {
			(Literal::None, Literal::None) => true,
			(Literal::Null, Literal::Null) => true,
			(Literal::UnboundedRange, Literal::UnboundedRange) => true,
			(Literal::Bool(a), Literal::Bool(b)) => a == b,
			(Literal::Float(a), Literal::Float(b)) => a.to_bits() == b.to_bits(),
			(Literal::Integer(a), Literal::Integer(b)) => a == b,
			(Literal::Decimal(a), Literal::Decimal(b)) => a == b,
			(Literal::String(a), Literal::String(b)) => a == b,
			(Literal::Bytes(a), Literal::Bytes(b)) => a == b,
			(Literal::Regex(a), Literal::Regex(b)) => a == b,
			(Literal::RecordId(a), Literal::RecordId(b)) => a == b,
			(Literal::Array(a), Literal::Array(b)) => a == b,
			(Literal::Set(a), Literal::Set(b)) => a == b,
			(Literal::Object(a), Literal::Object(b)) => a == b,
			(Literal::Duration(a), Literal::Duration(b)) => a == b,
			(Literal::Datetime(a), Literal::Datetime(b)) => a == b,
			(Literal::Uuid(a), Literal::Uuid(b)) => a == b,
			(Literal::Geometry(a), Literal::Geometry(b)) => a == b,
			(Literal::File(a), Literal::File(b)) => a == b,
			// Every variant must appear above. This arm is for mismatched
			// variants only: a variant missing from the list falls through to
			// it and compares unequal to itself, which breaks the `Eq` asserted
			// below and silently defeats every structural comparison of an
			// expression tree containing it.
			_ => false,
		}
	}
}
impl Eq for Literal {}

impl Hash for Literal {
	fn hash<H: Hasher>(&self, state: &mut H) {
		std::mem::discriminant(self).hash(state);
		match self {
			Literal::None => {}
			Literal::Null => {}
			Literal::UnboundedRange => {}
			Literal::Bool(x) => x.hash(state),
			Literal::Float(x) => x.to_bits().hash(state),
			Literal::Integer(x) => x.hash(state),
			Literal::Decimal(x) => x.hash(state),
			Literal::String(x) => x.hash(state),
			Literal::Bytes(x) => x.hash(state),
			Literal::Regex(x) => x.hash(state),
			Literal::RecordId(x) => x.hash(state),
			Literal::Array(x) => x.hash(state),
			Literal::Set(x) => x.hash(state),
			Literal::Object(x) => x.hash(state),
			Literal::Duration(x) => x.hash(state),
			Literal::Datetime(x) => x.hash(state),
			Literal::Uuid(x) => x.hash(state),
			Literal::Geometry(x) => x.hash(state),
			Literal::File(x) => x.hash(state),
		}
	}
}

impl ToSql for Literal {
	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
		let lit: crate::sql::Literal = self.clone().into();
		lit.fmt_sql(f, fmt);
	}
}

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ObjectEntry {
	pub key: Strand,
	pub value: Expr,
}

impl ToSql for ObjectEntry {
	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
		let entry: crate::sql::literal::ObjectEntry = self.clone().into();
		entry.fmt_sql(f, fmt);
	}
}

#[cfg(test)]
mod equality_tests {
	use rust_decimal::Decimal;

	use super::Literal;
	use crate::val::Strand;

	/// `Eq` is asserted for `Literal`, so every variant must equal itself.
	///
	/// The impl is hand-written with a `_ => false` fallthrough, so a variant
	/// omitted from it compares unequal to itself rather than failing to
	/// compile. That breaks any structural comparison of an expression
	/// containing it: a change-detector sees a spurious difference, and an
	/// index-guard match sees none.
	#[test]
	fn every_variant_equals_itself() {
		// The exhaustive match is the point: it makes adding a variant a
		// compile error here, so the sample list cannot fall behind the enum
		// the way a hand-maintained subset does.
		fn _forces_this_list_to_be_updated(l: &Literal) {
			match l {
				Literal::None
				| Literal::Null
				| Literal::UnboundedRange
				| Literal::Bool(_)
				| Literal::Float(_)
				| Literal::Integer(_)
				| Literal::Decimal(_)
				| Literal::String(_)
				| Literal::Bytes(_)
				| Literal::Regex(_)
				| Literal::RecordId(_)
				| Literal::Array(_)
				| Literal::Set(_)
				| Literal::Object(_)
				| Literal::Duration(_)
				| Literal::Datetime(_)
				| Literal::Uuid(_)
				| Literal::Geometry(_)
				| Literal::File(_) => {}
			}
		}
		let samples = [
			Literal::None,
			Literal::Null,
			Literal::UnboundedRange,
			Literal::Bool(true),
			Literal::Float(1.5),
			Literal::Float(-0.0),
			Literal::Integer(7),
			Literal::Decimal(Decimal::new(150, 2)),
			Literal::String(Strand::new("s")),
			Literal::Bytes(crate::val::Bytes::from(vec![1u8])),
			Literal::Regex("a".parse::<crate::val::Regex>().unwrap()),
			Literal::RecordId(crate::expr::RecordIdLit {
				table: "t".into(),
				key: crate::expr::RecordIdKeyLit::Number(1),
			}),
			Literal::Array(vec![]),
			Literal::Set(vec![]),
			Literal::Object(vec![]),
			Literal::Duration(crate::val::Duration::from_secs(1)),
			Literal::Datetime(crate::val::Datetime::MIN_UTC),
			Literal::Uuid(crate::val::Uuid::nil()),
			Literal::Geometry(crate::val::Geometry::Point(geo::Point::new(1.0, 2.0))),
			Literal::File(crate::val::File::new("b".to_owned(), "p".to_owned())),
		];
		for s in samples {
			assert_eq!(s, s.clone(), "{s:?} must equal itself");
		}
	}

	/// `Float` compares by bit pattern, so the two zeroes are distinct — they
	/// render differently (`-0f` vs `0f`), and treating them as equal would let
	/// a rewrite that changes one into the other be dropped as a no-op.
	#[test]
	fn signed_zero_is_not_equal_to_zero() {
		assert_ne!(Literal::Float(-0.0), Literal::Float(0.0));
	}
}

#[cfg(test)]
mod static_value_tests {
	use surrealdb_strand::Strand;

	use super::{Literal, ObjectEntry};
	use crate::expr::record_id::RecordIdKeyGen;
	use crate::expr::{Expr, RecordIdKeyLit, RecordIdLit};
	use crate::val::Value;

	fn entry(key: &str, value: Literal) -> ObjectEntry {
		ObjectEntry {
			key: Strand::new(key),
			value: Expr::Literal(value),
		}
	}

	/// An object's entries come out ordered by key regardless of source order,
	/// because the value has to be indistinguishable from the evaluated one.
	#[test]
	fn object_entries_are_ordered_by_key() {
		let lit =
			Literal::Object(vec![entry("z", Literal::Integer(1)), entry("a", Literal::Integer(2))]);
		let Some(Value::Object(obj)) = lit.as_static_value() else {
			panic!("a literal object of literals is a value");
		};
		let keys: Vec<&str> = obj.iter().map(|(k, _)| k.as_str()).collect();
		assert_eq!(keys, ["a", "z"]);
	}

	/// A repeated key resolves to its last occurrence, matching what inserting
	/// each entry in source order produces.
	#[test]
	fn repeated_object_key_keeps_the_last() {
		let lit =
			Literal::Object(vec![entry("a", Literal::Integer(1)), entry("a", Literal::Integer(3))]);
		let Some(Value::Object(obj)) = lit.as_static_value() else {
			panic!("a literal object of literals is a value");
		};
		assert_eq!(obj.len(), 1);
		assert_eq!(obj.get("a"), Some(&Value::Number(crate::val::Number::Int(3))));
	}

	/// A generated record-id key is allocated during evaluation, so a literal
	/// carrying one is not a value on its own — and neither is any collection
	/// holding it, or the fast path would hand out ids the evaluator never
	/// allocated.
	#[test]
	fn generated_record_id_key_is_not_static() {
		let lit = Literal::RecordId(RecordIdLit {
			table: "t".into(),
			key: RecordIdKeyLit::Generate(RecordIdKeyGen::Ulid),
		});
		assert!(lit.as_static_value().is_none());
		let nested = Literal::Object(vec![entry("id", lit.clone())]);
		assert!(nested.as_static_value().is_none());
		let in_array = Literal::Array(vec![Expr::Literal(lit)]);
		assert!(in_array.as_static_value().is_none());
	}

	/// A collection is a value only if every element is, so one non-literal
	/// element withholds the whole collection from the fast path.
	#[test]
	fn non_literal_element_withholds_the_collection() {
		let lit = Literal::Array(vec![
			Expr::Literal(Literal::Integer(1)),
			Expr::Idiom(crate::expr::Idiom::field("f".to_owned())),
		]);
		assert!(lit.as_static_value().is_none());
	}

	/// Simple record-id keys are values, which is what makes an exported
	/// record's `id` field take the fast path.
	#[test]
	fn simple_record_id_keys_are_static() {
		for key in [
			RecordIdKeyLit::Number(1),
			RecordIdKeyLit::String(Strand::new("a")),
			RecordIdKeyLit::Uuid(crate::val::Uuid::nil()),
		] {
			let lit = Literal::RecordId(RecordIdLit {
				table: "t".into(),
				key,
			});
			assert!(matches!(lit.as_static_value(), Some(Value::RecordId(_))));
		}
	}
}