File: sql_query_grammar.proto

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1663 lines) | stat: -rw-r--r-- 39,023 bytes parent folder | download | duplicates (7)
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
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

syntax = "proto2";

import "icu_codes.proto";

package sql_query_grammar;

/* Generates SQL queries in protobuf format, using enums for the tables and
   integers for column names. The column name integers will be wrapped.
   Relies heavily on the sqlite grammar from here:
   https://www.sqlite.org/lang.html

   Yes, it is all in one big blobby proto file, as everything is extremely
   mutually recursive and there is no such thing as circular imports or forward
   declarations in protobuf.
*/

message SQLQuery {
  // No comments generated, nor EXPLAIN queries
  oneof query_oneof {
    Select select = 1;
    CreateTable create_table = 2;
    Insert insert = 3;
    Delete delete = 4;
    CreateFTS3Table fts3_table = 5;
    FTS3SpecificQuery fts_query = 6;
    BeginTransaction begin_txn = 7;
    CommitTransaction commit_txn = 8;
    RollbackStatement rollback_stmt = 9;
    CreateSavePoint create_save_point = 10;
    ReleaseSavePoint release_save_point = 11;
    Analyze analyze = 12;
    Vacuum vacuum = 13;
    Pragma pragma = 14;
    Update update = 15;
    CreateIndex create_index = 16;
    CreateView create_view = 17;
    CreateTrigger create_trigger = 18;
    ReIndex reindex = 19;
    Drop drop = 20;
    AlterTable alter_table = 21;
    AttachDatabase attach_db = 22;
    DetachDatabase detach_db = 23;
    FTS3HiddenTableInsert fts3_insert = 24;
    FTS3HiddenTableUpdate fts3_update = 25;
    FTS3HiddenTableDelete fts3_delete = 26;
  }
}

// ~~~~FTS3~~~

message FTS3SpecificQuery {
  oneof query_oneof {
    FTS3SpecialCommand command = 1;
    // The following exists to increase the probability of hitting MATCH
    // queries.
    FTS3SelectMatch select = 2;
  }
}

message FTS3SelectMatch {
  // SELECT * FROM |table| WHERE |col| MATCH |match_pattern|
  required FTS3Table table = 1;
  required Column col = 2;
  required FTS3MatchFormat match_pattern = 3;
}

message FTS3SpecialCommand {
  // INSERT INTO xyz(xyz) VALUES('optimize');
  // is a special command that optimizes FTS table xyz.
  required FTS3Table table = 1;
  enum Command {
    OPTIMIZE = 0;
    REBUILD = 1;
    INTEGRITY_CHECK = 2;
    MERGE = 3;
    AUTOMERGE = 4;
  }
  required Command command = 2;
  required uint32 val1 = 3;
  required uint32 val2 = 4;
}

message ICULocale {
  required IsoLangCode iso_lang_code = 1;
  // least significant 2 bytes will each be clamped to within 'A' and
  // 'Z' and then concatenated together to form a country code.
  required uint32 country_code = 2;
  // TODO(mpdenton) generate anything else here? Will sqlite care?
}

enum TokenizerType {
  TT_SIMPLE = 0;
  TT_PORTER = 1;
  TT_ICU = 2;
  TT_UNICODE61 = 3;  // FIXME in the future: remove_diacritics, separators,
                     // token_chars.... (not supported in Chrome)
}

// FIXME in the future: maybe also fuzz the tokenizer type/icu locales with
// random strings???
message CreateFTS3Table {
  required bool if_not_exists = 1;
  optional Schema schema = 6;
  required FTS3Table table = 2;
  optional ColumnList col_list = 3;
  optional TokenizerType tokenizer_type = 4;
  required ICULocale locale = 5;  // used if tokenizer_type == TT_ICU.
}

message FTS3Table {
  required uint32 table = 1;
}

// TODO(mpdenton) query by row id?

message FTS3MatchFormat {
  repeated FTS3MatchCompoundFormat ft = 1;
}

message FTS3MatchCompoundFormat {
  required FTS3MatchFormatCore core = 1;
  repeated FTS3CompoundAndCore compound_and_cores = 2;
}

message FTS3CompoundAndCore {
  enum CompoundOp {
    OR = 0;
    // Chrome does not enable the enhanced query syntax. FIXME in the future.
    // AND = 1;
    // NOT = 2;
  }
  required CompoundOp op = 1;
  required FTS3MatchFormatCore core = 2;
}

message FTS3MatchFormatCore {
  oneof ft_oneof {
    FTS3PhraseQuery pq = 1;
    FTS3NearQuery nq = 2;
  }
  required FTS3MatchToken mt_fallback = 3;
}

message FTS3NearQuery {
  // May generate into another NEAR query, this is fine.
  required FTS3MatchFormatCore format_core1 = 1;
  optional uint32 num_tokens_near = 2;
  required FTS3MatchFormatCore format_core2 = 3;
}

message FTS3PhraseQuery {
  required FTS3MatchToken mt = 1;
  repeated FTS3MatchToken extra_mts = 2;
}

message FTS3MatchToken {
  optional Column col = 1;  // search in this column specifically
  required bool negate = 4;
  required string token = 2;  // token (or prefix) to search for
  required bool prefix = 3;   // append star to end
}

// FIXME in the future: https://www.sqlite.org/fts3.html Section 8.2: Query
// tokenizers themselves. Unsupported by Chrome.

message FTS3OffsetsFn {
  required FTS3Table table = 1;
}

message FTS3SnippetsFn {
  required FTS3Table table = 1;
  enum NumArgs {
    A0 = 0;
    A1 = 1;
    A2 = 2;
    A3 = 3;
    A4 = 4;
    A5 = 5;
  }
  required NumArgs num_optional_args = 2;
  required string start_match = 3;
  required string end_match = 4;
  required string ellipses = 5;
  optional uint32 col_number = 6;  // capped by KMaxColumnNumber
  required uint32 num_tokens = 7;  // in [-64, 64].
}

message FTS3MatchInfoFn {
  required FTS3Table table = 1;

  // The following will be wrapped to any of the characters p,c,n,a,,l,s,x,y,b.
  repeated uint32 chars = 2;
}

