hawdb 0.41.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

use super::*;

fn seed(database: &mut Database, index: Option<&str>) {
    database.query_sql("CREATE TABLE ranges (id BIGINT PRIMARY KEY, tenant TEXT NOT NULL, score BIGINT, body TEXT NOT NULL)").unwrap();
    let rows = (0..180)
        .map(|id| {
            let score = if id % 19 == 0 {
                "NULL".into()
            } else {
                (id / 3).to_string()
            };
            format!(
                "({id}, '{}', {score}, '{}')",
                if id < 120 { "a" } else { "b" },
                if id % 2 == 0 { "even" } else { "odd" }
            )
        })
        .collect::<Vec<_>>()
        .join(",");
    database
        .query_sql(&format!(
            "INSERT INTO ranges (id, tenant, score, body) VALUES {rows}"
        ))
        .unwrap();
    if let Some(columns) = index {
        database
            .query_sql(&format!("CREATE INDEX ranges_order ON ranges ({columns})"))
            .unwrap();
    }
}

fn assert_same(database: &mut Database, reference: &mut Database, sql: &str, params: &[Value]) {
    let actual = database.query_sql_with_params(sql, params).unwrap();
    let expected = reference.query_sql_with_params(sql, params).unwrap();
    assert_eq!(actual.schema(), expected.schema(), "{sql}");
    assert_eq!(actual.rows, expected.rows, "{sql}, {params:?}");
}

fn assert_ordered_range(database: &mut Database, sql: &str, params: &[Value], column: &str) {
    let plan = database
        .query_sql_with_params(&format!("EXPLAIN {sql}"), params)
        .unwrap();
    assert!(plan.rows.iter().any(|row| matches!(row.get("operator info"), Some(Value::String(info)) if info.contains(&format!("range_column={column}")))), "{plan:?}");
    assert!(plan.rows.iter().any(|row| matches!(row.get("id"), Some(Value::String(id)) if id.contains("IndexRangeScanExec"))), "{plan:?}");
    assert!(plan.rows.iter().all(|row| !matches!(row.get("id"), Some(Value::String(id)) if id.contains("TopNExec") || id.contains("SortExec"))), "{plan:?}");
}

