surrealdb-core 3.2.3

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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::ops::Bound;

use reblessive::Stk;

use super::{ParseResult, Parser};
use crate::syn::error::bail;
use crate::syn::lexer::Lexer;
use crate::syn::lexer::compound::{self, Numeric};
use crate::syn::parser::mac::{expected, expected_whitespace};
use crate::syn::parser::{enter_object_recursion, unexpected};
use crate::syn::token::{Span, TokenKind, t};
use crate::types::{
	PublicArray, PublicDuration, PublicFile, PublicGeometry, PublicNumber, PublicObject,
	PublicRange, PublicRecordId, PublicRecordIdKey, PublicSet, PublicTable, PublicUuid,
	PublicValue,
};

trait ValueParseFunc {
	async fn parse(parser: &mut Parser<'_>, stk: &mut Stk) -> ParseResult<PublicValue>;
}

struct SurrealQL;
struct Json;

impl ValueParseFunc for SurrealQL {
	async fn parse(parser: &mut Parser<'_>, stk: &mut Stk) -> ParseResult<PublicValue> {
		parser.parse_value(stk).await
	}
}

impl ValueParseFunc for Json {
	async fn parse(parser: &mut Parser<'_>, stk: &mut Stk) -> ParseResult<PublicValue> {
		parser.parse_json(stk).await
	}
}

impl Parser<'_> {
	/// Parse a complete value which cannot contain non-literal expressions.
	pub async fn parse_value(&mut self, stk: &mut Stk) -> ParseResult<PublicValue> {
		let token = self.peek();
		let res = match token.kind {
			t!("NONE") => {
				self.pop_peek();
				PublicValue::None
			}
			t!("NULL") => {
				self.pop_peek();
				PublicValue::Null
			}
			TokenKind::NaN => {
				self.pop_peek();
				PublicValue::Number(PublicNumber::Float(f64::NAN))
			}
			TokenKind::Infinity => {
				self.pop_peek();
				PublicValue::Number(PublicNumber::Float(f64::INFINITY))
			}
			t!("true") => {
				self.pop_peek();
				PublicValue::Bool(true)
			}
			t!("false") => {
				self.pop_peek();
				PublicValue::Bool(false)
			}
			t!("{") => {
				let open = self.pop_peek().span;

				if self.eat(t!("}")) {
					return Ok(PublicValue::Object(PublicObject::new()));
				}

				// First, check if it's an empty set. `{,}` is an empty set.
				if self.eat(t!(",")) {
					self.expect_closing_delimiter(t!("}"), open)?;
					return Ok(PublicValue::Set(PublicSet::new()));
				}

				enter_object_recursion!(this = self => {
					if let t!("\"")
					| t!("'")
					| TokenKind::Identifier
					| TokenKind::Digits
					| TokenKind::Keyword(_)
					| TokenKind::Language(_)
					| TokenKind::Algorithm(_)
					| TokenKind::Distance(_)
					| TokenKind::VectorType(_) = this.peek().kind
						&& let Some(x) = this
							.speculate(stk, async |stk, this| {
								let key = this.parse_object_key()?;
								if !this.eat(t!(":")) {
									return Ok(None);
								}
								let value = stk.run(|stk| this.parse_value(stk)).await?;
								let mut res = BTreeMap::new();
								res.insert(key, value);

								if this.eat(t!(",")) {
									this.parse_value_object::<SurrealQL>(stk, open, res).await.map(Some)
								} else {
									this.expect_closing_delimiter(t!("}"), open)?;
									Ok(Some(PublicObject::from(res)))
								}
							})
							.await?
					{
						if let Some(x) = PublicGeometry::try_from_object(&x) {
							return Ok(PublicValue::Geometry(x));
						} else {
							return Ok(PublicValue::Object(x));
						}
					}

					// It must be a set: `{1, 2, 3}` or `{value}`
					let set = this.parse_value_set::<SurrealQL>(stk, token.span).await?;
					PublicValue::Set(set)
				})
			}
			t!("[") => {
				self.pop_peek();
				enter_object_recursion!(this = self => {
					this.parse_value_array::<SurrealQL>(stk, token.span)
						.await
						.map(PublicValue::Array)?
				})
			}
			t!("\"") | t!("'") => {
				let strand = self.parse_string_lit()?;
				if self.settings.legacy_strands {
					self.reparse_json_legacy_strand(stk, strand).await
				} else {
					PublicValue::String(strand)
				}
			}
			t!("d\"") | t!("d'") => PublicValue::Datetime(self.next_token_value()?),
			t!("u\"") | t!("u'") => PublicValue::Uuid(self.next_token_value()?),
			t!("b\"") | t!("b'") => PublicValue::Bytes(self.next_token_value()?),
			t!("f\"") | t!("f'") => {
				if !self.settings.files_enabled {
					unexpected!(self, token, "the experimental files feature to be enabled");
				}

				let file = self.next_token_value::<PublicFile>()?;
				PublicValue::File(file)
			}
			t!("/") => {
				let regex = self.next_token_value()?;
				PublicValue::Regex(regex)
			}
			t!("(") => {
				let open = self.pop_peek().span;
				let peek = self.peek();
				match peek.kind {
					t!("+") | t!("-") | TokenKind::Digits => {
						let before = peek.span;
						let number = self.next_token_value::<Numeric>()?;
						let number_span = before.covers(self.last_span());
						if self.peek().kind == t!(",") {
							let x = match number {
								Numeric::Duration(_) | Numeric::Decimal(_) => {
									bail!("Unexpected token, expected a non-decimal, non-NaN, number",
										@number_span => "Coordinate numbers can't be NaN or a decimal");
								}
								Numeric::Float(x) if x.is_nan() => {
									bail!("Unexpected token, expected a non-decimal, non-NaN, number",
										@number_span => "Coordinate numbers can't be NaN or a decimal");
								}
								Numeric::Float(x) => x,
								Numeric::Integer(x) => x.into_int(number_span)? as f64,
							};

							self.pop_peek();

							let y = self.next_token_value::<f64>()?;
							self.expect_closing_delimiter(t!(")"), open)?;
							PublicValue::Geometry(PublicGeometry::Point(geo::Point::new(x, y)))
						} else {
							self.expect_closing_delimiter(t!(")"), open)?;

							match number {
								Numeric::Float(x) => PublicValue::Number(PublicNumber::Float(x)),
								Numeric::Integer(x) => {
									PublicValue::Number(PublicNumber::Int(x.into_int(number_span)?))
								}
								Numeric::Decimal(x) => {
									PublicValue::Number(PublicNumber::Decimal(x))
								}
								Numeric::Duration(duration) => {
									PublicValue::Duration(PublicDuration::from(duration))
								}
							}
						}
					}
					_ => {
						enter_object_recursion!(this = self => {
							let res = stk.run(|stk| this.parse_value(stk)).await?;
							this.expect_closing_delimiter(t!(")"), open)?;
							res
						})
					}
				}
			}
			t!("..") => {
				self.pop_peek();
				match self.peek_whitespace().map(|x| x.kind) {
					Some(t!("=")) => {
						self.pop_peek();
						enter_object_recursion!(this = self => {
							let v = stk.run(|stk| this.parse_value(stk)).await?;
							PublicValue::Range(Box::new(PublicRange {
								start: Bound::Unbounded,
								end: Bound::Included(v),
							}))
						})
					}
					Some(x) if Self::kind_starts_expression(x) => {
						enter_object_recursion!(this = self => {
							let v = stk.run(|stk| this.parse_value(stk)).await?;
							PublicValue::Range(Box::new(PublicRange {
								start: Bound::Unbounded,
								end: Bound::Excluded(v),
							}))
						})
					}
					_ => PublicValue::Range(Box::new(PublicRange {
						start: Bound::Unbounded,
						end: Bound::Unbounded,
					})),
				}
			}
			t!("-") | t!("+") | TokenKind::Digits => {
				self.pop_peek();
				let compound = self.lex_compound(token, compound::numeric)?;
				match compound.value {
					Numeric::Duration(x) => PublicValue::Duration(PublicDuration::from(x)),
					Numeric::Integer(x) => {
						PublicValue::Number(PublicNumber::Int(x.into_int(compound.span)?))
					}
					Numeric::Float(x) => PublicValue::Number(PublicNumber::Float(x)),
					Numeric::Decimal(x) => PublicValue::Number(PublicNumber::Decimal(x)),
				}
			}
			_ => self
				.parse_value_record_id_inner::<SurrealQL>(stk)
				.await
				.map(PublicValue::RecordId)?,
		};

		match self.peek_whitespace().map(|x| x.kind) {
			Some(t!(">")) => {
				self.pop_peek();
				expected_whitespace!(self, t!(".."));
				match self.peek_whitespace().map(|x| x.kind) {
					Some(t!("=")) => {
						self.pop_peek();
						enter_object_recursion!(this = self => {
							let v = stk.run(|stk| this.parse_value(stk)).await?;
							Ok(PublicValue::Range(Box::new(PublicRange {
								start: Bound::Excluded(res),
								end: Bound::Included(v),
							})))
						})
					}
					Some(x) if Self::kind_starts_expression(x) => {
						enter_object_recursion!(this = self => {
							let v = stk.run(|stk| this.parse_value(stk)).await?;
							Ok(PublicValue::Range(Box::new(PublicRange {
								start: Bound::Excluded(res),
								end: Bound::Excluded(v),
							})))
						})
					}
					_ => Ok(PublicValue::Range(Box::new(PublicRange {
						start: Bound::Excluded(res),
						end: Bound::Unbounded,
					}))),
				}
			}
			Some(t!("..")) => {
				self.pop_peek();

				match self.peek_whitespace().map(|x| x.kind) {
					Some(t!("=")) => {
						self.pop_peek();
						enter_object_recursion!(this = self => {
							let v = stk.run(|stk| this.parse_value(stk)).await?;
							Ok(PublicValue::Range(Box::new(PublicRange {
								start: Bound::Included(res),
								end: Bound::Included(v),
							})))
						})
					}
					Some(x) if Self::kind_starts_expression(x) => {
						enter_object_recursion!(this = self => {
							let v = stk.run(|stk| this.parse_value(stk)).await?;
							Ok(PublicValue::Range(Box::new(PublicRange {
								start: Bound::Included(res),
								end: Bound::Excluded(v),
							})))
						})
					}
					_ => Ok(PublicValue::Range(Box::new(PublicRange {
						start: Bound::Included(res),
						end: Bound::Unbounded,
					}))),
				}
			}
			_ => Ok(res),
		}
	}

