solidb 1.2.1

A lightweight, high-performance structured database server written in Rust.
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
//! Unit tests for the SDBQL parser.

use super::*;

#[test]
fn test_parse_simple_for_return() {
    let query = parse("FOR doc IN users RETURN doc").unwrap();
    assert_eq!(query.for_clauses.len(), 1);
    assert!(query.return_clause.is_some());
}

#[test]
fn test_parse_for_filter_return() {
    let query = parse("FOR doc IN users FILTER doc.age > 18 RETURN doc").unwrap();
    assert_eq!(query.filter_clauses.len(), 1);
    assert!(query.return_clause.is_some());
}

#[test]
fn test_parse_for_sort_limit_return() {
    let query = parse("FOR doc IN users SORT doc.name ASC LIMIT 10 RETURN doc").unwrap();
    assert!(query.sort_clause.is_some());
    assert!(query.limit_clause.is_some());
}

#[test]
fn test_parse_insert() {
    let query = parse("INSERT { name: \"Alice\" } INTO users").unwrap();
    assert!(query
        .body_clauses
        .iter()
        .any(|c| matches!(c, BodyClause::Insert(_))));
}

#[test]
fn test_parse_update() {
    let query = parse("FOR doc IN users UPDATE doc WITH { active: true } IN users").unwrap();
    assert!(query
        .body_clauses
        .iter()
        .any(|c| matches!(c, BodyClause::Update(_))));
}

#[test]
fn test_parse_remove() {
    let query = parse("FOR doc IN users REMOVE doc IN users").unwrap();
    assert!(query
        .body_clauses
        .iter()
        .any(|c| matches!(c, BodyClause::Remove(_))));
}

#[test]
fn test_parse_collect() {
    let query = parse("FOR doc IN users COLLECT city = doc.city RETURN city").unwrap();
    assert!(query
        .body_clauses
        .iter()
        .any(|c| matches!(c, BodyClause::Collect(_))));
}

#[test]
fn test_parse_let_clause() {
    let query = parse("LET x = 5 RETURN x").unwrap();
    assert_eq!(query.let_clauses.len(), 1);
}

#[test]
fn test_parse_let_multiple_bindings() {
    // Test comma-separated LET bindings
    let query = parse("LET a = 1, b = 2, c = 3 RETURN a + b + c").unwrap();
    assert_eq!(query.let_clauses.len(), 3);
    assert_eq!(query.let_clauses[0].variable, "a");
    assert_eq!(query.let_clauses[1].variable, "b");
    assert_eq!(query.let_clauses[2].variable, "c");
}

#[test]
fn test_parse_let_multiple_in_body() {
    // Test comma-separated LET bindings after FOR
    let query = parse("FOR doc IN users LET x = doc.a, y = doc.b RETURN {x, y}").unwrap();
    let let_count = query
        .body_clauses
        .iter()
        .filter(|c| matches!(c, BodyClause::Let(_)))
        .count();
    assert_eq!(let_count, 2);
}

#[test]
fn test_parse_return_arithmetic() {
    let query = parse("RETURN 1 + 2 * 3").unwrap();
    assert!(query.return_clause.is_some());
    let ret = query.return_clause.unwrap();
    assert!(matches!(ret.expression, Expression::BinaryOp { .. }));
}

#[test]
fn test_parse_error_incomplete() {
    let result = parse("FOR doc IN");
    assert!(result.is_err());
}

#[test]
fn test_parse_error_invalid_token() {
    let result = parse("FOR 123 IN users");
    assert!(result.is_err());
}

#[test]
fn test_parse_sort_desc() {
    let query = parse("FOR doc IN users SORT doc.age DESC RETURN doc").unwrap();
    let sort = query.sort_clause.unwrap();
    assert_eq!(sort.fields.len(), 1);
    assert!(!sort.fields[0].1);
}