#[test]
fn sql_composite_range_bounds_preserve_nulls_duplicates_and_parameter_rebinding() {
    let mut db = Database::new();
    let mut reference = Database::new();
    seed(&mut db, Some("tenant, score, id"));
    seed(&mut reference, None);
    for lower in [">", ">="] {
        for upper in ["<", "<="] {
            for direction in ["ASC", "DESC"] {
                let sql = format!("SELECT id, score, body FROM ranges AS r WHERE r.tenant = 'a' AND r.score {lower} $1 AND r.score {upper} $2 ORDER BY score {direction}, id {direction} OFFSET 1 LIMIT 5");
                for params in [
                    vec![Value::Int(9), Value::Int(14)],
                    vec![Value::Int(14), Value::Int(14)],
                    vec![Value::Int(18), Value::Int(9)],
                    vec![Value::Null, Value::Int(10)],
                ] {
                    assert_same(&mut db, &mut reference, &sql, &params);
                    assert_ordered_range(&mut db, &sql, &params, "score");
                }
            }
        }
    }
    for predicate in [
        "tenant = 'a' AND score <= 3",
        "tenant = 'a' AND score >= 30",
        "tenant = 'a' AND score > 9 AND score >= 9 AND score < 12 AND score <= 15",
        "tenant = 'a' AND score >= 9 AND score > 9 AND score <= 15 AND score < 12",
    ] {
        for direction in ["ASC", "DESC"] {
            let sql = format!("SELECT id, score FROM ranges WHERE {predicate} ORDER BY score {direction}, id {direction} LIMIT 7");
            assert_same(&mut db, &mut reference, &sql, &[]);
            assert_ordered_range(&mut db, &sql, &[], "score");
        }
    }
    // A keyset continuation intersects the domain bounds before LIMIT; neither
    // the OR cursor nor the additional interval may become an unhandled filter.
    for (op, direction) in [(">", "ASC"), ("<", "DESC")] {
        let sql = format!("SELECT id, score FROM ranges WHERE tenant = 'a' AND score >= $1 AND score <= $2 AND (score {op} $3 OR (score = $3 AND id {op} $4)) ORDER BY score {direction}, id {direction} LIMIT 5");
        for params in [
            vec![
                Value::Int(9),
                Value::Int(15),
                Value::Int(11),
                Value::Int(34),
            ],
            vec![
                Value::Int(12),
                Value::Int(15),
                Value::Int(30),
                Value::Int(90),
            ],
        ] {
            assert_same(&mut db, &mut reference, &sql, &params);
            assert_ordered_range(&mut db, &sql, &params, "score");
        }
    }
    // Uncovered conditions must still be evaluated, even after an interval was
    // derived. They cannot authorize the predicate-free ordered projection.
    for predicate in [
        "tenant = 'a' AND score > 9 AND score < 16 AND body = 'odd'",
        "tenant = 'a' AND tenant = 'b' AND score > 9",
        "tenant = 'a' AND score > 9 AND (score < 12 OR body = 'odd')",
        "tenant = 'a' AND score > 9 AND score < 16 AND (score > 10 OR (score = 10 AND id > 30)) AND body = 'odd'",
    ] {
        let sql = format!("SELECT id FROM ranges WHERE {predicate} ORDER BY score, id LIMIT 5");
        assert_same(&mut db, &mut reference, &sql, &[]);
    }
    for sql in [
        "SELECT COUNT(*) AS n FROM ranges WHERE tenant = 'a' AND score > 9 AND score < 14",
        "SELECT COUNT(*) AS n FROM ranges WHERE tenant = 'a' AND score > NULL",
    ] {
        assert_same(&mut db, &mut reference, sql, &[]);
    }
}

#[test]
fn sql_index_range_on_first_column_needs_no_equality_prefix() {
    let mut db = Database::new();
    let mut reference = Database::new();
    seed(&mut db, Some("score, id"));
    seed(&mut reference, None);
    for predicate in [
        "score > 10",
        "score >= 10 AND score <= 14",
        "score < 4",
        "score <= 0",
        "score > NULL",
    ] {
        for direction in ["ASC", "DESC"] {
            let sql = format!("SELECT id, score FROM ranges WHERE {predicate} ORDER BY score {direction}, id {direction} LIMIT 5");
            assert_same(&mut db, &mut reference, &sql, &[]);
            assert_ordered_range(&mut db, &sql, &[], "score");
        }
    }
}

