qusql-parse 0.4.0

Parser for sql
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
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{
    DataType, Identifier, QualifiedName, SString, Span, Spanned, Statement,
    create_constraint_trigger::parse_create_constraint_trigger,
    create_function::{parse_create_function, parse_create_procedure},
    create_index::parse_create_index,
    create_option::{CreateAlgorithm, CreateOption},
    create_role::parse_create_role,
    create_table::parse_create_table_or_partition_of,
    create_trigger::parse_create_trigger,
    create_view::parse_create_view,
    data_type::{DataTypeContext, parse_data_type},
    expression::{Expression, PRIORITY_MAX, parse_expression_unreserved},
    keywords::Keyword,
    lexer::Token,
    operator::{parse_create_operator, parse_create_operator_class, parse_create_operator_family},
    parser::{ParseError, Parser},
    qualified_name::parse_qualified_name_unreserved,
};
use alloc::{boxed::Box, vec::Vec};

#[derive(Clone, Debug)]
pub struct CreateTypeEnum<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Options after "CREATE"
    pub create_options: Vec<CreateOption<'a>>,
    /// Span of "TYPE"
    pub type_span: Span,
    /// Name of the created type
    pub name: QualifiedName<'a>,
    /// Span of "AS ENUM"
    pub as_enum_span: Span,
    /// Enum values
    pub values: Vec<SString<'a>>,
}

impl<'a> Spanned for CreateTypeEnum<'a> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.create_options)
            .join_span(&self.type_span)
            .join_span(&self.name)
            .join_span(&self.as_enum_span)
            .join_span(&self.values)
    }
}

fn parse_create_type<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateTypeEnum<'a>, ParseError> {
    let type_span = parser.consume_keyword(Keyword::TYPE)?;
    parser.postgres_only(&type_span);
    let name = parse_qualified_name_unreserved(parser)?;
    let as_enum_span = parser.consume_keywords(&[Keyword::AS, Keyword::ENUM])?;
    parser.consume_token(Token::LParen)?;
    let mut values = Vec::new();
    loop {
        parser.recovered(
            "')' or ','",
            &|t| matches!(t, Token::RParen | Token::Comma),
            |parser| {
                values.push(parser.consume_string()?);
                Ok(())
            },
        )?;
        if matches!(parser.token, Token::RParen) {
            break;
        }
        parser.consume_token(Token::Comma)?;
    }
    parser.consume_token(Token::RParen)?;
    Ok(CreateTypeEnum {
        create_span,
        create_options,
        type_span,
        name,
        as_enum_span,
        values,
    })
}

#[derive(Clone, Debug)]
pub enum CreateDatabaseOption<'a> {
    CharSet {
        identifier: Span,
        default_span: Option<Span>,
        value: Identifier<'a>,
    },
    Collate {
        identifier: Span,
        default_span: Option<Span>,
        value: Identifier<'a>,
    },
    Encryption {
        identifier: Span,
        default_span: Option<Span>,
        value: SString<'a>,
    },
}

impl Spanned for CreateDatabaseOption<'_> {
    fn span(&self) -> Span {
        match self {
            CreateDatabaseOption::CharSet {
                identifier,
                default_span,
                value,
            } => identifier.join_span(default_span).join_span(value),
            CreateDatabaseOption::Collate {
                identifier,
                default_span,
                value,
            } => identifier.join_span(default_span).join_span(value),
            CreateDatabaseOption::Encryption {
                identifier,
                default_span,
                value,
            } => identifier.join_span(default_span).join_span(value),
        }
    }
}

#[derive(Clone, Debug)]
pub struct CreateDatabase<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Span of "DATABASE"
    pub database_span: Span,
    /// Span of "IF NOT EXISTS" if specified
    pub if_not_exists: Option<Span>,
    /// Name of the created database
    pub name: Identifier<'a>,
    /// Options specified for database creation
    pub create_options: Vec<CreateDatabaseOption<'a>>,
}

impl Spanned for CreateDatabase<'_> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.create_options)
            .join_span(&self.database_span)
            .join_span(&self.if_not_exists)
            .join_span(&self.name)
    }
}

/// CREATE EXTENSION statement (PostgreSQL)
#[derive(Clone, Debug)]
pub struct CreateExtension<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Span of "EXTENSION"
    pub extension_span: Span,
    /// Span of "IF NOT EXISTS" if specified
    pub if_not_exists: Option<Span>,
    /// Name of the extension
    pub name: Identifier<'a>,
    /// Optional SCHEMA clause
    pub schema: Option<(Span, Identifier<'a>)>,
    /// Optional VERSION clause
    pub version: Option<(Span, SString<'a>)>,
    /// CASCADE option
    pub cascade: Option<Span>,
}

impl Spanned for CreateExtension<'_> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.extension_span)
            .join_span(&self.if_not_exists)
            .join_span(&self.name)
            .join_span(&self.schema)
            .join_span(&self.version)
            .join_span(&self.cascade)
    }
}