// FTS3 Auxiliary function format
message FTS3AuxiliaryFn {
  required FTS3OffsetsFn offsets_fallback = 1;
  oneof fts_aux_fn_oneof {
    FTS3SnippetsFn snippets = 2;
    FTS3MatchInfoFn matchinfo = 3;
  }
}

// Types of hidden tables in FTS3
message FTS3HiddenTable {
  enum HiddenTableVal {
    // CONTENT = 0; // Taken care of in |Table|
    SEGDIR = 1;
    SEGMENTS = 2;
    // stat and docsize are FTS4
  }
  required HiddenTableVal htv = 1;
  required FTS3Table table = 2;
}

// The different columns that go into the FTS3 hidden tables above
enum FTS3HiddenTableColumn {
  FTS3_HT_BLOCKID = 0;
  FTS3_HT_BLOCK = 1;
  FTS3_HT_LEVEL = 2;
  FTS3_HT_IDX = 3;
  FTS3_HT_START_BLOCK = 4;
  FTS3_HT_LEAVES_END_BLOCK = 5;
  FTS3_HT_END_BLOCK = 6;
  FTS3_HT_ROOT = 7;
}

// Some extra stuff to modify the FTS3 Hidden tables, only used for FTS3
// Fuzzing.

// Used to insert single values in.
message FTS3HiddenTableInsert {
  required FTS3HiddenTable fht = 1;
  repeated FTS3HiddenTableColumnValue col_vals = 2;
}

message FTS3HiddenTableUpdate {
  required FTS3HiddenTable fht = 1;
  repeated FTS3HiddenTableColumnValue col_vals = 2;

  // mini WHERE clause with some BinOp
  optional FTS3HiddenTableColumn col_where = 3;
  required BinaryOperator bin_op = 4;
  required Expr comp_expr = 5;
}

message FTS3HiddenTableDelete {
  required FTS3HiddenTable fht = 1;
  optional FTS3HiddenTableColumn col_where = 2;
  required BinaryOperator bin_op = 3;
  required Expr comp_expr = 4;
}

message FTS3HiddenTableColumnValue {
  required FTS3HiddenTableColumn col = 1;
  required Expr expr = 2;
}

// ~~~~TRANSACTIONs~~~~
// TODO(mpdenton) these could be meta-statements that enclose a bunch of other
// statements. Is that worthwhile or should I just let the fuzzer generate them
// haphazardly?
message BeginTransaction {
  enum TransactionType {
    DEFERRED = 0;
    IMMEDIATE = 1;
    EXCLUSIVE = 2;
  }
  optional TransactionType type = 1;
}

message CommitTransaction {
  enum CommitText {
    COMMIT = 0;
    END = 1;
    COMMIT_TRANSACTION = 2;
    END_TRANSACTION = 3;
  }
  required CommitText text = 1;
}

message SavePoint {
  required uint32 savepoint_num = 1;
}

message RollbackStatement {
  optional SavePoint save_point = 1;
}

message CreateSavePoint {
  required SavePoint save_point = 1;
}

message ReleaseSavePoint {
  required SavePoint save_point = 1;
}

// ~~~~ANALYZE~~~~
message Analyze {
  optional Schema schema_name = 1;
  optional Table table_name = 2;
  optional Index index_name = 3;
}

// ~~~~VACUUM~~~~
message Vacuum {
  optional Schema schema = 1;
}

// ~~~~PRAGMA~~~~
message Pragma {
  // FIXME in the future: full list here: https://www.sqlite.org/pragma.html
  // These are the ones I've seen in Chrome
  enum PragmaCommand {
    // QUICK_CHECK = 0;
    // INTEGRITY_CHECK = 1;
    AUTO_VACUUM = 2;
    WRITEABLE_SCHEMA = 3;
    LOCKING_MODE = 4;
    TEMP_STORE = 5;
    PAGE_SIZE_ = 6;
    TABLE_INFO = 7;
    JOURNAL_MODE = 8;
    MMAP_SIZE = 9;
  }
  required PragmaCommand command = 1;
  optional Schema schema = 2;
  required int32 arg1 = 3;
}

// ~~~~CREATE_TABLE~~~~
enum TempModifier {
  TM_TEMP = 3;
  TM_TEMPORARY = 4;
}

message CreateTable {
  optional TempModifier temp_modifier = 1;
  required bool if_not_exists = 2;
  required ExprSchemaTable schema_table = 3;
  oneof create_table_oneof {
    CreateTableOpt1 op1 = 4;
    Select as_select_stmt = 5;  // AS select-stmt
  }
  required CreateTableOpt1 op = 6;  // used only if the above oneof is empty
}

message CreateTableOpt1 {
  required ColumnDef col_def = 1;
  repeated ColumnDef extra_col_defs = 2;
  repeated TableConstraint table_constraints = 3;
  required bool without_rowid = 4;
}

message ColumnDef {
  required Column col = 1;
  optional TypeName type_name = 2;
  repeated ColumnConstraint col_constraints = 3;
}

message TypeName {
  // Things like VARCHAR(100) are simply not enforced by SQLite (this example
  // would end up with a column affinity of TEXT). So I don't make much effort
  // to generate them.
  required CastTypeName ctn = 1;
  optional uint32 sn = 2;
}

message ColumnConstraint {
  optional ColumnConstraintName constraint_name = 1;
  oneof col_constraint_oneof {
    ColConstraintOpt1 opt1 = 2;
    ConflictClause not_null_conf_clause = 3;
    ConflictClause unique_conf_clause = 4;
    Expr check_expr = 5;  // CORPUS specialize??
    ColConstraintOpt2 opt2 = 6;
    CollateType collate = 7;
    ForeignKeyClause fkey_clause = 8;
  }
  required ColConstraintOpt2 opt2_fallback = 9;
}

message ColConstraintOpt1 {
  required AscDesc asc_desc = 1;
  required ConflictClause conflict = 2;
  required bool autoincrement = 3;
}

message ColConstraintOpt2 {
  required LiteralValue lit_val = 1;
  optional Expr expr = 2;  // CORPUS specialize?
}

message ConflictClause {
  enum OnConflict {
    ROLLBACK = 0;
    ABORT = 1;
    FAIL = 2;
    IGNORE = 3;
    REPLACE = 4;
  }
  optional OnConflict on_conflict = 1;
}

