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
//! Error-message parity keeper: frank's error text must match stock SQLite
//! (3.53) VERBATIM for the common error conditions below. A differential probe
//! (frank vs rusqlite) confirmed these match; asserted here against the exact
//! expected string so a regression (e.g. an `Internal("internal error: …")`
//! wrapping, or reworded text) is caught.
//!
//! Scope note (bd-ttof2, FIX-FORWARD): SQLite does NOT append a
//! " in <SQL> at offset N" suffix to any error MESSAGE — the byte offset is
//! exposed only via the separate sqlite3_error_offset() C API. An earlier
//! bd-ttof2 slice misread rusqlite's Display wrapper (which formats the offset
//! into the string on the prepare path) as stock behavior; verified against
//! sqlite3 CLI 3.46.1 and rusqlite 3.53's bare `SqlInputError.msg` that neither
//! the CLI nor the C API carries the suffix. The `_offset`-named tests below
//! therefore assert the BARE stock message and guard against the suffix
//! regressing back in. Regression tracked in
//! bd-ttof2-offset-suffix-regression-npx41.
use fsqlite_core::connection::Connection;
/// Run `setup` then `sql`; assert `sql` fails with EXACTLY `expected` message.
async fn err_is(setup: &[&str], sql: &str, expected: &str) {
let f = Connection::open(":memory:").await.unwrap();
let _ = f.execute("PRAGMA foreign_keys=ON").await;
for s in setup {
let _ = f.execute(s).await;
}
match f.execute(sql).await {
Ok(_) => panic!("expected an error for `{sql}`, but it succeeded"),
Err(e) => assert_eq!(e.to_string(), expected, "sql=`{sql}`"),
}
}
/// Like [`err_is`] but drives `query` (the row-returning path). SELECT analysis
/// errors are asserted through `query`, since `execute` of a SELECT currently
/// discards rows via a fast path that can bypass some prepare-time validation
/// (tracked separately in bd-ttof2).
async fn query_err_is(setup: &[&str], sql: &str, expected: &str) {
let f = Connection::open(":memory:").await.unwrap();
for s in setup {
let _ = f.execute(s).await;
}
match f.query(sql).await {
Ok(_) => panic!("expected an error for `{sql}`, but it succeeded"),
Err(e) => assert_eq!(e.to_string(), expected, "sql=`{sql}`"),
}
}
#[test]
fn constraint_violations() {
asupersync::test_utils::run_test(|| async {
err_is(
&["CREATE TABLE t(x INT UNIQUE)", "INSERT INTO t VALUES (1)"],
"INSERT INTO t VALUES (1)",
"UNIQUE constraint failed: t.x",
)
.await;
err_is(
&["CREATE TABLE t(x INT NOT NULL)"],
"INSERT INTO t VALUES (NULL)",
"NOT NULL constraint failed: t.x",
)
.await;
err_is(
&["CREATE TABLE t(x INT CHECK (x > 0))"],
"INSERT INTO t VALUES (-1)",
"CHECK constraint failed: x > 0",
)
.await;
err_is(
&["CREATE TABLE t(x INT CONSTRAINT pos CHECK (x > 0))"],
"INSERT INTO t VALUES (-1)",
"CHECK constraint failed: pos",
)
.await;
err_is(
&[
"CREATE TABLE p(id INT PRIMARY KEY)",
"CREATE TABLE c(pid INT REFERENCES p(id))",
],
"INSERT INTO c VALUES (99)",
"FOREIGN KEY constraint failed",
)
.await;
});
}
#[test]
fn strict_type_errors() {
asupersync::test_utils::run_test(|| async {
err_is(
&["CREATE TABLE t(x INTEGER) STRICT"],
"INSERT INTO t VALUES ('notanint')",
"cannot store TEXT value in INTEGER column t.x",
)
.await;
});
}
#[test]
fn ddl_and_ordinal_errors() {
asupersync::test_utils::run_test(|| async {
// bd-ttof2 fix: ALTER TABLE ADD a duplicate column now reports verbatim
// (was Internal "internal error: duplicate column name: b").
err_is(
&["CREATE TABLE t(a INT, b INT)"],
"ALTER TABLE t ADD COLUMN b INT",
"duplicate column name: b",
)
.await;
// bd-ttof2 fix: a GROUP BY ordinal out of range now carries the 1-based
// term-position prefix ("1st GROUP BY term ..."), matching ORDER BY.
err_is(
&["CREATE TABLE t(a INT)"],
"SELECT a FROM t GROUP BY 9",
"1st GROUP BY term out of range - should be between 1 and 1",
)
.await;
err_is(
&["CREATE TABLE t(a INT)"],
"SELECT a FROM t ORDER BY 5",
"1st ORDER BY term out of range - should be between 1 and 1",
)
.await;
});
}
#[test]
fn name_resolution_and_txn_errors() {
asupersync::test_utils::run_test(|| async {
err_is(
&[],
"SELECT * FROM nonexistent",
"no such table: nonexistent",
)
.await;
// bd-ttof2 fix: an unknown INSERT column now matches stock verbatim
// (was Internal("internal error: column '…' not found in table '…'")).
err_is(
&["CREATE TABLE t(a INT)"],
"INSERT INTO t(nope) VALUES (1)",
"table t has no column named nope",
)
.await;
err_is(
&[],
"RELEASE nonexistent_sp",
"no such savepoint: nonexistent_sp",
)
.await;
err_is(&[], "COMMIT", "cannot commit - no transaction is active").await;
});
}
#[test]
fn view_modify_errors() {
asupersync::test_utils::run_test(|| async {
// bd-ttof2 fix: DML against a plain view (no INSTEAD OF trigger) reports
// "cannot modify V because it is a view" verbatim, matching stock — not
// the "no such table: V" a table-DML fallthrough raised before, which
// leaked because the :memory: INSERT/UPDATE/DELETE prepared fast-path
// bypassed the view-DML dispatch check. Covers all three DML verbs.
let setup: &[&str] = &["CREATE TABLE t(x INT)", "CREATE VIEW v AS SELECT x FROM t"];
err_is(
setup,
"INSERT INTO v VALUES (1)",
"cannot modify v because it is a view",
)
.await;
err_is(
setup,
"DELETE FROM v",
"cannot modify v because it is a view",
)
.await;
err_is(
setup,
"UPDATE v SET x = 1",
"cannot modify v because it is a view",
)
.await;
});
}
#[test]
fn aggregate_in_where_misuse_offset() {
asupersync::test_utils::run_test(|| async {
// An aggregate in WHERE names the offending aggregate and uses stock's
// two spellings: "misuse of aggregate: NAME()" when the SELECT is itself
// an aggregate query, else "misuse of aggregate function NAME()"; the
// two-aggregate case names the RIGHTMOST. bd-ttof2 FIX-FORWARD: there is
// NO " in <SQL> at offset N" suffix — stock never puts the byte offset in
// the message (verified vs rusqlite 3.53 + sqlite3 CLI 3.46.1).
let t: &[&str] = &["CREATE TABLE t(x INT)"];
query_err_is(
t,
"SELECT max(x) FROM t WHERE max(x) > 0",
"misuse of aggregate: max()",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE sum(x) > 0",
"misuse of aggregate function sum()",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE x > sum(x)",
"misuse of aggregate function sum()",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE sum(x) + count(*) > 0",
"misuse of aggregate function count()",
)
.await;
});
}
#[test]
fn window_misuse_offset() {
asupersync::test_utils::run_test(|| async {
// A window function where one is not allowed (WHERE / GROUP BY / HAVING)
// names the offending call: "misuse of window function NAME()".
// bd-ttof2 FIX-FORWARD: NO " in <SQL> at offset N" suffix (stock never
// puts the byte offset in the message).
let t: &[&str] = &["CREATE TABLE t(x INT, g TEXT)"];
query_err_is(
t,
"SELECT x FROM t WHERE row_number() OVER () = 1",
"misuse of window function row_number()",
)
.await;
query_err_is(
t,
"SELECT g FROM t GROUP BY row_number() OVER ()",
"misuse of window function row_number()",
)
.await;
query_err_is(
t,
"SELECT g FROM t GROUP BY g HAVING row_number() OVER () > 0",
"misuse of window function row_number()",
)
.await;
query_err_is(
t,
"SELECT x FROM t WHERE x > sum(x) OVER ()",
"misuse of window function sum()",
)
.await;
});
}
#[test]
fn nested_aggregate_misuse_offset() {
asupersync::test_utils::run_test(|| async {
// An aggregate nested inside another aggregate names the INNER call:
// "misuse of aggregate function NAME()". bd-ttof2 FIX-FORWARD: NO
// " in <SQL> at offset N" suffix (stock never puts the byte offset in
// the message; verified vs sqlite3 CLI 3.46.1).
let t: &[&str] = &["CREATE TABLE t(x INT, g TEXT)"];
query_err_is(
t,
"SELECT sum(count(*)) FROM t",
"misuse of aggregate function count()",
)
.await;
query_err_is(
t,
"SELECT max(avg(x)) FROM t GROUP BY g",
"misuse of aggregate function avg()",
)
.await;
query_err_is(
t,
"SELECT g FROM t GROUP BY g HAVING sum(count(*)) > 0",
"misuse of aggregate function count()",
)
.await;
// a non-nested aggregate precedes the nested one: still names the inner
query_err_is(
t,
"SELECT avg(x) + sum(count(*)) FROM t",
"misuse of aggregate function count()",
)
.await;
// triple nesting reports the innermost
query_err_is(
t,
"SELECT sum(avg(count(*))) FROM t",
"misuse of aggregate function count()",
)
.await;
});
}
#[test]
fn function_resolution_offset() {
asupersync::test_utils::run_test(|| async {
// Function-resolution and function-modifier errors. bd-ttof2
// FIX-FORWARD: NO " in <SQL> at offset N" suffix — stock reports the
// bare message (verified vs sqlite3 CLI 3.46.1); the byte offset is
// only reachable via a separate sqlite3_error_offset()-style accessor.
let t: &[&str] = &["CREATE TABLE t(a INT, b INT)"];
query_err_is(t, "SELECT nosuchfn(a) FROM t", "no such function: nosuchfn").await;
// nested inner call resolves the same bare message
query_err_is(
t,
"SELECT abs(nosuchfn(a)) FROM t",
"no such function: nosuchfn",
)
.await;
query_err_is(
&[],
"SELECT abs(1, 2)",
"wrong number of arguments to function abs()",
)
.await;
query_err_is(
t,
"SELECT a + abs(1, 2) FROM t",
"wrong number of arguments to function abs()",
)
.await;
query_err_is(
t,
"SELECT abs(a) FILTER (WHERE a > 0) FROM t",
"FILTER may not be used with non-aggregate abs()",
)
.await;
query_err_is(
t,
"SELECT abs(a ORDER BY a) FROM t",
"ORDER BY may not be used with non-aggregate abs()",
)
.await;
// DISTINCT-on-window: base message only (no offset), matching stock.
query_err_is(
t,
"SELECT count(DISTINCT a) OVER () FROM t",
"DISTINCT is not supported for window functions",
)
.await;
});
}
#[test]
fn offset_suffix_on_parameterized_entrypoint() {
asupersync::test_utils::run_test(|| async {
// The parameterized entry points (query_with_params/execute_with_params)
// surface the same bare stock error message as query/execute. A
// function-resolution error raised through query_with_params reports the
// stock message with NO offset suffix (bd-ttof2 FIX-FORWARD). (The
// dispatch-level *misuse* validation is skipped on the parameterized fast
// path — a separate pre-existing gap tracked in bd-ttof2 — so this
// asserts a function-resolution error, which does fire.)
let f = Connection::open(":memory:").await.unwrap();
let _ = f.execute("CREATE TABLE t(x INT)").await;
match f.query_with_params("SELECT nosuchfn(x) FROM t", &[]).await {
Ok(_) => panic!("expected an error"),
Err(e) => assert_eq!(e.to_string(), "no such function: nosuchfn"),
}
});
}
#[test]
fn distinct_aggregate_arg_count_precedence() {
asupersync::test_utils::run_test(|| async {
// For a DISTINCT aggregate with more than one argument, stock reports the
// DISTINCT-single-argument error ONLY when the function legitimately
// accepts that arity (group_concat takes 1 or 2). When the arity is
// invalid for the function itself (count/sum/avg take 1), the arg-count
// error takes precedence. bd-errmsg-parity-batch4-deqcb.
let t: &[&str] = &["CREATE TABLE t(a INT, b INT)"];
query_err_is(
t,
"SELECT count(DISTINCT a, b) FROM t",
"wrong number of arguments to function count()",
)
.await;
query_err_is(
t,
"SELECT sum(DISTINCT a, b) FROM t",
"wrong number of arguments to function sum()",
)
.await;
query_err_is(
t,
"SELECT avg(DISTINCT a, b) FROM t",
"wrong number of arguments to function avg()",
)
.await;
// group_concat legitimately takes 2 args, so the arg count is valid and
// the DISTINCT-single-argument rule is what fires.
query_err_is(
t,
"SELECT group_concat(DISTINCT a, b) FROM t",
"DISTINCT aggregates must have exactly one argument",
)
.await;
// control: a valid single-argument DISTINCT aggregate is accepted.
let f = Connection::open(":memory:").await.unwrap();
let _ = f.execute("CREATE TABLE t(a INT, b INT)").await;
assert!(
f.query("SELECT count(DISTINCT a) FROM t").await.is_ok(),
"count(DISTINCT a) should be accepted"
);
});
}
#[test]
fn multicolumn_subquery_comparison_operand_row_value_misused() {
asupersync::test_utils::run_test(|| async {
// A multi-column subquery as the IMMEDIATE operand of a comparison operator
// (=, <>, <, <=, >, >=, IS, IS NOT) against a scalar is "row value misused",
// NOT the generic "sub-select returns N columns - expected 1". Verified vs
// sqlite3 CLI 3.46.1 (bd-errmsg-parity-batch4-deqcb).
let t: &[&str] = &["CREATE TABLE t(a INT)"];
query_err_is(
t,
"SELECT * FROM t WHERE a = (SELECT 1, 2)",
"row value misused",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE a < (SELECT 1, 2)",
"row value misused",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE a IS (SELECT 1, 2)",
"row value misused",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE a = (SELECT 1, 2, 3)",
"row value misused",
)
.await;
// symmetric: subquery on the left
query_err_is(
t,
"SELECT * FROM t WHERE (SELECT 1, 2) = a",
"row value misused",
)
.await;
// NOTE: stock reports "row value misused" even before resolving a bad
// column INSIDE the subquery (`a = (SELECT nope, 2)` -> "row value misused",
// not "no such column: nope"). frank resolves subquery column names in an
// earlier pass, so for that doubly-invalid shape it reports the name error
// first — a minor, defensible ordering divergence, not worth reordering the
// name-resolution pass; both are errors and every clean case here matches.
// still fires on the `=` operand when nested inside a larger boolean
query_err_is(
t,
"SELECT * FROM t WHERE a = (SELECT 1, 2) AND a > 0",
"row value misused",
)
.await;
// NEGATIVE: a multi-column subquery under a NON-comparison operator keeps
// the generic arity error (the subquery is an arithmetic/list/bare operand,
// not an immediate comparison operand).
query_err_is(
t,
"SELECT * FROM t WHERE a = (1 + (SELECT 1, 2))",
"sub-select returns 2 columns - expected 1",
)
.await;
query_err_is(
&[],
"SELECT (SELECT 1, 2)",
"sub-select returns 2 columns - expected 1",
)
.await;
query_err_is(
t,
"SELECT * FROM t WHERE (SELECT 1, 2)",
"sub-select returns 2 columns - expected 1",
)
.await;
// NEGATIVE: a single-column subquery comparison is valid.
let f = Connection::open(":memory:").await.unwrap();
f.execute("CREATE TABLE t(a INT)").await.unwrap();
f.execute("INSERT INTO t VALUES (1)").await.unwrap();
assert!(
f.query("SELECT * FROM t WHERE a = (SELECT 1)")
.await
.is_ok(),
"single-column subquery comparison is valid"
);
});
}
#[test]
fn bare_window_function_without_over_is_misuse() {
asupersync::test_utils::run_test(|| async {
// A window-only function (row_number, rank, lag, ...) called WITHOUT OVER
// is "misuse of window function NAME()", NOT "no such function: NAME".
// Verified vs sqlite3 CLI 3.46.1 (bd-errmsg-parity-batch3-3brmm).
let t: &[&str] = &["CREATE TABLE t(x INT)"];
query_err_is(
t,
"SELECT row_number() FROM t",
"misuse of window function row_number()",
)
.await;
query_err_is(
t,
"SELECT rank() FROM t",
"misuse of window function rank()",
)
.await;
query_err_is(
t,
"SELECT dense_rank() FROM t",
"misuse of window function dense_rank()",
)
.await;
query_err_is(t, "SELECT lag(x) FROM t", "misuse of window function lag()").await;
query_err_is(
t,
"SELECT ntile(2) FROM t",
"misuse of window function ntile()",
)
.await;
query_err_is(
t,
"SELECT cume_dist() FROM t",
"misuse of window function cume_dist()",
)
.await;
// NEGATIVE: an unknown (non-window) function still reports "no such function".
query_err_is(t, "SELECT nosuchfn() FROM t", "no such function: nosuchfn").await;
// NEGATIVE: an aggregate/window dual (sum) without OVER is a valid
// aggregate; and a window function WITH OVER is valid.
let f = Connection::open(":memory:").await.unwrap();
f.execute("CREATE TABLE t(x INT)").await.unwrap();
assert!(
f.query("SELECT sum(x) FROM t").await.is_ok(),
"sum without OVER is a valid aggregate"
);
assert!(
f.query("SELECT row_number() OVER () FROM t").await.is_ok(),
"row_number() OVER () is valid"
);
});
}
#[test]
fn insert_select_column_count_mismatch() {
asupersync::test_utils::run_test(|| async {
// INSERT ... SELECT whose (star-expanded) width != the target column count
// is rejected, matching the VALUES-path counts and messages exactly.
// Verified vs sqlite3 CLI 3.46.1 (bd-errmsg-parity-batch3-3brmm).
// no target list -> "table T has N columns but M values were supplied"
err_is(
&["CREATE TABLE s(x)", "CREATE TABLE t(a, b)"],
"INSERT INTO t SELECT * FROM s",
"table t has 2 columns but 1 values were supplied",
)
.await;
err_is(
&["CREATE TABLE s(x, y, z)", "CREATE TABLE t(a, b)"],
"INSERT INTO t SELECT * FROM s",
"table t has 2 columns but 3 values were supplied",
)
.await;
// explicit target list -> "M values for N columns"
err_is(
&["CREATE TABLE s(x)", "CREATE TABLE t(a, b, c)"],
"INSERT INTO t(a, b) SELECT * FROM s",
"1 values for 2 columns",
)
.await;
// NEGATIVE: matching widths are accepted (no false reject) — both the
// star-expanded and the explicit-projection forms.
let f = Connection::open(":memory:").await.unwrap();
for s in ["CREATE TABLE s2(x, y)", "CREATE TABLE t2(a, b)"] {
f.execute(s).await.unwrap();
}
f.execute("INSERT INTO t2 SELECT * FROM s2")
.await
.expect("matching-width INSERT..SELECT (* -> 2 cols) is valid");
f.execute("INSERT INTO t2 SELECT x, y FROM s2")
.await
.expect("matching-width explicit-projection INSERT..SELECT is valid");
});
}
#[test]
fn returning_aggregate_or_window_misuse() {
asupersync::test_utils::run_test(|| async {
// A bare aggregate/window function in a RETURNING projection is a misuse —
// RETURNING is a per-row projection, not an aggregate context. Verified vs
// sqlite3 CLI 3.46.1 (bd-errmsg-parity-batch3-3brmm).
err_is(
&["CREATE TABLE t(a)"],
"INSERT INTO t VALUES (1) RETURNING count(*)",
"misuse of aggregate function count()",
)
.await;
err_is(
&["CREATE TABLE t(a)", "INSERT INTO t VALUES(1)"],
"UPDATE t SET a = 2 RETURNING sum(a)",
"misuse of aggregate function sum()",
)
.await;
err_is(
&["CREATE TABLE t(a)", "INSERT INTO t VALUES(1)"],
"DELETE FROM t RETURNING count(*)",
"misuse of aggregate function count()",
)
.await;
// a window function in RETURNING is likewise a misuse
err_is(
&["CREATE TABLE t(a)"],
"INSERT INTO t VALUES (1) RETURNING row_number() OVER ()",
"misuse of window function row_number()",
)
.await;
// NEGATIVE: a plain projection, and an aggregate INSIDE a subquery, are
// both legal (the subquery has its own aggregate context).
let f = Connection::open(":memory:").await.unwrap();
f.execute("CREATE TABLE t(a)").await.unwrap();
assert!(
f.query("INSERT INTO t VALUES (5) RETURNING a + 1")
.await
.is_ok(),
"plain RETURNING expression is valid"
);
assert!(
f.query("INSERT INTO t VALUES (6) RETURNING (SELECT count(*) FROM t)")
.await
.is_ok(),
"aggregate inside a subquery in RETURNING is legal"
);
});
}
#[test]
fn cte_self_reference_no_anchor_is_circular() {
asupersync::test_utils::run_test(|| async {
// A CTE whose anchor (first/main SELECT core) references itself has no base
// case: SQLite reports "circular reference: NAME", not "no such table". This
// holds for WITH RECURSIVE and plain WITH, and even when a compound (UNION)
// arm is present but the FIRST term is the self-reference. bd-errmsg batch3.
query_err_is(
&[],
"WITH RECURSIVE c AS (SELECT * FROM c) SELECT * FROM c",
"circular reference: c",
)
.await;
query_err_is(
&[],
"WITH c AS (SELECT * FROM c) SELECT * FROM c",
"circular reference: c",
)
.await;
query_err_is(
&[],
"WITH RECURSIVE c AS (SELECT * FROM c UNION SELECT 1) SELECT * FROM c",
"circular reference: c",
)
.await;
// NEGATIVE: a valid recursive CTE (anchor does NOT self-reference; the
// recursive term is a later UNION arm) and a plain CTE both still work.
let f = Connection::open(":memory:").await.unwrap();
assert!(
f.query("WITH RECURSIVE c(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM c WHERE n<3) SELECT n FROM c")
.await
.is_ok(),
"valid recursive CTE (anchor is a base case) is accepted"
);
assert!(
f.query("WITH c AS (SELECT 1) SELECT * FROM c")
.await
.is_ok(),
"plain non-self-referencing CTE is accepted"
);
});
}
#[test]
fn parameterized_query_rejects_aggregate_misuse() {
asupersync::test_utils::run_test(|| async {
// EVERY prepared/VDBE fast-lane entry point must run the same
// aggregate/window misuse validation as the interpreted query()/execute()
// paths, so an aggregate-in-WHERE is rejected rather than silently
// accepted. The fast lane is taken by the parameterized and single-row
// entry points (query_with_params / query_with_params_for_each /
// query_row / query_row_with_params / execute_with_params); the plain
// query() path is fully interpreted and already rejects it. bd-w39k0.
const MISUSE: &str = "SELECT max(x) FROM t WHERE max(x) > 0";
const EXPECTED: &str = "misuse of aggregate: max()";
let f = Connection::open(":memory:").await.unwrap();
f.execute("CREATE TABLE t(x INT)").await.unwrap();
f.execute("INSERT INTO t VALUES (1)").await.unwrap();
// query_with_params fast lane.
match f.query_with_params(MISUSE, &[]).await {
Ok(_) => panic!("expected a misuse error via query_with_params"),
Err(e) => assert_eq!(e.to_string(), EXPECTED),
}
// query_with_params_for_each fast lane.
match f
.query_with_params_for_each(MISUSE, &[], |_row| Ok(()))
.await
{
Ok(()) => panic!("expected a misuse error via query_with_params_for_each"),
Err(e) => assert_eq!(e.to_string(), EXPECTED),
}
// query_row fast lane.
match f.query_row(MISUSE).await {
Ok(_) => panic!("expected a misuse error via query_row"),
Err(e) => assert_eq!(e.to_string(), EXPECTED),
}
// query_row_with_params fast lane.
match f.query_row_with_params(MISUSE, &[]).await {
Ok(_) => panic!("expected a misuse error via query_row_with_params"),
Err(e) => assert_eq!(e.to_string(), EXPECTED),
}
// execute_with_params fast lane (the row-discarding parameterized path).
match f.execute_with_params(MISUSE, &[]).await {
Ok(_) => panic!("expected a misuse error via execute_with_params"),
Err(e) => assert_eq!(e.to_string(), EXPECTED),
}
// controls: valid queries on the same entry points still work.
assert!(
f.query_with_params("SELECT x FROM t WHERE x > 0", &[])
.await
.is_ok(),
"a valid query_with_params is accepted"
);
assert!(
f.query_row_with_params("SELECT x FROM t WHERE x > 0", &[])
.await
.is_ok(),
"a valid query_row_with_params is accepted"
);
assert!(
f.execute_with_params("SELECT x FROM t WHERE x > 0", &[])
.await
.is_ok(),
"a valid execute_with_params is accepted"
);
});
}
#[test]
fn tokenizer_error_surfaces_verbatim_end_to_end() {
asupersync::test_utils::run_test(|| async {
// A tokenizer (lexer) error reaches the user as SQLite's bare
// "unrecognized token: \"X\"" — with NO "SQL error at offset N:" prefix and
// NO "unexpected token in expression: Error(..)" Debug-wrap. This is the
// end-to-end (Connection::query) contract, distinct from the TokenKind-level
// lexer tests. bd-parser-syntax-error-format-6w6kp (Part A).
for (sql, expected) in [
("SELECT 0x", "unrecognized token: \"0x\""),
("SELECT 0xGG", "unrecognized token: \"0xGG\""),
("SELECT x'123'", "unrecognized token: \"x'123'\""),
("SELECT x'1G'", "unrecognized token: \"x'1G'\""),
("SELECT 'abc", "unrecognized token: \"'abc\""),
("SELECT [abc", "unrecognized token: \"[abc\""),
// A byte that cannot start any token, and a bare parameter prefix with
// no name, both surface as stock's `unrecognized token: "<text>"`
// rather than a frank-specific "unexpected character" / "empty
// parameter name". bd-errmsg-parity-batch3-3brmm.
("SELECT #", "unrecognized token: \"#\""),
("SELECT ^", "unrecognized token: \"^\""),
("SELECT :", "unrecognized token: \":\""),
("SELECT @", "unrecognized token: \"@\""),
("SELECT $", "unrecognized token: \"$\""),
] {
query_err_is(&[], sql, expected).await;
}
});
}