qua 0.75.0

Structure-aware query CLI for the Quarb engine
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
//! The diagnostic codes (ruling 2026-11-17): one word per reason
//! where `NOT_PUSHED` was the one kind, each line ending with the
//! door to its remedy, `qua man CODE` the page behind the door, and
//! every code the workspace raises a constant of the engine's list,
//! which a test in the engine holds against the inventory
//! (`langspec/diagnostics.tsv`, vendored).

use std::path::{Path, PathBuf};

fn fixture(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("quarb-codes-{}-{tag}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let db = rusqlite::Connection::open(dir.join("lib.db")).unwrap();
    db.execute_batch(
        "CREATE TABLE authors (id INTEGER PRIMARY KEY, name TEXT, born INTEGER);
         INSERT INTO authors VALUES
           (1, 'Homer', -800), (2, 'Austen', 1775), (3, 'Twain', 1835), (4, 'Dante', 1265);
         CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT, pages INTEGER,
           author_id INTEGER REFERENCES authors(id));
         INSERT INTO books VALUES
           (1, 'Iliad', 300, 1), (2, 'Emma', 320, 2), (3, 'Tom Sawyer', 240, 3),
           (4, 'Persuasion', 250, 2);",
    )
    .unwrap();
    dir
}

/// Whether the run succeeded, stdout, and stderr.
fn run(dir: &Path, args: &[&str]) -> (bool, String, String) {
    let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
        .env("QUARB_LOCALE", "C")
        .current_dir(dir)
        .args(args)
        .stdin(std::process::Stdio::null())
        .output()
        .unwrap();
    (
        out.status.success(),
        String::from_utf8(out.stdout).unwrap().trim().to_string(),
        String::from_utf8_lossy(&out.stderr).trim().to_string(),
    )
}

/// The info lines of a query over the fixture, in order.
fn told(dir: &Path, query: &str) -> Vec<String> {
    let (ok, _, err) = run(dir, &["--diagnostics=info", query, "lib.db"]);
    assert!(ok, "{query}: {err}");
    err.lines().map(str::to_string).collect()
}

/// What a pushed plan keyed by a text column says beside its
/// statement (the user's ruling of 2026-11-29, relayed by lang:
/// push, and say so): the scan tells where the texts a command tells
/// apart write numbers, and a statement reads no value to judge.
fn keyed_by_text(column: &str) -> String {
    format!(
        "info PUSHED (query): the plan is keyed by {column}, which is no numeric column, and \
         reads no value: where its texts write numbers the scan says so (NUMBERS_AS_TEXT) \
         and a plan cannot; '--no-pushdown' scans"
    )
}

