powdb-query 0.18.0

PowQL lexer, parser, planner, and executor — compiled query engine for PowDB
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Adversarial end-to-end battery for the v0.12 JSON path surface (design
//! 2026-07-13, sections 4.3 and 4.4), driven entirely through `Engine` on the
//! public PowQL text interface.
//!
//! This is the hostile complement to `json_type.rs` (the happy-path coverage).
//! It attacks:
//!
//!   1. `->` precedence and `-`/`->` lexing torture,
//!   2. scalarization + no-implicit-coercion across every leaf type,
//!   3. the #137 plan-cache class (path segments are STRUCTURAL, not literals),
//!   4. spill interaction: paths into >4070B docs and inline<->spilled flips,
//!   5. crash recovery then path query,
//!   6. mutation / constraint / grouping / ordering edges,
//!   7. `json_type` including missing paths and a non-json base.
//!
//! Every case asserts a concrete correct result. Nothing may panic.

use powdb_query::executor::Engine;
use powdb_query::result::{QueryError, QueryResult};
use powdb_storage::types::Value;

fn temp_dir(name: &str) -> std::path::PathBuf {
    std::env::temp_dir().join(format!(
        "powdb_jsonpath_{name}_{}_{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ))
}

fn exec(engine: &mut Engine, q: &str) -> QueryResult {
    engine
        .execute_powql(q)
        .unwrap_or_else(|e| panic!("failed `{q}`: {e}"))
}

fn rows(engine: &mut Engine, q: &str) -> Vec<Vec<Value>> {
    match exec(engine, q) {
        QueryResult::Rows { rows, .. } => rows,
        other => panic!("expected rows for `{q}`, got {other:?}"),
    }
}

/// Number of rows a query returns (order-insensitive checks).
fn count(engine: &mut Engine, q: &str) -> usize {
    rows(engine, q).len()
}

/// A query that returns a single scalar (e.g. `count(distinct ...)`).
fn scalar(engine: &mut Engine, q: &str) -> Value {
    match exec(engine, q) {
        QueryResult::Scalar(v) => v,
        other => panic!("expected a scalar for `{q}`, got {other:?}"),
    }
}

fn engine_with_posts(name: &str) -> Engine {
    let mut engine = Engine::new(&temp_dir(name)).unwrap();
    exec(&mut engine, "type Post { required id: int, data: json }");
    engine
}

/// Insert one Post whose `data` is the given JSON text. `json` is embedded into
/// a PowQL string literal, so its double-quotes are escaped.
fn insert(engine: &mut Engine, id: i64, json: &str) {
    let escaped = json.replace('\\', "\\\\").replace('"', "\\\"");
    exec(
        engine,
        &format!(r#"insert Post {{ id := {id}, data := "{escaped}" }}"#),
    );
}

// ─── 1. precedence / lexing torture ──────────────────────────────────────────

#[test]
fn arrow_binds_tighter_than_comparison_and_arithmetic() {
    let mut e = engine_with_posts("prec");
    insert(&mut e, 1, r#"{"age":21,"n":[10,20]}"#);
    // `.data->age > 20` groups as `(.data->age) > 20`.
    assert_eq!(
        rows(&mut e, "Post filter .data->age > 20 { .id }"),
        vec![vec![Value::Int(1)]]
    );
    // Arithmetic binds looser than the path: `(.data->age) - 1 = 20`.
    assert_eq!(
        rows(&mut e, "Post filter .data->age - 1 = 20 { .id }"),
        vec![vec![Value::Int(1)]]
    );
    // Chained index + compare: `(.data->n->1) = 20`.
    assert_eq!(
        rows(&mut e, "Post filter .data->n->1 = 20 { .id }"),
        vec![vec![Value::Int(1)]]
    );
    // A compound predicate mixing a path index and a path key.
    insert(&mut e, 2, r#"{"age":5,"n":[1,2]}"#);
    let r = rows(
        &mut e,
        "Post filter .data->n->0 = 10 and .data->age > 2 { .id }",
    );
    assert_eq!(r, vec![vec![Value::Int(1)]]);
}

#[test]
fn dash_versus_arrow_lexing() {
    let mut e = engine_with_posts("dash");
    insert(&mut e, 1, r#"{"a":[100,200]}"#);
    insert(&mut e, 2, r#"{"a":[9,9],"n":5}"#);
    // `.data->a->1` is an index path (arrow glued to digit).
    assert_eq!(
        rows(&mut e, "Post filter .data->a->1 = 200 { .id }"),
        vec![vec![Value::Int(1)]]
    );
    // Spaced `.data->n - 1` is subtraction, not a second arrow.
    let r = rows(&mut e, "Post filter .data->n - 1 = 4 { .id }");
    assert_eq!(r, vec![vec![Value::Int(2)]], "` - ` lexes as minus");
}

#[test]
fn string_form_keys_quotes_unicode_and_empty() {
    let mut e = engine_with_posts("strkey");
    insert(&mut e, 1, r#"{"weird key!":1,"":2,"é":3,"a\"b":4}"#);
    assert_eq!(
        rows(&mut e, r#"Post filter .data->"weird key!" = 1 { .id }"#),
        vec![vec![Value::Int(1)]],
        "spaced/punctuated string key"
    );
    assert_eq!(
        rows(&mut e, r#"Post filter .data->"" = 2 { .id }"#),
        vec![vec![Value::Int(1)]],
        "empty-string key"
    );
    assert_eq!(
        rows(&mut e, "Post filter .data->\"é\" = 3 { .id }"),
        vec![vec![Value::Int(1)]],
        "unicode key"
    );
    assert_eq!(
        rows(&mut e, r#"Post filter .data->"a\"b" = 4 { .id }"#),
        vec![vec![Value::Int(1)]],
        "key containing an escaped quote"
    );
}

// ─── 2. scalarization + no implicit coercion ─────────────────────────────────

#[test]
fn scalarization_matrix_through_project_and_filter() {
    let mut e = engine_with_posts("scalar");
    insert(
        &mut e,
        1,
        r#"{"s":"hi","i":7,"f":1.5,"bt":true,"bf":false,"nul":null,"arr":[1],"obj":{"k":1}}"#,
    );
    let r = rows(
        &mut e,
        "Post filter .id = 1 { \
         s: .data->s, i: .data->i, f: .data->f, bt: .data->bt, bf: .data->bf, \
         nul: .data->nul, arr: .data->arr, obj: .data->obj, miss: .data->nope }",
    );
    assert_eq!(r[0][0], Value::Str("hi".into()));
    assert_eq!(r[0][1], Value::Int(7));
    assert_eq!(r[0][2], Value::Float(1.5));
    assert_eq!(r[0][3], Value::Bool(true));
    assert_eq!(r[0][4], Value::Bool(false));
    assert_eq!(r[0][5], Value::Empty, "JSON null -> Empty");
    assert!(matches!(r[0][6], Value::Json(_)), "array -> Json subdoc");
    assert!(matches!(r[0][7], Value::Json(_)), "object -> Json subdoc");
    assert_eq!(r[0][8], Value::Empty, "missing -> Empty");
}

#[test]
fn no_implicit_cross_type_coercion() {
    // `.data->age` scalarizes to whatever the node is; comparisons then follow
    // existing Value rules with NO implicit coercion. Two distinctions matter:
    //   - `=` is TYPED equality (Value::eq): Int(21) == literal 21, but a
    //     Float(21.0) node does NOT equal the int literal 21, and no node
    //     equals a string literal.
    //   - range ops use Value::cmp, which DOES compare Int/Float numerically.
    let mut e = engine_with_posts("nocoerce");
    // Row 1 is an Int node; row 2 is a Float node.
    insert(&mut e, 1, r#"{"age":21}"#);
    insert(&mut e, 2, r#"{"age":21.0}"#);
    // Comparing a number node to a STRING literal never matches (different type
    // ranks) and never panics.
    assert_eq!(
        count(&mut e, r#"Post filter .data->age = "21""#),
        0,
        "number node != Str literal"
    );
    // Typed `=`: only the Int(21) node equals the int literal 21.
    assert_eq!(
        count(&mut e, "Post filter .data->age = 21"),
        1,
        "typed equality: Float(21.0) does NOT equal Int literal 21"
    );
    // Range `>` uses Value::cmp, so both the Int and Float nodes clear 20.
    assert_eq!(
        count(&mut e, "Post filter .data->age > 20"),
        2,
        "range comparison is numeric across Int/Float"
    );
}

// ─── 3. plan-cache adversarial (#137 class) ──────────────────────────────────

#[test]
fn same_path_different_literal_shares_plan_and_stays_correct() {
    let mut e = engine_with_posts("samepath");
    insert(&mut e, 1, r#"{"age":30}"#);
    insert(&mut e, 2, r#"{"age":18}"#);
    // Two literals over the identical path: same canonical plan, different
    // substituted literal. Both must return the right single row.
    assert_eq!(
        rows(&mut e, "Post filter .data->age = 30 { .id }"),
        vec![vec![Value::Int(1)]]
    );
    assert_eq!(
        rows(&mut e, "Post filter .data->age = 18 { .id }"),
        vec![vec![Value::Int(2)]]
    );
}

#[test]
fn different_paths_same_shape_do_not_collide() {
    // Same query shape, different STRUCTURAL path segment. If the two hashed to
    // one plan (#137 regression), one query would return the other's row.
    let mut e = engine_with_posts("diffpath");
    insert(&mut e, 1, r#"{"age":30,"yrs":99}"#);
    insert(&mut e, 2, r#"{"age":99,"yrs":30}"#);
    assert_eq!(
        rows(&mut e, "Post filter .data->age = 30 { .id }"),
        vec![vec![Value::Int(1)]]
    );
    assert_eq!(
        rows(&mut e, "Post filter .data->yrs = 30 { .id }"),
        vec![vec![Value::Int(2)]]
    );
    // An index segment vs a different index segment likewise stay distinct.
    insert(&mut e, 3, r#"{"t":[5,6]}"#);
    assert_eq!(count(&mut e, "Post filter .data->t->0 = 5 { .id }"), 1);
    assert_eq!(count(&mut e, "Post filter .data->t->1 = 5 { .id }"), 0);
}

#[test]
fn alternating_literals_do_not_drift_over_repeats() {
    // Hammer the plan cache: alternate two literals many times. A cached plan
    // that mis-substitutes would drift to a wrong row on some iteration.
    let mut e = engine_with_posts("drift");
    insert(&mut e, 1, r#"{"age":30}"#);
    insert(&mut e, 2, r#"{"age":18}"#);
    for _ in 0..100 {
        assert_eq!(
            rows(&mut e, "Post filter .data->age = 30 { .id }"),
            vec![vec![Value::Int(1)]]
        );
        assert_eq!(
            rows(&mut e, "Post filter .data->age = 18 { .id }"),
            vec![vec![Value::Int(2)]]
        );
    }
}

#[test]
fn prepared_path_query_round_trips() {
    use powdb_query::ast::Literal;
    let mut e = engine_with_posts("prep");
    insert(&mut e, 1, r#"{"age":30}"#);
    insert(&mut e, 2, r#"{"age":18}"#);
    // Prepare a path-bearing filter and rebind the comparison literal. The path
    // segment is structural; only the literal slot rebinds.
    let prep = e
        .prepare("Post filter .data->age = 30 { .id }")
        .expect("prepare path filter");
    let got = match e
        .execute_prepared(&prep, &[Literal::Int(18)])
        .expect("execute_prepared")
    {
        QueryResult::Rows { rows, .. } => rows,
        other => panic!("expected rows, got {other:?}"),
    };
    assert_eq!(
        got,
        vec![vec![Value::Int(2)]],
        "rebinding the literal reaches row 2"
    );
}

// ─── 4. spill interaction ────────────────────────────────────────────────────

#[test]
fn path_into_spilled_document() {
    let mut e = engine_with_posts("spill");
    let blob = "z".repeat(6000);
    insert(
        &mut e,
        1,
        &format!(r#"{{"author":"amy","blob":"{blob}","n":9}}"#),
    );
    // Filter + project through the reassembled out-of-line document.
    let r = rows(
        &mut e,
        r#"Post filter .data->author = "amy" { .id, n: .data->n }"#,
    );
    assert_eq!(r, vec![vec![Value::Int(1), Value::Int(9)]]);
    // The oversized string node scalarizes back to full length.
    match &rows(&mut e, "Post filter .id = 1 { b: .data->blob }")[0][0] {
        Value::Str(s) => assert_eq!(s.len(), 6000),
        other => panic!("expected Str, got {other:?}"),
    }
}

#[test]
fn update_flips_inline_spilled_inline_and_path_finds_it_each_step() {
    // The v0.11 overflow defect class, now with json: a doc that moves inline ->
    // spilled -> inline across updates must remain path-queryable at every step.
    let mut e = engine_with_posts("flip");
    // Step 1: small, inline.
    insert(&mut e, 1, r#"{"k":"small","tag":1}"#);
    assert_eq!(
        rows(&mut e, "Post filter .data->tag = 1 { s: .data->k }"),
        vec![vec![Value::Str("small".into())]]
    );
    // Step 2: grow past the inline cap -> spills.
    let big = "b".repeat(6000);
    exec(
        &mut e,
        &format!(r#"Post filter .id = 1 update {{ data := "{{\"k\":\"{big}\",\"tag\":2}}" }}"#),
    );
    assert_eq!(
        count(&mut e, "Post filter .data->tag = 2 { .id }"),
        1,
        "found while spilled"
    );
    match &rows(&mut e, "Post filter .id = 1 { s: .data->k }")[0][0] {
        Value::Str(s) => assert_eq!(s.len(), 6000, "spilled value reassembles"),
        other => panic!("expected Str, got {other:?}"),
    }
    // Step 3: shrink back inline.
    exec(
        &mut e,
        r#"Post filter .id = 1 update { data := "{\"k\":\"tiny\",\"tag\":3}" }"#,
    );
    assert_eq!(
        rows(&mut e, "Post filter .data->tag = 3 { s: .data->k }"),
        vec![vec![Value::Str("tiny".into())]],
        "found again once inline"
    );
    // The intermediate tag values must not linger.
    assert_eq!(count(&mut e, "Post filter .data->tag = 1"), 0);
    assert_eq!(count(&mut e, "Post filter .data->tag = 2"), 0);
}

#[test]
fn crash_recovery_then_path_query() {
    // Commit rows (autocommit), drop the engine, reopen the same directory, and
    // confirm a path query still finds the recovered rows.
    let dir = temp_dir("recover");
    {
        let mut e = Engine::new(&dir).unwrap();
        exec(&mut e, "type Post { required id: int, data: json }");
        insert(&mut e, 1, r#"{"author":"ida","age":40}"#);
        let spilled = "s".repeat(6000);
        insert(
            &mut e,
            2,
            &format!(r#"{{"author":"jon","blob":"{spilled}"}}"#),
        );
    }
    let mut e2 = Engine::new(&dir).unwrap();
    assert_eq!(
        rows(
            &mut e2,
            r#"Post filter .data->author = "ida" { age: .data->age }"#
        ),
        vec![vec![Value::Int(40)]],
        "inline doc recovered and path-queryable"
    );
    match &rows(&mut e2, "Post filter .id = 2 { b: .data->blob }")[0][0] {
        Value::Str(s) => assert_eq!(s.len(), 6000, "spilled doc recovered whole"),
        other => panic!("expected Str, got {other:?}"),
    }
}

// ─── 5. mutation / constraint edges ──────────────────────────────────────────

#[test]
fn invalid_json_insert_is_typed_error_and_message_survives() {
    let mut e = engine_with_posts("badjson");
    let err = e
        .execute_powql(r#"insert Post { id := 1, data := "{oops" }"#)
        .unwrap_err();
    let msg = match err {
        QueryError::TypeError(m) | QueryError::Execution(m) => m,
        other => panic!("expected a coercion error, got {other:?}"),
    };
    assert!(
        msg.starts_with("invalid JSON"),
        "safe wire prefix survives: {msg}"
    );
    // The failed insert left no row behind.
    assert_eq!(count(&mut e, "Post filter .id = 1"), 0);
}

#[test]
fn required_json_column_rejects_missing_value() {
    let mut e = Engine::new(&temp_dir("required")).unwrap();
    exec(&mut e, "type Doc { required id: int, required body: json }");
    // Providing the json is fine.
    exec(&mut e, r#"insert Doc { id := 1, body := "{\"ok\":true}" }"#);
    // Omitting the required json column is rejected.
    let err = e.execute_powql("insert Doc { id := 2 }").unwrap_err();
    assert!(
        matches!(err, QueryError::TypeError(_) | QueryError::Execution(_)),
        "missing required json -> typed error, got {err:?}"
    );
    assert_eq!(
        count(&mut e, "Doc { .id }"),
        1,
        "only the valid row persisted"
    );
}

#[test]
fn group_by_json_column_uses_byte_equality() {
    // Grouping/DISTINCT on a whole json column keys on canonical PJ1 BYTES
    // (Value::Json Eq/Hash), which is byte-equality. Two texts that canonicalize
    // to identical bytes collapse; two that do not (`1` vs `1.0`) stay separate,
    // EVEN THOUGH pj1_cmp treats them as numerically equal. This Eq(bytes) vs
    // Ord(pj1_cmp) split is the same deliberate choice Value already makes for
    // Int vs Float (see types.rs); pin the observable grouping behaviour.
    let mut e = engine_with_posts("group");
    insert(&mut e, 1, r#"{"a":1,"b":2}"#);
    insert(&mut e, 2, r#"{"b":2,"a":1}"#); // same canonical bytes as id 1
    insert(&mut e, 3, r#"1"#);
    insert(&mut e, 4, r#"1.0"#); // numerically == id 3 but different bytes
    let groups = rows(&mut e, "Post group .data { .data, n: count(.id) }");
    // 3 groups: {the shared object}, `1`, `1.0`.
    assert_eq!(
        groups.len(),
        3,
        "byte-equal object rows collapse; 1 and 1.0 do not"
    );
    let counts: Vec<i64> = groups
        .iter()
        .map(|g| match g[1] {
            Value::Int(n) => n,
            ref other => panic!("count not int: {other:?}"),
        })
        .collect();
    let mut sorted = counts.clone();
    sorted.sort_unstable();
    assert_eq!(
        sorted,
        vec![1, 1, 2],
        "the object group has 2, the two numbers 1 each"
    );
    // count(distinct) mirrors the same equality (returns a scalar).
    assert_eq!(
        scalar(&mut e, "count(distinct Post { .data })"),
        Value::Int(3),
        "3 distinct json documents by bytes"
    );
}

#[test]
fn order_by_json_column_follows_the_pj1_total_order() {
    // ORDER BY a whole json column sorts by Value::cmp = pj1_cmp = the type
    // ladder null < false < true < number < string < array < object.
    let mut e = engine_with_posts("orderladder");
    insert(&mut e, 1, r#"{}"#); // object
    insert(&mut e, 2, r#"[1]"#); // array
    insert(&mut e, 3, r#""s""#); // string
    insert(&mut e, 4, r#"5"#); // number
    insert(&mut e, 5, r#"true"#); // true
    insert(&mut e, 6, r#"false"#); // false
    insert(&mut e, 7, r#"null"#); // null
    let ordered: Vec<i64> = rows(&mut e, "Post order .data { .id }")
        .into_iter()
        .map(|r| match r[0] {
            Value::Int(n) => n,
            ref o => panic!("id not int: {o:?}"),
        })
        .collect();
    assert_eq!(
        ordered,
        vec![7, 6, 5, 4, 3, 2, 1],
        "null < false < true < number < string < array < object"
    );
}

#[test]
fn path_expressions_work_in_order_group_and_aggregate_positions() {
    let mut e = engine_with_posts("pathkey");
    insert(&mut e, 1, r#"{"age":30,"kind":"a"}"#);
    insert(&mut e, 2, r#"{"age":18,"kind":"b"}"#);
    insert(&mut e, 3, r#"{"age":12,"kind":"a"}"#);

    assert_eq!(
        rows(&mut e, "Post order .data->age { .id }"),
        vec![
            vec![Value::Int(3)],
            vec![Value::Int(2)],
            vec![Value::Int(1)]
        ]
    );
    assert_eq!(
        rows(
            &mut e,
            "Post group .data->kind { kind: .data->kind, total: sum(.data->age) }",
        ),
        vec![
            vec![Value::Str("a".into()), Value::Int(42)],
            vec![Value::Str("b".into()), Value::Int(18)]
        ]
    );
    assert_eq!(scalar(&mut e, "sum(Post { .data->age })"), Value::Int(60));
}

#[test]
fn grouped_order_by_path_rebinds_through_projection_alias() {
    let mut e = engine_with_posts("grouped_path_order");
    insert(&mut e, 1, r#"{"kind":"z"}"#);
    insert(&mut e, 2, r#"{"kind":"a"}"#);
    insert(&mut e, 3, r#"{"kind":"z"}"#);
    assert_eq!(
        rows(
            &mut e,
            "Post group .data->kind order .data->kind { kind: .data->kind, n: count(*) }",
        ),
        vec![
            vec![Value::Str("a".into()), Value::Int(1)],
            vec![Value::Str("z".into()), Value::Int(2)],
        ]
    );
}

#[test]
fn qualified_joined_paths_work_in_filter_group_and_aggregate_positions() {
    let mut e = engine_with_posts("qualified_joined_paths");
    exec(&mut e, "type Meta { required post_id: int, data: json }");
    insert(&mut e, 1, r#"{"kind":"a"}"#);
    insert(&mut e, 2, r#"{"kind":"b"}"#);
    exec(
        &mut e,
        r#"insert Meta { post_id := 1, data := "{\"kind\":\"a\",\"value\":7}" }"#,
    );
    exec(
        &mut e,
        r#"insert Meta { post_id := 2, data := "{\"kind\":\"wrong\",\"value\":11}" }"#,
    );

    assert_eq!(
        rows(
            &mut e,
            "Post as p inner join Meta as m on p.id = m.post_id \
             filter p.data->kind = m.data->kind \
             group p.data->kind { kind: p.data->kind, total: sum(m.data->value) }",
        ),
        vec![vec![Value::Str("a".into()), Value::Int(7)]],
    );
}

#[test]
fn window_partition_and_order_accept_json_paths() {
    let mut e = engine_with_posts("window_paths");
    insert(&mut e, 1, r#"{"kind":"a","score":10}"#);
    insert(&mut e, 2, r#"{"kind":"a","score":20}"#);
    insert(&mut e, 3, r#"{"kind":"b","score":15}"#);

    assert_eq!(
        rows(
            &mut e,
            "Post order .id { .id, rn: row_number() over \
             (partition .data->kind order .data->score desc) }",
        ),
        vec![
            vec![Value::Int(1), Value::Int(2)],
            vec![Value::Int(2), Value::Int(1)],
            vec![Value::Int(3), Value::Int(1)],
        ],
    );
}

#[test]
fn non_json_path_bases_are_rejected_in_all_new_expression_slots() {
    let mut e = Engine::new(&temp_dir("non_json_expression_slots")).unwrap();
    exec(&mut e, "type Metric { age: int }");
    exec(&mut e, "insert Metric { age := 7 }");
    for query in [
        "Metric order .age->x",
        "Metric group .age->x { .age->x }",
        "sum(Metric { .age->x })",
        "Metric group .age { total: sum(.age->x) }",
        "Metric { n: row_number() over (partition .age->x) }",
    ] {
        let err = e.execute_powql(query).expect_err(query);
        assert!(
            err.to_string().contains("not json"),
            "`{query}` should reject non-json path base, got {err}"
        );
    }
}

#[test]
fn stray_aggregates_are_rejected_in_every_expression_slot() {
    let mut e = Engine::new(&temp_dir("stray_aggregate_slots")).unwrap();
    exec(&mut e, "type Metric { age: int }");
    exec(&mut e, "insert Metric { age := 7 }");
    for query in [
        "Metric order sum(.age)",
        "Metric group sum(.age) { .age }",
        "sum(Metric { sum(.age) })",
        "Metric { n: row_number() over (partition sum(.age)) }",
        "Metric as a join Metric as b on sum(a.age) = b.age",
    ] {
        let err = e.execute_powql(query).expect_err(query);
        assert!(
            err.to_string()
                .contains("aggregate function in an unsupported position"),
            "`{query}` should reject stray aggregate, got {err}"
        );
    }
}

#[test]
fn limit_offset_fast_path_over_json_table() {
    let mut e = engine_with_posts("limit");
    for id in 1..=5 {
        insert(&mut e, id, &format!(r#"{{"v":{id}}}"#));
    }
    // ORDER by the real id column, project a path value, then slice.
    let r = rows(
        &mut e,
        "Post order .id { .id, v: .data->v } limit 2 offset 1",
    );
    assert_eq!(
        r,
        vec![
            vec![Value::Int(2), Value::Int(2)],
            vec![Value::Int(3), Value::Int(3)],
        ],
        "limit/offset applied after path projection"
    );
}

// ─── 6. json_type full matrix ────────────────────────────────────────────────

#[test]
fn json_type_matrix_including_missing_and_non_json_base() {
    let mut e = engine_with_posts("jtype");
    insert(
        &mut e,
        1,
        r#"{"nul":null,"s":"x","i":1,"f":1.5,"bt":true,"arr":[1],"obj":{}}"#,
    );
    let r = rows(
        &mut e,
        "Post filter .id = 1 { \
         nul: json_type(.data->nul), s: json_type(.data->s), i: json_type(.data->i), \
         f: json_type(.data->f), bt: json_type(.data->bt), arr: json_type(.data->arr), \
         obj: json_type(.data->obj), miss: json_type(.data->nope) }",
    );
    assert_eq!(r[0][0], Value::Str("null".into()), "present null");
    assert_eq!(r[0][1], Value::Str("string".into()));
    assert_eq!(r[0][2], Value::Str("number".into()), "integral is 'number'");
    assert_eq!(r[0][3], Value::Str("number".into()), "float is 'number'");
    assert_eq!(r[0][4], Value::Str("bool".into()));
    assert_eq!(r[0][5], Value::Str("array".into()));
    assert_eq!(r[0][6], Value::Str("object".into()));
    assert_eq!(
        r[0][7],
        Value::Empty,
        "missing path -> Empty, distinct from 'null'"
    );

    // json_type over a NON-json base column (`.id` is int) currently scalarizes
    // to Empty rather than raising a typed error. NOTE (reported as a finding):
    // this is inconsistent with `->` on a non-json column, which IS a hard type
    // error (see json_type.rs::path_on_non_json_column_is_a_typed_error). Here
    // we pin the actual behaviour so the suite is green and the inconsistency is
    // tracked, not silently baked in as "correct".
    let t = rows(&mut e, "Post filter .id = 1 { t: json_type(.id) }");
    assert_eq!(
        t[0][0],
        Value::Empty,
        "json_type(non-json column) yields Empty (see finding: should arguably error)"
    );
    // Because it is Empty, a predicate over it simply matches nothing (no panic).
    assert_eq!(count(&mut e, "Post filter json_type(.id) = \"number\""), 0);
}