fn parse_create_extension<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateExtension<'a>, ParseError> {
    let extension_span = parser.consume_keyword(Keyword::EXTENSION)?;
    parser.postgres_only(&extension_span);

    for option in create_options {
        parser.err("Not supported for CREATE EXTENSION", &option.span());
    }

    let if_not_exists = if let Some(if_span) = parser.skip_keyword(Keyword::IF) {
        Some(
            parser
                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                .join_span(&if_span),
        )
    } else {
        None
    };

    let name = parser.consume_plain_identifier_unreserved()?;

    // Optional WITH
    parser.skip_keyword(Keyword::WITH);

    // Parse optional SCHEMA, VERSION, CASCADE (any order)
    let mut schema = None;
    let mut version = None;
    let mut cascade = None;
    loop {
        match &parser.token {
            Token::Ident(_, Keyword::SCHEMA) => {
                let schema_span = parser.consume_keyword(Keyword::SCHEMA)?;
                let schema_name = parser.consume_plain_identifier_unreserved()?;
                schema = Some((schema_span, schema_name));
            }
            Token::Ident(_, Keyword::VERSION) => {
                let version_span = parser.consume_keyword(Keyword::VERSION)?;
                // Version can be identifier or string
                let version_value = match &parser.token {
                    Token::String(v, _) => SString::new((*v).into(), parser.consume()),
                    _ => {
                        let ident = parser.consume_plain_identifier_unreserved()?;
                        SString::new(ident.value.into(), ident.span)
                    }
                };
                version = Some((version_span, version_value));
            }
            Token::Ident(_, Keyword::CASCADE) => {
                cascade = Some(parser.consume_keyword(Keyword::CASCADE)?);
            }
            _ => break,
        }
    }

    Ok(CreateExtension {
        create_span,
        extension_span,
        if_not_exists,
        name,
        schema,
        version,
        cascade,
    })
}

/// CREATE DOMAIN statement (PostgreSQL)
#[derive(Clone, Debug)]
pub struct CreateDomain<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Span of "DOMAIN"
    pub domain_span: Span,
    /// Span of "IF NOT EXISTS" if specified
    pub if_not_exists: Option<Span>,
    /// Name of the domain (optionally schema-qualified)
    pub name: QualifiedName<'a>,
    /// Underlying data type
    pub data_type: DataType<'a>,
    /// Optional COLLATE clause
    pub collate: Option<(Span, Identifier<'a>)>,
    /// Optional DEFAULT clause
    pub default: Option<(Span, Expression<'a>)>,
    /// List of domain constraints (CONSTRAINT name, NOT NULL, NULL, CHECK)
    pub constraints: Vec<DomainConstraint<'a>>,
}

impl<'a> Spanned for CreateDomain<'a> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.domain_span)
            .join_span(&self.if_not_exists)
            .join_span(&self.name)
            .join_span(&self.data_type)
            .join_span(&self.collate)
            .join_span(&self.default)
            .join_span(&self.constraints)
    }
}