#[test]
fn test_parse_multiple_filters() {
    let query = parse("FOR doc IN users FILTER doc.age > 18 FILTER doc.active RETURN doc").unwrap();
    assert_eq!(query.filter_clauses.len(), 2);
}

#[test]
fn test_parse_nested_for() {
    let query = parse("FOR a IN users FOR b IN orders RETURN { user: a, order: b }").unwrap();
    assert_eq!(query.for_clauses.len(), 2);
}

#[test]
fn test_parse_not_in() {
    let query = parse("FOR x IN collection FILTER x.id NOT IN [1, 2, 3] RETURN x").unwrap();
    if let BodyClause::Filter(filter) = &query.body_clauses[1] {
        if let Expression::BinaryOp { op, .. } = &filter.expression {
            assert_eq!(*op, BinaryOperator::NotIn);
        } else {
            panic!("Expected BinaryOp::NotIn");
        }
    } else {
        panic!("Expected FilterClause");
    }
}

#[test]
fn test_parse_create_stream() {
    let input = r#"
            CREATE STREAM high_value_txns AS
            FOR txn IN transactions
            WINDOW TUMBLING (SIZE "1m")
            FILTER txn.amount > 1000
            RETURN txn
        "#;
    let mut parser = Parser::new(input).unwrap();
    let query = parser.parse().unwrap();

    assert!(query.create_stream_clause.is_some());
    assert_eq!(query.create_stream_clause.unwrap().name, "high_value_txns");
    assert!(query.window_clause.is_some());
    assert_eq!(query.window_clause.unwrap().duration, "1m");
    assert_eq!(query.for_clauses.len(), 1);
    assert_eq!(query.for_clauses[0].collection, "transactions");
}

#[test]
fn test_any_syntax() {
    let query = parse("FOR doc IN collection FILTER ANY member IN doc.members RETURN doc");
    assert!(
        query.is_ok(),
        "Failed to parse ANY syntax: {:?}",
        query.err()
    );
}

#[test]
fn test_any_satisfies_syntax() {
    let query = parse(
        "FOR doc IN collection FILTER ANY member IN doc.members SATISFIES member.age > 10 RETURN doc",
    );
    assert!(
        query.is_ok(),
        "Failed to parse ANY ... SATISFIES syntax: {:?}",
        query.err()
    );
}

#[test]
fn test_cte_simple() {
    let query = parse("WITH temp AS (FOR doc IN coll RETURN doc) FOR t IN temp RETURN t");
    assert!(query.is_ok(), "Failed to parse CTE: {:?}", query.err());
    let query = query.unwrap();
    assert!(query.with_clause.is_some());
    let with = query.with_clause.unwrap();
    assert_eq!(with.ctes.len(), 1);
    assert_eq!(with.ctes[0].name, "temp");
}

#[test]
fn test_cte_multiple() {
    let query = parse(
        "WITH a AS (FOR x IN coll RETURN x), b AS (FOR y IN coll2 RETURN y) FOR t IN a RETURN t",
    );
    assert!(
        query.is_ok(),
        "Failed to parse multiple CTEs: {:?}",
        query.err()
    );
    let query = query.unwrap();
    assert!(query.with_clause.is_some());
    let with = query.with_clause.unwrap();
    assert_eq!(with.ctes.len(), 2);
    assert_eq!(with.ctes[0].name, "a");
    assert_eq!(with.ctes[1].name, "b");
}

#[test]
fn test_cte_with_columns() {
    let query =
        parse("WITH temp(col1, col2) AS (FOR doc IN coll RETURN doc) FOR t IN temp RETURN t");
    assert!(
        query.is_ok(),
        "Failed to parse CTE with columns: {:?}",
        query.err()
    );
    let query = query.unwrap();
    assert!(query.with_clause.is_some());
    let with = query.with_clause.unwrap();
    assert_eq!(with.ctes[0].columns, vec!["col1", "col2"]);
}

#[test]
fn test_no_cte() {
    let query = parse("FOR doc IN coll RETURN doc").unwrap();
    assert!(query.with_clause.is_none());
}