#[test]
fn a_refusal_is_told_under_its_word_with_the_door_to_its_remedy() {
    let dir = fixture("word");
    // the user's example of the ruling
    assert_eq!(
        told(&dir, "/books/* @| sort_by(::title) @| [..2] | ::title"),
        [
            "info SORT_NOT_PUSHED (query): ORDER BY collations differ per engine; scanning. \
             Narrow first: qua man SORT_NOT_PUSHED"
        ]
    );
    // a word whose remedy is another spelling
    assert_eq!(
        told(&dir, r#"/books/* <=> /authors/*[::name = "Homer"]"#),
        [
            "info JOIN_NOT_PUSHED (query): a correlation without a '$$' equality (no JOIN \
             condition); scanning. Spell it another way: qua man JOIN_NOT_PUSHED"
        ]
    );
    // a word with nothing to rewrite has no tail
    assert_eq!(
        told(&dir, "/books/*->author::name @| count"),
        [
            "info WALK_NOT_PUSHED (query): navigation beyond /table/* (SQL sees tables and \
             rows); scanning"
        ]
    );
    // a negation is pushed in the form that keeps the rows SQL's NOT
    // would drop; over a key comparison of a join it is SQL's own
    // NOT, whose unknown is the engine's there (0.71: it stayed with
    // the engine)
    assert_eq!(
        told(&dir, "/books/*[!(::pages > 245)] @| count"),
        [
            "info PUSHED (query): pushdown: SELECT COUNT(*) FROM books WHERE (CASE WHEN \
             (pages > 245) THEN 0 ELSE 1 END = 1)"
        ]
    );
    assert_eq!(
        told(
            &dir,
            "/books/* <=>* /authors/*[::id = $$0::author_id && !(::born > $$0::pages)] @| count"
        ),
        [
            "info PUSHED (query): pushdown: SELECT COUNT(*) FROM books JOIN authors ON \
             authors.id = books.author_id WHERE NOT ((authors.born > books.pages))"
        ]
    );
    // a negated key comparison alone is no JOIN condition
    assert_eq!(
        told(
            &dir,
            "/books/* <=>* /authors/*[!(::id = $$0::author_id)] @| count"
        ),
        [
            "info JOIN_NOT_PUSHED (query): a correlation without a '$$' equality (no JOIN \
             condition); scanning. Spell it another way: qua man JOIN_NOT_PUSHED"
        ]
    );
}

/// The prefilter's line (the first cleanup): told where a condition
/// was sent ahead, and where one could not be; silent where there
/// was none to send, and where its refusal is the whole plan's own.
#[test]
fn the_prefilter_speaks_only_of_a_condition() {
    let dir = fixture("prefilter");
    // a leading condition, sent ahead
    assert_eq!(
        told(
            &dir,
            "/books/*[::pages > 245] @| sort_by(::title) | ::title"
        ),
        [
            "info SORT_NOT_PUSHED (query): ORDER BY collations differ per engine; scanning. \
             Narrow first: qua man SORT_NOT_PUSHED",
            "info PUSHED (prefilter): partial pushdown: WHERE pages > 245 on books; the rest \
             scans the filtered set"
        ]
    );
    // a leading condition that what follows would see through
    assert_eq!(
        told(&dir, "/books/*[::pages > 245] | %(::title; n = :::n-rows)"),
        [
            "info OPERATOR_NOT_PUSHED (query): the adapter metadata projection; scanning",
            "info CONDITION_NOT_PUSHED (prefilter): metadata would observe the filtering \
             (:::n-rows, :index); scanning"
        ]
    );
    assert_eq!(
        told(
            &dir,
            "/books/*[::pages > 245] | %(::title; n = (/books/* @| count))"
        )[1],
        "info CONDITION_NOT_PUSHED (prefilter): the table is reached more than once; scanning"
    );
    // no leading condition: nothing more than the whole plan's line
    assert_eq!(
        told(&dir, "/books/* @| sort_by(::title) | ::title").len(),
        1
    );
    // the partial plan refused for the whole plan's reason: said once
    assert_eq!(
        told(&dir, "/books/*[::pages > 245](->author) || /authors/*"),
        ["info WALK_NOT_PUSHED (query): path patterns (groups and quantifiers); scanning"]
    );
}

/// The second cleanup: a query that does not parse is told by its
/// error alone; the planner, which reads with the same parser, adds
/// no line of its own.
#[test]
fn a_query_that_does_not_parse_has_no_planner_line() {
    let dir = fixture("syntax");
    let (ok, out, err) = run(&dir, &["--diagnostics=info", "/books/*[", "lib.db"]);
    assert!(!ok);
    assert_eq!(out, "");
    assert!(
        err.starts_with("error SYNTAX (query): parse error: "),
        "{err}"
    );
    assert_eq!(err.lines().count(), 1, "{err}");
}

/// An error the engine raises is told as its diagnostic's line: the
/// code and the site, the message as it always read, and the door
/// to the code's remedy where it has one (rulings 2026-11-18; until
/// 0.72 `Error: MESSAGE`). A failure that is no engine error has no
/// code and prints as before.
#[test]
fn an_error_is_told_under_its_code_with_the_door_to_its_remedy() {
    let dir = fixture("errors");
    std::fs::write(
        dir.join("deep.json"),
        r#"{"a": {"b": {"c": {"d": {"e": 1}}}}}"#,
    )
    .unwrap();
    std::fs::write(dir.join("bad.defs"), "def &f: /a ||| ;\n").unwrap();
    let told = |args: &[&str]| -> String {
        let (ok, out, err) = run(&dir, args);
        assert!(!ok, "{args:?}");
        assert_eq!(out, "", "{args:?}");
        err.trim_end().to_string()
    };
    assert_eq!(
        told(&["/a/* @| nosuch", "deep.json"]),
        "error SYNTAX (query): parse error: 'nosuch' is not a command ('qua man' lists the \
         commands) (at character 9)"
    );
    assert_eq!(
        told(&["= \"1 kg\" | convert(m)"]),
        "error REFUSED (query): refused: convert(m): a quantity converts within its own \
         dimension, and these are unlike (kg and m)"
    );
    // a code with a remedy: the door after the message
    assert_eq!(
        told(&["--quantifier-bound=2", "/a(/*)+", "deep.json"]),
        "error QUANTIFIER_BOUND (round 2): refused: quantifier bound reached at round 2 with \
         edges still to take — the closure would be silently incomplete; raise \
         --quantifier-bound. Give a bound: qua man QUANTIFIER_BOUND"
    );
    // a mount argument the engine refuses: the same record, the site
    // the mount (0.72.0 flattened the error to text on its way up and
    // printed the bare `Error: parse error: …`)
    assert_eq!(
        told(&["/a", "deep.json", "py=python:python3"]),
        "error SYNTAX (mount): parse error: 'py=python:python3' names no module: an interpreter \
         alone is no target; mount a module file ('py=python:./helpers.py') or an import path \
         ('py=python:pkg.helpers')"
    );
    assert_eq!(
        told(&["/a", "deep.json", "py=python:./nosuch.py"]),
        "error SYNTAX (mount): parse error: 'py=python:./nosuch.py': no such module file"
    );
    // no engine error, no code: the runtime's own layout
    assert_eq!(
        told(&["/a", "nosuch.json"]),
        "Error: reading nosuch.json\n\nCaused by:\n    No such file or directory (os error 2)"
    );
    // under a host's context the engine's error is its one line
    // still, the context in front of the message (the one teller,
    // the user's word of 2026-11-24: the session's form; it was
    // `Error: parsing definitions in bad.defs`, a blank line,
    // `Caused by:` and the engine's line under it)
    assert_eq!(
        told(&["--defs", "bad.defs", "/a", "deep.json"]),
        "error SYNTAX (query): parsing definitions in bad.defs: lex error: '||' is an \
         operator and takes a space on both sides: write ' || '"
    );
    // a message of several lines (an expansion's error draws the
    // operand it was given) is told as the run tells it: the line,
    // and the drawing under it as the engine laid it
    let several = told(&[
        "macro &show($p): //* @| tree; &show(/a/*[::e > 30])",
        "--expand",
        "deep.json",
    ]);
    let lines: Vec<&str> = several.lines().collect();
    assert!(
        lines[0].starts_with("error EXPANSION (query): expansion error: "),
        "{several}"
    );
    assert!(lines.len() > 2, "{several}");
    assert!(!several.contains("Caused by"), "{several}");
}

#[test]
fn a_run_with_pushdown_off_names_what_turned_it_off() {
    let dir = fixture("option");
    let (ok, out, err) = run(
        &dir,
        &[
            "--diagnostics=info",
            "--no-pushdown",
            "/books/* @| count",
            "lib.db",
        ],
    );
    assert!(ok, "{err}");
    assert_eq!(out, "4");
    assert_eq!(
        err,
        "info OPTION_NOT_PUSHED (query): nothing is pushed under --no-pushdown; scanning. \
         A choice to make: qua man OPTION_NOT_PUSHED"
    );
}

/// A translator's refusal is one line under the word of its reason,
/// the bridge its site, and the run ends with status 1. The tail
/// names `--lenient` only where the flag would translate what was
/// refused, and never under the flag itself.
#[test]
fn a_translators_refusal_is_one_coded_line() {
    let dir = fixture("bridge");
    std::fs::write(dir.join("e.json"), r#"{"events":[{"id":1,"ms":5}]}"#).unwrap();
    let refused = |args: &[&str]| {
        let (ok, out, err) = run(&dir, args);
        assert!(!ok && out.is_empty(), "{args:?}: {out}");
        assert_eq!(err.lines().count(), 1, "{err}");
        err
    };
    // jq: the lenient translator takes a by-key function
    let err = refused(&["--jq", ".events | sort_by(.ms)", "e.json"]);
    assert!(
        err.starts_with("error SORT_NOT_TRANSLATED (jq): the sort_by function (")
            && err.ends_with("). Or pass --lenient: qua man SORT_NOT_TRANSLATED"),
        "{err}"
    );
    // under the flag a refusal of the same word names no flag
    let err = refused(&["--jq", "--lenient", ".events | group_by(.id)", "e.json"]);
    assert!(
        err.starts_with("error SORT_NOT_TRANSLATED (jq): the group_by function without")
            && !err.contains("qua man"),
        "{err}"
    );
    // a construct no flag lifts
    assert_eq!(
        refused(&["--jq", "..", "e.json"]),
        "error DESCENT_NOT_TRANSLATED (jq): recursive descent .. (use Quarb's // in a \
         hand-written query). Write it in Quarb: qua man DESCENT_NOT_TRANSLATED"
    );
    // XPath
    assert_eq!(
        refused(&["--xpath", "/a/following::b", "e.json"]),
        "error AXIS_NOT_TRANSLATED (xpath): the following:: axis (no Quarb equivalent). \
         Write it in Quarb: qua man AXIS_NOT_TRANSLATED"
    );
    // SQL: the bridge has no lenient form
    assert_eq!(
        refused(&[
            "--sql",
            "SELECT a.name FROM authors a FULL JOIN books b ON b.author_id = a.id",
            "lib.db"
        ]),
        "error JOIN_NOT_TRANSLATED (sql): a FULL JOIN (no correlation keeps the unmatched \
         rows of both tables). Write it in Quarb: qua man JOIN_NOT_TRANSLATED"
    );
    assert!(
        refused(&["--sql", "DELETE FROM books", "lib.db"])
            .starts_with("error STATEMENT_NOT_TRANSLATED (sql): only SELECT statements"),
    );
    // a statement that is no SQL at all is an error with no word
    let (ok, _, err) = run(&dir, &["--sql", "SELECT FROM", "lib.db"]);
    assert!(!ok);
    assert!(err.starts_with("Error: translating SQL"), "{err}");
}

/// The SQL bridge end to end, the plan on: what `qua --sql` answers
/// over a database is what SQLite answers (the importer's own rig
/// holds the translations against the scan; here the translated
/// query goes through the planner too).
#[test]
fn the_sql_bridge_answers_as_the_database() {
    let dir = fixture("sqlbridge");
    let db = rusqlite::Connection::open(dir.join("lib.db")).unwrap();
    db.execute_batch("INSERT INTO books VALUES (5, 'Anonymous', NULL, NULL);")
        .unwrap();
    let cases = [
        "SELECT a.name, b.title FROM authors a JOIN books b ON b.author_id = a.id",
        "SELECT a.name, b.title FROM authors a LEFT JOIN books b ON b.author_id = a.id",
        "SELECT a.name, b.title FROM authors a LEFT JOIN books b ON b.author_id = a.id \
         WHERE b.pages IS NULL OR b.pages > 300",
        "SELECT title FROM books WHERE author_id <> 2",
        "SELECT title FROM books WHERE NOT (author_id = 2 OR pages < 250)",
        "SELECT title FROM books WHERE title LIKE '_m%' OR title LIKE '%a%n'",
        "SELECT COUNT(author_id) FROM books",
        "SELECT COUNT(pages) FROM books WHERE author_id = 2",
        "SELECT name FROM authors WHERE born > 1700 UNION ALL SELECT title FROM books \
         WHERE pages > 300",
    ];
    for sql in cases {
        let mut stmt = db.prepare(sql).unwrap();
        let n = stmt.column_count();
        let mut want: Vec<String> = stmt
            .query_map([], |r| {
                Ok((0..n)
                    .map(|i| match r.get_ref(i).unwrap() {
                        rusqlite::types::ValueRef::Null => "null".to_string(),
                        rusqlite::types::ValueRef::Integer(v) => v.to_string(),
                        rusqlite::types::ValueRef::Text(t) => {
                            format!("{:?}", String::from_utf8_lossy(t))
                        }
                        other => format!("{other:?}"),
                    })
                    .collect::<Vec<_>>()
                    .join("; "))
            })
            .unwrap()
            .collect::<Result<_, _>>()
            .unwrap();
        let (ok, out, err) = run(&dir, &["--sql", sql, "lib.db"]);
        assert!(ok, "{sql}: {err}");
        // a SELECT of several columns answers one table (ruling
        // 2026-11-24): its header line, then a row a line, `%(v; …)`;
        // the values alone
        let mut got: Vec<String> = out
            .lines()
            .filter(|l| !l.starts_with("#("))
            .map(|l| {
                let l = l.trim_start();
                let l = l
                    .strip_suffix(");")
                    .or_else(|| l.strip_suffix("))"))
                    .map_or(l.to_string(), |row| format!("{row})"));
                let l = l.as_str();
                match l.strip_prefix("%(").and_then(|l| l.strip_suffix(')')) {
                    Some(fields) => fields
                        .split("; ")
                        .map(|f| f.split_once(" = ").map_or(f, |(_, v)| v))
                        .collect::<Vec<_>>()
                        .join("; "),
                    // a bare value: text prints unquoted
                    None if l.parse::<f64>().is_ok() => l.to_string(),
                    None => format!("{l:?}"),
                }
            })
            .collect();
        want.sort();
        got.sort();
        assert_eq!(got, want, "{sql}\n{err}");
    }
}

/// The planner's exact edges of 0.69, through the tool: a sort by a
/// numeric column, the first rows, a fold by a numeric key and a
/// count of distinct values are pushed, the driver writing the
/// order; over a text column each stays with the engine under its
/// own word, the sort under the line of the ruling's example.
#[test]
fn a_numeric_order_is_pushed_and_a_text_one_is_told() {
    let dir = fixture("order");
    let pushed = |q: &str, sql: &str| {
        let lines = told(&dir, q);
        assert_eq!(
            lines,
            [format!("info PUSHED (query): pushdown: {sql}")],
            "{q}"
        );
        // the plan's answer is the scan's
        let plan = run(&dir, &[q, "lib.db"]).1;
        let scan = run(&dir, &["--no-pushdown", q, "lib.db"]).1;
        assert_eq!(plan, scan, "{q}");
        assert!(!plan.is_empty(), "{q}");
    };
    pushed(
        "/books/* @| sort_by(::pages) @| [..2] | ::title",
        "SELECT title FROM books ORDER BY (pages IS NULL), pages, books.id LIMIT 2",
    );
    pushed(
        "/books/* @| sort_by(::pages; --desc) | ::title",
        "SELECT title FROM books ORDER BY (pages IS NULL), pages DESC, books.id",
    );
    pushed(
        "/books/* @| reverse @| [..3] | ::id",
        "SELECT id FROM books ORDER BY books.id DESC LIMIT 3",
    );
    pushed(
        "/books/* | .a(::author_id) @|{.a} count | .n | %.",
        "SELECT author_id AS a, COUNT(*) AS n FROM books GROUP BY author_id ORDER BY \
         MIN(books.id)",
    );
    pushed(
        "/books/*::author_id @| unique @| count",
        "SELECT COUNT(DISTINCT author_id) + COALESCE(MAX(CASE WHEN author_id IS NULL THEN 1 \
         ELSE 0 END), 0) FROM books",
    );
    // more shapes: pushed, and what the tool prints is what it prints
    // with --no-pushdown, names and order included
    for q in [
        "/books/* @| sort_by(::pages; --desc) @| [..3] | %(::id; ::title; p = ::pages)",
        "/books/*[::pages > 245] @| sort_by(::author_id) @| reverse | %(::title)",
        "/books/* | ::pages | .a(::author_id) @|{.a} sum | .total | %.",
        "/books/* | .a(::author_id) @|{.a} count | .n | [$_ > 1] | %.",
        "/books/* | .a(::author_id) @|{.a} count",
        "/books/*[::pages != 300] @| [..2] | ::title",
        "/books/* @| top(2; ::pages) | %(::title; ::pages)",
        "/books/* @| bottom(3; ::author_id) | ::title",
        "/books/*[::pages > 245] | %(::title)",
        "/books/* @| sort_by(::pages) @| count",
    ] {
        let lines = told(&dir, q);
        assert!(
            lines.len() == 1 && lines[0].starts_with("info PUSHED (query): pushdown: SELECT "),
            "{q}: {lines:?}"
        );
        let plan = run(&dir, &[q, "lib.db"]).1;
        let scan = run(&dir, &["--no-pushdown", q, "lib.db"]).1;
        assert_eq!(plan, scan, "{q}");
        assert!(!plan.is_empty(), "{q}");
    }
    // a text key: each under its own word, the answer the scan's
    assert_eq!(
        told(&dir, "/books/* @| sort_by(::title) | ::title"),
        [
            "info SORT_NOT_PUSHED (query): ORDER BY collations differ per engine; scanning. \
             Narrow first: qua man SORT_NOT_PUSHED"
        ]
    );
    // (a fold by a text key is a plan on SQLite since 0.75, where
    // its keys stay apart: qua/tests/fold_record_pushdown.rs)
    assert_eq!(
        told(&dir, "/books/* | .t(::title) @|{.t} count | .n | %."),
        [
            "info PUSHED (query): pushdown: SELECT title AS t, COUNT(*) AS n FROM books GROUP \
             BY title COLLATE BINARY ORDER BY MIN(books.id)"
                .to_string(),
            keyed_by_text("title"),
        ]
    );
    // (and so is a count of the distinct values of a text the
    // database compares exactly, since the ruling of 2026-11-29: it
    // was told UNIQUE_NOT_PUSHED and scanned)
    assert_eq!(
        told(&dir, "/books/*::title @| unique @| count"),
        [
            "info PUSHED (query): pushdown: SELECT COUNT(DISTINCT title) + \
             COALESCE(MAX(CASE WHEN title IS NULL THEN 1 ELSE 0 END), 0) FROM books"
                .to_string(),
            keyed_by_text("title"),
        ]
    );
    assert_eq!(
        run(&dir, &["/books/*::title @| unique @| count", "lib.db"]).0,
        run(
            &dir,
            &[
                "--no-pushdown",
                "/books/*::title @| unique @| count",
                "lib.db"
            ]
        )
        .0
    );
}

/// The page behind the door, the list, and a code that is none.
#[test]
fn man_answers_a_code() {
    let dir = fixture("man");
    let (ok, out, _) = run(&dir, &["man", "SORT_NOT_PUSHED"]);
    assert!(ok);
    assert!(
        out.starts_with("SORT_NOT_PUSHED — info, planner\n  raised    a sort"),
        "{out}"
    );
    assert!(out.contains("\n  remedy    "), "{out}");
    let (ok, out, _) = run(&dir, &["man", "diagnostics", "planner"]);
    assert!(ok);
    assert!(
        out.lines().count() >= 16 && out.lines().all(|l| l.contains("_NOT_PUSHED")),
        "{out}"
    );
    let (ok, _, err) = run(&dir, &["man", "SORT_NOT_PUSHD"]);
    assert!(!ok);
    assert!(
        err.contains("no diagnostic code 'SORT_NOT_PUSHD'; the nearest: SORT_NOT_PUSHED"),
        "{err}"
    );
}

/// No source of the workspace raises a code written out as a
/// literal: every code is a constant of `quarb::diagnostic`, the
/// list the engine's test holds against the inventory, so a new
/// code cannot be raised without its row. Test code is free to
/// make diagnostics of its own.
#[test]
fn no_code_is_raised_as_a_literal() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
    let raisers = [
        "Diagnostic::error(",
        "Diagnostic::warning(",
        "Diagnostic::info(",
        "Diagnostic::new(",
        "record_info(",
        "record_warning(",
        "record_refusal_coded(",
        "record_debug(",
        "note_info(",
        "note_warning(",
        "note_debug(",
    ];
    let mut found: Vec<String> = Vec::new();
    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        for entry in std::fs::read_dir(&dir).unwrap().flatten() {
            let path = entry.path();
            let name = entry.file_name().to_string_lossy().to_string();
            if path.is_dir() {
                // sources only: no build output, no tests, no vendored trees
                if !matches!(
                    name.as_str(),
                    "target" | "tests" | "node_modules" | ".git" | "benches" | "examples"
                ) {
                    stack.push(path);
                }
                continue;
            }
            if !name.ends_with(".rs") {
                continue;
            }
            let text = std::fs::read_to_string(&path).unwrap();
            // a file's own unit tests stand after `#[cfg(test)]`
            let code = text.split("#[cfg(test)]").next().unwrap();
            let lines: Vec<&str> = code.lines().collect();
            for (i, line) in lines.iter().enumerate() {
                let Some(at) = raisers
                    .iter()
                    .find_map(|r| line.find(r).map(|p| p + r.len()))
                else {
                    continue;
                };
                // the first argument: on the line, or on the next
                // (the severity of `Diagnostic::new` comes first)
                let rest = line[at..].trim();
                let args: Vec<&str> = if rest.is_empty() {
                    lines[i + 1..(i + 3).min(lines.len())]
                        .iter()
                        .map(|l| l.trim())
                        .collect()
                } else {
                    rest.split(',').map(str::trim).collect()
                };
                for arg in args.iter().take(2) {
                    let lit = arg.trim_end_matches(',');
                    if lit.len() > 4
                        && lit.starts_with('"')
                        && lit.ends_with('"')
                        && lit[1..lit.len() - 1]
                            .chars()
                            .all(|c| c.is_ascii_uppercase() || c == '_')
                    {
                        found.push(format!(
                            "{}:{}: {lit}",
                            path.strip_prefix(root).unwrap().display(),
                            i + 1
                        ));
                    }
                }
            }
        }
    }
    assert!(
        found.is_empty(),
        "codes raised as literals (name them in quarb::diagnostic's list, with a row in \
         langspec/diagnostics.tsv):\n{}",
        found.join("\n")
    );
}