/// Domain constraint for CREATE DOMAIN
#[derive(Clone, Debug)]
pub enum DomainConstraint<'a> {
    ConstraintName(Span, Identifier<'a>),
    NotNull(Span),
    Null(Span),
    Check(Span, Expression<'a>),
}

impl<'a> Spanned for DomainConstraint<'a> {
    fn span(&self) -> Span {
        match self {
            DomainConstraint::ConstraintName(span, name) => span.join_span(name),
            DomainConstraint::NotNull(span) => span.clone(),
            DomainConstraint::Null(span) => span.clone(),
            DomainConstraint::Check(span, expr) => span.join_span(expr),
        }
    }
}

/// Parse CREATE DOMAIN statement (PostgreSQL)
pub fn parse_create_domain<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateDomain<'a>, ParseError> {
    let domain_span = parser.consume_keyword(Keyword::DOMAIN)?;
    parser.postgres_only(&domain_span);

    for option in create_options {
        parser.err("Not supported for CREATE DOMAIN", &option.span());
    }

    let if_not_exists = if let Some(if_span) = parser.skip_keyword(Keyword::IF) {
        Some(
            parser
                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                .join_span(&if_span),
        )
    } else {
        None
    };

    let name = parse_qualified_name_unreserved(parser)?;

    // Optional AS
    parser.skip_keyword(Keyword::AS);

    let data_type = parse_data_type(parser, DataTypeContext::Column)?;

    // Optional COLLATE
    let collate = if let Some(collate_span) = parser.skip_keyword(Keyword::COLLATE) {
        let collate_name = parser.consume_plain_identifier_unreserved()?;
        Some((collate_span, collate_name))
    } else {
        None
    };

    // Optional DEFAULT
    let default = if let Some(default_span) = parser.skip_keyword(Keyword::DEFAULT) {
        let expr = parse_expression_unreserved(parser, PRIORITY_MAX)?;
        Some((default_span, expr))
    } else {
        None
    };

    // Parse domain constraints
    let mut constraints = Vec::new();
    loop {
        match &parser.token {
            Token::Ident(_, Keyword::CONSTRAINT) => {
                let constraint_span = parser.consume_keyword(Keyword::CONSTRAINT)?;
                let name = parser.consume_plain_identifier_unreserved()?;
                constraints.push(DomainConstraint::ConstraintName(constraint_span, name));
            }
            Token::Ident(_, Keyword::NOT) => {
                let not_span = parser.consume_keyword(Keyword::NOT)?;
                let null_span = parser.consume_keyword(Keyword::NULL)?;
                constraints.push(DomainConstraint::NotNull(not_span.join_span(&null_span)));
            }
            Token::Ident(_, Keyword::NULL) => {
                let null_span = parser.consume_keyword(Keyword::NULL)?;
                constraints.push(DomainConstraint::Null(null_span));
            }
            Token::Ident(_, Keyword::CHECK) => {
                let check_span = parser.consume_keyword(Keyword::CHECK)?;
                parser.consume_token(Token::LParen)?;
                let expr = parse_expression_unreserved(parser, PRIORITY_MAX)?;
                parser.consume_token(Token::RParen)?;
                constraints.push(DomainConstraint::Check(check_span, expr));
            }
            _ => break,
        }
    }

    Ok(CreateDomain {
        create_span,
        domain_span,
        if_not_exists,
        name,
        data_type,
        collate,
        default,
        constraints,
    })
}

/// CREATE SCHEMA statement (PostgreSQL)
#[derive(Clone, Debug)]
pub struct CreateSchema<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Span of "SCHEMA"
    pub schema_span: Span,
    /// Span of "IF NOT EXISTS" if specified
    pub if_not_exists: Option<Span>,
    /// Name of the created schema (optional if AUTHORIZATION is present)
    pub name: Option<Identifier<'a>>,
    /// AUTHORIZATION clause with role name
    pub authorization: Option<(Span, Identifier<'a>)>,
}

impl Spanned for CreateSchema<'_> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.schema_span)
            .join_span(&self.if_not_exists)
            .join_span(&self.name)
            .join_span(&self.authorization)
    }
}

/// Sequence option for CREATE SEQUENCE / ALTER SEQUENCE
#[derive(Clone, Debug)]
pub enum SequenceOption<'a> {
    /// AS data_type
    As {
        as_span: Span,
        data_type: DataType<'a>,
    },
    /// INCREMENT [BY] value
    IncrementBy {
        increment_span: Span,
        by_span: Option<Span>,
        value: i64,
        value_span: Span,
    },
    /// MINVALUE value
    MinValue {
        minvalue_span: Span,
        value: i64,
        value_span: Span,
    },
    /// NO MINVALUE
    NoMinValue(Span),
    /// MAXVALUE value
    MaxValue {
        maxvalue_span: Span,
        value: i64,
        value_span: Span,
    },
    /// NO MAXVALUE
    NoMaxValue(Span),
    /// START [WITH] value
    StartWith {
        start_span: Span,
        with_span: Option<Span>,
        value: i64,
        value_span: Span,
    },
    /// CACHE value
    Cache {
        cache_span: Span,
        value: i64,
        value_span: Span,
    },
    /// CYCLE
    Cycle(Span),
    /// NO CYCLE
    NoCycle(Span),
    /// OWNED BY table.column
    OwnedBy {
        owned_span: Span,
        by_span: Span,
        table_column: QualifiedName<'a>,
    },
    /// OWNED BY NONE
    OwnedByNone {
        owned_span: Span,
        by_span: Span,
        none_span: Span,
    },
}