#[test]
fn test_recursive_cte() {
    let query = parse(
        "WITH RECURSIVE tree AS (FOR d IN nodes FILTER d._key == @root RETURN d._key \
         UNION ALL FOR n IN nodes FILTER n.parent IN tree RETURN n._key) \
         FOR x IN tree RETURN x",
    );
    assert!(
        query.is_ok(),
        "Failed to parse recursive CTE: {:?}",
        query.err()
    );
    let query = query.unwrap();
    let with = query.with_clause.unwrap();
    assert_eq!(with.ctes.len(), 1);
    assert!(with.ctes[0].recursive);
    // Body must be anchor UNION ALL step
    assert_eq!(with.ctes[0].query.set_operations.len(), 1);
}

#[test]
fn test_return_distinct() {
    let query = parse("FOR doc IN coll RETURN DISTINCT doc.city").unwrap();
    let rc = query.return_clause.unwrap();
    assert!(rc.distinct);

    // Plain RETURN must not set the flag
    let query = parse("FOR doc IN coll RETURN doc.city").unwrap();
    assert!(!query.return_clause.unwrap().distinct);
}

#[test]
fn test_set_operations() {
    for (sql, expected) in [
        (
            "FOR a IN c1 RETURN a.x UNION FOR b IN c2 RETURN b.y",
            "Union",
        ),
        (
            "FOR a IN c1 RETURN a.x UNION ALL FOR b IN c2 RETURN b.y",
            "UnionAll",
        ),
        (
            "FOR a IN c1 RETURN a.x INTERSECT FOR b IN c2 RETURN b.y",
            "Intersect",
        ),
        (
            "FOR a IN c1 RETURN a.x EXCEPT FOR b IN c2 RETURN b.y",
            "Except",
        ),
    ] {
        let query = parse(sql).unwrap_or_else(|e| panic!("Failed to parse {sql}: {e:?}"));
        assert_eq!(query.set_operations.len(), 1, "{sql}");
        let op_name = format!("{:?}", query.set_operations[0].op);
        assert_eq!(op_name, expected, "{sql}");
    }
}

#[test]
fn test_set_operation_chain_is_flat_and_left_to_right() {
    // `a EXCEPT b EXCEPT c` must be one flat chain — nesting it to the right
    // would mean `a EXCEPT (b EXCEPT c)`.
    let query =
        parse("FOR a IN c1 RETURN a.x EXCEPT FOR b IN c2 RETURN b.x EXCEPT FOR c IN c3 RETURN c.x")
            .unwrap();
    assert_eq!(query.set_operations.len(), 2);
    assert!(query.set_operations[0].query.set_operations.is_empty());
}

#[test]
fn test_intersect_binds_tighter_than_union() {
    // `a UNION b INTERSECT c` groups as `a UNION (b INTERSECT c)`
    let query = parse(
        "FOR a IN c1 RETURN a.x UNION FOR b IN c2 RETURN b.x INTERSECT FOR c IN c3 RETURN c.x",
    )
    .unwrap();
    assert_eq!(query.set_operations.len(), 1);
    assert!(matches!(query.set_operations[0].op, SetOperator::Union));
    let nested = &query.set_operations[0].query.set_operations;
    assert_eq!(nested.len(), 1);
    assert!(matches!(nested[0].op, SetOperator::Intersect));
}

#[test]
fn test_parenthesized_left_operand() {
    // Explicit grouping on the left: `(a UNION b) INTERSECT c` intersects the
    // union, so the chain stays flat instead of nesting under `b`.
    let query = parse(
        "(FOR a IN c1 RETURN a.x UNION FOR b IN c2 RETURN b.x)          INTERSECT FOR c IN c3 RETURN c.x",
    )
    .unwrap();
    assert_eq!(query.set_operations.len(), 2);
    assert!(matches!(query.set_operations[0].op, SetOperator::Union));
    assert!(matches!(query.set_operations[1].op, SetOperator::Intersect));
    assert!(query.set_operations[0].query.set_operations.is_empty());
}

