halephant-core 0.1.0

A proxy and pooler for Postgres
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
//! Token-level statement parser.
//!
//! Given a slice of [`Token`]s from [`super::lexer::lex`], walk the
//! leading tokens and build a [`Statement`] AST.
//!
//! This is deliberately a tiny hand-written recursive-descent walker
//! rather than a second chumsky parser layer. Chumsky shines on
//! complex grammars with backtracking and error recovery, neither of
//! which apply here: the grammar is ~10 statement shapes, every
//! unrecognised input falls through to `Statement::Other`, and there
//! are no errors to surface. Imperative cursor-based parsing is
//! clearer and produces less machinery for a reader to digest.

use super::ast::{
    DeallocateTarget, DiscardTarget, ResetTarget, SetScope, SetValue, Statement,
    TransactionOptions, UnlistenTarget,
};
use super::lexer::{Token, lex};

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Parse a SQL string into a [`Statement`].
///
/// The parser inspects only the leading tokens of the query, so for
/// multi-statement input it classifies by the first statement — the
/// same convention every proxy-level parser uses.
pub fn parse(sql: &str) -> Statement {
    let tokens = lex(sql);
    let mut cur = Cursor::new(&tokens);
    parse_statement(&mut cur)
}

// ---------------------------------------------------------------------------
// Cursor
// ---------------------------------------------------------------------------

/// Linear iterator over the token slice with enough lookahead for the
/// shapes we care about (one-token for most branches, two-token for
/// `SET SESSION CHARACTERISTICS`). Everything is `O(1)`: no cloning,
/// no backtracking buffer.
struct Cursor<'a> {
    tokens: &'a [Token],
    pos: usize,
}

impl<'a> Cursor<'a> {
    fn new(tokens: &'a [Token]) -> Self {
        Self { tokens, pos: 0 }
    }