impl<'a> Spanned for SequenceOption<'a> {
    fn span(&self) -> Span {
        match self {
            SequenceOption::As { as_span, data_type } => as_span.join_span(data_type),
            SequenceOption::IncrementBy {
                increment_span,
                value_span,
                ..
            } => increment_span.join_span(value_span),
            SequenceOption::MinValue {
                minvalue_span,
                value_span,
                ..
            } => minvalue_span.join_span(value_span),
            SequenceOption::NoMinValue(s) => s.span(),
            SequenceOption::MaxValue {
                maxvalue_span,
                value_span,
                ..
            } => maxvalue_span.join_span(value_span),
            SequenceOption::NoMaxValue(s) => s.span(),
            SequenceOption::StartWith {
                start_span,
                value_span,
                ..
            } => start_span.join_span(value_span),
            SequenceOption::Cache {
                cache_span,
                value_span,
                ..
            } => cache_span.join_span(value_span),
            SequenceOption::Cycle(s) => s.span(),
            SequenceOption::NoCycle(s) => s.span(),
            SequenceOption::OwnedBy {
                owned_span,
                table_column,
                ..
            } => owned_span.join_span(table_column),
            SequenceOption::OwnedByNone {
                owned_span,
                none_span,
                ..
            } => owned_span.join_span(none_span),
        }
    }
}

/// Parse sequence options (used by CREATE SEQUENCE and ALTER TABLE ADD GENERATED AS IDENTITY)
pub(crate) fn parse_sequence_options<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<Vec<SequenceOption<'a>>, ParseError> {
    let mut options = Vec::new();
    loop {
        match &parser.token {
            Token::Ident(_, Keyword::AS) => {
                let as_span = parser.consume_keyword(Keyword::AS)?;
                let data_type = parse_data_type(parser, DataTypeContext::TypeRef)?;
                options.push(SequenceOption::As { as_span, data_type });
            }
            Token::Ident(_, Keyword::INCREMENT) => {
                let increment_span = parser.consume_keyword(Keyword::INCREMENT)?;
                let by_span = parser.skip_keyword(Keyword::BY); // BY is optional
                let (value, value_span) = parser.consume_signed_int::<i64>()?;
                options.push(SequenceOption::IncrementBy {
                    increment_span,
                    by_span,
                    value,
                    value_span,
                });
            }
            Token::Ident(_, Keyword::MINVALUE) => {
                let minvalue_span = parser.consume_keyword(Keyword::MINVALUE)?;
                let (value, value_span) = parser.consume_signed_int::<i64>()?;
                options.push(SequenceOption::MinValue {
                    minvalue_span,
                    value,
                    value_span,
                });
            }
            Token::Ident(_, Keyword::MAXVALUE) => {
                let maxvalue_span = parser.consume_keyword(Keyword::MAXVALUE)?;
                let (value, value_span) = parser.consume_signed_int::<i64>()?;
                options.push(SequenceOption::MaxValue {
                    maxvalue_span,
                    value,
                    value_span,
                });
            }
            Token::Ident(_, Keyword::START) => {
                let start_span = parser.consume_keyword(Keyword::START)?;
                let with_span = parser.skip_keyword(Keyword::WITH); // WITH is optional
                let (value, value_span) = parser.consume_signed_int::<i64>()?;
                options.push(SequenceOption::StartWith {
                    start_span,
                    with_span,
                    value,
                    value_span,
                });
            }
            Token::Ident(_, Keyword::CACHE) => {
                let cache_span = parser.consume_keyword(Keyword::CACHE)?;
                let (value, value_span) = parser.consume_signed_int::<i64>()?;
                options.push(SequenceOption::Cache {
                    cache_span,
                    value,
                    value_span,
                });
            }
            Token::Ident(_, Keyword::CYCLE) => {
                let cycle_span = parser.consume_keyword(Keyword::CYCLE)?;
                options.push(SequenceOption::Cycle(cycle_span));
            }
            Token::Ident(_, Keyword::NO) => {
                // Could be NO MINVALUE, NO MAXVALUE, or NO CYCLE
                let no_span = parser.consume_keyword(Keyword::NO)?;
                match &parser.token {
                    Token::Ident(_, Keyword::MINVALUE) => {
                        let minvalue_span = parser.consume_keyword(Keyword::MINVALUE)?;
                        let span = no_span.join_span(&minvalue_span);
                        options.push(SequenceOption::NoMinValue(span));
                    }
                    Token::Ident(_, Keyword::MAXVALUE) => {
                        let maxvalue_span = parser.consume_keyword(Keyword::MAXVALUE)?;
                        let span = no_span.join_span(&maxvalue_span);
                        options.push(SequenceOption::NoMaxValue(span));
                    }
                    Token::Ident(_, Keyword::CYCLE) => {
                        let cycle_span = parser.consume_keyword(Keyword::CYCLE)?;
                        let span = no_span.join_span(&cycle_span);
                        options.push(SequenceOption::NoCycle(span));
                    }
                    _ => parser.expected_failure("'MINVALUE', 'MAXVALUE' or 'CYCLE' after 'NO'")?,
                }
            }
            Token::Ident(_, Keyword::OWNED) => {
                let owned_span = parser.consume_keyword(Keyword::OWNED)?;
                let by_span = parser.consume_keyword(Keyword::BY)?;
                if let Token::Ident(_, Keyword::NONE) = parser.token {
                    let none_span = parser.consume_keyword(Keyword::NONE)?;
                    options.push(SequenceOption::OwnedByNone {
                        owned_span,
                        by_span,
                        none_span,
                    });
                } else {
                    let table_column = parse_qualified_name_unreserved(parser)?;
                    options.push(SequenceOption::OwnedBy {
                        owned_span,
                        by_span,
                        table_column,
                    });
                }
            }
            _ => break,
        }
    }
    Ok(options)
}