#[test]
fn sql_composite_ranges_preserve_checkpoint_live_transaction_and_recovery_views() {
    use hawdb_storage::config::RelationalIndexMode;
    let sql = "SELECT id, score FROM ranges WHERE tenant = 'a' AND score > 9 AND score <= 15 ORDER BY score DESC, id DESC LIMIT 20";
    for mode in [
        RelationalIndexMode::Shadow,
        RelationalIndexMode::DemandPaged,
        RelationalIndexMode::Authoritative,
    ] {
        let path = unique_test_dir("sql-composite-range");
        let config = DatabaseConfig {
            relational_index_mode: mode,
            ..Default::default()
        };
        let mut reference = Database::new();
        seed(&mut reference, None);
        let bootstrap = DatabaseConfig {
            relational_index_mode: if mode == RelationalIndexMode::Authoritative {
                RelationalIndexMode::DemandPaged
            } else {
                mode
            },
            ..config.clone()
        };
        let mut db = Database::open_with_config(&path, bootstrap).unwrap();
        seed(&mut db, Some("tenant, score, id"));
        db.checkpoint().unwrap();
        drop(db);
        let mut db = Database::open_with_config(&path, config.clone()).unwrap();
        assert_same(&mut db, &mut reference, sql, &[]);
        assert_ordered_range(&mut db, sql, &[], "score");
        let pinned = db.begin_read_transaction().unwrap();
        let before = pinned.query_sql(sql).unwrap();
        for mutation in [
            "UPDATE ranges SET score = 15 WHERE id = 3",
            "UPDATE ranges SET score = 40 WHERE id = 31",
            "DELETE FROM ranges WHERE id = 32",
            "INSERT INTO ranges (id, tenant, score, body) VALUES (1000, 'a', 12, 'live')",
        ] {
            db.query_sql(mutation).unwrap();
            reference.query_sql(mutation).unwrap();
        }
        assert_same(&mut db, &mut reference, sql, &[]);
        assert_eq!(pinned.query_sql(sql).unwrap().rows, before.rows);
        let mut transaction = db.begin_transaction().unwrap();
        transaction
            .query_sql(
                "INSERT INTO ranges (id, tenant, score, body) VALUES (1001, 'a', 13, 'private')",
            )
            .unwrap();
        let rows = transaction.query_sql(sql).unwrap();
        assert!(rows
            .rows
            .iter()
            .any(|row| row.get("id") == Some(&Value::Int(1001))));
        transaction.rollback();
        assert_same(&mut db, &mut reference, sql, &[]);
        drop(pinned);
        drop(db);
        let mut db = Database::open_with_config(&path, config).unwrap();
        assert_same(&mut db, &mut reference, sql, &[]);
        db.checkpoint().unwrap();
        assert_same(&mut db, &mut reference, sql, &[]);
        drop(db);
        std::fs::remove_dir_all(path).unwrap();
    }
}

#[test]
fn sql_unique_composite_range_requires_complete_nullable_suffix_coverage() {
    let mut reference = Database::new();
    seed(&mut reference, None);
    let mut db = Database::new();
    seed(&mut db, None);
    db.query_sql("CREATE UNIQUE INDEX ranges_order ON ranges (tenant, score, id)")
        .unwrap();
    let sql = "SELECT id, score FROM ranges WHERE tenant = 'a' AND score <= 4 ORDER BY score DESC, id DESC LIMIT 5";
    assert_same(&mut db, &mut reference, sql, &[]);
    assert_ordered_range(&mut db, sql, &[], "score");
    let mut sparse = Database::new();
    seed(&mut sparse, None);
    sparse
        .query_sql("ALTER TABLE ranges ADD COLUMN extra TEXT")
        .unwrap();
    sparse
        .query_sql("CREATE UNIQUE INDEX ranges_sparse ON ranges (tenant, score, extra)")
        .unwrap();
    assert_same(&mut sparse, &mut reference, sql, &[]);
    let plan = sparse.query_sql(&format!("EXPLAIN {sql}")).unwrap();
    assert!(plan.rows.iter().all(|row| !matches!(row.get("access object"), Some(Value::String(access)) if access.contains("ranges_sparse"))));
}

