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
#![deny(clippy::wildcard_enum_match_arm)]

use std::cmp::Ordering;

use crate::expr::part::{Next, Part};
use crate::val::Value;

impl Value {
	pub fn compare(
		&self,
		other: &Self,
		path: &[Part],
		collate: bool,
		numeric: bool,
	) -> Option<Ordering> {
		let Some(p) = path.first() else {
			// Without the `collate` feature a collated comparison orders as an
			// uncollated one; the engine rejects a collated sort before it runs.
			return match (collate, numeric) {
				#[cfg(feature = "collate")]
				(true, true) => self.natural_lexical_cmp(other),
				#[cfg(feature = "collate")]
				(true, false) => self.lexical_cmp(other),
				(_, true) => self.natural_cmp(other),
				_ => self.partial_cmp(other),
			};
		};

		match (self, other) {
			// Current path part is an object
			(Value::Object(a), Value::Object(b)) => match p {
				Part::Field(f) => compare_optional(a.get(f), b.get(f), path, collate, numeric),
				Part::All
				| Part::Flatten
				| Part::Last
				| Part::First
				| Part::Where(_)
				| Part::Lookup(_)
				| Part::Value(_)
				| Part::Start(_)
				| Part::Method(_, _)
				| Part::Destructure(_)
				| Part::Optional
				| Part::Recurse(_, _, _)
				| Part::Doc
				| Part::RepeatRecurse => None,
			},
			// Current path part is an array
			(Value::Array(a), Value::Array(b)) => match p {
				Part::All => {
					for (a, b) in a.iter().zip(b.iter()) {
						match a.compare(b, path.next(), collate, numeric) {
							None | Some(Ordering::Equal) => continue,
							Some(o @ (Ordering::Less | Ordering::Greater)) => return Some(o),
						}
					}
					Some(a.len().cmp(&b.len()))
				}
				Part::First => compare_optional(a.first(), b.first(), path, collate, numeric),
				Part::Last => compare_optional(a.last(), b.last(), path, collate, numeric),
				//FIXME: It is kind of weird that a[1] works but `a[+(1)]` or `let $b = 1;
				// a[$b]` for example doesn't as
				Part::Flatten
				| Part::Field(_)
				| Part::Where(_)
				| Part::Lookup(_)
				| Part::Value(_)
				| Part::Start(_)
				| Part::Method(_, _)
				| Part::Destructure(_)
				| Part::Optional
				| Part::Recurse(_, _, _)
				| Part::Doc
				| Part::RepeatRecurse => {
					if let Some(idx) = p.as_old_index() {
						compare_optional(a.get(idx), b.get(idx), path, collate, numeric)
					} else {
						for (a, b) in a.iter().zip(b.iter()) {
							match a.compare(b, path, collate, numeric) {
								None | Some(Ordering::Equal) => continue,
								Some(o @ (Ordering::Less | Ordering::Greater)) => return Some(o),
							}
						}
						Some(a.len().cmp(&b.len()))
					}
				}
			},
			(Value::Set(a), Value::Set(b)) => match p {
				Part::All => {
					for (a, b) in a.iter().zip(b.iter()) {
						match a.compare(b, path.next(), collate, numeric) {
							None | Some(Ordering::Equal) => continue,
							Some(o @ (Ordering::Less | Ordering::Greater)) => return Some(o),
						}
					}

					Some(a.len().cmp(&b.len()))
				}
				Part::First => compare_optional(a.first(), b.first(), path, collate, numeric),
				Part::Last => compare_optional(a.last(), b.last(), path, collate, numeric),
				Part::Flatten
				| Part::Field(_)
				| Part::Where(_)
				| Part::Lookup(_)
				| Part::Value(_)
				| Part::Start(_)
				| Part::Method(_, _)
				| Part::Destructure(_)
				| Part::Optional
				| Part::Recurse(_, _, _)
				| Part::Doc
				| Part::RepeatRecurse => {
					if let Some(idx) = p.as_old_index() {
						compare_optional(a.nth(idx), b.nth(idx), path, collate, numeric)
					} else {
						for (a, b) in a.iter().zip(b.iter()) {
							match a.compare(b, path, collate, numeric) {
								None | Some(Ordering::Equal) => continue,
								Some(o @ (Ordering::Less | Ordering::Greater)) => return Some(o),
							}
						}
						Some(a.len().cmp(&b.len()))
					}
				}
			},
			// Handle field access on mixed or non-object types
			(a, b) => match p {
				Part::Field(f) => match (a, b) {
					// If one is an Object and the other is not, treat non-Object as missing field
					(Value::Object(a), _) => match a.get(f) {
						Some(a) => a.compare(&Value::None, path.next(), collate, numeric),
						None => Some(Ordering::Equal),
					},
					(_, Value::Object(b)) => match b.get(f) {
						Some(b) => Value::None.compare(b, path.next(), collate, numeric),
						None => Some(Ordering::Equal),
					},
					// Both are non-object types, so both are missing the field
					_ => Some(Ordering::Equal),
				},
				// For non-field path parts, continue comparing with remaining path
				Part::All
				| Part::Flatten
				| Part::Last
				| Part::First
				| Part::Where(_)
				| Part::Lookup(_)
				| Part::Value(_)
				| Part::Start(_)
				| Part::Method(_, _)
				| Part::Destructure(_)
				| Part::Optional
				| Part::Recurse(_, _, _)
				| Part::Doc
				| Part::RepeatRecurse => a.compare(b, path.next(), collate, numeric),
			},
		}
	}
}