/// CREATE SEQUENCE statement (PostgreSQL)
#[derive(Clone, Debug)]
pub struct CreateSequence<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Span of optional TEMPORARY/TEMP keyword
    pub temporary: Option<Span>,
    /// Span of "SEQUENCE"
    pub sequence_span: Span,
    /// Span of "IF NOT EXISTS" if specified
    pub if_not_exists: Option<Span>,
    /// Name of the created sequence
    pub name: QualifiedName<'a>,
    /// Sequence options
    pub options: Vec<SequenceOption<'a>>,
}

impl Spanned for CreateSequence<'_> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.temporary)
            .join_span(&self.sequence_span)
            .join_span(&self.if_not_exists)
            .join_span(&self.name)
            .join_span(&self.options)
    }
}

fn parse_create_database<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateDatabase<'a>, ParseError> {
    for option in create_options {
        parser.err("Not supported fo CREATE DATABASE", &option.span());
    }

    let database_span = parser.consume();

    let if_not_exists = if let Some(if_) = parser.skip_keyword(Keyword::IF) {
        Some(
            parser
                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                .join_span(&if_),
        )
    } else {
        None
    };

    let mut create_options = Vec::new();
    let name = parser.consume_plain_identifier_unreserved()?;
    loop {
        let default_span = parser.skip_keyword(Keyword::DEFAULT);
        match &parser.token {
            Token::Ident(_, Keyword::CHARSET) => {
                let identifier = parser.consume_keyword(Keyword::CHARSET)?;
                parser.skip_token(Token::Eq);
                create_options.push(CreateDatabaseOption::CharSet {
                    default_span,
                    identifier,
                    value: parser.consume_plain_identifier_unreserved()?,
                });
            }
            Token::Ident(_, Keyword::CHARACTER) => {
                let identifier = parser.consume_keywords(&[Keyword::CHARACTER, Keyword::SET])?;
                parser.skip_token(Token::Eq);
                create_options.push(CreateDatabaseOption::CharSet {
                    default_span,
                    identifier,
                    value: parser.consume_plain_identifier_unreserved()?,
                });
            }
            Token::Ident(_, Keyword::COLLATE) => {
                let identifier = parser.consume_keyword(Keyword::COLLATE)?;
                parser.skip_token(Token::Eq);
                create_options.push(CreateDatabaseOption::Collate {
                    default_span,
                    identifier,
                    value: parser.consume_plain_identifier_unreserved()?,
                });
            }
            Token::Ident(_, Keyword::ENCRYPTION) => {
                let identifier = parser.consume_keyword(Keyword::ENCRYPTION)?;
                parser.skip_token(Token::Eq);
                let value = parser.consume_string()?;

                create_options.push(CreateDatabaseOption::Encryption {
                    default_span,
                    identifier,
                    value,
                });
            }
            _ => {
                if default_span.is_some() {
                    parser.expected_failure("'CHARSET', 'COLLATE' or 'ENCRYPTION'")?;
                }
                break;
            }
        }
    }

    Ok(CreateDatabase {
        create_span,
        create_options,
        database_span,
        if_not_exists,
        name,
    })
}

fn parse_create_schema<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateSchema<'a>, ParseError> {
    let schema_span = parser.consume_keyword(Keyword::SCHEMA)?;
    parser.postgres_only(&schema_span);

    for option in create_options {
        parser.err("Not supported for CREATE SCHEMA", &option.span());
    }

    let if_not_exists = if let Some(if_span) = parser.skip_keyword(Keyword::IF) {
        Some(
            parser
                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                .join_span(&if_span),
        )
    } else {
        None
    };

    // Parse schema name or AUTHORIZATION
    let name = if matches!(parser.token, Token::Ident(_, Keyword::AUTHORIZATION)) {
        None
    } else {
        Some(parser.consume_plain_identifier_unreserved()?)
    };

    let authorization = if let Token::Ident(_, Keyword::AUTHORIZATION) = parser.token {
        let auth_span = parser.consume_keyword(Keyword::AUTHORIZATION)?;
        let role_name = parser.consume_plain_identifier_unreserved()?;
        Some((auth_span, role_name))
    } else {
        None
    };

    // TODO: Parse schema elements (CREATE TABLE, CREATE VIEW, GRANT, etc.)

    Ok(CreateSchema {
        create_span,
        schema_span,
        if_not_exists,
        name,
        authorization,
    })
}