message TableConstraint {
  optional TableConstraintName name = 1;
  oneof table_constraint_oneof {
    TableConstraintOpt1 opt1 = 2;
    Expr check_expr = 3;  // CORPUS specialize?
    TableConstraintOpt2 opt2 = 4;
  }
}

message TableConstraintOpt1 {
  enum ConstraintType {
    PRIMARY_KEY = 0;
    UNIQUE = 1;
  }
  required ConstraintType constraint_type = 1;
  required IndexedColumnList indexed_col_list = 2;
  required ConflictClause conf_clause = 3;
}

message TableConstraintOpt2 {
  required ColumnList cols = 1;
  required ForeignKeyClause fkey_clause = 2;
}

message IndexedColumn {
  optional Expr expr = 1;   // CORPUS specialize?
  required Column col = 2;  // only used if expr non-existent
  optional CollateType collate_type = 3;
  required AscDesc asc_desc = 4;
}

message IndexedColumnList {
  required IndexedColumn indexed_col = 1;
  repeated IndexedColumn extra_indexed_cols = 2;
}

message ForeignKeyClause {
  required Table foreign_table = 1;
  optional ColumnList col_list = 2;
  repeated ForeignKeyClauseCore fkey_cores = 3;
  optional DeferStrategy defer_strat = 4;
}

message ForeignKeyClauseCore {
  optional ForeignKeyClauseNotMatch fkey_not_match = 1;
  // Sqlite doesn't obey MATCH expressions, MATCH SIMPLE is always assumed
}

message ForeignKeyClauseNotMatch {
  enum DeleteOrUpdate {
    DELETE = 0;
    UPDATE = 1;
  }
  required DeleteOrUpdate del_or_update = 1;

  enum Action {
    SET_NULL = 0;
    SET_DEFAULT = 1;
    CASCADE = 2;
    RESTRICT = 3;
    NO_ACTION = 4;
  }
  required Action action = 2;
}

message DeferStrategy {
  required bool not = 1;
  enum DeferStratEnum {
    INITIALLY_DEFERRED = 0;
    INITIALLY_IMMEDIATE = 1;
    NONE = 2;
  }
  required DeferStratEnum strat = 2;
}

// ~~~~DELETE~~~~
// FIXME in the future: SQL_ENABLE_UPDATE_DELETE_LIMIT
message Delete {
  optional WithStatement with = 1;
  required QualifiedTableName qtn = 2;
  optional WhereStatement where = 3;
}

message QualifiedTableName {
  required SchemaTableAsAlias staa = 1;
  required bool indexed = 2;
  required bool not_indexed = 3;
  required Index indexed_by = 4;
}

// ~~~~INSERT~~~~

message UpsertClausePart1 {
  required IndexedColumnList icol_list = 1;
  optional WhereStatement where_stmt = 2;
}

message ColEqualsExpr {
  oneof uclause_p2_oneof {
    Column col = 1;
    ColumnList col_list = 2;
  }
  required Expr expr = 3;
}

message UpsertClausePart2 {
  required ColEqualsExpr cee = 1;
  repeated ColEqualsExpr extra_cees = 2;
  optional WhereStatement where_stmt = 3;
}

message UpsertClause {
  optional UpsertClausePart1 uclause_p1 = 1;
  optional UpsertClausePart2 uclause_p2 = 2;
}

message SchemaTableAsAlias {
  required ExprSchemaTable schema_table = 1;
  optional Table as_table_alias = 2;
}

message Insert {
  optional WithStatement with = 1;
  enum InsertType {
    INSERT = 0;
    REPLACE = 1;
    INSERT_OR_REPLACE = 2;
    INSERT_OR_ROLLBACK = 3;
    INSERT_OR_ABORT = 4;
    INSERT_OR_FAIL = 5;
    INSERT_OR_IGNORE = 6;
  }
  required InsertType insert_type = 2;
  required SchemaTableAsAlias staa = 3;
  optional ColumnList col_list = 5;
  oneof insert_oneof {
    ValuesStatement values = 6;
    Select select = 7;
  }  // if empty, DEFAULT VALUES
  optional UpsertClause upsert_clause = 8;
}

// ~~~~UPDATE~~~~
message Update {
  optional WithStatement with = 1;
  enum UpdateType {
    OR_ROLLBACK = 0;
    OR_ABORT = 1;
    OR_REPLACE = 2;
    OR_FAIL = 3;
    OR_IGNORE = 4;
  }
  optional UpdateType update_type = 2;
  required QualifiedTableName qtn = 3;
  required UpsertClausePart2 ucp2 = 4;
}

// ~~~~CREATE INDEX~~~~
message CreateIndex {
  required bool unique = 1;
  required bool if_not_exists = 2;
  optional Schema schema = 3;
  required Index index = 4;
  required Table table = 5;
  required IndexedColumnList icol_list = 6;
  optional WhereStatement where = 7;
}

// ~~~~CREATE VIEW~~~~
message View {
  required uint32 view = 1;
}
message CreateView {
  optional TempModifier temp_modifier = 1;
  required bool if_not_exists = 2;
  optional Schema schema = 3;
  required View view = 4;
  optional ColumnList col_list = 5;
  required Select select = 6;
}

// ~~~~CREATE TRIGGER~~~~
message Trigger {
  required uint32 trigger = 1;
}