#[inline]
fn compare_optional(
	a: Option<&Value>,
	b: Option<&Value>,
	path: &[Part],
	collate: bool,
	numeric: bool,
) -> Option<Ordering> {
	match (a, b) {
		(Some(a), Some(b)) => a.compare(b, path.next(), collate, numeric),
		(Some(_), None) => Some(Ordering::Greater),
		(None, Some(_)) => Some(Ordering::Less),
		(None, None) => Some(Ordering::Equal),
	}
}

#[cfg(test)]
mod tests {

	use rstest::rstest;

	use super::*;
	use crate::expr::idiom::Idiom;
	use crate::syn;

	macro_rules! parse_val {
		($input:expr) => {
			crate::val::convert_public_value_to_internal(syn::value($input).unwrap())
		};
	}

	#[rstest]
	#[case::none_eq_none(Value::None, Value::None, Idiom::default(), Some(Ordering::Equal))]
	#[case::none_eq_none(parse_val!("{ test: { other: null, something: 456 } }"), parse_val!("{ test: { other: null, something: 123 } }"), syn::idiom("test.something").unwrap(), Some(Ordering::Greater))]
	fn test_compare(
		#[case] a: Value,
		#[case] b: Value,
		#[case] path: Idiom,
		#[case] expected: Option<Ordering>,
	) {
		let res = a.compare(&b, &path, false, false);
		assert_eq!(res, expected);
	}

	#[test]
	fn compare_none() {
		let idi: Idiom = Default::default();
		let one = parse_val!("{ test: { other: null, something: 456 } }");
		let two = parse_val!("{ test: { other: null, something: 123 } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_basic() {
		let idi: Idiom = syn::idiom("test.something").unwrap();
		let one = parse_val!("{ test: { other: null, something: 456 } }");
		let two = parse_val!("{ test: { other: null, something: 123 } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_basic_missing_left() {
		let idi: Idiom = syn::idiom("test.something").unwrap();
		let one = parse_val!("{ test: { other: null } }");
		let two = parse_val!("{ test: { other: null, something: 123 } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Less));
	}

	#[test]
	fn compare_basic_missing_right() {
		let idi: Idiom = syn::idiom("test.something").unwrap();
		let one = parse_val!("{ test: { other: null, something: 456 } }");
		let two = parse_val!("{ test: { other: null } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_array() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: [4, 5, 6] } }");
		let two = parse_val!("{ test: { other: null, something: [1, 2, 3] } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_array_longer_left() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: [1, 2, 3, 4, 5, 6] } }");
		let two = parse_val!("{ test: { other: null, something: [1, 2, 3] } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_array_longer_right() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: [1, 2, 3] } }");
		let two = parse_val!("{ test: { other: null, something: [1, 2, 3, 4, 5, 6] } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Less));
	}

	#[test]
	fn compare_array_missing_left() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: null } }");
		let two = parse_val!("{ test: { other: null, something: [1, 2, 3] } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Less));
	}

	#[test]
	fn compare_array_missing_right() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: [4, 5, 6] } }");
		let two = parse_val!("{ test: { other: null, something: null } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_array_missing_value_left() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: [1, null, 3] } }");
		let two = parse_val!("{ test: { other: null, something: [1, 2, 3] } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Less));
	}