fn parse_create_sequence<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateSequence<'a>, ParseError> {
    let sequence_span = parser.consume_keyword(Keyword::SEQUENCE)?;
    parser.postgres_only(&sequence_span);

    // Extract TEMPORARY option if present, reject others
    let mut temporary = None;
    for option in create_options {
        match option {
            CreateOption::Temporary {
                local_span,
                temporary_span,
            } => {
                temporary = Some(temporary_span.join_span(&local_span));
            }
            _ => {
                parser.err("Not supported for CREATE SEQUENCE", &option.span());
            }
        }
    }

    let if_not_exists = if let Some(if_span) = parser.skip_keyword(Keyword::IF) {
        Some(
            parser
                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                .join_span(&if_span),
        )
    } else {
        None
    };

    // Parse sequence name
    let name = parse_qualified_name_unreserved(parser)?;

    // Parse sequence options
    let options = parse_sequence_options(parser)?;

    Ok(CreateSequence {
        create_span,
        temporary,
        sequence_span,
        if_not_exists,
        name,
        options,
    })
}

/// CREATE SERVER statement (PostgreSQL)
#[derive(Clone, Debug)]
pub struct CreateServer<'a> {
    /// Span of "CREATE"
    pub create_span: Span,
    /// Span of "SERVER"
    pub server_span: Span,
    /// Span of "IF NOT EXISTS" if specified
    pub if_not_exists: Option<Span>,
    /// Name of the server
    pub server_name: Identifier<'a>,
    /// Optional TYPE 'server_type'
    pub type_: Option<(Span, SString<'a>)>,
    /// Optional VERSION 'server_version'
    pub version: Option<(Span, SString<'a>)>,
    /// FOREIGN DATA WRAPPER fdw_name
    pub foreign_data_wrapper: (Span, Identifier<'a>),
    /// OPTIONS (option 'value', ...)
    pub options: Vec<(Identifier<'a>, SString<'a>)>,
}

impl Spanned for CreateServer<'_> {
    fn span(&self) -> Span {
        self.create_span
            .join_span(&self.server_span)
            .join_span(&self.if_not_exists)
            .join_span(&self.server_name)
            .join_span(&self.type_)
            .join_span(&self.version)
            .join_span(&self.foreign_data_wrapper)
            .join_span(&self.options)
    }
}

fn parse_create_server<'a>(
    parser: &mut Parser<'a, '_>,
    create_span: Span,
    create_options: Vec<CreateOption<'a>>,
) -> Result<CreateServer<'a>, ParseError> {
    let server_span = parser.consume_keyword(Keyword::SERVER)?;
    parser.postgres_only(&server_span);

    for option in create_options {
        parser.err("Not supported for CREATE SERVER", &option.span());
    }

    let if_not_exists = if let Some(if_span) = parser.skip_keyword(Keyword::IF) {
        Some(
            parser
                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                .join_span(&if_span),
        )
    } else {
        None
    };

    // Parse server name
    let server_name = parser.consume_plain_identifier_unreserved()?;

    // Parse optional TYPE 'server_type'
    let type_ = if let Some(type_span) = parser.skip_keyword(Keyword::TYPE) {
        let type_value = parser.consume_string()?;
        Some((type_span, type_value))
    } else {
        None
    };

    // Parse optional VERSION 'server_version'
    let version = if let Some(version_span) = parser.skip_keyword(Keyword::VERSION) {
        let version_value = parser.consume_string()?;
        Some((version_span, version_value))
    } else {
        None
    };

    // Parse FOREIGN DATA WRAPPER fdw_name
    let fdw_span = parser.consume_keywords(&[Keyword::FOREIGN, Keyword::DATA, Keyword::WRAPPER])?;
    let fdw_name = parser.consume_plain_identifier_unreserved()?;
    let foreign_data_wrapper = (fdw_span, fdw_name);

    // Parse optional OPTIONS (option 'value', ...)
    let mut options = Vec::new();
    if parser.skip_keyword(Keyword::OPTIONS).is_some() {
        parser.consume_token(Token::LParen)?;
        loop {
            let option_name = parser.consume_plain_identifier_unreserved()?;
            let option_value = parser.consume_string()?;
            options.push((option_name, option_value));

            if parser.skip_token(Token::Comma).is_none() {
                break;
            }
        }
        parser.consume_token(Token::RParen)?;
    }

    Ok(CreateServer {
        create_span,
        server_span,
        if_not_exists,
        server_name,
        type_,
        version,
        foreign_data_wrapper,
        options,
    })
}