message CreateTrigger {
  optional TempModifier temp_modifier = 1;
  required bool if_not_exists = 2;
  optional Schema schema = 3;
  required Trigger trigger = 4;
  enum TriggerType {
    BEFORE = 0;
    AFTER = 1;
    INSTEAD_OF = 2;
  }
  optional TriggerType trigger_type = 5;
  enum TriggerInstr {
    DELETE = 0;
    UPDATE = 1;
    INSERT = 2;
  }
  optional TriggerInstr trigger_instr = 6;
  required ColumnList col_list = 7;  // CORPUS create corpus item with an Update
                                     // using the same ColumnList as this
  required Table table = 8;
  required bool for_each_row = 9;
  optional ExprComparisonHighProbability when = 10;  // for the WHEN statement
  // There are significant restrictions on update, insert, select, and delete
  // expressions used in triggers. However, we might as well generate normal
  // queries and see if these restrictions are adequately enforced.

  // Also, from https://www.sqlite.org/lang_createtrigger.html: "If a BEFORE
  // UPDATE or BEFORE DELETE trigger modifies or deletes a row that was to have
  // been updated or deleted, then the result of the subsequent update or delete
  // operation is undefined. Furthermore, if a BEFORE trigger modifies or
  // deletes a row, then it is undefined whether or not AFTER triggers that
  // would have otherwise run on those rows will in fact run."

  // It is unclear what is meant by "undefined". Are we talking memory
  // corruption? I suppose the fuzzer will find it and we'll see.
  required TypicalQuery tq = 11;
  repeated TypicalQuery extra_tqs = 12;
}

message TypicalQuery {
  oneof tq_oneof {
    Update update = 1;
    Insert insert = 2;
    Select select = 3;
  }
  required Delete delete_fallback = 4;
}

// ~~~~REINDEX~~~~
message ReIndex {
  required bool empty = 5;
  optional CollateType collate_type = 4;  // used if schema does not exist
  optional Schema schema = 1;
  optional Table table = 2;
  required Index index = 3;  // used if table does not exist.
}

// ~~~~DROP *~~~~
message Drop {
  required bool if_exists = 5;
  optional Schema schema = 6;
  oneof drop_oneof {
    Index index = 1;
    Table table = 2;
    Trigger trigger = 3;
  }
  required View view_fallback = 4;
}

// ~~~~ALTER TABLE~~~~
message AlterTable {
  required ExprSchemaTable schema_table = 1;
  required bool column = 2;
  optional Column col = 4;
  required Column col_to = 5;
  optional ColumnDef col_def = 6;
  required Table table_fallback = 3;
}

// ~~~~ATTACH DATABASE~~~~

// SEE: https://www.sqlite.org/inmemorydb.html
// Lots of different options.
// TODO(mpdenton) may want to experiment with on-filesystem main dbs as well...
message AttachDatabase {
  required bool in_memory = 1;
  required bool file_uri = 2;
  optional Schema db_name = 3;
  required bool shared_cache = 4;
  required Schema schema = 5;
}

// ~~~~DETACH DATABASE~~~~
message DetachDatabase {
  required Schema schema = 1;
}

// ~~~~SELECT~~~~
/*
  Select is obviously the most complicated syntax in the language, and the
  fuzzer will likely generate plenty of invalid SELECT statements. As long as
  it does not generate too many, we should still be perfectly fine.

  From sqlite docs:
  Note that there are paths through the syntax diagrams that are not allowed in
  practice. Some examples:

  A VALUES clause can be the first element in a compound SELECT that uses a WITH
  clause, but a simple SELECT that consists of just a VALUES clause cannot be
  preceded by a WITH clause. The WITH clause must occur on the first SELECT of a
  compound SELECT. It cannot follow a compound-operator.


  See https://www.sqlite.org/lang_select.html.
*/
message Select {
  optional WithStatement with = 1;

  // SQL grammar specifies SelectSubStatement here but that just leads
  // to a bunch of unparseable VALUES ... ; statements. So require
  // SelectCore here.
  required SelectStatementCore select_core = 2;
  repeated ExtraSelectSubStatement extra_select_substatements = 3;
  optional OrderByStatement orderby = 4;
  optional LimitStatement limit = 5;
}

message OrderByStatement {
  required ExprOrderingTerm ord_term = 1;
  repeated ExprOrderingTerm extra_ord_terms = 2;
}

message LimitStatement {
  // CORPUS specialize these exprs???
  required Expr limit_expr = 1;
  required bool offset = 2;  // this is only used if second_expr exists
  optional Expr second_expr = 3;
}

message ExtraSelectSubStatement {
  required CompoundOperator compound_op = 1;
  required SelectSubStatement select_substatement = 2;
}

enum CompoundOperator {
  CO_UNION = 0;
  CO_UNION_ALL = 1;
  CO_INTERSECT = 2;
  CO_EXCEPT = 3;
}

message SelectSubStatement {
  oneof select_subexpr_oneof {
    SelectStatementCore select_core = 1;
    ValuesStatement values = 2;
  }
  required ValuesStatement values_fallback = 3;
}

message ValuesStatement {
  required ExprList expr_list = 1;
  repeated ExprList extra_expr_lists = 2;  // CORPUS specialize?
}

message ExprColAlias {
  required Expr expr = 1;
  optional Column col_alias = 2;
  required bool as = 3;
}

message ResultColumn {
  oneof result_col_oneof {
    Column col = 1;
    ExprColAlias eca = 2;
    Table table_star = 3;
    FTS3AuxiliaryFn fts3_fn = 4;  // Only emitted when FUZZ_FTS3 enabled
  }                               // if nothing, use star *
}

message SelectStatementCore {
  enum SelectOrDistinct {
    SELECT_DISTINCT = 0;
    SELECT = 1;
    SELECT_ALL = 2;
  }
  required SelectOrDistinct s_or_d = 1;
  repeated ResultColumn result_columns = 2;
  optional FromStatement from = 3;
  optional WhereStatement where = 4;
  optional GroupByStatement groupby = 5;
  optional WindowStatement window = 6;
}

message WithStatement {
  required bool recursive = 1;
  required CommonTableExpr table_expr = 2;
  repeated CommonTableExpr extra_table_exprs = 3;
}

message FromStatement {
  required TableOrSubqueryOption3 tos3 = 1;
}

message WindowStatement {
  required WindowStatementNaming win = 1;
  repeated WindowStatementNaming extra_wins = 2;
}

message WindowStatementNaming {
  required WindowName name = 1;
  required WindowDefn defn = 2;
}

message GroupByStatement {
  // CORPUS specialize all these exprs????
  required ExprList exprs = 1;
  optional Expr having_expr = 3;
}

message WhereStatement {
  required ExprComparisonHighProbability expr = 1;
}

message CommonTableExpr {
  required Table table = 1;
  repeated Column columns = 2;
  required Select select = 3;
}

// ~~~~Join stuff~~~~~
message JoinClause {
  required TableOrSubquery tos = 1;
  repeated JoinClauseCore clauses = 2;
}