#[test]
fn sql_composite_range_seeks_a_late_page_under_a_four_row_read_budget() {
    use hawdb_storage::config::RelationalIndexMode;
    let path = unique_test_dir("sql-range-budget");
    let config = DatabaseConfig {
        max_read_result_rows: Some(4),
        relational_index_mode: RelationalIndexMode::DemandPaged,
        ..Default::default()
    };
    let mut db = Database::open_with_config(&path, config).unwrap();
    db.query_sql("CREATE TABLE budget_ranges (id BIGINT PRIMARY KEY, tenant TEXT NOT NULL, score BIGINT NOT NULL)").unwrap();
    db.query_sql("CREATE INDEX budget_ranges_order ON budget_ranges (tenant, score, id)")
        .unwrap();
    for start in (0..5_000).step_by(100) {
        let rows = (start..start + 100)
            .map(|id| format!("({id}, 'a', {id})"))
            .collect::<Vec<_>>()
            .join(",");
        db.query_sql(&format!(
            "INSERT INTO budget_ranges (id, tenant, score) VALUES {rows}"
        ))
        .unwrap();
    }
    db.checkpoint().unwrap();
    let sql = "SELECT id FROM budget_ranges WHERE tenant = 'a' AND score >= 4900 AND score < 4950 ORDER BY score DESC, id DESC LIMIT 3";
    assert_ordered_range(&mut db, sql, &[], "score");
    let rows = db.query_sql(sql).unwrap();
    assert_eq!(
        rows.rows
            .iter()
            .map(|row| row["id"].clone())
            .collect::<Vec<_>>(),
        [Value::Int(4949), Value::Int(4948), Value::Int(4947)]
    );
    let profile = db.query_sql(&format!("EXPLAIN ANALYZE {sql}")).unwrap();
    assert!(profile.rows.iter().any(|row| matches!(row.get("operator info"), Some(Value::String(info)) if info.contains("runtime_path=demand_paged") && info.contains("index_rows=3,") && info.contains("early_stop_lookups=1"))), "{profile:?}");
    let error = db.query_sql("SELECT id FROM budget_ranges WHERE tenant = 'a' AND score >= 4900 AND score < 4950 ORDER BY score DESC, id DESC").unwrap_err();
    assert!(
        matches!(
            error,
            HawDBError::ReadBudgetExceeded(_) | HawDBError::Execution(_)
        ),
        "{error}"
    );
    drop(db);
    std::fs::remove_dir_all(path).unwrap();
}

fn query_ids(database: &mut Database, sql: &str, parameters: &[Value]) -> Vec<Value> {
    database
        .query_sql_with_params(sql, parameters)
        .unwrap()
        .rows
        .into_iter()
        .map(|row| row["id"].clone())
        .collect()
}

#[test]
fn sql_row_comparisons_preserve_postgresql_null_and_lexicographic_semantics() {
    let mut db = Database::new();
    db.query_sql(
        "CREATE TABLE row_cmp (id BIGINT PRIMARY KEY, a BIGINT, b BIGINT, body TEXT NOT NULL)",
    )
    .unwrap();
    db.query_sql("INSERT INTO row_cmp (id, a, b, body) VALUES (1, 1, 1, 'one'), (2, 1, 2, 'two'), (3, 2, 0, 'three'), (4, NULL, 0, 'null-a'), (5, 1, NULL, 'null-b')")
        .unwrap();

    for (predicate, expected) in [
        ("(a, b) > (1, 1)", vec![2, 3]),
        ("(a, b) >= (1, 1)", vec![1, 2, 3]),
        ("(a, b) < (1, 1)", vec![]),
        ("(a, b) <= (1, 1)", vec![1]),
        ("(a, b) = (1, 1)", vec![1]),
        ("(a, b) <> (1, 1)", vec![2, 3, 4]),
        ("ROW(a, b) <= ROW(1, 2)", vec![1, 2]),
        ("(a + 1, b) > (2, 1)", vec![2, 3]),
        ("(1, 1) < (a, b)", vec![2, 3]),
        // The first field decides these rows, so a later NULL is not read.
        ("(a, b) < (2, NULL)", vec![1, 2, 5]),
        // The first fields tie, so the NULL second field makes the result unknown.
        ("(a, b) < (1, NULL)", vec![]),
    ] {
        let sql = format!("SELECT id FROM row_cmp WHERE {predicate} ORDER BY id");
        assert_eq!(
            query_ids(&mut db, &sql, &[]),
            expected.into_iter().map(Value::Int).collect::<Vec<_>>(),
            "{predicate}"
        );
    }
    assert_eq!(
        query_ids(
            &mut db,
            "SELECT id FROM row_cmp WHERE (a, b) > ($1, $2) ORDER BY id",
            &[Value::Int(1), Value::Int(1)],
        ),
        vec![Value::Int(2), Value::Int(3)]
    );
    assert!(query_ids(
        &mut db,
        "SELECT id FROM row_cmp WHERE (a, b) > ($1, $2) ORDER BY id",
        &[Value::Null, Value::Int(0)],
    )
    .is_empty());
    let type_error = db
        .query_sql("SELECT id FROM row_cmp WHERE (a, b) > (1, 'not-a-number')")
        .unwrap_err();
    assert!(
        matches!(
            type_error,
            HawDBError::Semantic(_) | HawDBError::Execution(_)
        ),
        "{type_error}"
    );

    let joined = db
        .query_sql("SELECT r.id FROM row_cmp AS l JOIN row_cmp AS r ON (l.a, l.b) = (r.a, r.b) WHERE l.id = 2 ORDER BY r.id")
        .unwrap();
    assert_eq!(joined.rows.len(), 1);
    assert_eq!(joined.rows[0]["id"], Value::Int(2));

    let grouped = db
        .query_sql("SELECT a, COUNT(*) AS n FROM row_cmp GROUP BY a HAVING (a, COUNT(*)) = (1, 3)")
        .unwrap();
    assert_eq!(grouped.rows.len(), 1);
    assert_eq!(grouped.rows[0]["a"], Value::Int(1));
    assert_eq!(grouped.rows[0]["n"], Value::Int(3));

    db.query_sql("UPDATE row_cmp SET body = 'hit' WHERE (a, b) >= (2, 0)")
        .unwrap();
    assert_eq!(
        query_ids(
            &mut db,
            "SELECT id FROM row_cmp WHERE body = 'hit' ORDER BY id",
            &[]
        ),
        vec![Value::Int(3)]
    );
    db.query_sql("DELETE FROM row_cmp WHERE (a, b) <> (1, 1)")
        .unwrap();
    assert_eq!(
        query_ids(&mut db, "SELECT id FROM row_cmp ORDER BY id", &[]),
        vec![Value::Int(1), Value::Int(5)]
    );
}