pub(crate) fn parse_create<'a>(parser: &mut Parser<'a, '_>) -> Result<Statement<'a>, ParseError> {
    let create_span = parser.span.clone();
    parser.consume_keyword(Keyword::CREATE)?;

    let mut create_options = Vec::new();
    const CREATABLE: &str = "'TABLE' | 'VIEW' | 'TRIGGER' | 'FUNCTION' | 'PROCEDURE' | 'INDEX' | 'TYPE' | 'DATABASE' | 'DOMAIN' |'EXTENSION' | 'SCHEMA' | 'SEQUENCE' | 'ROLE' | 'SERVER' | 'OPERATOR' | 'CONSTRAINT'";

    parser.recovered(
        CREATABLE,
        &|t| {
            matches!(
                t,
                Token::Ident(
                    _,
                    Keyword::TABLE
                        | Keyword::MATERIALIZED
                        | Keyword::VIEW
                        | Keyword::TRIGGER
                        | Keyword::FUNCTION
                        | Keyword::PROCEDURE
                        | Keyword::INDEX
                        | Keyword::TYPE
                        | Keyword::DATABASE
                        | Keyword::SCHEMA
                        | Keyword::SEQUENCE
                        | Keyword::ROLE
                        | Keyword::SERVER
                        | Keyword::OPERATOR
                        | Keyword::EXTENSION
                        | Keyword::DOMAIN
                        | Keyword::CONSTRAINT
                )
            )
        },
        |parser| {
            loop {
                let v = match &parser.token {
                    Token::Ident(_, Keyword::OR) => CreateOption::OrReplace(
                        parser.consume_keywords(&[Keyword::OR, Keyword::REPLACE])?,
                    ),
                    Token::Ident(_, Keyword::LOCAL) => {
                        // LOCAL TEMPORARY
                        let local_span = parser.consume_keyword(Keyword::LOCAL)?;
                        parser.postgres_only(&local_span);
                        let temporary_span = parser.consume_keyword(Keyword::TEMPORARY)?;
                        CreateOption::Temporary {
                            local_span: Some(local_span),
                            temporary_span,
                        }
                    }
                    Token::Ident(_, Keyword::TEMPORARY) => {
                        let temporary_span = parser.consume_keyword(Keyword::TEMPORARY)?;
                        CreateOption::Temporary {
                            local_span: None,
                            temporary_span,
                        }
                    }
                    Token::Ident(_, Keyword::UNIQUE) => {
                        CreateOption::Unique(parser.consume_keyword(Keyword::UNIQUE)?)
                    }
                    Token::Ident(_, Keyword::ALGORITHM) => {
                        let algorithm_span = parser.consume_keyword(Keyword::ALGORITHM)?;
                        parser.consume_token(Token::Eq)?;
                        let algorithm = match &parser.token {
                            Token::Ident(_, Keyword::UNDEFINED) => CreateAlgorithm::Undefined(
                                parser.consume_keyword(Keyword::UNDEFINED)?,
                            ),
                            Token::Ident(_, Keyword::MERGE) => {
                                CreateAlgorithm::Merge(parser.consume_keyword(Keyword::MERGE)?)
                            }
                            Token::Ident(_, Keyword::TEMPTABLE) => CreateAlgorithm::TempTable(
                                parser.consume_keyword(Keyword::TEMPTABLE)?,
                            ),
                            _ => parser.expected_failure("'UNDEFINED', 'MERGE' or 'TEMPTABLE'")?,
                        };
                        CreateOption::Algorithm(algorithm_span, algorithm)
                    }
                    Token::Ident(_, Keyword::DEFINER) => {
                        let definer_span = parser.consume_keyword(Keyword::DEFINER)?;
                        parser.consume_token(Token::Eq)?;
                        // TODO user | CURRENT_USER | role | CURRENT_ROLE
                        // Accept both plain identifiers and string literals
                        let user = match &parser.token {
                            Token::String(v, _) => {
                                let v = *v;
                                Identifier::new(v, parser.consume())
                            }
                            _ => parser.consume_plain_identifier_unreserved()?,
                        };
                        parser.consume_token(Token::At)?;
                        let host = match &parser.token {
                            Token::String(v, _) => {
                                let v = *v;
                                Identifier::new(v, parser.consume())
                            }
                            _ => parser.consume_plain_identifier_unreserved()?,
                        };
                        CreateOption::Definer {
                            definer_span,
                            user,
                            host,
                        }
                    }
                    Token::Ident(_, Keyword::SQL) => {
                        let sql_security =
                            parser.consume_keywords(&[Keyword::SQL, Keyword::SECURITY])?;
                        match &parser.token {
                            Token::Ident(_, Keyword::DEFINER) => CreateOption::SqlSecurityDefiner(
                                sql_security,
                                parser.consume_keyword(Keyword::DEFINER)?,
                            ),
                            Token::Ident(_, Keyword::INVOKER) => CreateOption::SqlSecurityInvoker(
                                sql_security,
                                parser.consume_keyword(Keyword::INVOKER)?,
                            ),
                            Token::Ident(_, Keyword::USER) => CreateOption::SqlSecurityUser(
                                sql_security,
                                parser.consume_keyword(Keyword::USER)?,
                            ),
                            _ => parser.expected_failure("'DEFINER', 'INVOKER', 'USER'")?,
                        }
                    }
                    _ => break,
                };
                create_options.push(v);
            }
            Ok(())
        },
    )?;

    let r =
        match &parser.token {
            Token::Ident(_, Keyword::INDEX) => Statement::CreateIndex(Box::new(
                parse_create_index(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::TABLE) => {
                parse_create_table_or_partition_of(parser, create_span, create_options)?
            }
            Token::Ident(_, Keyword::MATERIALIZED) => {
                // MATERIALIZED VIEW
                let materialized_span = parser.consume_keyword(Keyword::MATERIALIZED)?;
                parser.postgres_only(&materialized_span);
                // Don't consume VIEW here, parse_create_view will do it
                create_options.push(CreateOption::Materialized(materialized_span));
                Statement::CreateView(Box::new(parse_create_view(
                    parser,
                    create_span,
                    create_options,
                )?))
            }
            Token::Ident(_, Keyword::VIEW) => Statement::CreateView(Box::new(parse_create_view(
                parser,
                create_span,
                create_options,
            )?)),
            Token::Ident(_, Keyword::CONSTRAINT) => Statement::CreateConstraintTrigger(Box::new(
                parse_create_constraint_trigger(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::DATABASE) => Statement::CreateDatabase(Box::new(
                parse_create_database(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::DOMAIN) => Statement::CreateDomain(Box::new(
                parse_create_domain(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::EXTENSION) => Statement::CreateExtension(Box::new(
                parse_create_extension(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::SCHEMA) => Statement::CreateSchema(Box::new(
                parse_create_schema(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::SEQUENCE) => Statement::CreateSequence(Box::new(
                parse_create_sequence(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::FUNCTION) => Statement::CreateFunction(Box::new(
                parse_create_function(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::PROCEDURE) => Statement::CreateProcedure(Box::new(
                parse_create_procedure(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::TRIGGER) => Statement::CreateTrigger(Box::new(
                parse_create_trigger(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::TYPE) => Statement::CreateTypeEnum(Box::new(
                parse_create_type(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::ROLE) => Statement::CreateRole(Box::new(parse_create_role(
                parser,
                create_span,
                create_options,
            )?)),
            Token::Ident(_, Keyword::SERVER) => Statement::CreateServer(Box::new(
                parse_create_server(parser, create_span, create_options)?,
            )),
            Token::Ident(_, Keyword::OPERATOR) => {
                let operator_span = parser.consume_keyword(Keyword::OPERATOR)?;
                match parser.token {
                    Token::Ident(_, Keyword::FAMILY) => {
                        // CREATE OPERATOR FAMILY
                        let family_span = parser.consume_keyword(Keyword::FAMILY)?;
                        parser.postgres_only(&family_span);
                        Statement::CreateOperatorFamily(Box::new(parse_create_operator_family(
                            parser,
                            create_span
                                .join_span(&operator_span)
                                .join_span(&family_span),
                            create_options,
                        )?))
                    }
                    Token::Ident(_, Keyword::CLASS) => {
                        // CREATE OPERATOR CLASS
                        let class_span = parser.consume_keyword(Keyword::CLASS)?;
                        Statement::CreateOperatorClass(Box::new(parse_create_operator_class(
                            parser,
                            create_span.join_span(&operator_span).join_span(&class_span),
                            create_options,
                        )?))
                    }
                    _ => {
                        // CREATE OPERATOR
                        Statement::CreateOperator(Box::new(parse_create_operator(
                            parser,
                            create_span.join_span(&operator_span),
                            create_options,
                        )?))
                    }
                }
            }
            _ => return parser.expected_failure(CREATABLE),
        };
    Ok(r)
}