message JoinClauseCore {
  required JoinOperator join_op = 1;
  required TableOrSubquery tos = 2;
  required JoinConstraint join_constraint = 3;
}

message JoinOperator {
  required bool comma = 1;
  // the following fields only used if comma is false
  required bool natural = 2;
  enum JoinType {
    LEFT = 0;
    LEFT_OUTER = 1;
    INNER = 2;
    CROSS = 3;
    NONE = 4;
  }
  required JoinType join_type = 3;
}

message JoinConstraint {
  oneof join_constraint_oneof {
    Expr on_expr = 1;
    UsingExpr using_expr = 2;
  }  // fine if empty
}

message UsingExpr {
  required ColumnList col_list = 1;
}

// ~~~~~Table names etc.~~~~~

// First checks if main is set. Then checks if temp is set. Then checks if
// other schema number is set.
message Schema {
  required uint32 schema = 1;
  required bool main = 2;
  required bool temp = 3;
}

message Table {
  required uint32 table = 1;
  optional bool fts3_content_table = 2;  // only used for FTS3 fuzzing
}

message Column {
  required uint32 column = 1;
  optional bool rowid = 2;  // can also have "rowid" column

  // FTS has a hidden column with the same name as the table.
  optional FTS3Table fts3_table = 3;
  // FTS3 tables have "docid" as an alias for "rowid".
  optional bool fts3_docid = 4;
}

message WindowName {
  required uint32 window_name = 1;
}

message ColumnConstraintName {
  required uint32 constraint_name = 1;
}

message TableConstraintName {
  required uint32 constraint_name = 1;
}

message Index {
  required uint32 index = 1;
}

// Example:
// column1, column2, column3
message ColumnList {
  required Column col = 1;
  repeated Column extra_cols = 2;
}

// ~~~~table-or-subquery~~~~
message TableOrSubquery {
  oneof tos_oneof {
    QualifiedTableName qtn = 1;
    TableOrSubqueryOption2 toso2 = 2;
    TableOrSubqueryOption3 toso3 = 3;
    TableOrSubqueryOption4 toso4 = 4;
  }
  required ExprSchemaTable schema_table_expr = 5;  // used if the oneof is empty
}

message TableOrSubqueryOption2 {
  required ExprSchemaTableFn schema_table_fn = 1;
  optional AsTableAlias as_table_alias = 2;
}

message TableOrSubqueryOption3 {
  repeated TableOrSubquery tos_list = 1;  // if empty, use the join clause
  required JoinClause join_clause = 2;
}

message TableOrSubqueryOption4 {
  required Select select = 1;
  optional AsTableAlias as_table_alias = 2;
}

message AsTableAlias {
  required bool as = 2;
  required Table table_alias = 3;
}

// ~~~~Expressions~~~~
message Expr {
  oneof expr_oneof {
    LiteralValue lit_val = 1;
    ComplicatedExpr comp_expr = 2;
  }
}

message NumericLiteral {
  repeated uint32 hex_digits = 1;
  repeated uint32 digits = 2;
  required bool decimal_point = 3;
  repeated uint32 dec_digits = 4;
  repeated uint32 exp_digits = 5;
  required bool negative_exp = 6;
}

message LiteralValue {
  enum SpecialVal {
    VAL_NULL = 0;  // using just "NULL" vauses it not to compile.
    VAL_TRUE = 1;
    VAL_FALSE = 2;
    CURRENT_TIME = 3;
    CURRENT_DATE = 4;
    CURRENT_TIMESTAMP = 5;
  }
  oneof lit_val_oneof {
    int64 num_lit = 1;
    string string_lit = 2;
    bytes blob_lit = 3;
    SpecialVal special_val = 4;
    NumericLiteral numeric_lit = 5;
  }  // If no value, just use int64(1)
}

enum UnaryOperator {
  UNOP_MINUS = 1;
  UNOP_PLUS = 2;
  UNOP_TILDE = 3;
  UNOP_NOT = 4;
}

message UnaryExpr {
  required UnaryOperator unary_op = 1;
  required Expr expr = 2;
}

enum BinaryOperator {
  BINOP_CONCAT = 1;  // double pipe
  BINOP_STAR = 2;
  BINOP_SLASH = 3;
  BINOP_PERCENT = 4;
  BINOP_PLUS = 5;
  BINOP_MINUS = 6;
  BINOP_LELE = 7;  // <<
  BINOP_GRGR = 8;  // >>
  BINOP_AMPERSAND = 9;
  BINOP_PIPE = 10;
  BINOP_LE = 11;
  BINOP_LEQ = 12;
  BINOP_GR = 13;
  BINOP_GREQ = 14;
  BINOP_EQ = 15;
  BINOP_EQEQ = 16;
  BINOP_NOTEQ = 17;
  BINOP_LEGR = 18;  // <> (not equal)
  BINOP_IS = 19;
  BINOP_ISNOT = 20;
  BINOP_IN = 21;
  BINOP_LIKE = 22;
  BINOP_GLOB = 23;
  BINOP_MATCH = 24;
  BINOP_REGEXP = 25;
  BINOP_AND = 26;
  BINOP_OR = 27;
}

message BinaryExpr {
  required Expr lhs = 1;
  required BinaryOperator op = 2;
  required Expr rhs = 3;

  // In FUZZ_FTS3 mode, if |fmt| exists we will use it instead of rhs to
  // help generate better MATCH queries.
  optional FTS3MatchFormat fmt = 4;
}

// Used to inflate the probability that we get a comparison of a column with an
// expr. This is useful, as an example, for WHERE expressions.
message ExprComparisonHighProbability {
  oneof expr_comp_oneof {
    ColumnComparison cc = 1;
    Expr expr = 2;
  }
}

// Special version of expr that only generates predicates with a column on the
// left.
message ColumnComparison {
  required ExprSchemaTableColumn col = 1;
  required BinaryOperator op = 2;
  required Expr expr = 3;
  // In FUZZ_FTS3 mode, if |fmt| exists we will use it instead of rhs to
  // help generate better MATCH queries.
  optional FTS3MatchFormat fmt = 4;
}