#[test]
fn test_offset_without_limit_has_no_count() {
    // A standalone OFFSET must not invent a count: the count is pushed into
    // storage scans as an allocation size.
    let query = parse("FOR d IN coll OFFSET 5 RETURN d").unwrap();
    let limit = query.limit_clause.expect("OFFSET produces a limit clause");
    assert!(limit.count.is_none());

    let query = parse("FOR d IN coll LIMIT 10 OFFSET 5 RETURN d").unwrap();
    let limit = query.limit_clause.expect("limit clause");
    assert_eq!(
        limit.count,
        Some(Expression::Literal(serde_json::json!(10)))
    );
    assert_eq!(limit.offset, Expression::Literal(serde_json::json!(5)));
}

#[test]
fn test_has_mutations_sees_nested_blocks() {
    // The HTTP handler decides caching, transaction handling and write
    // permission from this: a mutation hidden in an operand or a CTE body must
    // not read as a read-only query.
    let query =
        parse("FOR a IN c1 RETURN a.x UNION FOR d IN c2 REMOVE d IN c2 RETURN d._key").unwrap();
    assert!(query.has_mutations());

    let query =
        parse("WITH gone AS (FOR d IN c2 REMOVE d IN c2 RETURN d._key) FOR x IN gone RETURN x")
            .unwrap();
    assert!(query.has_mutations());

    let query = parse("FOR a IN c1 RETURN a.x UNION FOR b IN c2 RETURN b.x").unwrap();
    assert!(!query.has_mutations());
}

#[test]
fn test_set_operations_parenthesized_operand() {
    let query =
        parse("FOR a IN c1 RETURN a.x UNION (FOR b IN c2 FILTER b.z > 1 RETURN b.y)").unwrap();
    assert_eq!(query.set_operations.len(), 1);
    assert!(matches!(query.set_operations[0].op, SetOperator::Union));
}

#[test]
fn test_collect_keep() {
    let query = parse(
        "FOR u IN users COLLECT city = u.city INTO groups KEEP name, age SORT city RETURN city",
    )
    .unwrap();
    let collect = query
        .body_clauses
        .iter()
        .find_map(|c| match c {
            BodyClause::Collect(cc) => Some(cc.clone()),
            _ => None,
        })
        .expect("COLLECT clause");
    assert_eq!(collect.keep_vars, vec!["name", "age"]);
}

#[test]
fn test_parse_collect_with_aggregate_count() {
    let query =
        parse("FOR u IN users COLLECT city = u.city AGGREGATE count = COUNT() RETURN count");
    assert!(query.is_ok(), "Failed to parse: {:?}", query.err());
}

#[test]
fn test_parse_collect_aggregate_no_group_var() {
    let query = parse("FOR u IN users COLLECT AGGREGATE count = COUNT() RETURN count");
    assert!(query.is_ok(), "Failed to parse: {:?}", query.err());
}

// ---------------------------------------------------------------------------
// Recursion depth
// ---------------------------------------------------------------------------

/// Chained unary operators recurse in `parse_unary_expression` without going
/// back through `parse_expression`, so they escaped the SEC-130 depth guard
/// entirely: a query of N nested unary operators recursed N frames deep and
/// overflowed the stack, which in Rust aborts the whole process rather than
/// unwinding. 200k operators is a 200 KB body, well under the request limit.
///
/// Note `--` lexes as a line comment, so a negation chain has to be written
/// with separators; `!` and `~` chain directly.
#[test]
fn test_unary_chain_is_depth_limited() {
    for chain in ["!".repeat(500), "~".repeat(500), "- ".repeat(500)] {
        let query = format!("RETURN {}5", chain);
        let err = parse(&query).expect_err("chained unary should hit the depth limit");
        assert!(
            err.to_string().contains("too deep"),
            "expected the depth guard, got: {}",
            err
        );
    }
}