	pub async fn parse_json(&mut self, stk: &mut Stk) -> ParseResult<PublicValue> {
		let token = self.peek();
		match token.kind {
			t!("NULL") => {
				self.pop_peek();
				Ok(PublicValue::Null)
			}
			t!("true") => {
				self.pop_peek();
				Ok(PublicValue::Bool(true))
			}
			t!("false") => {
				self.pop_peek();
				Ok(PublicValue::Bool(false))
			}
			t!("{") => {
				let open = self.pop_peek().span;

				if self.eat(t!("}")) {
					return Ok(PublicValue::Object(PublicObject::new()));
				}

				enter_object_recursion!(this = self => {
					this.parse_value_object::<Json>(stk, open, BTreeMap::new())
						.await
						.map(PublicValue::Object)
				})
			}
			t!("[") => {
				self.pop_peek();
				enter_object_recursion!(this = self => {
					this.parse_value_array::<Json>(stk, token.span).await.map(PublicValue::Array)
				})
			}
			t!("\"") | t!("'") => {
				let strand = self.parse_string_lit()?;
				if self.settings.legacy_strands {
					Ok(self.reparse_json_legacy_strand(stk, strand).await)
				} else {
					Ok(PublicValue::String(strand))
				}
			}
			t!("-") | t!("+") | TokenKind::Digits => {
				self.pop_peek();
				let compound = self.lex_compound(token, compound::numeric)?;
				match compound.value {
					Numeric::Duration(x) => Ok(PublicValue::Duration(PublicDuration::from(x))),
					Numeric::Integer(x) => {
						Ok(PublicValue::Number(PublicNumber::Int(x.into_int(compound.span)?)))
					}
					Numeric::Float(x) => Ok(PublicValue::Number(PublicNumber::Float(x))),
					Numeric::Decimal(x) => Ok(PublicValue::Number(PublicNumber::Decimal(x))),
				}
			}
			_ => {
				match self.parse_value_record_id_inner::<Json>(stk).await.map(PublicValue::RecordId)
				{
					Ok(x) => Ok(x),
					Err(err) => {
						tracing::debug!("Error parsing record id: {err:?}");
						self.parse_value_table().await.map(PublicValue::Table)
					}
				}
			}
		}
	}