#[test]
fn row_comparison_mutations_evaluate_fields_left_to_right() {
    let path = unique_test_dir("row-comparison-lazy-mutations");
    let mut db = Database::open(&path).unwrap();
    db.query_sql(
        "CREATE TABLE row_cmp_lazy (\
           id BIGINT PRIMARY KEY, a BIGINT, divisor BIGINT NOT NULL, body TEXT NOT NULL)",
    )
    .unwrap();
    let predicate = "(a, 10 / divisor) > (1, 0)";

    db.query_sql("INSERT INTO row_cmp_lazy (id, a, divisor, body) VALUES (1, 2, 0, 'decided')")
        .unwrap();
    assert_eq!(
        query_ids(
            &mut db,
            &format!("SELECT id FROM row_cmp_lazy WHERE {predicate}"),
            &[],
        ),
        vec![Value::Int(1)]
    );
    db.query_sql(&format!(
        "UPDATE row_cmp_lazy SET body = 'updated' WHERE {predicate}"
    ))
    .unwrap();
    assert_eq!(
        db.query_sql("SELECT body FROM row_cmp_lazy WHERE id = 1")
            .unwrap()
            .rows[0]["body"],
        Value::String("updated".into())
    );
    drop(db);
    let mut db = Database::open(&path).unwrap();
    assert_eq!(
        db.query_sql("SELECT body FROM row_cmp_lazy WHERE id = 1")
            .unwrap()
            .rows[0]["body"],
        Value::String("updated".into())
    );
    db.query_sql(&format!("DELETE FROM row_cmp_lazy WHERE {predicate}"))
        .unwrap();
    drop(db);
    let mut db = Database::open(&path).unwrap();
    assert!(db
        .query_sql("SELECT id FROM row_cmp_lazy")
        .unwrap()
        .rows
        .is_empty());

    db.query_sql(
        "INSERT INTO row_cmp_lazy (id, a, divisor, body) VALUES \
         (2, 1, 0, 'tied'), (3, NULL, 0, 'unknown')",
    )
    .unwrap();
    for sql in [
        format!("SELECT id FROM row_cmp_lazy WHERE id = 2 AND {predicate}"),
        format!("UPDATE row_cmp_lazy SET body = 'wrong' WHERE id = 2 AND {predicate}"),
        format!("DELETE FROM row_cmp_lazy WHERE id = 2 AND {predicate}"),
    ] {
        let error = db.query_sql(&sql).unwrap_err();
        assert!(
            error.to_string().contains("division by zero"),
            "{sql}: {error}"
        );
    }
    assert_eq!(
        db.query_sql("SELECT body FROM row_cmp_lazy WHERE id = 2")
            .unwrap()
            .rows[0]["body"],
        Value::String("tied".into())
    );

    assert!(db
        .query_sql(&format!(
            "SELECT id FROM row_cmp_lazy WHERE id = 3 AND {predicate}"
        ))
        .unwrap()
        .rows
        .is_empty());
    db.query_sql(&format!(
        "UPDATE row_cmp_lazy SET body = 'wrong' WHERE id = 3 AND {predicate}"
    ))
    .unwrap();
    db.query_sql(&format!(
        "DELETE FROM row_cmp_lazy WHERE id = 3 AND {predicate}"
    ))
    .unwrap();
    let rows = db
        .query_sql("SELECT id, body FROM row_cmp_lazy ORDER BY id")
        .unwrap()
        .rows;
    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0]["id"], Value::Int(2));
    assert_eq!(rows[1]["id"], Value::Int(3));
    assert_eq!(rows[1]["body"], Value::String("unknown".into()));
    drop(db);
    std::fs::remove_dir_all(path).unwrap();
}