    fn peek(&self) -> Option<&'a Token> {
        self.tokens.get(self.pos)
    }

    /// Look at the token `offset` positions ahead without advancing.
    /// `peek_at(0)` is equivalent to `peek()`.
    fn peek_at(&self, offset: usize) -> Option<&'a Token> {
        self.tokens.get(self.pos + offset)
    }

    fn advance(&mut self) -> Option<&'a Token> {
        let t = self.tokens.get(self.pos)?;
        self.pos += 1;
        Some(t)
    }

    /// If the next token is a `Word(expected)`, consume it and return
    /// `true`. Otherwise leave the cursor untouched.
    fn eat_word(&mut self, expected: &str) -> bool {
        if matches!(self.peek(), Some(Token::Word(w)) if w == expected) {
            self.pos += 1;
            return true;
        }
        false
    }

    /// Consume the next token as an identifier — either an unquoted
    /// `Word` (lowercased by the lexer) or a `QuotedIdent` (case
    /// preserved). Returns `None` on any other token.
    fn eat_identifier(&mut self) -> Option<String> {
        match self.peek()? {
            Token::Word(name) | Token::QuotedIdent(name) => {
                let s = name.clone();
                self.pos += 1;
                Some(s)
            }
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------
// Statement dispatch
// ---------------------------------------------------------------------------

fn parse_statement(cur: &mut Cursor<'_>) -> Statement {
    let Some(Token::Word(first)) = cur.peek() else {
        return Statement::Other;
    };
    match first.as_str() {
        "listen" => parse_listen(cur),
        "unlisten" => parse_unlisten(cur),
        "set" => parse_set(cur),
        "reset" => parse_reset(cur),
        "discard" => parse_discard(cur),
        "deallocate" => parse_deallocate(cur),
        "begin" | "start" => parse_begin(cur),
        "commit" | "end" => Statement::Commit,
        "rollback" | "abort" => Statement::Rollback,
        "select" | "table" | "values" => Statement::Select,
        "insert" => Statement::Insert,
        "update" => Statement::Update,
        "delete" | "truncate" => Statement::Delete,
        "copy" => Statement::Copy,
        "with" => parse_cte_wrapped(cur),
        _ => Statement::Other,
    }
}

// ---------------------------------------------------------------------------
// LISTEN / UNLISTEN
// ---------------------------------------------------------------------------

fn parse_listen(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // LISTEN
    match cur.eat_identifier() {
        Some(channel) => Statement::Listen { channel },
        // `LISTEN` without a following identifier is malformed SQL;
        // collapse to Other rather than fabricate an empty channel.
        None => Statement::Other,
    }
}

fn parse_unlisten(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // UNLISTEN

    // Note: the `Token::Star` arm is only reachable for an unquoted
    // `*` in the source text. A double-quoted `"*"` comes in as
    // `Token::QuotedIdent("*")` and falls into the identifier arm
    // below, becoming `Channel("*")` — the right behaviour, since
    // the quoted form is a literal identifier, not the wildcard.
    match cur.peek() {
        Some(Token::Star) => {
            cur.advance();
            Statement::Unlisten {
                target: UnlistenTarget::Star,
            }
        }
        Some(Token::Word(name) | Token::QuotedIdent(name)) => {
            let name = name.clone();
            cur.advance();
            Statement::Unlisten {
                target: UnlistenTarget::Channel(name),
            }
        }
        _ => Statement::Other,
    }
}

// ---------------------------------------------------------------------------
// SET / RESET / DISCARD
// ---------------------------------------------------------------------------

fn parse_set(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // SET

    // `SET TRANSACTION <modes>` — transaction characteristics for
    // the current transaction. Must come before the LOCAL/SESSION
    // check because it takes no scope prefix.
    if cur.eat_word("transaction") {
        return Statement::SetTransaction {
            options: parse_transaction_options(cur),
        };
    }

    // Scope prefix: LOCAL or SESSION. SESSION has the extra special
    // case `SET SESSION CHARACTERISTICS AS TRANSACTION <modes>`, which
    // sets session-level transaction defaults and is also a
    // SetTransaction variant — because the grammar after `AS
    // TRANSACTION` is the same.
    let scope = if cur.eat_word("local") {
        SetScope::Local
    } else if cur.eat_word("session") {
        if cur.eat_word("characteristics") {
            if cur.eat_word("as") && cur.eat_word("transaction") {
                return Statement::SetTransaction {
                    options: parse_transaction_options(cur),
                };
            }
            // `SET SESSION CHARACTERISTICS ...` without `AS
            // TRANSACTION` is malformed.
            return Statement::Other;
        }
        SetScope::Session
    } else {
        SetScope::Session
    };

    // `SET [scope] parameter (= | TO) value`
    let Some(parameter) = cur.eat_identifier() else {
        return Statement::Other;
    };

    // Consume the `=` (arrives as `Token::Other` from the lexer) or
    // the `TO` keyword.
    match cur.peek() {
        Some(Token::Word(w)) if w == "to" => {
            cur.advance();
        }
        Some(Token::Other) => {
            cur.advance();
        }
        _ => return Statement::Other,
    }

    let value = match cur.peek() {
        Some(Token::Word(w)) if w == "default" => {
            cur.advance();
            SetValue::Default
        }
        Some(Token::Word(v) | Token::Literal(v) | Token::Number(v)) => {
            let v = v.clone();
            cur.advance();
            SetValue::Literal(v)
        }
        // Signed numeric values like `SET work_mem = -1` or
        // `SET seed = +0.5`: the sign character lexes as
        // `Token::Other` because the lexer collapses unknown
        // punctuation uniformly. If the token immediately after the
        // sign is a Number, consume both and record the parameter
        // as dirty. The actual numeric value is irrelevant for
        // `dirty_vars` tracking (which only keys on the parameter
        // name), so we store the bare number without the sign.
        //
        // Known limitation: `SET default_transaction_read_only = -0`
        // is pathological — PostgreSQL would treat it as
        // `0`/read-write, but halephant's `is_read_write_override`
        // check runs `as_boolean("0")` against the stored literal
        // (sign stripped), so it would correctly flag this. For any
        // other signed value, `as_boolean` on the bare digits
        // returns `None`, so no false read-write override fires.
        Some(Token::Other) if matches!(cur.peek_at(1), Some(Token::Number(_))) => {
            cur.advance(); // sign
            let Some(Token::Number(v)) = cur.peek() else {
                unreachable!("peek_at(1) matched Token::Number above");
            };
            let v = v.clone();
            cur.advance();
            SetValue::Literal(v)
        }
        _ => return Statement::Other,
    };

    Statement::Set {
        scope,
        parameter,
        value,
    }
}

fn parse_reset(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // RESET
    match cur.peek() {
        Some(Token::Word(w)) if w == "all" => {
            cur.advance();
            Statement::Reset {
                target: ResetTarget::All,
            }
        }
        Some(Token::Word(_) | Token::QuotedIdent(_)) => {
            // Unwrap is safe: we just matched Word/QuotedIdent above.
            let name = cur.eat_identifier().expect("identifier after RESET");
            Statement::Reset {
                target: ResetTarget::Parameter(name),
            }
        }
        _ => Statement::Other,
    }
}

fn parse_discard(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // DISCARD
    let Some(Token::Word(w)) = cur.peek() else {
        // Bare `DISCARD` is not valid SQL — fall through so the
        // server surfaces the syntax error.
        return Statement::Other;
    };
    let target = match w.as_str() {
        "all" => DiscardTarget::All,
        "plans" => DiscardTarget::Plans,
        "sequences" => DiscardTarget::Sequences,
        "temporary" | "temp" => DiscardTarget::Temp,
        // Unknown DISCARD subtarget — PostgreSQL would reject it too.
        _ => return Statement::Other,
    };
    cur.advance();
    Statement::Discard { target }
}

fn parse_deallocate(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // DEALLOCATE

    // `PREPARE` is an optional noise keyword between DEALLOCATE and
    // the target; consume it if present and keep going.
    cur.eat_word("prepare");

    match cur.peek() {
        // `DEALLOCATE [PREPARE] ALL` — must be the unquoted `ALL`
        // keyword, not a (case-preserved) quoted identifier named
        // "ALL".
        Some(Token::Word(w)) if w == "all" => {
            cur.advance();
            Statement::Deallocate {
                target: DeallocateTarget::All,
            }
        }
        // `DEALLOCATE [PREPARE] name` — lowercased for unquoted,
        // case-preserved for `"quoted"`.
        Some(Token::Word(_) | Token::QuotedIdent(_)) => {
            // Unwrap is safe: we just matched Word/QuotedIdent.
            let name = cur.eat_identifier().expect("identifier after DEALLOCATE");
            Statement::Deallocate {
                target: DeallocateTarget::Name(name),
            }
        }
        // Bare `DEALLOCATE` or `DEALLOCATE PREPARE` alone is
        // malformed — PostgreSQL requires a target. Fall through so
        // the server surfaces the syntax error rather than silently
        // succeeding with a synthetic response.
        _ => Statement::Other,
    }
}

// ---------------------------------------------------------------------------
// BEGIN / START TRANSACTION
// ---------------------------------------------------------------------------

fn parse_begin(cur: &mut Cursor<'_>) -> Statement {
    // Consume the leading keyword and remember which one it was:
    // `START` requires a following `TRANSACTION`; `BEGIN` accepts an
    // optional `WORK` or `TRANSACTION` keyword.
    let is_start = matches!(cur.peek(), Some(Token::Word(w)) if w == "start");
    cur.advance();

    if is_start {
        // `START` on its own (or `START WORK`, `START READ ONLY`,
        // ...) is not valid SQL — PostgreSQL only accepts `START
        // TRANSACTION [...]`. Fall through to `Other` so the server
        // surfaces the syntax error to the client instead of us
        // fabricating a `Begin` and routing malformed `START READ
        // ONLY` to a replica.
        if !cur.eat_word("transaction") {
            return Statement::Other;
        }
    } else {
        // BEGIN accepts an optional trailing WORK or TRANSACTION
        // noise word; neither changes semantics, both are skipped.
        let _ = cur.eat_word("work") || cur.eat_word("transaction");
    }

    Statement::Begin {
        options: parse_transaction_options(cur),
    }
}

/// Scan the remaining tokens for transaction-mode keywords, collecting
/// `READ ONLY` / `READ WRITE` into [`TransactionOptions`]. Other
/// transaction-mode tokens (`ISOLATION LEVEL SERIALIZABLE`,
/// `DEFERRABLE`, comma separators) are skipped without error — the
/// router only cares about the read/write flag.
///
/// Crucially, a bare `READ` without a following `ONLY`/`WRITE` (as in
/// `ISOLATION LEVEL READ COMMITTED` or `REPEATABLE READ`) does not
/// break the scan: the loop simply continues, so the later `READ ONLY`
/// in `ISOLATION LEVEL REPEATABLE READ READ ONLY` is still picked up.
fn parse_transaction_options(cur: &mut Cursor<'_>) -> TransactionOptions {
    let mut opts = TransactionOptions::default();
    while let Some(tok) = cur.peek() {
        match tok {
            Token::Word(w) if w == "read" => {
                cur.advance();
                match cur.peek() {
                    Some(Token::Word(w)) if w == "only" => {
                        cur.advance();
                        opts.read_only = Some(true);
                    }
                    Some(Token::Word(w)) if w == "write" => {
                        cur.advance();
                        opts.read_only = Some(false);
                    }
                    // `READ` was a decoy from an isolation-level
                    // clause — don't touch read_only, keep scanning.
                    _ => {}
                }
            }
            _ => {
                cur.advance();
            }
        }
    }
    opts
}

// ---------------------------------------------------------------------------
// WITH <cte> ... <statement>
// ---------------------------------------------------------------------------

/// Scan past CTE body parens at depth zero to find the wrapped
/// statement's leading keyword. A `WITH` query that isn't followed by
/// a recognised DML keyword returns `Other` — PostgreSQL would reject
/// it too.
fn parse_cte_wrapped(cur: &mut Cursor<'_>) -> Statement {
    cur.advance(); // WITH
    let mut depth = 0u32;
    while let Some(t) = cur.advance() {
        match t {
            Token::LParen => depth += 1,
            Token::RParen => depth = depth.saturating_sub(1),
            Token::Word(w) if depth == 0 => match w.as_str() {
                "select" | "table" | "values" => return Statement::Select,
                "insert" => return Statement::Insert,
                "update" => return Statement::Update,
                "delete" | "truncate" => return Statement::Delete,
                _ => {}
            },
            _ => {}
        }
    }
    Statement::Other
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // ---- LISTEN ----------------------------------------------------------

    #[test]
    fn listen_unquoted() {
        assert_eq!(
            parse("LISTEN my_channel"),
            Statement::Listen {
                channel: "my_channel".into()
            }
        );
    }

    #[test]
    fn listen_quoted_preserves_case() {
        assert_eq!(
            parse(r#"LISTEN "MyChannel""#),
            Statement::Listen {
                channel: "MyChannel".into()
            }
        );
    }

    #[test]
    fn listen_without_channel_is_other() {
        // Malformed `LISTEN` with no following identifier.
        assert_eq!(parse("LISTEN"), Statement::Other);
    }

    // ---- UNLISTEN --------------------------------------------------------

    #[test]
    fn unlisten_star() {
        assert_eq!(
            parse("UNLISTEN *"),
            Statement::Unlisten {
                target: UnlistenTarget::Star
            }
        );
    }

    #[test]
    fn unlisten_quoted_star_is_channel_not_wildcard() {
        // The quoted form is a literal identifier named `*`, not the
        // wildcard.
        assert_eq!(
            parse(r#"UNLISTEN "*""#),
            Statement::Unlisten {
                target: UnlistenTarget::Channel("*".into())
            }
        );
    }

    #[test]
    fn unlisten_channel_name() {
        assert_eq!(
            parse("UNLISTEN my_channel"),
            Statement::Unlisten {
                target: UnlistenTarget::Channel("my_channel".into())
            }
        );
    }

    // ---- SET -------------------------------------------------------------

    #[test]
    fn set_session_default() {
        assert_eq!(
            parse("SET work_mem = '256MB'"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "work_mem".into(),
                value: SetValue::Literal("256mb".into()),
            }
        );
    }

    #[test]
    fn set_to_syntax() {
        assert_eq!(
            parse("SET search_path TO public"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "search_path".into(),
                value: SetValue::Literal("public".into()),
            }
        );
    }

    #[test]
    fn set_local_scope() {
        assert_eq!(
            parse("SET LOCAL work_mem = '1MB'"),
            Statement::Set {
                scope: SetScope::Local,
                parameter: "work_mem".into(),
                value: SetValue::Literal("1mb".into()),
            }
        );
    }

    #[test]
    fn set_session_scope_explicit() {
        assert_eq!(
            parse("SET SESSION work_mem = '1GB'"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "work_mem".into(),
                value: SetValue::Literal("1gb".into()),
            }
        );
    }

    #[test]
    fn set_default_value() {
        assert_eq!(
            parse("SET default_transaction_read_only TO DEFAULT"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "default_transaction_read_only".into(),
                value: SetValue::Default,
            }
        );
    }

    #[test]
    fn set_quoted_parameter_preserves_case() {
        assert_eq!(
            parse(r#"SET "MyVar" = 'value'"#),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "MyVar".into(),
                value: SetValue::Literal("value".into()),
            }
        );
    }

    /// Regression guard: a SET whose value has a leading sign
    /// character must still parse as `Statement::Set` with the
    /// parameter name populated — not collapse to `Other`. Otherwise
    /// `track_set_reset` never inserts the parameter into
    /// `dirty_vars` and `reset_connection` fails to RESET it on
    /// checkin, leaking the modified value to the next pool client.
    #[test]
    fn set_negative_number() {
        assert_eq!(
            parse("SET work_mem = -1"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "work_mem".into(),
                value: SetValue::Literal("1".into()),
            }
        );
    }

    #[test]
    fn set_positive_sign_number() {
        assert_eq!(
            parse("SET work_mem = +5"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "work_mem".into(),
                value: SetValue::Literal("5".into()),
            }
        );
    }

    #[test]
    fn set_negative_with_to_syntax() {
        assert_eq!(
            parse("SET seed TO -1"),
            Statement::Set {
                scope: SetScope::Session,
                parameter: "seed".into(),
                value: SetValue::Literal("1".into()),
            }
        );
    }

    #[test]
    fn set_local_negative_number() {
        // LOCAL scope must still be tracked; otherwise the
        // transaction-level GUC change leaks the wrong way.
        assert_eq!(
            parse("SET LOCAL work_mem = -1"),
            Statement::Set {
                scope: SetScope::Local,
                parameter: "work_mem".into(),
                value: SetValue::Literal("1".into()),
            }
        );
    }

    // ---- SET TRANSACTION -------------------------------------------------

    #[test]
    fn set_transaction_read_write() {
        assert_eq!(
            parse("SET TRANSACTION READ WRITE"),
            Statement::SetTransaction {
                options: TransactionOptions {
                    read_only: Some(false)
                }
            }
        );
    }

    #[test]
    fn set_session_characteristics() {
        assert_eq!(
            parse("SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE"),
            Statement::SetTransaction {
                options: TransactionOptions {
                    read_only: Some(false)
                }
            }
        );
    }

    #[test]
    fn set_session_characteristics_malformed_is_other() {
        assert_eq!(
            parse("SET SESSION CHARACTERISTICS work_mem = '1GB'"),
            Statement::Other
        );
    }

    // ---- RESET -----------------------------------------------------------

    #[test]
    fn reset_all() {
        assert_eq!(
            parse("RESET ALL"),
            Statement::Reset {
                target: ResetTarget::All
            }
        );
    }

    #[test]
    fn reset_parameter() {
        assert_eq!(
            parse("RESET work_mem"),
            Statement::Reset {
                target: ResetTarget::Parameter("work_mem".into())
            }
        );
    }

    // ---- DISCARD ---------------------------------------------------------

    #[test]
    fn discard_variants() {
        assert_eq!(
            parse("DISCARD ALL"),
            Statement::Discard {
                target: DiscardTarget::All
            }
        );
        assert_eq!(
            parse("DISCARD PLANS"),
            Statement::Discard {
                target: DiscardTarget::Plans
            }
        );
        assert_eq!(
            parse("DISCARD SEQUENCES"),
            Statement::Discard {
                target: DiscardTarget::Sequences
            }
        );
        assert_eq!(
            parse("DISCARD TEMPORARY"),
            Statement::Discard {
                target: DiscardTarget::Temp
            }
        );
        assert_eq!(
            parse("DISCARD TEMP"),
            Statement::Discard {
                target: DiscardTarget::Temp
            }
        );
    }

    #[test]
    fn discard_malformed_is_other() {
        assert_eq!(parse("DISCARD"), Statement::Other);
        assert_eq!(parse("DISCARD FOO"), Statement::Other);
    }

    // ---- DEALLOCATE ------------------------------------------------------

    #[test]
    fn deallocate_named() {
        assert_eq!(
            parse("DEALLOCATE my_stmt"),
            Statement::Deallocate {
                target: DeallocateTarget::Name("my_stmt".into())
            }
        );
    }

    #[test]
    fn deallocate_all() {
        assert_eq!(
            parse("DEALLOCATE ALL"),
            Statement::Deallocate {
                target: DeallocateTarget::All
            }
        );
    }

    #[test]
    fn deallocate_prepare_noise_keyword() {
        // The optional `PREPARE` keyword is part of the grammar but
        // contributes no semantics; both forms collapse to the same
        // variant.
        assert_eq!(
            parse("DEALLOCATE PREPARE my_stmt"),
            Statement::Deallocate {
                target: DeallocateTarget::Name("my_stmt".into())
            }
        );
        assert_eq!(
            parse("DEALLOCATE PREPARE ALL"),
            Statement::Deallocate {
                target: DeallocateTarget::All
            }
        );
    }

    #[test]
    fn deallocate_mixed_case() {
        assert_eq!(
            parse("Deallocate Prepare My_Stmt"),
            Statement::Deallocate {
                target: DeallocateTarget::Name("my_stmt".into())
            }
        );
        assert_eq!(
            parse("deallocate all"),
            Statement::Deallocate {
                target: DeallocateTarget::All
            }
        );
    }

    #[test]
    fn deallocate_quoted_identifier_preserves_case() {
        assert_eq!(
            parse(r#"DEALLOCATE "MyStmt""#),
            Statement::Deallocate {
                target: DeallocateTarget::Name("MyStmt".into())
            }
        );
        assert_eq!(
            parse(r#"DEALLOCATE PREPARE "MyStmt""#),
            Statement::Deallocate {
                target: DeallocateTarget::Name("MyStmt".into())
            }
        );
    }

    /// A quoted `"ALL"` is a literal identifier, not the wildcard
    /// keyword — mirrors the `UNLISTEN "*"` case. PostgreSQL itself
    /// treats the unquoted `ALL` as a reserved keyword in this
    /// position.
    #[test]
    fn deallocate_quoted_all_is_name_not_wildcard() {
        assert_eq!(
            parse(r#"DEALLOCATE "ALL""#),
            Statement::Deallocate {
                target: DeallocateTarget::Name("ALL".into())
            }
        );
    }

    #[test]
    fn deallocate_malformed_is_other() {
        // Bare DEALLOCATE — no target.
        assert_eq!(parse("DEALLOCATE"), Statement::Other);
        // DEALLOCATE PREPARE without a target.
        assert_eq!(parse("DEALLOCATE PREPARE"), Statement::Other);
    }

    // ---- BEGIN / START ---------------------------------------------------

    #[test]
    fn begin_plain() {
        assert_eq!(
            parse("BEGIN"),
            Statement::Begin {
                options: TransactionOptions::default()
            }
        );
    }

    #[test]
    fn begin_read_only() {
        assert_eq!(
            parse("BEGIN READ ONLY"),
            Statement::Begin {
                options: TransactionOptions {
                    read_only: Some(true)
                }
            }
        );
    }

    #[test]
    fn begin_read_write() {
        assert_eq!(
            parse("BEGIN READ WRITE"),
            Statement::Begin {
                options: TransactionOptions {
                    read_only: Some(false)
                }
            }
        );
    }

    #[test]
    fn begin_with_isolation_before_read_only() {
        // The `READ` inside `REPEATABLE READ` must not break the scan
        // — the later `READ ONLY` is still picked up.
        assert_eq!(
            parse("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"),
            Statement::Begin {
                options: TransactionOptions {
                    read_only: Some(true)
                }
            }
        );
    }

    #[test]
    fn begin_isolation_read_committed_no_mode() {
        // `READ COMMITTED` has no effect on the read_only flag.
        assert_eq!(
            parse("BEGIN ISOLATION LEVEL READ COMMITTED"),
            Statement::Begin {
                options: TransactionOptions { read_only: None }
            }
        );
    }

    #[test]
    fn start_transaction_read_only() {
        assert_eq!(
            parse("START TRANSACTION READ ONLY"),
            Statement::Begin {
                options: TransactionOptions {
                    read_only: Some(true)
                }
            }
        );
    }

    /// Regression guard: `START` without a following `TRANSACTION`
    /// must parse as `Other`, not `Begin`. Misclassifying it as
    /// `Begin` lets the router send `START READ ONLY` to a replica
    /// where PostgreSQL rejects it with a syntax error the client
    /// doesn't expect.
    #[test]
    fn start_without_transaction_is_other() {
        assert_eq!(parse("START"), Statement::Other);
        assert_eq!(parse("START WORK"), Statement::Other);
        assert_eq!(parse("START READ ONLY"), Statement::Other);
        assert_eq!(parse("START WORK READ ONLY"), Statement::Other);
    }

    // ---- COMMIT / ROLLBACK -----------------------------------------------

    #[test]
    fn commit_aliases() {
        assert_eq!(parse("COMMIT"), Statement::Commit);
        assert_eq!(parse("END"), Statement::Commit);
    }

    #[test]
    fn rollback_aliases() {
        assert_eq!(parse("ROLLBACK"), Statement::Rollback);
        assert_eq!(parse("ABORT"), Statement::Rollback);
    }

    // ---- DML -------------------------------------------------------------

    #[test]
    fn select_variants() {
        assert_eq!(parse("SELECT 1"), Statement::Select);
        assert_eq!(parse("TABLE t"), Statement::Select);
        assert_eq!(parse("VALUES (1)"), Statement::Select);
    }

    #[test]
    fn dml_variants() {
        assert_eq!(parse("INSERT INTO t VALUES (1)"), Statement::Insert);
        assert_eq!(parse("UPDATE t SET x = 1"), Statement::Update);
        assert_eq!(parse("DELETE FROM t"), Statement::Delete);
        assert_eq!(parse("TRUNCATE t"), Statement::Delete);
        assert_eq!(parse("COPY t FROM stdin"), Statement::Copy);
    }

    // ---- WITH CTE --------------------------------------------------------

    #[test]
    fn with_cte_select() {
        assert_eq!(
            parse("WITH cte AS (SELECT 1) SELECT * FROM cte"),
            Statement::Select
        );
    }

    #[test]
    fn with_cte_insert() {
        assert_eq!(
            parse("WITH cte AS (SELECT 1) INSERT INTO t SELECT * FROM cte"),
            Statement::Insert
        );
    }

    #[test]
    fn with_recursive() {
        assert_eq!(
            parse("WITH RECURSIVE tree AS (SELECT 1) SELECT * FROM tree"),
            Statement::Select
        );
    }

    // ---- Degenerate ------------------------------------------------------

    #[test]
    fn empty_input() {
        assert_eq!(parse(""), Statement::Other);
        assert_eq!(parse("   "), Statement::Other);
    }

    #[test]
    fn only_comment() {
        assert_eq!(parse("-- just a comment"), Statement::Other);
    }

    #[test]
    fn ddl_is_other() {
        assert_eq!(parse("CREATE TABLE t (id int)"), Statement::Other);
    }

    // ---- is_read_only / is_read_write_override on the AST ---------------

    #[test]
    fn is_read_only_pattern() {
        assert!(matches!(
            parse("BEGIN READ ONLY"),
            Statement::Begin {
                options: TransactionOptions {
                    read_only: Some(true)
                }
            }
        ));
        assert!(!matches!(
            parse("BEGIN"),
            Statement::Begin {
                options: TransactionOptions {
                    read_only: Some(true)
                }
            }
        ));
    }

    #[test]
    fn is_read_write_override_covers_all_paths() {
        assert!(parse("BEGIN READ WRITE").is_read_write_override());
        assert!(parse("SET TRANSACTION READ WRITE").is_read_write_override());
        assert!(
            parse("SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE").is_read_write_override()
        );
        assert!(parse("SET default_transaction_read_only = off").is_read_write_override());
        assert!(parse("SET default_transaction_read_only = 'off'").is_read_write_override());
        assert!(parse("SET default_transaction_read_only TO DEFAULT").is_read_write_override());

        assert!(!parse("BEGIN READ ONLY").is_read_write_override());
        assert!(!parse("BEGIN").is_read_write_override());
        assert!(!parse("SET default_transaction_read_only = on").is_read_write_override());
        assert!(!parse("SET work_mem = '1MB'").is_read_write_override());
        assert!(!parse("SELECT 1").is_read_write_override());
    }

    /// Regression: `RESET default_transaction_read_only` is
    /// semantically identical to `SET ... TO DEFAULT` (both revert
    /// the GUC to the compiled default, which is `off` / read-write)
    /// and must be flagged as a read-write override so the router
    /// rejects it on replica-routed transactions. Without this, a
    /// client could issue RESET to silently flip the connection back
    /// to read-write and leak that state to the next pool user.
    #[test]
    fn reset_default_transaction_read_only_is_override() {
        assert!(parse("RESET default_transaction_read_only").is_read_write_override());
        assert!(parse("reset DEFAULT_TRANSACTION_READ_ONLY").is_read_write_override());
    }

    /// Regression: `RESET ALL` sweeps every session setting including
    /// `default_transaction_read_only`, so it has the same effect as
    /// the targeted RESET above and must be rejected the same way.
    #[test]
    fn reset_all_is_override() {
        assert!(parse("RESET ALL").is_read_write_override());
        assert!(parse("reset all").is_read_write_override());
    }

    /// Narrow check: a RESET of an unrelated GUC is NOT an override.
    /// Only `default_transaction_read_only` and `ALL` matter.
    #[test]
    fn reset_other_parameter_is_not_override() {
        assert!(!parse("RESET work_mem").is_read_write_override());
        assert!(!parse("RESET search_path").is_read_write_override());
    }
}