/// DuckDB's door asks the catalog (information_schema.columns) for
/// the type of a plan's key column, as PostgreSQL's does: a sort, a
/// fold, a distinct count, `top` and `bottom` by a numeric column
/// are pushed, a text key is told under the construct's own word,
/// and either way the answer is the scan's.
#[test]
fn the_duckdb_door_pushes_a_numeric_key_and_tells_a_text_one() {
    let dir = std::env::temp_dir().join(format!("quarb-codes-{}-duck", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    {
        let db = duckdb::Connection::open(dir.join("lib.duckdb")).unwrap();
        db.execute_batch(
            "CREATE TABLE books (id INTEGER PRIMARY KEY, title VARCHAR, pages INTEGER,
               price DOUBLE);
             INSERT INTO books VALUES
               (1, 'Iliad', 300, 9.5), (2, 'Emma', 320, 7.25), (3, 'Tom Sawyer', 240, NULL),
               (4, 'Persuasion', 250, 7.25), (5, 'Odyssey', NULL, 8.0);",
        )
        .unwrap();
    }
    let told = |q: &str| -> Vec<String> {
        let (ok, _, err) = run(&dir, &["--diagnostics=info", q, "lib.duckdb"]);
        assert!(ok, "{q}: {err}");
        err.lines().map(str::to_string).collect()
    };
    let same = |q: &str| {
        let plan = run(&dir, &[q, "lib.duckdb"]).1;
        let scan = run(&dir, &["--no-pushdown", q, "lib.duckdb"]).1;
        assert_eq!(plan, scan, "{q}");
        assert!(!plan.is_empty(), "{q}");
    };
    for q in [
        "/books/* @| sort_by(::pages) | ::title",
        "/books/* @| sort_by(::price; --desc) @| [..3] | %(::id; ::price)",
        "/books/* @| top(2; ::pages) | ::title",
        "/books/* @| bottom(2; ::price) | %(::title; ::price)",
        "/books/* | .p(::price) @|{.p} count | .n | %.",
        "/books/*::price @| unique @| count",
        "/books/* @| reverse @| [..2] | ::id",
        "/books/*[::pages != 300]::id",
        // the whole row: its NULL cells are left out, as the scan's
        "/books/* | %",
        "/books/* @| sort_by(::price) | %",
    ] {
        let lines = told(q);
        assert!(
            lines.len() == 1 && lines[0].starts_with("info PUSHED (query): pushdown: SELECT "),
            "{q}: {lines:?}"
        );
        same(q);
    }
    for (q, word) in [
        ("/books/* @| sort_by(::title) | ::id", "SORT_NOT_PUSHED"),
        ("/books/* @| top(2; ::title) | ::id", "SORT_NOT_PUSHED"),
        (
            "/books/* | .t(::title) @|{.t} count | .n | %.",
            "FOLD_NOT_PUSHED",
        ),
    ] {
        let lines = told(q);
        assert!(
            lines.len() == 1 && lines[0].starts_with(&format!("info {word} (query): ")),
            "{q}: {lines:?}"
        );
        same(q);
    }
    // a count of the distinct values of a text the database compares
    // exactly is a plan here too, and says what it cannot
    let q = "/books/*::title @| unique @| count";
    let lines = told(q);
    assert!(
        lines.len() == 2
            && lines[0].starts_with("info PUSHED (query): pushdown: SELECT COUNT(DISTINCT title)")
            && lines[1] == keyed_by_text("title"),
        "{q}: {lines:?}"
    );
    same(q);
}

/// A query that ends on row nodes answers their locators, pushed or
/// not (ruling 2026-11-18): a whole plan answers values, and the
/// rows' names are the scan's to write. It was pushed as SELECT *
/// and printed each row as a record. Its condition still goes ahead
/// of the scan, and no refusal is told: nothing was refused.
#[test]
fn a_selection_of_row_nodes_answers_locators_pushed_or_not() {
    let dir = fixture("nodes");
    let (ok, out, err) = run(
        &dir,
        &["--diagnostics=info", "/books/*[::pages > 245]", "lib.db"],
    );
    assert!(ok, "{err}");
    assert_eq!(out, "/books/1\n/books/2\n/books/4");
    assert_eq!(
        err,
        "info PUSHED (prefilter): partial pushdown: WHERE pages > 245 on books; the rest scans \
         the filtered set"
    );
    for q in [
        "/books/*[::pages > 245]",
        "/books/*",
        "/books/*[::author_id = 2 && ::pages != 320]",
        "/authors/* <=>* /books/*[::author_id = $$0::id]",
        "/authors/*[::born > 0] <=> /books/*[::author_id = $$0::id && ::pages > 245]",
    ] {
        let plan = run(&dir, &[q, "lib.db"]).1;
        let scan = run(&dir, &["--no-pushdown", q, "lib.db"]).1;
        assert_eq!(plan, scan, "{q}");
        assert!(plan.lines().all(|l| l.starts_with('/')), "{q}: {plan}");
    }
    // nothing to send ahead and nothing refused: no line at all
    assert_eq!(told(&dir, "/books/*"), Vec::<String>::new());
    // a column or a record after the selection is the plan's, whole
    assert_eq!(
        told(&dir, "/books/*[::pages > 245] | %(::title)"),
        [
            "info PUSHED (query): pushdown: SELECT title FROM books WHERE pages > 245 ORDER BY books.id"
        ]
    );
}

/// The rows themselves, as records: `| %` on a row node is the
/// record of its properties, and that is the plan a bare selection
/// had, SELECT *. A NULL cell is no property of its row: the scan's
/// record leaves it out, and so does the plan's.
#[test]
fn the_whole_row_as_a_record_is_pushed_and_leaves_null_cells_out() {
    let dir = std::env::temp_dir().join(format!("quarb-codes-{}-row", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let db = rusqlite::Connection::open(dir.join("lib.db")).unwrap();
    db.execute_batch(
        "CREATE TABLE authors (id INTEGER PRIMARY KEY, name TEXT, born INTEGER);
         INSERT INTO authors VALUES (1, 'Homer', NULL), (2, 'Austen', 1775), (3, 'Twain', 1835);
         CREATE TABLE loose (t TEXT, n INTEGER);
         INSERT INTO loose VALUES ('a', 1), (NULL, 2), (NULL, NULL), ('', 4);",
    )
    .unwrap();
    drop(db);
    let (ok, out, err) = run(&dir, &["--diagnostics=info", "/authors/* | %", "lib.db"]);
    assert!(ok, "{err}");
    assert_eq!(
        out,
        "%(id = 1; name = \"Homer\")\n%(id = 2; name = \"Austen\"; born = 1775)\n\
         %(id = 3; name = \"Twain\"; born = 1835)"
    );
    assert_eq!(
        err,
        "info PUSHED (query): pushdown: SELECT * FROM authors ORDER BY authors.id"
    );
    for q in [
        "/authors/* | %",
        "/authors/*[::born = null] | %",
        "/authors/*[::born > 1700] | %",
        "/authors/* @| sort_by(::born; --desc) | %",
        "/authors/* @| reverse @| [..2] | %",
        "/authors/* @| top(1; ::born) | %",
        "/loose/* | %",
        "/loose/*[::n != 2] | %",
    ] {
        let lines: Vec<String> = {
            let (ok, _, err) = run(&dir, &["--diagnostics=info", q, "lib.db"]);
            assert!(ok, "{q}: {err}");
            err.lines().map(str::to_string).collect()
        };
        assert!(
            lines.len() == 1 && lines[0].starts_with("info PUSHED (query): pushdown: SELECT * "),
            "{q}: {lines:?}"
        );
        let plan = run(&dir, &[q, "lib.db"]);
        let scan = run(&dir, &["--no-pushdown", q, "lib.db"]);
        assert!(plan.0 && scan.0, "{q}: {}{}", plan.2, scan.2);
        assert_eq!(plan.1, scan.1, "{q}");
        // every printer reads the same records
        for format in ["--json", "--jsonl", "--csv", "--pretty"] {
            let plan = run(&dir, &[format, q, "lib.db"]);
            let scan = run(&dir, &[format, "--no-pushdown", q, "lib.db"]);
            assert!(plan.0 && scan.0, "{format} {q}: {}{}", plan.2, scan.2);
            assert_eq!(plan.1, scan.1, "{format} {q}");
        }
    }
    // after a column the thread holds a value, and over a join the
    // record is the driver's alone: the engine's, and the same answer
    for q in [
        "/authors/*::name | %",
        "/authors/* | % @| count",
        "/authors/* <=>* /loose/*[::n = $$0::id] | %",
    ] {
        let plan = run(&dir, &[q, "lib.db"]);
        let scan = run(&dir, &["--no-pushdown", q, "lib.db"]);
        assert_eq!(plan.1, scan.1, "{q}");
    }
}

/// A mount's `crosslinks=` names references by their labels, and a
/// label no reference carries is refused. A query pushed whole opens
/// no adapter, so the label was not looked at there and a misspelled
/// one passed in silence; it is held against the catalog before the
/// plan answers, and refused as where the scan answers (0.71).
#[test]
fn a_crosslinks_label_is_checked_where_the_plan_is_pushed_whole() {
    let dir = fixture("crosslabel");
    let (ok, out, err) = run(
        &dir,
        &[
            "--diagnostics=info",
            "/books/* @| count",
            "lib.db?crosslinks=author",
        ],
    );
    assert!(ok, "{err}");
    assert_eq!(out, "4");
    assert_eq!(
        err,
        "info PUSHED (query): pushdown: SELECT COUNT(*) FROM books"
    );
    let (ok, out, err) = run(&dir, &["/books/* @| count", "lib.db?crosslinks=writer"]);
    assert!(!ok);
    assert_eq!(out, "");
    assert!(
        err.contains(
            "crosslinks=\"writer\": no reference of this database is labelled \"writer\" (its \
             labels: author)"
        ),
        "{err}"
    );
    // the scan's own refusal, word for word
    let (ok, _, scanned) = run(
        &dir,
        &["/books/1->author::name", "lib.db?crosslinks=writer"],
    );
    assert!(!ok);
    assert_eq!(err, scanned);
}

/// DuckDB's door sends the leading conditions ahead of the scan, as
/// the other doors do (0.71: it opened the file whole), and a regex
/// narrows the fetch by its literals there (`strpos`). Each answer
/// is the scan's.
#[test]
fn the_duckdb_door_sends_leading_conditions_ahead() {
    let dir = std::env::temp_dir().join(format!("quarb-codes-{}-duckpart", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    {
        let db = duckdb::Connection::open(dir.join("lib.duckdb")).unwrap();
        db.execute_batch(
            "CREATE TABLE books (id INTEGER PRIMARY KEY, title VARCHAR, pages INTEGER);
             INSERT INTO books VALUES
               (1, 'Iliad', 300), (2, 'Emma', 320), (3, 'Tom Sawyer', 240),
               (4, 'Persuasion', 250), (5, 'Odyssey', NULL), (6, NULL, 700),
               (7, 'tom thumb', 90), (8, 'Tom Jones', 900);",
        )
        .unwrap();
    }
    let lines = |q: &str| -> Vec<String> {
        let (ok, plan, err) = run(&dir, &["--diagnostics=info", q, "lib.duckdb"]);
        assert!(ok, "{q}: {err}");
        let (ok, scan, _) = run(&dir, &["--no-pushdown", q, "lib.duckdb"]);
        assert!(ok, "{q}");
        assert_eq!(plan, scan, "{q}");
        assert!(!plan.is_empty(), "{q}");
        err.lines().map(str::to_string).collect()
    };
    let sent = |q: &str, filter: &str| {
        let lines = lines(q);
        assert!(
            lines.iter().any(|l| l
                == &format!(
                    "info PUSHED (prefilter): partial pushdown: WHERE {filter} on books; the \
                     rest scans the filtered set"
                )),
            "{q}: {lines:?}"
        );
    };
    // a plain condition before a selection the plan cannot take
    sent("/books/*[::pages > 245] @| [2] | ::title", "pages > 245");
    sent(
        "/books/*[::pages != 300 && ::pages < 800] @| [3] | ::id",
        "((pages <> 300 OR pages IS NULL) AND pages < 800)",
    );
    // a regex: the literals it cannot match without
    sent(
        "/books/*[::title == (/^Tom .*s$/)]::id",
        "strpos(title, 'Tom ') > 0 AND strpos(title, 's') > 0",
    );
    sent(
        "/books/*[::pages > 100 && ::title == (/on/)]::title",
        "(pages > 100 AND strpos(title, 'on') > 0)",
    );
    // not narrowed: no literal, and a number column (the database
    // would search its own rendering of the number)
    for q in [
        "/books/*[::title == (/(?i)^tom/)]::id",
        "/books/*[::pages == (/^3/)]::id",
    ] {
        let lines = lines(q);
        assert!(
            !lines.iter().any(|l| l.contains("partial pushdown")),
            "{q}: {lines:?}"
        );
    }
}