/// The guard must not be so tight that ordinary negation breaks, and the
/// depth it consumes has to be released again so later expressions in the
/// same query still parse.
#[test]
fn test_short_unary_chains_still_parse() {
    assert!(parse("RETURN -5").is_ok());
    assert!(parse("RETURN - -5").is_ok());
    assert!(parse("RETURN !!true").is_ok());
    assert!(parse("RETURN ~~1").is_ok());
    assert!(parse("FOR u IN users FILTER !u.deleted RETURN -u.score").is_ok());
    // Many *sibling* unary expressions are fine; only nesting is bounded.
    let siblings = (0..200)
        .map(|i| format!("-{}", i))
        .collect::<Vec<_>>()
        .join(", ");
    assert!(
        parse(&format!("RETURN [{}]", siblings)).is_ok(),
        "sequential unary operands must not accumulate depth"
    );
}
/// `has_mutations()` decides whether `/cursor`, the live-query subscription
/// and the transactional query endpoint upgrade a caller from Read to Write.
/// It used to walk only `body_clauses`, so a mutation parked in an expression
/// was invisible: a parenthesised subquery is executed by the full body
/// executor, and the catalog builtins write `_views` / `_graphs` directly.
/// Both ran under a read-only principal.
#[test]
fn test_has_mutations_sees_expression_level_writes() {
    for query in [
        "RETURN (FOR e IN c INSERT {} INTO c)",
        "FOR d IN c LET y = (FOR e IN c REMOVE e IN c) RETURN y",
        "FOR d IN c FILTER (FOR e IN c INSERT {} INTO c) RETURN d",
        "FOR d IN c SORT (FOR e IN c REMOVE e IN c) RETURN d",
        "FOR d IN c RETURN {nested: (FOR e IN c INSERT {} INTO c)}",
        "FOR d IN c RETURN [1, (FOR e IN c INSERT {} INTO c)]",
        "RETURN LENGTH((FOR e IN c INSERT {} INTO c))",
        "RETURN true ? (FOR e IN c INSERT {} INTO c) : 1",
        "RETURN CREATE_VIEW(\"v\", {})",
        "RETURN DROP_VIEW(\"v\")",
        "RETURN CREATE_GRAPH(\"g\", {})",
        "RETURN DROP_GRAPH(\"g\")",
        "RETURN LENGTH([DROP_GRAPH(\"g\")])",
        // Audit C4: the ROW_POLICY setter lifts or rewrites a row policy.
        "RETURN ROW_POLICY(\"orders\", null)",
        "RETURN ROW_POLICY(\"orders\", \"doc.owner == CURRENT_USER\")",
        // Audit A11: dynamic dispatch hides the called function's name.
        "RETURN APPLY(\"DROP_GRAPH\", [\"prod\"])",
        "RETURN CALL(\"drop_view\", \"v\")",
        "RETURN APPLY(\"ROW_POLICY\", [\"orders\", null])",
        "RETURN CALL(\"APPLY\", \"DROP_GRAPH\", [\"g\"])",
        "FOR d IN c RETURN CALL(d.fn, 1)",
        "RETURN APPLY(@fn, [])",
    ] {
        let parsed = parse(query).unwrap_or_else(|e| panic!("parse {query}: {e}"));
        assert!(parsed.has_mutations(), "must require Write: {}", query);
    }
}