	#[test]
	fn compare_array_missing_value_right() {
		let idi: Idiom = syn::idiom("test.something.*").unwrap();
		let one = parse_val!("{ test: { other: null, something: [1, 2, 3] } }");
		let two = parse_val!("{ test: { other: null, something: [1, null, 3] } }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_last() {
		let idi: Idiom = syn::idiom("test[$]").unwrap();
		let one = parse_val!("{ test: [1,5] }");
		let two = parse_val!("{ test: [2,4] }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Greater))
	}

	#[test]
	fn compare_field_on_record_id() {
		// Both are RecordIds, trying to access a field should be Equal (both missing field)
		let idi: Idiom = syn::idiom("city.name").unwrap();
		let one = parse_val!("{ city: city:1 }");
		let two = parse_val!("{ city: city:2 }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Equal));
	}

	#[test]
	fn compare_field_on_record_id_different_order() {
		// Even with different record IDs, field access should treat them as equal (both missing)
		let idi: Idiom = syn::idiom("city.name").unwrap();
		let one = parse_val!("{ city: city:100 }");
		let two = parse_val!("{ city: city:1 }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Equal));
	}

	#[test]
	fn compare_field_object_vs_record_id() {
		// Object with field vs RecordId without field
		let idi: Idiom = syn::idiom("city.name").unwrap();
		let one = parse_val!("{ city: { name: 'San Francisco' } }");
		let two = parse_val!("{ city: city:1 }");
		let res = one.compare(&two, &idi, false, false);
		// Object has field, RecordId doesn't, so Object > RecordId (Greater)
		assert_eq!(res, Some(Ordering::Greater));
	}

	#[test]
	fn compare_field_record_id_vs_object() {
		// RecordId without field vs Object with field
		let idi: Idiom = syn::idiom("city.name").unwrap();
		let one = parse_val!("{ city: city:1 }");
		let two = parse_val!("{ city: { name: 'San Francisco' } }");
		let res = one.compare(&two, &idi, false, false);
		// RecordId doesn't have field, Object does, so RecordId < Object (Less)
		assert_eq!(res, Some(Ordering::Less));
	}

	#[test]
	fn compare_field_object_missing_vs_record_id() {
		// Object without the specific field vs RecordId
		let idi: Idiom = syn::idiom("city.name").unwrap();
		let one = parse_val!("{ city: { country: 'USA' } }");
		let two = parse_val!("{ city: city:1 }");
		let res = one.compare(&two, &idi, false, false);
		// Both missing field, should be Equal
		assert_eq!(res, Some(Ordering::Equal));
	}

	#[test]
	fn compare_field_on_string() {
		// Both are Strings, trying to access a field should be Equal (both missing field)
		let idi: Idiom = syn::idiom("value.nested").unwrap();
		let one = parse_val!("{ value: 'hello' }");
		let two = parse_val!("{ value: 'world' }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Equal));
	}

	#[test]
	fn compare_field_on_number() {
		// Both are Numbers, trying to access a field should be Equal (both missing field)
		let idi: Idiom = syn::idiom("value.nested").unwrap();
		let one = parse_val!("{ value: 42 }");
		let two = parse_val!("{ value: 100 }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Equal));
	}

	#[test]
	fn compare_field_mixed_types() {
		// String vs Number, both can't access field, should be Equal
		let idi: Idiom = syn::idiom("value.nested").unwrap();
		let one = parse_val!("{ value: 'hello' }");
		let two = parse_val!("{ value: 42 }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Equal));
	}

	#[test]
	fn compare_nested_field_on_record_id() {
		// Deeply nested field access on RecordId
		let idi: Idiom = syn::idiom("user.city.name").unwrap();
		let one = parse_val!("{ user: user:1 }");
		let two = parse_val!("{ user: user:2 }");
		let res = one.compare(&two, &idi, false, false);
		assert_eq!(res, Some(Ordering::Equal));
	}
}