message ExprSchemaTableColumn {
  optional Schema schema = 1;
  optional Table table = 2;
  required Column col = 3;
}

// Separate this out to inflate the probability of having a literal value
message ComplicatedExpr {
  // Don't want bind-parameter, unless fuzzing sql_bind
  oneof complicated_expr_oneof {
    ExprSchemaTableColumn expr_stc = 2;
    UnaryExpr unary_expr = 3;
    BinaryExpr binary_expr = 4;
    Fn fn_expr = 5;
    ParenthesesExpr par_expr = 6;
    CastExpr cast_expr = 7;
    CollateExpr collate_expr = 8;
    Expr1 expr1 = 9;
    ExprNullTests expr_null_tests = 10;
    ExprIs expr_is = 11;
    ExprBetween expr_between = 12;
    ExprIn expr_in = 17;
    ExprExists expr_exists = 13;
    ExprCase expr_case = 14;
    ExprRaiseFn expr_raise = 15;
  }
  required LiteralValue lit_val = 16;  // used if oneof is empty.
}

message ExprRaiseFn {
  required bool ignore = 3;
  enum RaiseFnEnum {
    ROLLBACK = 0;
    ABORT = 1;
    FAIL = 2;
  }
  required RaiseFnEnum raise_fn = 1;
  required string error_msg = 2;
}

message ExprCase {
  optional Expr expr = 1;
  required ExprWhenThen when_then = 2;
  repeated ExprWhenThen extra_when_thens = 3;
  optional Expr else_expr = 4;
}

message ExprWhenThen {
  required Expr when_expr = 1;
  required Expr then_expr = 2;
}

message ExprExists {
  required bool not = 1;
  required bool exists = 2;
  required Select select = 3;
}

message ExprIn {
  required Expr expr = 5;
  required bool not = 1;
  oneof exprin_oneof {
    ExprInParen expr_in_paren = 2;
    ExprSchemaTable schema_table = 3;
    ExprSchemaTableFn schema_table_fn = 4;
  }
}

message ExprSchemaTable {
  optional Schema schema_name = 1;
  required Table table_name = 2;
}

message ExprSchemaTableFn {
  required TableFn table_fn = 2;
  // FIXME in the future add more. For now, no exprs.
}

message ExprInParen {
  oneof exprin_paren_oneof {
    Select select = 1;
    ExprList exprs = 2;
  }  // if zero, can just emit closed parentheses
}

message ExprList {
  required Expr expr = 1;
  repeated Expr extra_exprs = 2;
}

message Expr1 {
  required Expr expr1 = 5;
  required bool not = 1;
  enum PossibleKeywords {
    LIKE = 0;
    GLOB = 1;
    REGEXP = 2;
    MATCH = 3;
  }
  required PossibleKeywords keyword = 2;
  required Expr expr2 = 3;
  optional Expr escape_expr = 4;  // CORPUS specialize?
}

message ExprNullTests {
  required Expr expr = 1;
  enum PossibleKeywords {
    ISNULL = 0;
    NOTNULL = 1;
    NOT_NULL = 2;
  }
  required PossibleKeywords keyword = 2;
}

message ExprIs {
  required bool not = 1;
  required Expr expr1 = 2;
  required Expr expr2 = 3;
}

message ExprBetween {
  required bool not = 1;
  required Expr expr1 = 2;
  required Expr expr2 = 3;
  required Expr expr3 = 4;
}

enum CollateType {
  COLLATE_BINARY = 1;
  COLLATE_NOCASE = 2;
  COLLATE_RTRIM = 3;
}

message CollateExpr {
  required Expr expr = 1;
  required CollateType collate_type = 2;
}

message CastTypeName {
  enum CastTypeNameEnum {
    BLOB = 0;
    TEXT = 1;
    REAL = 2;
    INTEGER = 3;
    NUMERIC = 4;
  }
  required CastTypeNameEnum type_enum = 1;
}

message CastExpr {
  required Expr expr = 1;
  required CastTypeName type_name = 2;
}

message ParenthesesExpr {
  required Expr expr = 1;
  repeated Expr other_exprs = 2;
}

message Fn {
  oneof fn_oneof {
    SimpleFn simple_fn = 1;
    FTS3AuxiliaryFn fts_aux_fn = 2;
    DateAndTimeFn dat_fn = 3;
    AggregateFn aggregate_fn = 4;
    Printf printf = 5;
  }
  // FIXME in the future: JSON functions. Not used in Chrome.
}

// Aggregate FNs
message AggregateFn {
  optional bool count_star = 7;
  enum FnName {
    AVG = 0;
    COUNT = 1;
    GROUP_CONCAT = 2;
    MAX = 3;
    MIN = 4;
    SUM = 5;
    TOTAL = 6;
  }
  required FnName fn_name = 6;
  required bool distinct = 5;
  optional Column col1 = 1;
  optional Column col2 = 2;
  required Expr expr1 = 3;
  optional Expr expr2 = 4;
}

// Date and Time Functions
message DateAndTimeFn {
  optional SimpleDateAndTimeFn simple = 1;
  required StrftimeFn strftime = 2;
}

message StrftimeFn {
  repeated StrftimeFormat fmts = 1;
  required TimeString time_string = 2;
  repeated TimeModifier modifiers = 3;
}

message StrftimeFormat {
  enum Substitution {
    D = 0;
    F = 1;
    H = 2;
    J = 3;
    M = 4;
    S = 5;
    W = 6;
    Y = 7;
  }
  required bool lowercase = 1;
  optional Substitution subs = 2;
  required string bytes = 3;
}

message SimpleDateAndTimeFn {
  enum FnName {
    DATE = 0;
    TIME = 1;
    DATETIME = 2;
    JULIANDAY = 3;
  }
  required FnName fn_name = 1;
  required TimeString time_string = 2;
  repeated TimeModifier modifiers = 3;
}

message HoursStuff {
  optional uint32 hh = 1;
  optional uint32 mm = 2;
  optional uint32 ss = 3;
  optional uint32 sss = 4;
}