#[test]
fn row_comparison_mutations_preserve_reversed_field_direction_after_replay() {
    let path = unique_test_dir("row-comparison-reversed-fields");
    let mut db = Database::open(&path).unwrap();
    db.query_sql(
        "CREATE TABLE row_cmp_reversed (\
         id BIGINT PRIMARY KEY, a BIGINT NOT NULL, b BIGINT NOT NULL, hit BOOLEAN NOT NULL)",
    )
    .unwrap();
    db.query_sql(
        "INSERT INTO row_cmp_reversed VALUES \
         (1, 2, -9, FALSE), (2, 1, 3, FALSE), (3, 1, 0, FALSE), (4, 0, 9, FALSE)",
    )
    .unwrap();
    db.checkpoint().unwrap();

    let params = [Value::Int(0)];
    let ids = query_ids(
        &mut db,
        "SELECT id FROM row_cmp_reversed WHERE (1, $1) < (a, b) ORDER BY id",
        &params,
    );
    assert_eq!(ids, vec![Value::Int(1), Value::Int(2)]);
    db.query_sql_with_params(
        "UPDATE row_cmp_reversed SET hit = TRUE WHERE (1, $1) < (a, b)",
        &params,
    )
    .unwrap();
    drop(db);
    let mut db = Database::open(&path).unwrap();
    assert_eq!(
        query_ids(
            &mut db,
            "SELECT id FROM row_cmp_reversed WHERE hit = TRUE ORDER BY id",
            &[]
        ),
        ids,
    );
    db.query_sql_with_params(
        "DELETE FROM row_cmp_reversed WHERE (1, $1) < (a, b)",
        &params,
    )
    .unwrap();
    drop(db);
    let mut db = Database::open(&path).unwrap();
    assert_eq!(
        query_ids(&mut db, "SELECT id FROM row_cmp_reversed ORDER BY id", &[]),
        vec![Value::Int(3), Value::Int(4)],
    );
    drop(db);
    std::fs::remove_dir_all(path).unwrap();
}

fn seed_row_keyset(database: &mut Database, indexed: bool) {
    database
        .query_sql("CREATE TABLE row_keyset (id BIGINT PRIMARY KEY, tenant TEXT NOT NULL, bucket BIGINT NOT NULL, rank BIGINT NOT NULL)")
        .unwrap();
    let rows = (0..120)
        .map(|id| {
            format!(
                "({id}, '{}', {}, {})",
                if id < 100 { "a" } else { "b" },
                id / 20,
                id % 5
            )
        })
        .collect::<Vec<_>>()
        .join(",");
    database
        .query_sql(&format!(
            "INSERT INTO row_keyset (id, tenant, bucket, rank) VALUES {rows}"
        ))
        .unwrap();
    if indexed {
        database
            .query_sql("CREATE INDEX row_keyset_cursor ON row_keyset (tenant, bucket, rank, id)")
            .unwrap();
    }
}