	async fn reparse_json_legacy_strand(&mut self, stk: &mut Stk, strand: String) -> PublicValue {
		if let Ok(x) = Parser::new(strand.as_bytes()).parse_value_record_id(stk).await {
			return PublicValue::RecordId(x);
		}

		if let Ok(x) = Lexer::lex_datetime(&strand) {
			return PublicValue::Datetime(x);
		}

		if let Ok(x) = Lexer::lex_uuid(&strand) {
			return PublicValue::Uuid(x);
		}

		PublicValue::String(strand)
	}

	async fn parse_value_object<VP>(
		&mut self,
		stk: &mut Stk,
		start: Span,
		mut obj: BTreeMap<String, PublicValue>,
	) -> ParseResult<PublicObject>
	where
		VP: ValueParseFunc,
	{
		loop {
			if self.eat(t!("}")) {
				return Ok(PublicObject::from(obj));
			}
			let key = self.parse_object_key()?;
			expected!(self, t!(":"));
			let value = stk.run(|ctx| VP::parse(self, ctx)).await?;
			obj.insert(key, value);

			if !self.eat(t!(",")) {
				self.expect_closing_delimiter(t!("}"), start)?;
				return Ok(PublicObject::from(obj));
			}
		}
	}

	async fn parse_value_set<VP>(&mut self, stk: &mut Stk, start: Span) -> ParseResult<PublicSet>
	where
		VP: ValueParseFunc,
	{
		let mut set = PublicSet::new();
		loop {
			if self.eat(t!("}")) {
				return Ok(set);
			}

			let value = stk.run(|stk| VP::parse(self, stk)).await?;
			set.insert(value);

			if !self.eat(t!(",")) {
				if set.len() <= 1 {
					// Single-element object: `{value}`
					// We could parse this in SQON, but in SurrealQL this is a block statement.
					// So we instead throw an error and require the user to add a trailing
					// comma for a set.
					unexpected!(
						self,
						self.peek(),
						"`,`",
						=> "Sets with a single value must have at least a single comma"
					);
				}

				self.expect_closing_delimiter(t!("}"), start)?;

				return Ok(set);
			}
		}
	}