message TimeString {
  optional uint32 yyyy = 1;
  optional uint32 mm = 2;
  optional uint32 dd = 3;
  optional HoursStuff hs = 4;
  required bool extra_t = 6;

  optional uint32 dddddddddd = 7;

  required bool now = 8;

  optional string random_bytes = 9;

  required bool z = 13;
  required bool plus = 14;
  optional bool tz_plus = 10;
  optional uint32 tz_hh = 11;
  optional uint32 tz_mm = 12;
}

message TimeModifier {
  enum NumberedModifiers {
    DAYS = 0;
    HOURS = 1;
    MINUTES = 2;
    SECONDS = 3;
    MONTHS = 4;
    YEARS = 5;
  }
  required uint32 num = 4;
  optional uint32 dot_num = 5;
  optional NumberedModifiers nm = 1;
  enum OtherModifiers {
    START_OF_MONTH = 0;
    START_OF_YEAR = 1;
    START_OF_DAY = 2;
    WEEKDAY = 3;
    UNIXEPOCH = 4;
    LOCALTIME = 5;
    UTC = 6;
  }
  required OtherModifiers om = 2;
  required uint32 weekday = 3;
}

// Simple Functions finish
message SimpleFn {
  oneof simple_fn_oneof {
    ZeroArgFn zero_arg_fn = 1;
    OneArgFn one_arg_fn = 2;
    TwoArgFn two_arg_fn = 3;
    ThreeArgFn three_arg_fn = 4;
    VarNumFn varnum_fn = 5;
    CharFn char_fn = 6;
  }
}

// Zero arguments fn
enum ZeroArgFn {
  ZFN_CHANGES = 0;
  ZFN_LAST_INSERT_ROWID = 1;
  ZFN_RANDOM = 2;
  ZFN_SQLITE_SOURCE_ID = 3;
  ZFN_SQLITE_VERSION = 4;
  ZFN_TOTAL_CHANGES = 5;
}

message OneArgFn {
  enum OneArgFnEnum {
    ABS = 1;
    HEX = 2;
    LENGTH = 3;
    LIKELY = 4;
    LOWER = 5;
    LTRIM = 6;
    LOAD_EXTENSION = 7;
    QUOTE = 8;
    ROUND = 9;
    RTRIM = 10;
    RANDOMBLOB = 11;
    SOUNDEX = 12;
    SQLITE_COMPILE_OPTION_GET = 13;
    SQLITE_COMPILE_OPTION_USED = 14;
    SQLITE_OFFSET = 15;
    TRIM = 16;
    TYPEOF = 17;
    UNICODE_ = 18;
    UNLIKELY = 19;
    UPPER = 20;
    ZEROBLOB = 21;
  }
  required OneArgFnEnum fn_enum = 1;
  required Expr arg1 = 2;
}

message TwoArgFn {
  enum TwoArgFnEnum {
    GLOB = 1;
    IFNULL = 2;
    INSTR = 3;
    LIKE = 4;
    LIKELIHOOD = 5;
    LOAD_EXTENSION = 6;
    LTRIM = 7;
    NULLIF = 8;
    ROUND = 9;
    RTRIM = 10;
    SUBSTR = 11;
    TRIM = 12;
  }
  required TwoArgFnEnum fn_enum = 1;
  required Expr arg1 = 2;
  required Expr arg2 = 3;
}

message ThreeArgFn {
  enum ThreeArgFnEnum {
    LIKE = 0;
    REPLACE = 1;
    SUBSTR = 2;
  }
  required ThreeArgFnEnum fn_enum = 1;
  required Expr arg1 = 2;
  required Expr arg2 = 3;
  required Expr arg3 = 4;
}

// Fns with two args required + an arbitrary number of other args.
message VarNumFn {
  enum VarNumFnEnum {
    COALESCE = 1;
    MAX = 2;
    MIN = 3;
  }
  required VarNumFnEnum fn_enum = 1;
  required Expr arg1 = 2;
  required Expr arg2 = 3;
  repeated Expr other_args = 4;
}

message CharFn {
  required uint64 char = 1;
  repeated uint64 extra_chars = 2;
}

message Printf {
  repeated string strings = 1;
  repeated PrintfFormatSpecifier specifiers = 2;
  repeated Expr exprs = 3;
}

message PrintfFormatSpecifier {
  enum Flags {
    MINUS = 0;
    PLUS = 1;
    SPACE = 2;
    ZERO = 3;
    HASH = 4;
    COMMA = 5;
    BANG = 6;
  }
  repeated Flags flags = 1;
  optional uint32 precision = 2;
  optional uint32 width = 3;
  optional bool width_star = 4;
  // The length modifiers make no difference for the sqlite function.
  optional uint32 length = 5;
  enum SubType {
    I = 0;
    D = 1;
    U = 2;
    F = 3;
    E = 4;
    G = 5;
    X = 6;
    O = 7;
    S = 8;
    Z = 9;
    C = 10;
    P = 11;
    N = 12;
    Q = 13;
    W = 14;
  }
  optional bool percent = 8;
  required SubType sub_type = 6;
  required bool lowercase = 7;
}

// Window fns!
// FIXME in the future: this may only be in the result set and the ORDER_BY
// clause of a SELECT statement.
message WindowFnInvocation {
  oneof window_fn_oneof {
    ZeroArgWinFn zero_arg_fn = 1;
    OneArgWinFn one_arg_fn = 2;
    NthValueFn nth_value_fn = 4;
    MiscWinFn misc_fn = 3;
  }

  // Used if the above oneof produces zero values.
  required ZeroArgWinFn fallback_zero_arg_fn = 5;

  // FIXME in the future: may just replace with a WHERE expr.
  optional ExprFilter expr_filter = 6;
  optional WindowDefn win_defn = 7;
  required WindowName win_name = 8;  // used only if win_defn above is unused.
}

message WindowDefn {
  // FIXME in the future: change to specialized expr
  optional ExprList partition_exprs = 1;
  optional MultipleOrderingTerm ordering_terms = 2;
  optional ExprFrameSpec frame_spec = 3;
}

message ExprFrameSpec {
  enum RangeRows {
    RANGE = 0;
    ROWS = 1;
  }
  required RangeRows range_rows = 1;
  required FrameSpecSubExpr left_expr = 2;
  // if this exists, then this is a BETWEEN statement.
  optional FrameSpecSubExprRight right_expr = 3;
}