#[test]
fn sql_arbitrary_row_keyset_uses_complete_index_bound_after_reopen() {
    use hawdb_storage::config::RelationalIndexMode;

    let path = unique_test_dir("sql-row-keyset");
    let config = DatabaseConfig {
        max_read_result_rows: Some(8),
        relational_index_mode: RelationalIndexMode::DemandPaged,
        ..Default::default()
    };
    let mut reference = Database::new();
    seed_row_keyset(&mut reference, false);
    let mut db = Database::open_with_config(&path, config.clone()).unwrap();
    seed_row_keyset(&mut db, true);
    db.checkpoint().unwrap();
    drop(db);
    let mut db = Database::open_with_config(&path, config).unwrap();

    for (operator, direction, parameters) in [
        (
            ">",
            "ASC",
            vec![Value::Int(3), Value::Int(1), Value::Int(61)],
        ),
        (
            "<",
            "DESC",
            vec![Value::Int(3), Value::Int(1), Value::Int(61)],
        ),
    ] {
        let sql = format!(
            "SELECT id, bucket, rank FROM row_keyset WHERE tenant = 'a' AND (bucket, rank, id) {operator} ($1, $2, $3) ORDER BY bucket {direction}, rank {direction}, id {direction} LIMIT 5"
        );
        assert_same(&mut db, &mut reference, &sql, &parameters);
        let plan = db
            .query_sql_with_params(&format!("EXPLAIN {sql}"), &parameters)
            .unwrap();
        assert!(plan.rows.iter().any(|row| matches!(row.get("operator info"), Some(Value::String(info)) if info.contains("exclusive_seek=true") && info.contains("order_prefix=3"))), "{plan:?}");
        assert!(plan.rows.iter().all(|row| !matches!(row.get("id"), Some(Value::String(id)) if id.contains("SortExec") || id.contains("SelectionExec"))), "{plan:?}");
    }

    for (operator, direction) in [(">", "ASC"), ("<", "DESC")] {
        let parameters = [
            Value::String("a".to_string()),
            Value::Int(3),
            Value::Int(1),
            Value::Int(61),
        ];
        let sql = format!(
            "SELECT id, bucket, rank FROM row_keyset WHERE (tenant, bucket, rank, id) {operator} ($1, $2, $3, $4) ORDER BY tenant {direction}, bucket {direction}, rank {direction}, id {direction} LIMIT 5"
        );
        assert_same(&mut db, &mut reference, &sql, &parameters);
        let plan = db
            .query_sql_with_params(&format!("EXPLAIN {sql}"), &parameters)
            .unwrap();
        assert!(plan.rows.iter().any(|row| matches!(row.get("operator info"), Some(Value::String(info)) if info.contains("exclusive_seek=true") && info.contains("order_prefix=4"))), "{plan:?}");
        assert!(plan.rows.iter().all(|row| !matches!(row.get("id"), Some(Value::String(id)) if id.contains("SortExec") || id.contains("SelectionExec"))), "{plan:?}");
    }

    let null_plan = db
        .query_sql_with_params(
            "EXPLAIN SELECT id FROM row_keyset WHERE (tenant, bucket, rank, id) > ($1, $2, $3, $4) ORDER BY tenant, bucket, rank, id LIMIT 5",
            &[Value::Null, Value::Int(3), Value::Int(1), Value::Int(61)],
        )
        .unwrap();
    assert!(null_plan.rows.iter().all(|row| !matches!(row.get("operator info"), Some(Value::String(info)) if info.contains("exclusive_seek=true"))), "{null_plan:?}");

    drop(db);
    std::fs::remove_dir_all(path).unwrap();
}