	async fn parse_value_array<VP>(
		&mut self,
		stk: &mut Stk,
		start: Span,
	) -> ParseResult<PublicArray>
	where
		VP: ValueParseFunc,
	{
		let mut array = Vec::new();
		loop {
			if self.eat(t!("]")) {
				return Ok(PublicArray::from(array));
			}
			let value = stk.run(|stk| VP::parse(self, stk)).await?;
			array.push(value);

			if !self.eat(t!(",")) {
				self.expect_closing_delimiter(t!("]"), start)?;
				return Ok(PublicArray::from(array));
			}
		}
	}

	async fn parse_value_table(&mut self) -> ParseResult<PublicTable> {
		let table = self.parse_ident()?.into_string();
		Ok(PublicTable::new(table))
	}

	pub async fn parse_value_record_id(&mut self, stk: &mut Stk) -> ParseResult<PublicRecordId> {
		self.parse_value_record_id_inner::<SurrealQL>(stk).await
	}

	async fn parse_value_record_id_inner<VP>(
		&mut self,
		stk: &mut Stk,
	) -> ParseResult<PublicRecordId>
	where
		VP: ValueParseFunc,
	{
		let table = self.parse_ident()?.into_string();
		expected!(self, t!(":"));
		let peek = self.peek();
		let key = match peek.kind {
			t!("u'") | t!("u\"") => PublicRecordIdKey::Uuid(self.next_token_value::<PublicUuid>()?),
			t!("{") => {
				let peek = self.pop_peek();
				enter_object_recursion!(this = self => {
					PublicRecordIdKey::Object(
						this.parse_value_object::<VP>(stk, peek.span, BTreeMap::new()).await?,
					)
				})
			}
			t!("[") => {
				let peek = self.pop_peek();
				enter_object_recursion!(this = self => {
					PublicRecordIdKey::Array(this.parse_value_array::<VP>(stk, peek.span).await?)
				})
			}
			t!("+") => {
				self.pop_peek();
				// starting with a + so it must be a number
				let digits_token = if let Some(digits_token) = self.peek_whitespace() {
					match digits_token.kind {
						TokenKind::Digits => digits_token,
						_ => unexpected!(self, digits_token, "an integer"),
					}
				} else {
					bail!("Unexpected whitespace",@self.last_span() => "No whitespace allowed after this token")
				};

				match self.peek_whitespace().map(|x| x.kind) {
					Some(t!(".")) => {
						// Numeric record-id keys are stored as `i64`, so a fractional part
						// (`.`), an exponent (`e`/`E`), or a numeric type suffix is not
						// allowed here.
						unexpected!(self, self.peek(), "an integer", => "Numeric Record-id keys can only be integers");
					}
					Some(x) if Self::kind_is_identifier(x) => {
						let span = peek.span.covers(self.peek().span);
						bail!("Unexpected token `{x}` expected an integer", @span);
					}
					// allowed
					_ => {}
				}

				let digits_str = self.span_str(digits_token.span);
				if let Ok(number) = digits_str.parse() {
					PublicRecordIdKey::Number(number)
				} else {
					PublicRecordIdKey::String(digits_str.to_owned())
				}
			}
			t!("-") => {
				self.pop_peek();
				let token = expected!(self, TokenKind::Digits);
				if let Ok(number) = self.lex_compound(token, compound::integer::<u64>) {
					// Parse to u64 and check if the value is equal to `-i64::MIN` via u64 as
					// `-i64::MIN` doesn't fit in an i64
					match number.value.cmp(&((i64::MAX as u64) + 1)) {
						Ordering::Less => PublicRecordIdKey::Number(-(number.value as i64)),
						Ordering::Equal => PublicRecordIdKey::Number(i64::MIN),
						Ordering::Greater => PublicRecordIdKey::String(format!(
							"-{}",
							self.lexer.span_str(number.span)
						)),
					}
				} else {
					PublicRecordIdKey::String(format!("-{}", self.lexer.span_str(token.span)))
				}
			}
			TokenKind::Digits => {
				if self.settings.flexible_record_id
					&& let Some(peek) = self.peek_whitespace1()
					&& Self::kind_is_identifier(peek.kind)
				{
					let ident = self.parse_flexible_ident()?;
					PublicRecordIdKey::String(ident)
				} else {
					self.pop_peek();

					let digits_str = self.span_str(peek.span);
					if let Ok(number) = digits_str.parse::<i64>() {
						PublicRecordIdKey::Number(number)
					} else {
						PublicRecordIdKey::String(digits_str.to_owned())
					}
				}
			}
			_ => {
				let ident = if self.settings.flexible_record_id {
					self.parse_flexible_ident()?
				} else {
					self.parse_ident()?.into_string()
				};
				PublicRecordIdKey::String(ident)
			}
		};

		Ok(PublicRecordId::new(table, key))
	}
}