message FrameSpecSubExpr {
  enum Which {
    UNBOUNDED_PRECEDING = 0;
    EXPR_PRECEDING = 1;
    CURRENT_ROW = 2;
    EXPR_FOLLOWING = 3;
  }
  required Which which = 1;
  // only used if which is EXPR_PRECEDING or EXPR_FOLLOWING
  optional Expr expr = 2;  // CORPUS specialize? integer?
}

message FrameSpecSubExprRight {
  enum Which {
    UNBOUNDED_FOLLOWING = 0;
    EXPR_PRECEDING = 1;
    CURRENT_ROW = 2;
    EXPR_FOLLOWING = 3;
  }
  required Which which = 1;
  // only used if which is EXPR_PRECEDING or EXPR_FOLLOWING
  optional Expr expr = 2;  // CORPUS specialize? integer?
}

message MultipleOrderingTerm {
  repeated ExprOrderingTerm terms = 1;
}

message ExprOrderingTerm {
  required Expr expr = 1;  // CORPUS specialize???
  optional CollateType collate_type = 2;
  required AscDesc asc_desc = 3;
}

enum AscDesc {
  ASCDESC_NONE = 0;
  ASCDESC_ASC = 1;
  ASCDESC_DESC = 2;
}

message ExprFilter {
  required Expr expr = 1;
}

message ZeroArgWinFn {
  enum ZeroArgWinFnEnum {
    ROW_NUMBER = 0;
    RANK = 1;
    DENSE_RANK = 2;
    PERCENT_RANK = 3;
    CUME_DIST = 4;
  }
  required ZeroArgWinFnEnum win_fn = 1;
}

message OneArgWinFn {
  enum OneArgWinFnEnum {
    NTILE = 0;
    FIRST_VALUE = 1;
    LAST_VALUE = 2;
  }
  required OneArgWinFnEnum win_fn = 1;
  required Expr expr = 2;
}

message NthValueFn {
  required Expr expr = 1;
  required uint32 row_num = 2;
}

message MiscWinFn {
  enum MiscWinFnEnum {
    LAG = 0;
    LEAD = 1;
  }
  required MiscWinFnEnum fn = 1;
  required Expr expr = 2;
  optional uint32 offset = 3;
  optional Expr default = 4;  // unused if offset non-existent
}
// FIXME in the future: all aggregate functions may be used as window aggregate
// functions!

// Table-valued FNs, including pragma table-valued fns.
enum PragmaFnZeroArgOneResult {
  PFN_ZO_APPLICATION_ID = 0;
  PFN_ZO_AUTO_VACUUM = 1;
  PFN_ZO_AUTOMATIC_INDEX = 2;
  PFN_ZO_BUSY_TIMEOUT = 3;
  PFN_ZO_CACHE_SIZE = 4;
  PFN_ZO_CACHE_SPILL = 5;
  PFN_ZO_CELL_SIZE_CHECK = 6;
  PFN_ZO_CHECKPOINT_FULL_FSYNC = 7;
  PFN_ZO_COLLATION_LIST = 8;
  PFN_ZO_COMPILE_OPTIONS = 9;
  PFN_ZO_COUNT_CHANGES = 10;
  PFN_ZO_DATA_VERSION = 11;
  PFN_ZO_DATABASE_LIST = 12;
  PFN_ZO_DEFAULT_CACHE_SIZE = 13;
  PFN_ZO_DEFER_FOREIGN_KEYS = 14;
  PFN_ZO_EMPTY_RESULT_CALLBACKS = 15;
  PFN_ZO_ENCODING = 16;
  PFN_ZO_FOREIGN_KEY_CHECK = 17;
  PFN_ZO_FOREIGN_KEYS = 18;
  PFN_ZO_FREELIST_COUNT = 19;
  PFN_ZO_FULL_COLUMN_NAMES = 20;
  PFN_ZO_FULLFSYNC = 21;
  PFN_ZO_FUNCTION_LIST = 22;
  PFN_ZO_IGNORE_CHECK_CONSTRAINTS = 23;
  PFN_ZO_INTEGRITY_CHECK = 24;
  PFN_ZO_JOURNAL_SIZE_LIMIT = 25;
  PFN_ZO_LEGACY_ALTER_TABLE = 26;
  PFN_ZO_LEGACY_FILE_FORMAT = 27;
  PFN_ZO_LOCK_STATUS = 28;
  PFN_ZO_LOCKING_MODE = 29;
  PFN_ZO_MAX_PAGE_COUNT = 30;
  PFN_ZO_MODULE_LIST = 31;
  PFN_ZO_PAGE_COUNT = 32;
  PFN_ZO_PAGE_SIZE = 33;
  PFN_ZO_PRAGMA_LIST = 34;
  PFN_ZO_QUERY_ONLY = 35;
  PFN_ZO_QUICK_CHECK = 36;
  PFN_ZO_READ_UNCOMMITTED = 37;
  PFN_ZO_RECURSIVE_TRIGGERS = 38;
  PFN_ZO_REVERSE_UNORDERED_SELECTS = 39;
  PFN_ZO_SCHEMA_VERSION = 40;
  PFN_ZO_SECURE_DELETE = 41;
  PFN_ZO_SHORT_COLUMN_NAMES = 42;
  PFN_ZO_SOFT_HEAP_LIMIT = 43;
  PFN_ZO_SQL_TRACE = 44;
  PFN_ZO_STATS = 45;
  PFN_ZO_SYNCHRONOUS = 46;
  PFN_ZO_TEMP_STORE = 47;
  PFN_ZO_THREADS = 48;
  PFN_ZO_USER_VERSION = 49;
  // omit vdbe_* debug pragmas
  PFN_ZO_WRITEABLE_SCHEMA = 50;
}

message TableFn {
  required PragmaFnZeroArgOneResult no_arg_one_result = 10;
  oneof pragma_fn_oneof {
    Table foreign_key_list = 1;
    Index index_info = 2;
    Table index_list = 3;
    Index index_xinfo = 4;
    uint32 integrity_check = 5;
    uint32 optimize = 6;
    uint32 quick_check = 7;
    Table table_info = 8;
    Table table_xinfo = 9;
  }
}