/// The counterpart: ordinary reads must not be pushed up to Write, or every
/// read-only principal loses query access.
#[test]
fn test_has_mutations_leaves_reads_alone() {
    for query in [
        "FOR d IN users RETURN d",
        "FOR d IN users FILTER d.age > 25 SORT d.age DESC LIMIT 10 RETURN d",
        "FOR d IN users RETURN (FOR o IN orders FILTER o.user == d._key RETURN o)",
        "RETURN LENGTH(users)",
        "FOR d IN c COLLECT g = d.kind AGGREGATE n = COUNT() RETURN {g, n}",
        "WITH t AS (FOR d IN c RETURN d) FOR x IN t RETURN x",
        "FOR d IN c RETURN CONCAT(d.a, d.b)",
        "RETURN ROW_POLICY(\"orders\")",
        "RETURN CALL(\"ABS\", -3)",
        "RETURN APPLY(\"CONCAT\", [\"a\", \"b\"])",
    ] {
        let parsed = parse(query).unwrap_or_else(|e| panic!("parse {query}: {e}"));
        assert!(!parsed.has_mutations(), "must stay a read: {}", query);
    }
}

/// Clause-level mutations were always detected; keep them covered so the
/// rewrite above cannot regress them. (UPSERT is omitted: the query-level
/// validator rejects UPSERT-only queries as "missing RETURN clause or
/// mutation", a pre-existing parser gap unrelated to authorization.)
#[test]
fn test_has_mutations_still_sees_clause_level_writes() {
    for query in [
        "INSERT {a: 1} INTO c",
        "FOR d IN c UPDATE d WITH {x: 1} IN c",
        "FOR d IN c REMOVE d IN c",
        "FOR d IN c RETURN d UNION (FOR e IN c INSERT {} INTO c)",
    ] {
        let parsed = parse(query).unwrap_or_else(|e| panic!("parse {query}: {e}"));
        assert!(parsed.has_mutations(), "must require Write: {}", query);
    }
}

// --- LET after LIMIT --------------------------------------------------------
//
// AQL allows a binding past the limit; this parser used to stop there with
// "Unexpected token: Let", because the clause order is fixed (body, SORT,
// LIMIT, RETURN) and a `LET` written afterwards had nowhere to go.

#[test]
fn test_parse_let_after_limit() {
    let query = parse("FOR c IN companies SORT c.name ASC LIMIT 0, 50 LET n = 1 RETURN n").unwrap();

    assert_eq!(query.post_limit_lets.len(), 1);
    assert_eq!(query.post_limit_lets[0].variable, "n");
    assert!(query.limit_clause.is_some());
    assert!(query.return_clause.is_some());
}

#[test]
fn test_parse_let_after_limit_keeps_body_lets_apart() {
    // A binding on each side of the limit: the one before belongs to the body
    // and runs for every row, the one after only for the survivors. Folding
    // them together would lose exactly that distinction.
    let query = parse("FOR c IN companies LET a = 1 LIMIT 10 LET b = 2 RETURN [a, b]").unwrap();

    assert_eq!(query.post_limit_lets.len(), 1);
    assert_eq!(query.post_limit_lets[0].variable, "b");
    assert!(query
        .body_clauses
        .iter()
        .any(|clause| matches!(clause, BodyClause::Let(l) if l.variable == "a")));
}

#[test]
fn test_parse_several_lets_after_limit() {
    let query = parse("FOR c IN users LIMIT 5 LET a = 1 LET b = 2 RETURN [a, b]").unwrap();
    assert_eq!(query.post_limit_lets.len(), 2);
}

#[test]
fn test_parse_let_after_limit_with_subquery() {
    // The case this was written for: a correlated subquery paid for once per
    // returned row rather than once per row scanned.
    let query = parse(
        "FOR c IN companies SORT c.name ASC LIMIT 0, 50 \
         LET dus = (FOR o IN orders FILTER o.company_id == c._key RETURN o.total) \
         RETURN MERGE(c, { \"encours\": SUM(dus) })",
    )
    .unwrap();

    assert_eq!(query.post_limit_lets.len(), 1);
    assert_eq!(query.post_limit_lets[0].variable, "dus");
}

#[test]
fn test_parse_let_after_standalone_offset() {
    let query = parse("FOR c IN users OFFSET 10 LET n = 1 RETURN n").unwrap();
    assert_eq!(query.post_limit_lets.len(), 1);
}