File size: 94,074 Bytes
b247dc4
 
 
 
 
 
5a40a87
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b84806
9e806a7
 
 
 
 
 
 
0b84806
 
9e806a7
 
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ea5642
 
 
 
 
 
 
 
 
 
 
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ea5642
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8719af2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1da0a65
8719af2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b247dc4
 
 
 
 
 
1da0a65
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edbe15e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Rajkumar prompt formatter."""

from random import shuffle
from manifest import Manifest
from schema import Table
import re
import time


class RajkumarFormatter:
    """RajkumarFormatter class.

    From https://arxiv.org/pdf/2204.00498.pdf.
    """

    table_sep: str = "\n\n"
    shuffle_table_order: bool = True
    _cache: dict[tuple[str, str, str], list[str]] = {}
    clean_whitespace = False

    @classmethod
    def format_table(cls, table: Table) -> str:
        """Get table format."""
        table_fmt = []
        for col in table.columns or []:
            # This is technically an incorrect type, but it should be a catchall word
            table_fmt.append(f"    {col.name} {col.dtype or 'any'}")
        if table_fmt:
            all_cols = ",\n".join(table_fmt)
            create_tbl = f"CREATE TABLE {table.name} (\n{all_cols}\n)"
        else:
            create_tbl = f"CREATE TABLE {table.name}"
        return create_tbl

    @classmethod
    def format_all_tables(cls, tables: list[Table], instruction: str) -> list[str]:
        """Get all tables format."""
        table_texts = [cls.format_table(table) for table in tables]
        key = ("tables", instruction, str(tables))
        if key not in cls._cache:
            shuffle(table_texts)
            cls._cache[key] = table_texts
        else:
            table_texts = cls._cache[key]
        return table_texts

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n\n/*\nHere is additional documentation about DuckDB that could be useful.\n--------\n{context_str}\n--------\n*/"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        return f"""{table_text}\n\n\n-- Using valid DuckDB SQL, answer the following question for the tables provided above.{context_text}\n\n-- {instruction}\n"""  # noqa: E501

    @classmethod
    def format_model_output(cls, output_sql: str, prompt: str) -> str:
        print(output_sql)
        pattern = r"```(?:sql|mysql|duckdb)?\n?(.*?)```"
        match = re.search(pattern, output_sql, re.DOTALL)
        sql = match.group(1).strip() if match else output_sql.strip()
        
        # Handle edge case where regex captured empty content
        if not sql:
            sql = output_sql.strip()

        print(sql)
        # Ensure single trailing semicolon 
        return sql.rstrip(';') + ';'

    @classmethod
    def format_gold_output(cls, output_sql: str) -> str:
        """Format gold output for demonstration."""
        return output_sql

class MotherDuckFormatter(RajkumarFormatter):
    """MotherDuck class."""

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        return f"""{table_text}\n\n\n-- Using valid DuckDB SQL, answer the following question for the tables provided above.{context_text}\n\n-- {instruction}\n```sql\n"""  # noqa: E501


class DuckDBFormatter(RajkumarFormatter):
    """DuckDB class."""

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        return f"""{table_text}\n\n\n-- Using valid DuckDB SQL, answer the following question for the tables provided above.{context_text}\n\n-- {instruction}\n```sql\n"""  # noqa: E501


class DuckDBInstFormatter(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """### Instruction:
Your task is to generate valid duckdb SQL to answer the following question, given a duckdb database schema.

Here is the database schema that the SQL query will run on:
{schema}

### Question:
{question}

### Response (use duckdb shorthand if possible):
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction,
        )
        return instruction

class DuckDBInstFormatterLlamaShort(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.

Here are some DuckDB SQL syntax specifics you should be aware of:

- DuckDB uses double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity and single quotes (') to define string literals
- DuckDB can query CSV, Parquet, and JSON directly without loading them first, e.g. `SELECT * FROM 'data.csv';`
- DuckDB supports CREATE TABLE AS (CTAS): `CREATE TABLE new_table AS SELECT * FROM old_table;`
- DuckDB queries can start with FROM, and optionally omit SELECT *, e.g. `FROM my_table WHERE condition;` is equivalent to `SELECT * FROM my_table WHERE condition;`
- DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly, e.g. `SELECT 1 + 1 AS result;`
- DuckDB supports attaching multiple databases, unsing the ATTACH statement: `ATTACH 'my_database.duckdb' AS mydb;`. Tables within attached databases can be accessed using the dot notation (.), e.g. `SELECT * FROM mydb.table_name syntax`. The default databases doesn't require the do notation to access tables. The default database can be changed with the USE statement, e.g. `USE my_db;`.
- DuckDB is generally more lenient with implicit type conversions (e.g. `SELECT '42' + 1;` - Implicit cast, result is 43), but you can always be explicit using `::`, e.g. `SELECT '42'::INTEGER + 1;`
- DuckDB can extract parts of strings and lists using [start:end] or [start:end:step] syntax. Indexes start at 1. String slicing: `SELECT 'DuckDB'[1:4];`. Array/List slicing: `SELECT [1, 2, 3, 4][1:3];`
- DuckDB has a powerful way to select or transform multiple columns using patterns or functions. You can select columns matching a pattern: `SELECT COLUMNS('sales_.*') FROM sales_data;` or transform multiple columns with a function: `SELECT AVG(COLUMNS('sales_.*')) FROM sales_data;`
- DuckDB has an easy way to include/exclude or modify columns when selecting all: e.g. Exclude: `SELECT * EXCLUDE (sensitive_data) FROM users;` Replace: `SELECT * REPLACE (UPPER(name) AS name) FROM users;`
- DuckDB has a shorthand for grouping/ordering by all non-aggregated/all columns. e.g `SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;` and `SELECT * FROM my_table ORDER BY ALL;`
- DuckDB can combine tables by matching column names, not just their positions using UNION BY NAME. E.g. `SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;`
- DuckDB has an inutitive syntax to create List/Struct/Map and Array types. Create complex types using intuitive syntax. List: `SELECT [1, 2, 3] AS my_list;`, Struct: `{{'a': 1, 'b': 'text'}} AS my_struct;`, Map: `MAP([1,2],['one','two']) as my_map;`. All types can also be nested into each other. Array types are fixed size, while list types have variable size. Compared to Structs, MAPs do not need to have the same keys present for each row, but keys can only be of type Integer or Varchar. Example: `CREATE TABLE example (my_list INTEGER[], my_struct STRUCT(a INTEGER, b TEXT), my_map MAP(INTEGER, VARCHAR),  my_array INTEGER[3], my_nested_struct STRUCT(a INTEGER, b Integer[3]));`
- DuckDB has an inutive syntax to access struct fields using dot notation (.) or brackets ([]) with the field name. Maps fields can be accessed by brackets ([]).
- DuckDB's way of converting between text and timestamps, and extract date parts. Current date as 'YYYY-MM-DD': `SELECT strftime(NOW(), '%Y-%m-%d');` String to timestamp: `SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP;`, Extract Year from date: `SELECT EXTRACT(YEAR FROM DATE '2023-07-23');`
- Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses. E.g.: `SELECT a + b AS total FROM my_table WHERE total > 10 GROUP BY total HAVING total < 20;`
- DuckDB allows generating lists using expressions similar to Python list comprehensions. E.g. `SELECT [x*2 FOR x IN [1, 2, 3]];` Returns [2, 4, 6].
- DuckDB allows chaining multiple function calls together using the dot (.) operator. E.g.: `SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB';`
- DuckDB has a JSON data type. It supports selecting fields from the JSON with a JSON-Path expression using the arrow operator, -> (returns JSON) or ->> (returns text) with JSONPath expressions. For example: `SELECT data->'$.user.id' AS user_id, data->>'$.event_type' AS event_type FROM events;`
- DuckDB has built-in functions for regex regexp_matches(column, regex), regexp_replace(column, regex), and regexp_extract(column, regex).
- DuckDB has a way to quickly get a subset of your data with `SELECT * FROM large_table USING SAMPLE 10%;`<|eot_id|>
<|start_header_id|>user<|end_header_id|>

Database Schema:
Here is the schema of the DuckDB database that the SQL query will run on:
```sql
{schema}
```

Question:
Here is the question or an instruction the user provided:
{question}

Task:
Write a DuckDB SQL query for the given question!<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>

```sql
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterLlamaBasic(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.<|eot_id|>
<|start_header_id|>user<|end_header_id|>

Database Schema:
Here is the schema of the DuckDB database that the SQL query will run on:
```sql
{schema}
```

Question:
Here is the question or an instruction the user provided:
{question}

Task:
Write a DuckDB SQL query for the given question!<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>

```sql
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterLlamaSyntax(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.

Here are some DuckDB SQL syntax specifics you should be aware of:

- DuckDB uses double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity and single quotes (') to define string literals
- DuckDB can query CSV, Parquet, and JSON directly without loading them first, e.g. `SELECT * FROM 'data.csv';`
- DuckDB supports CREATE TABLE AS (CTAS): `CREATE TABLE new_table AS SELECT * FROM old_table;`
- DuckDB queries can start with FROM, and optionally omit SELECT *, e.g. `FROM my_table WHERE condition;` is equivalent to `SELECT * FROM my_table WHERE condition;`
- DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly, e.g. `SELECT 1 + 1 AS result;`
- DuckDB supports attaching multiple databases, unsing the ATTACH statement: `ATTACH 'my_database.duckdb' AS mydb;`. Tables within attached databases can be accessed using the dot notation (.), e.g. `SELECT * FROM mydb.table_name syntax`. The default databases doesn't require the do notation to access tables. The default database can be changed with the USE statement, e.g. `USE my_db;`.
- DuckDB is generally more lenient with implicit type conversions (e.g. `SELECT '42' + 1;` - Implicit cast, result is 43), but you can always be explicit using `::`, e.g. `SELECT '42'::INTEGER + 1;`
- DuckDB can extract parts of strings and lists using [start:end] or [start:end:step] syntax. Indexes start at 1. String slicing: `SELECT 'DuckDB'[1:4];`. Array/List slicing: `SELECT [1, 2, 3, 4][1:3];`
- DuckDB has a powerful way to select or transform multiple columns using patterns or functions. You can select columns matching a pattern: `SELECT COLUMNS('sales_.*') FROM sales_data;` or transform multiple columns with a function: `SELECT AVG(COLUMNS('sales_.*')) FROM sales_data;`
- DuckDB has an easy way to include/exclude or modify columns when selecting all: e.g. Exclude: `SELECT * EXCLUDE (sensitive_data) FROM users;` Replace: `SELECT * REPLACE (UPPER(name) AS name) FROM users;`
- DuckDB has a shorthand for grouping/ordering by all non-aggregated/all columns. e.g `SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;` and `SELECT * FROM my_table ORDER BY ALL;`
- DuckDB can combine tables by matching column names, not just their positions using UNION BY NAME. E.g. `SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;`
- DuckDB has an inutitive syntax to create List/Struct/Map and Array types. Create complex types using intuitive syntax. List: `SELECT [1, 2, 3] AS my_list;`, Struct: `{{'a': 1, 'b': 'text'}} AS my_struct;`, Map: `MAP([1,2],['one','two']) as my_map;`. All types can also be nested into each other. Array types are fixed size, while list types have variable size. Compared to Structs, MAPs do not need to have the same keys present for each row, but keys can only be of type Integer or Varchar. Example: `CREATE TABLE example (my_list INTEGER[], my_struct STRUCT(a INTEGER, b TEXT), my_map MAP(INTEGER, VARCHAR),  my_array INTEGER[3], my_nested_struct STRUCT(a INTEGER, b Integer[3]));`
- DuckDB has an inutive syntax to access struct fields using dot notation (.) or brackets ([]) with the field name. Maps fields can be accessed by brackets ([]).
- DuckDB's way of converting between text and timestamps, and extract date parts. Current date as 'YYYY-MM-DD': `SELECT strftime(NOW(), '%Y-%m-%d');` String to timestamp: `SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP;`, Extract Year from date: `SELECT EXTRACT(YEAR FROM DATE '2023-07-23');`
- Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses. E.g.: `SELECT a + b AS total FROM my_table WHERE total > 10 GROUP BY total HAVING total < 20;`
- DuckDB allows generating lists using expressions similar to Python list comprehensions. E.g. `SELECT [x*2 FOR x IN [1, 2, 3]];` Returns [2, 4, 6].
- DuckDB allows chaining multiple function calls together using the dot (.) operator. E.g.: `SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB';`
- DuckDB has a JSON data type. It supports selecting fields from the JSON with a JSON-Path expression using the arrow operator, -> (returns JSON) or ->> (returns text) with JSONPath expressions. For example: `SELECT data->'$.user.id' AS user_id, data->>'$.event_type' AS event_type FROM events;`
- DuckDB has built-in functions for regex regexp_matches(column, regex), regexp_replace(column, regex), and regexp_extract(column, regex).
- DuckDB has a way to quickly get a subset of your data with `SELECT * FROM large_table USING SAMPLE 10%;`<|eot_id|>
<|start_header_id|>user<|end_header_id|>

Database Schema:
Here is the schema of the DuckDB database that the SQL query will run on:
```sql
{schema}
```

Question:
Here is the question or an instruction the user provided:
{question}

Task:
Write a DuckDB SQL query for the given question!<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>

```sql
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterGraniteShort(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """System:
Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.

Here are some DuckDB SQL syntax specifics you should be aware of:


- DuckDB use double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity and single quotes (') to define string literals
- DuckDB can query CSV, Parquet, and JSON directly without loading them first, e.g. `SELECT * FROM 'data.csv';`
- DuckDB supports CREATE TABLE AS (CTAS): `CREATE TABLE new_table AS SELECT * FROM old_table;`
- DuckDB queries can start with FROM, and optionally omit SELECT *, e.g. `FROM my_table WHERE condition;` is equivalent to `SELECT * FROM my_table WHERE condition;`
- DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly, e.g. `SELECT 1 + 1 AS result;`
- DuckDB supports attaching multiple databases, unsing the ATTACH statement: `ATTACH 'my_database.duckdb' AS mydb;`. Tables within attached databases can be accessed using the dot notation (.), e.g. `SELECT * FROM mydb.table_name syntax`. The default databases doesn't require the do notation to access tables. The default database can be changed with the USE statement, e.g. `USE my_db;`.
- DuckDB is generally more lenient with implicit type conversions (e.g. `SELECT '42' + 1;` - Implicit cast, result is 43), but you can always be explicit using `::`, e.g. `SELECT '42'::INTEGER + 1;`
- DuckDB can extract parts of strings and lists using [start:end] or [start:end:step] syntax. Indexes start at 1. String slicing: `SELECT 'DuckDB'[1:4];`. Array/List slicing: `SELECT [1, 2, 3, 4][1:3];`
- DuckDB has a powerful way to select or transform multiple columns using patterns or functions. You can select columns matching a pattern: `SELECT COLUMNS('sales_.*') FROM sales_data;` or transform multiple columns with a function: `SELECT AVG(COLUMNS('sales_.*')) FROM sales_data;`
- DuckDB an easy way to include/exclude or modify columns when selecting all: e.g. Exclude: `SELECT * EXCLUDE (sensitive_data) FROM users;` Replace: `SELECT * REPLACE (UPPER(name) AS name) FROM users;`
- DuckDB has a shorthand for grouping/ordering by all non-aggregated/all columns. e.g `SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;` and `SELECT * FROM my_table ORDER BY ALL;`
- DuckDB can combine tables by matching column names, not just their positions using UNION BY NAME. E.g. `SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;`
- DuckDB has an inutitive syntax to create List/Struct/Map and Array types. Create complex types using intuitive syntax. List: `SELECT [1, 2, 3] AS my_list;`, Struct: `{{'a': 1, 'b': 'text'}} AS my_struct;`, Map: `MAP([1,2],['one','two']) as my_map;`. All types can also be nested into each other. Array types are fixed size, while list types have variable size. Compared to Structs, MAPs do not need to have the same keys present for each row, but keys can only be of type Integer or Varchar. Example: `CREATE TABLE example (my_list INTEGER[], my_struct STRUCT(a INTEGER, b TEXT), my_map MAP(INTEGER, VARCHAR),  my_array INTEGER[3], my_nested_struct STRUCT(a INTEGER, b Integer[3]));`
- DuckDB has an inutive syntax to access struct fields using dot notation (.) or brackets ([]) with the field name. Maps fields can be accessed by brackets ([]).
- DuckDB's way of converting between text and timestamps, and extract date parts. Current date as 'YYYY-MM-DD': `SELECT strftime(NOW(), '%Y-%m-%d');` String to timestamp: `SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP;`, Extract Year from date: `SELECT EXTRACT(YEAR FROM DATE '2023-07-23');`
- Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses. E.g.: `SELECT a + b AS total FROM my_table WHERE total > 10 GROUP BY total HAVING total < 20;`
- DuckDB allows generating lists using expressions similar to Python list comprehensions. E.g. `SELECT [x*2 FOR x IN [1, 2, 3]];` Returns [2, 4, 6].
- DuckDB allows chaining multiple function calls together using the dot (.) operator. E.g.: `SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB';`
- DuckDB has a JSON data type. It supports selecting fields from the JSON with a JSON-Path expression using the arrow operator, -> (returns JSON) or ->> (returns text) with JSONPath expressions. For example: `SELECT data->'$.user.id' AS user_id, data->>'$.event_type' AS event_type FROM events;`
- DuckDB has built-in functions for regex regexp_matches(column, regex), regexp_replace(column, regex), and regexp_extract(column, regex).
- DuckDB has a way to quickly get a subset of your data with `SELECT * FROM large_table USING SAMPLE 10%;`

Here is the schema of the DuckDB database that the SQL query will run on:
{schema}

Question:
Here is the question or an instruction the user provided:
{question}

Write a DuckDB SQL query for the given question!

Answer:
```
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterGraniteBench(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """System:
Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.

Here are some DuckDB SQL syntax specifics you should be aware of:

- DuckDB use double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity and single quotes (') to define string literals
- DuckDB can query CSV, Parquet, and JSON directly without loading them first, e.g. `SELECT * FROM 'data.csv';`
- DuckDB supports CREATE TABLE AS (CTAS): `CREATE TABLE new_table AS SELECT * FROM old_table;`
- DuckDB queries can start with FROM, and optionally omit SELECT *, e.g. `FROM my_table WHERE condition;` is equivalent to `SELECT * FROM my_table WHERE condition;`
- DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly, e.g. `SELECT 1 + 1 AS result;`
- DuckDB supports attaching multiple databases, unsing the ATTACH statement: `ATTACH 'my_database.duckdb' AS mydb;`. Tables within attached databases can be accessed using the dot notation (.), e.g. `SELECT * FROM mydb.table_name syntax`. The default databases doesn't require the do notation to access tables. The default database can be changed with the USE statement, e.g. `USE my_db;`.
- DuckDB is generally more lenient with implicit type conversions (e.g. `SELECT '42' + 1;` - Implicit cast, result is 43), but you can always be explicit using `::`, e.g. `SELECT '42'::INTEGER + 1;`
- DuckDB can extract parts of strings and lists using [start:end] or [start:end:step] syntax. Indexes start at 1. String slicing: `SELECT 'DuckDB'[1:4];`. Array/List slicing: `SELECT [1, 2, 3, 4][1:3];`
- DuckDB has a powerful way to select or transform multiple columns using patterns or functions. You can select columns matching a pattern: `SELECT COLUMNS('sales_.*') FROM sales_data;` or transform multiple columns with a function: `SELECT AVG(COLUMNS('sales_.*')) FROM sales_data;`
- DuckDB an easy way to include/exclude or modify columns when selecting all: e.g. Exclude: `SELECT * EXCLUDE (sensitive_data) FROM users;` Replace: `SELECT * REPLACE (UPPER(name) AS name) FROM users;`
- DuckDB has a shorthand for grouping/ordering by all non-aggregated/all columns. e.g `SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;` and `SELECT * FROM my_table ORDER BY ALL;`
- DuckDB can combine tables by matching column names, not just their positions using UNION BY NAME. E.g. `SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;`
- DuckDB has an inutitive syntax to create List/Struct/Map and Array types. Create complex types using intuitive syntax. List: `SELECT [1, 2, 3] AS my_list;`, Struct: `{{'a': 1, 'b': 'text'}} AS my_struct;`, Map: `MAP([1,2],['one','two']) as my_map;`. All types can also be nested into each other. Array types are fixed size, while list types have variable size. Compared to Structs, MAPs do not need to have the same keys present for each row, but keys can only be of type Integer or Varchar. Example: `CREATE TABLE example (my_list INTEGER[], my_struct STRUCT(a INTEGER, b TEXT), my_map MAP(INTEGER, VARCHAR),  my_array INTEGER[3], my_nested_struct STRUCT(a INTEGER, b Integer[3]));`
- DuckDB has an inutive syntax to access struct fields using dot notation (.) or brackets ([]) with the field name. Maps fields can be accessed by brackets ([]).
- DuckDB's way of converting between text and timestamps, and extract date parts. Current date as 'YYYY-MM-DD': `SELECT strftime(NOW(), '%Y-%m-%d');` String to timestamp: `SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP;`, Extract Year from date: `SELECT EXTRACT(YEAR FROM DATE '2023-07-23');`
- Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses. E.g.: `SELECT a + b AS total FROM my_table WHERE total > 10 GROUP BY total HAVING total < 20;`
- DuckDB allows generating lists using expressions similar to Python list comprehensions. E.g. `SELECT [x*2 FOR x IN [1, 2, 3]];` Returns [2, 4, 6].
- DuckDB allows chaining multiple function calls together using the dot (.) operator. E.g.: `SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB';`
- DuckDB has a JSON data type. It supports selecting fields from the JSON with a JSON-Path expression using the arrow operator, -> (returns JSON) or ->> (returns text) with JSONPath expressions. For example: `SELECT data->'$.user.id' AS user_id, data->>'$.event_type' AS event_type FROM events;`
- DuckDB has built-in functions for regex regexp_matches(column, regex), regexp_replace(column, regex), and regexp_extract(column, regex).
- DuckDB has a way to quickly get a subset of your data with `SELECT * FROM large_table USING SAMPLE 10%;`

DuckDB Functions:
`count`: Calculates the total number of rows returned by a SQL query result. This function is commonly used to determine the row count of a SELECT operation., Parameters: ['result: The result object']
`sum`: Calculates the total of all non-null values in a specified column or expression across rows., Parameters: ['arg: Values to be aggregated']
`max`: Returns the largest value from all values in a specified column or expression., Parameters: ['arg: expression to evaluate maximum', "n: top 'n' value list size(optional)"]
`min`: Finds the smallest value in a group of input values., Parameters: ['expression: The input value to consider']
`avg`: Calculates the average of non-null values., Parameters: ['arg: Data to be averaged']
`read_csv_auto`: Automatically reads a CSV file and infers the data types of its columns., Parameters: ['file_path: Path to the CSV file', 'MD_RUN: Execution control parameter(optional)']
`replace`: Replacement scans in DuckDB allow users to register a callback that gets triggered when a query references a non-existent table. The callback can replace this table with a custom table function, effectively 'replacing' the non-existent table in the query execution process., Parameters: ['db: Database object where replacement applies', 'replacement: Handler for when table is missing', 'extra_data: Extra data given to callback(optional)', 'delete_callback: Cleanup for extra data provided(optional)']
`length`: Returns the length of a string, Parameters: ['value: String to measure length of']
`read_json_auto`: Automatically infers the schema from JSON data and reads it into a table format., Parameters: ['filename: Path to the JSON file.', 'compression: File compression type.(optional)', 'auto_detect: Auto-detect key names/types.(optional)', 'columns: Manual specification of keys/types.(optional)', 'dateformat: Date format for parsing dates.(optional)', 'format: JSON file format.(optional)', 'hive_partitioning: Hive partitioned path interpretation.(optional)', 'ignore_errors: Ignore parse errors option.(optional)', 'maximum_depth: Max depth for schema detection.(optional)', 'maximum_object_size: Max size of JSON object.(optional)', 'records: JSON record unpacking option.(optional)', 'sample_size: Number of objects for sampling.(optional)', 'timestampformat: Timestamp parsing format.(optional)', 'union_by_name: Unify schemas of files.(optional)']
`regexp_extract`: If a string matches a given regular expression pattern, it returns the specified capturing group or groups with optional capture group names., Parameters: ['string: Input string to search in.', 'pattern: Regex pattern to match.', 'group: Specifies which group to capture.(optional)', 'name_list: Named capture groups struct.(optional)', 'options: Regex matching options.(optional)']
`upper`: Converts a given string to uppercase characters., Parameters: ['string: String to make uppercase']
`substring`: Extracts a substring from a given string starting at a specified position and with a specified length., Parameters: ['string: The original string to extract from', 'start: Starting position for extraction', 'length: Number of characters to extract']
`datediff`: Calculates the number of specified partition boundaries between two dates., Parameters: ['part: Time unit to measure', 'startdate: The starting date', 'enddate: The ending date']
`split_part`: Splits a string by a specified separator and returns the part at a given index., Parameters: ['string: The string to be split', 'separator: The delimiter to split by', 'index: 1-based index to retrieve']
`position`: Locates the position of the first occurrence of "search_string" after position 1 in the provided "string". It returns 0 if "search_string" is not found., Parameters: ['search_string: The substring to find.', 'string: The string to search in.']
`pragma_version`: Retrieves the current version of DuckDB., Parameters: []
`ascii`: Returns the Unicode code point of the first character of a given string., Parameters: ['string: Input string for conversion.']

DuckDB Statements:
`FROM`: The FROM clause specifies the source of the data for the query. It can include a single table, multiple joined tables, or subqueries. The JOIN clause is used to combine rows from two or more tables based on a related column between them. There are several types of joins, including INNER, OUTER, CROSS, NATURAL, SEMI, ANTI, LATERAL, POSITIONAL, ASOF, and self-joins., Examples: ['SELECT * FROM table_name;', 'FROM table_name SELECT *;', 'FROM table_name;', 'SELECT tn.* FROM table_name tn;', 'SELECT * FROM schema_name.table_name;', 'SELECT t.i FROM range(100) AS t(i);', "SELECT * FROM 'test.csv';", 'SELECT * FROM (SELECT * FROM table_name);', 'SELECT t FROM t;', "SELECT t FROM (SELECT unnest(generate_series(41, 43)) AS x, 'hello' AS y) t;", 'SELECT * FROM table_name JOIN other_table ON table_name.key = other_table.key;', 'SELECT * FROM table_name TABLESAMPLE 10%;', 'SELECT * FROM table_name TABLESAMPLE 10 ROWS;', 'FROM range(100) AS t(i) SELECT sum(t.i) WHERE i % 2 = 0;', 'SELECT a.*, b.* FROM a CROSS JOIN b;', 'SELECT a.*, b.* FROM a, b;', 'SELECT n.*, r.* FROM l_nations n JOIN l_regions r ON (n_regionkey = r_regionkey);', 'SELECT * FROM city_airport NATURAL JOIN airport_names;', 'SELECT * FROM city_airport JOIN airport_names USING (iata);', 'SELECT * FROM city_airport SEMI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata IN (SELECT iata FROM airport_names);', 'SELECT * FROM city_airport ANTI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata NOT IN (SELECT iata FROM airport_names WHERE iata IS NOT NULL);', 'SELECT * FROM range(3) t(i), LATERAL (SELECT i + 1) t2(j);', 'SELECT * FROM generate_series(0, 1) t(i), LATERAL (SELECT i + 10 UNION ALL SELECT i + 100) t2(j);', 'SELECT * FROM trades t ASOF JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF LEFT JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF JOIN prices p USING (symbol, "when");', 'SELECT t.symbol, t.when AS trade_when, p.when AS price_when, price FROM trades t ASOF LEFT JOIN prices p USING (symbol, "when");', 'SELECT * FROM t AS t t1 JOIN t t2 USING(x);', 'FROM tbl SELECT i, s;', 'FROM tbl;']
`SELECT`: The SELECT statement retrieves rows from the database. It is used to query the database and retrieve data according to specific requirements. The statement can include several clauses, such as FROM, WHERE, GROUP BY, ORDER BY, and LIMIT, to filter, organize, and limit the query results., Examples: ['SELECT * FROM tbl;', 'SELECT j FROM tbl WHERE i = 3;', 'SELECT i, sum(j) FROM tbl GROUP BY i;', 'SELECT * FROM tbl ORDER BY i DESC LIMIT 3;', 'SELECT * FROM t1 JOIN t2 USING (a, b);', 'SELECT #1, #3 FROM tbl;', 'SELECT DISTINCT city FROM addresses;', 'SELECT d FROM (SELECT 1 AS a, 2 AS b) d;', 'SELECT rowid, id, content FROM t;']
`WHERE`: The WHERE clause specifies filters to apply to the data being queried, allowing selection of a specific subset of data. It is logically applied immediately after the FROM clause in a SQL query., Examples: ['SELECT * FROM table_name WHERE id = 3;', "SELECT * FROM table_name WHERE name ILIKE '%mark%';", 'SELECT * FROM table_name WHERE id = 3 OR id = 7;']
`ORDER BY`: The ORDER BY clause is an output modifier used to sort the rows in a query result set according to specified sorting criteria. It allows sorting in either ascending or descending order, and can also specify the position of NULL values (either at the beginning or end). The clause can contain multiple expressions that determine the sort order, and supports the sorting of columns by name, column position number, or the ALL keyword, which sorts by all columns in left-to-right order., Examples: ['SELECT * FROM addresses ORDER BY city;', 'SELECT * FROM addresses ORDER BY city DESC NULLS LAST;', 'SELECT * FROM addresses ORDER BY city, zip;', 'SELECT * FROM addresses ORDER BY city COLLATE DE;', 'SELECT * FROM addresses ORDER BY ALL;', 'SELECT * FROM addresses ORDER BY ALL DESC;']
`GROUP BY`: The `GROUP BY` clause is used to specify which columns should be used for grouping when performing aggregations in a `SELECT` statement. It aggregates data based on matching data in the specified columns, allowing other columns to be combined using aggregate functions. The query becomes an aggregate query if a `GROUP BY` clause is specified, even if no aggregates are present in the `SELECT` clause., Examples: ['SELECT city, count(*) FROM addresses GROUP BY city;', 'SELECT city, street_name, avg(income) FROM addresses GROUP BY city, street_name;', 'SELECT city, street_name FROM addresses GROUP BY ALL;', 'SELECT city, street_name, avg(income) FROM addresses GROUP BY ALL;']
`JOIN`: The FROM clause specifies the source of the data for the query. It can include a single table, multiple joined tables, or subqueries. The JOIN clause is used to combine rows from two or more tables based on a related column between them. There are several types of joins, including INNER, OUTER, CROSS, NATURAL, SEMI, ANTI, LATERAL, POSITIONAL, ASOF, and self-joins., Examples: ['SELECT * FROM table_name;', 'FROM table_name SELECT *;', 'FROM table_name;', 'SELECT tn.* FROM table_name tn;', 'SELECT * FROM schema_name.table_name;', 'SELECT t.i FROM range(100) AS t(i);', "SELECT * FROM 'test.csv';", 'SELECT * FROM (SELECT * FROM table_name);', 'SELECT t FROM t;', "SELECT t FROM (SELECT unnest(generate_series(41, 43)) AS x, 'hello' AS y) t;", 'SELECT * FROM table_name JOIN other_table ON table_name.key = other_table.key;', 'SELECT * FROM table_name TABLESAMPLE 10%;', 'SELECT * FROM table_name TABLESAMPLE 10 ROWS;', 'FROM range(100) AS t(i) SELECT sum(t.i) WHERE i % 2 = 0;', 'SELECT a.*, b.* FROM a CROSS JOIN b;', 'SELECT a.*, b.* FROM a, b;', 'SELECT n.*, r.* FROM l_nations n JOIN l_regions r ON (n_regionkey = r_regionkey);', 'SELECT * FROM city_airport NATURAL JOIN airport_names;', 'SELECT * FROM city_airport JOIN airport_names USING (iata);', 'SELECT * FROM city_airport SEMI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata IN (SELECT iata FROM airport_names);', 'SELECT * FROM city_airport ANTI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata NOT IN (SELECT iata FROM airport_names WHERE iata IS NOT NULL);', 'SELECT * FROM range(3) t(i), LATERAL (SELECT i + 1) t2(j);', 'SELECT * FROM generate_series(0, 1) t(i), LATERAL (SELECT i + 10 UNION ALL SELECT i + 100) t2(j);', 'SELECT * FROM trades t ASOF JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF LEFT JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF JOIN prices p USING (symbol, "when");', 'SELECT t.symbol, t.when AS trade_when, p.when AS price_when, price FROM trades t ASOF LEFT JOIN prices p USING (symbol, "when");', 'SELECT * FROM t AS t t1 JOIN t t2 USING(x);', 'FROM tbl SELECT i, s;', 'FROM tbl;']
`WITH`: The WITH clause in SQL is used to define common table expressions (CTEs), which are temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs simplify complex queries by breaking them into more manageable parts, and they can be recursive, allowing them to reference themselves. The WITH clause can include multiple CTEs, and DuckDB supports specifying whether a CTE should be materialized explicitly or not., Examples: ['WITH cte AS (SELECT 42 AS x) SELECT * FROM cte;', 'WITH cte1 AS (SELECT 42 AS i), cte2 AS (SELECT i * 100 AS x FROM cte1) SELECT * FROM cte2;', 'WITH t(x) AS (⟨complex_query⟩) SELECT * FROM t AS t1, t AS t2, t AS t3;', 'WITH t(x) AS MATERIALIZED (⟨complex_query⟩) SELECT * FROM t AS t1, t AS t2, t AS t3;', 'WITH RECURSIVE FibonacciNumbers (RecursionDepth, FibonacciNumber, NextNumber) AS (SELECT 0 AS RecursionDepth, 0 AS FibonacciNumber, 1 AS NextNumber UNION ALL SELECT fib.RecursionDepth + 1 AS RecursionDepth, fib.NextNumber AS FibonacciNumber, fib.FibonacciNumber + fib.NextNumber AS NextNumber FROM FibonacciNumbers fib WHERE fib.RecursionDepth + 1 < 10) SELECT fn.RecursionDepth AS FibonacciNumberIndex, fn.FibonacciNumber FROM FibonacciNumbers fn;']
`LIMIT`: The LIMIT clause restricts the number of rows returned by a query. The OFFSET clause specifies the starting point within the result set from which to begin returning rows. LIMIT is commonly used to return a specified number of rows from a result set, while OFFSET is used to skip a specified number of rows before beginning to return rows., Examples: ['SELECT * FROM addresses LIMIT 5;', 'SELECT * FROM addresses LIMIT 5 OFFSET 5;', 'SELECT city, count(*) AS population FROM addresses GROUP BY city ORDER BY population DESC LIMIT 5;']
`CREATE TABLE`: The `CREATE TABLE` statement is used to create a new table in the catalog. It allows for the definition of columns, data types, constraints, and primary keys. Additionally, it supports features like creating temporary tables, using `CREATE TABLE ... AS SELECT` for replicating schemas or importing data from CSV files, incorporating `OR REPLACE` to overwrite existing tables, using `IF NOT EXISTS` to conditionally create tables, and defining check and foreign key constraints., Examples: ['CREATE TABLE t1 (i INTEGER, j INTEGER);', 'CREATE TABLE t1 (id INTEGER PRIMARY KEY, j VARCHAR);', 'CREATE TABLE t1 (id INTEGER, j VARCHAR, PRIMARY KEY (id, j));', 'CREATE TABLE t1 (\n    i INTEGER NOT NULL,\n    decimalnr DOUBLE CHECK (decimalnr < 10),\n    date DATE UNIQUE,\n    time TIMESTAMP\n);', 'CREATE TABLE t1 AS SELECT 42 AS i, 84 AS j;', "CREATE TEMP TABLE t1 AS SELECT * FROM read_csv('path/file.csv');", 'CREATE OR REPLACE TABLE t1 (i INTEGER, j INTEGER);', 'CREATE TABLE IF NOT EXISTS t1 (i INTEGER, j INTEGER);', 'CREATE TABLE nums AS SELECT i FROM range(0, 3) t(i);', 'CREATE TABLE t1 (id INTEGER PRIMARY KEY, percentage INTEGER CHECK (0 <= percentage AND percentage <= 100));', 'CREATE TABLE t1 (id INTEGER PRIMARY KEY, j VARCHAR);\nCREATE TABLE t2 (\n    id INTEGER PRIMARY KEY,\n    t1_id INTEGER,\n    FOREIGN KEY (t1_id) REFERENCES t1 (id)\n);', 'CREATE TABLE t1 (x FLOAT, two_x AS (2 * x));']
`SET`: The SET statement modifies a DuckDB configuration option at the specified scope, while the RESET statement changes the option to its default value. The scope can be GLOBAL, SESSION, or LOCAL (not yet implemented)., Examples: ["SET memory_limit = '10GB';", 'SET threads = 1;', 'SET threads TO 1;', 'RESET threads;', "SELECT current_setting('threads');", "SET GLOBAL search_path = 'db1,db2'", "SET SESSION default_collation = 'nocase';"]
`DROP`: The `DROP` statement in DuckDB is used to remove a catalog entry that was previously added with the `CREATE` command. It can drop various types of objects such as tables, views, functions, indexes, schemas, sequences, macros, and types. It also has options like `IF EXISTS` to prevent errors if the object does not exist and `CASCADE` to also drop all dependent objects., Examples: ['DROP TABLE tbl;', 'DROP VIEW IF EXISTS v1;', 'DROP FUNCTION fn;', 'DROP INDEX idx;', 'DROP SCHEMA sch;', 'DROP SEQUENCE seq;', 'DROP MACRO mcr;', 'DROP MACRO TABLE mt;', 'DROP TYPE typ;', 'DROP SCHEMA myschema CASCADE;']
`ALTER TABLE`: The `ALTER TABLE` statement is used to modify the schema of an existing table in the catalog. This includes adding, dropping, or modifying columns, renaming tables and columns, and setting or dropping default values and not null constraints. Changes made with `ALTER TABLE` are transactional, meaning they are not visible to other transactions until committed and can be rolled back., Examples: ['ALTER TABLE integers ADD COLUMN k INTEGER;', 'ALTER TABLE integers ADD COLUMN l INTEGER DEFAULT 10;', 'ALTER TABLE integers DROP k;', 'ALTER TABLE integers ALTER i TYPE VARCHAR;', "ALTER TABLE integers ALTER i SET DATA TYPE VARCHAR USING concat(i, '_', j);", 'ALTER TABLE integers ALTER COLUMN i SET DEFAULT 10;', 'ALTER TABLE integers ALTER COLUMN i DROP DEFAULT;', 'ALTER TABLE t ALTER COLUMN x SET NOT NULL;', 'ALTER TABLE t ALTER COLUMN x DROP NOT NULL;', 'ALTER TABLE integers RENAME TO integers_old;', 'ALTER TABLE integers RENAME i TO j;']
`HAVING`: The HAVING clause is used after the GROUP BY clause in SQL to filter the grouped results. It performs filtering based on aggregate functions and conditions imposed on the grouped data. Unlike the WHERE clause, which filters rows before grouping, the HAVING clause filters after the grouping has been completed., Examples: ['SELECT city, count(*) FROM addresses GROUP BY city HAVING count(*) >= 50;', 'SELECT city, street_name, avg(income) FROM addresses GROUP BY city, street_name HAVING avg(income) > 2 * median(income);']
`UPDATE`: The UPDATE statement modifies the values of rows in a table. It allows updating specific columns for rows that meet certain conditions, retaining previous values for unspecified columns. The statement can use data from other tables or the same table to determine the new values, using joins or subqueries., Examples: ['UPDATE tbl SET i = 0 WHERE i IS NULL;', 'UPDATE tbl SET i = 1, j = 2;', 'UPDATE original SET value = new.value FROM new WHERE original.key = new.key;', 'UPDATE original SET value = (SELECT new.value FROM new WHERE original.key = new.key);', "UPDATE original AS true_original SET value = (SELECT new.value || ' a change!' AS value FROM original AS new WHERE true_original.key = new.key);", "UPDATE city SET revenue = revenue + 100 FROM country WHERE city.country_code = country.code AND country.name = 'France';"]
`USE`: The `USE` statement selects a database and optional schema to use as the default for future operations, such as when creating tables without a fully qualified name., Examples: ['USE memory;', 'USE duck.main;']
`INSERT`: The INSERT statement is used to insert new data into a table in DuckDB. It can insert specific values, results from a query, handle conflicts with ON CONFLICT clauses, and return inserted rows using the RETURNING clause., Examples: ['INSERT INTO tbl VALUES (1), (2), (3);', 'INSERT INTO tbl SELECT * FROM other_tbl;', 'INSERT INTO tbl (i) VALUES (1), (2), (3);', 'INSERT INTO tbl (i) VALUES (1), (DEFAULT), (3);', 'INSERT OR IGNORE INTO tbl (i) VALUES (1);', 'INSERT OR REPLACE INTO tbl (i) VALUES (1);', 'INSERT INTO tbl BY POSITION VALUES (5, 42);', 'INSERT INTO tbl BY NAME (SELECT 42 AS b, 32 AS a);', 'INSERT INTO tbl VALUES (1, 84) ON CONFLICT DO NOTHING;', 'INSERT INTO tbl VALUES (1, 84) ON CONFLICT DO UPDATE SET j = EXCLUDED.j;', 'INSERT INTO tbl (j, i) VALUES (168, 1) ON CONFLICT DO UPDATE SET j = EXCLUDED.j;', 'INSERT INTO tbl BY NAME (SELECT 84 AS j, 1 AS i) ON CONFLICT DO UPDATE SET j = EXCLUDED.j;', 'INSERT INTO t1 SELECT 42 RETURNING *;', 'INSERT INTO t2 SELECT 2 AS i, 3 AS j RETURNING *, i * j AS i_times_j;', "CREATE TABLE t3 (i INTEGER PRIMARY KEY, j INTEGER); CREATE SEQUENCE 't3_key'; INSERT INTO t3 SELECT nextval('t3_key') AS i, 42 AS j UNION ALL SELECT nextval('t3_key') AS i, 43 AS j RETURNING *;"]
`COPY`: The COPY statement in DuckDB is used to transfer data between DuckDB tables and external files. It supports various file formats such as CSV, Parquet, and JSON, and can either import data from these files into a DuckDB table or export data from a DuckDB table to these files. The statement is versatile, allowing customization of options to handle different data formats, delimiters, headers, and more, making it useful for bulk data operations., Examples: ["COPY lineitem FROM 'lineitem.csv';", "COPY lineitem FROM 'lineitem.csv' (DELIMITER '|');", "COPY lineitem FROM 'lineitem.pq' (FORMAT PARQUET);", "COPY lineitem FROM 'lineitem.json' (FORMAT JSON, AUTO_DETECT true);", "COPY lineitem TO 'lineitem.csv' (FORMAT CSV, DELIMITER '|', HEADER);", "COPY (SELECT l_orderkey, l_partkey FROM lineitem) TO 'lineitem.parquet' (COMPRESSION ZSTD);", 'COPY FROM DATABASE db1 TO db2;', 'COPY FROM DATABASE db1 TO db2 (SCHEMA);']
`ATTACH`: The ATTACH statement in DuckDB is used to add a new database file to the catalog, allowing the database to be read from and written to. It supports file paths as well as HTTP and S3 endpoints. Detached databases need to be re-attached in new sessions. The DETACH statement is used to close and detach such databases, thereby releasing any locks held on the database file. ATTACH and DETACH allow for operating on multiple database files simultaneously, enabling data transfer across different databases., Examples: ["ATTACH 'file.db';", "ATTACH 'file.db' AS file_db;", "ATTACH 'file.db' (READ_ONLY);", "ATTACH 'file.db' (BLOCK_SIZE 16384);", "ATTACH 'sqlite_file.db' AS sqlite_db (TYPE SQLITE);", "ATTACH IF NOT EXISTS 'file.db';", "ATTACH IF NOT EXISTS 'file.db' AS file_db;", 'CREATE TABLE file.new_table (i INTEGER);', 'DETACH file;', 'SHOW DATABASES;', 'USE file;', "ATTACH 'https://blobs.duckdb.org/databases/stations.duckdb' AS stations_db;", "ATTACH 's3://duckdb-blobs/databases/stations.duckdb' AS stations_db (READ_ONLY);"]
`CALL`: The CALL statement invokes the given table function and returns the results., Examples: ['CALL duckdb_functions();', "CALL pragma_table_info('pg_am');"]
`VALUES`: The VALUES clause is used to specify a fixed number of rows. It can be utilized as a stand-alone statement, as part of the FROM clause, or as input to an INSERT INTO statement., Examples: ["VALUES ('Amsterdam', 1), ('London', 2);", "SELECT * FROM (VALUES ('Amsterdam', 1), ('London', 2)) cities(name, id);", "INSERT INTO cities VALUES ('Amsterdam', 1), ('London', 2);", "CREATE TABLE cities AS SELECT * FROM (VALUES ('Amsterdam', 1), ('London', 2)) cities(name, id);"]
`EXPLAIN`: DuckDB supports profiling queries using the EXPLAIN and EXPLAIN ANALYZE statements. The EXPLAIN statement provides the query plan and estimated cardinalities for each operator without executing the query. The EXPLAIN ANALYZE statement executes the query and shows the actual cardinalities and the wall-clock time spent in each operator., Examples: ['EXPLAIN SELECT * FROM table_name;', 'EXPLAIN ANALYZE SELECT * FROM table_name;']
`SUMMARIZE`: The SUMMARIZE statement returns summary statistics for a table, view, or a query., Examples: ['SUMMARIZE tbl;', 'SUMMARIZE SELECT * FROM tbl;']
`SAMPLE`: The SAMPLE clause allows you to run a query on a sample from the base table, speeding up query processing at the expense of accuracy, and is applied after the FROM clause but before the WHERE clause or any aggregates., Examples: ['SELECT * FROM addresses USING SAMPLE 1%;', 'SELECT * FROM addresses USING SAMPLE 1% (bernoulli);', 'SELECT * FROM (SELECT * FROM addresses) USING SAMPLE 10 ROWS;']

DuckDB Types:
`VARCHAR`: `VARCHAR` is a versatile data type used to store variable-length character strings, accommodating a wide range of text and string data without enforcing a specific length., Examples: ['CREATE TABLE people (name VARCHAR, age INTEGER);', "INSERT INTO documents (text) VALUES ('This is a VARCHAR example text.');", "SELECT * FROM employees WHERE department = 'Engineering';", 'ALTER TABLE students ADD COLUMN email VARCHAR;', "UPDATE orders SET status = 'Shipped' WHERE order_id = 102;", "COPY products TO 'products.csv' DELIMITER ',' HEADER;"]
`INTEGER`: The INTEGER data type, with aliases such as int, signed, int4, int32, integer, and integral, represents whole numbers and is commonly used to store numeric data without fractional components., Examples: ['-- Assigning integer values to columns in a CREATE TABLE statement\nCREATE TABLE my_table (id INTEGER, age INTEGER);', '-- Inserting integer values as literals within an INSERT statement\nINSERT INTO my_table VALUES (1, 25);', '-- Using integer operations in a SELECT statement\nSELECT id + 10 AS new_id FROM my_table;', '-- Casting a float to an integer\nSELECT CAST(3.7 AS INTEGER) AS whole_number;', '-- Defining a column to only accept non-negative integers using a CHECK constraint\nCREATE TABLE my_table (id INTEGER CHECK (id >= 0));', '-- Using the INTEGER type in a primary key definition\nCREATE TABLE users (user_id INTEGER PRIMARY KEY, username VARCHAR);', '-- Updating integer columns\nUPDATE my_table SET age = age + 1 WHERE id = 1;', '-- Comparing integer values in a WHERE clause\nSELECT * FROM my_table WHERE age > 20;']
`NULL`: The `NULL` type in SQL represents a missing or unknown value, allowing for fields within a table to be uninitialized or absent in data., Examples: ['SELECT NULL = NULL;', 'SELECT NULL IS NULL;', "INSERT INTO table_name (column1, column2) VALUES (NULL, 'data');", "SELECT coalesce(NULL, 'default_value');", 'UPDATE table_name SET column1 = NULL WHERE condition;', "SELECT CASE WHEN column IS NULL THEN 'Value is NULL' ELSE column END FROM table_name;"]
`STRUCT`: The `STRUCT` data type in SQL is used to create a column that contains an ordered list of columns, referred to as entries, which are accessed using named keys. This type is ideal for nesting multiple columns into a single column, allowing a structured and consistent data schema across all rows., Examples: ["SELECT struct_pack(key1 := 'value1', key2 := 42) AS s;", "SELECT {{'key1': 'value1', 'key2': 42}} AS s;", "SELECT a.x FROM (SELECT {{'x': 1, 'y': 2, 'z': 3}} AS a);", "SELECT struct_insert({{'a': 1, 'b': 2, 'c': 3}}, d := 4) AS s;", 'CREATE TABLE t1 (s STRUCT(v VARCHAR, i INTEGER));', "INSERT INTO t1 VALUES (row('a', 42));", "SELECT a.* FROM (SELECT {{'x': 1, 'y': 2, 'z': 3}} AS a);", "SELECT struct_extract({{'x space': 1, 'y': 2, 'z': 3}}, 'x space');"]
`FLOAT`: The FLOAT data type, also known by aliases FLOAT4, REAL, or float, represents a single precision floating-point number, facilitating approximate calculations and efficient handling of numerical data with precision typically up to 6 decimal digits and a range of at least 1E-37 to 1E+37., Examples: ['-- Example: Creating a table with a FLOAT column\nCREATE TABLE example_table (id INTEGER, value FLOAT);', '-- Example: Inserting values into a FLOAT column\nINSERT INTO example_table VALUES (1, 3.14), (2, 2.718);', '-- Example: Performing arithmetic operations with FLOAT values\nSELECT id, value * 2.0::FLOAT AS doubled_value FROM example_table;', '-- Example: Casting a numeric value to FLOAT\nSELECT CAST(100 AS FLOAT) AS float_value;', '-- Example: Using FLOAT values in a mathematical function\nSELECT SQRT(value) FROM example_table WHERE value > 0;', '-- Example: Comparing FLOAT values\nSELECT * FROM example_table WHERE value > 3.0::FLOAT;']

DuckDB Other Keywords:
`AS`: The `AS` keyword in SQL is used to create an alias for columns or tables, helping to simplify query logic and improve readability., Examples: ['SELECT first_name AS name FROM employees;', 'SELECT department AS dept FROM company;', 'CREATE VIEW sales_report AS SELECT * FROM sales WHERE year = 2023;', 'SELECT product_name AS name, SUM(sales) AS total_sales FROM store GROUP BY product_name;', 'SELECT c.customer_id, c.name AS customer_name, o.order_id, o.total_amount AS amount FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id;']
`AND`: "AND" is a logical operator used to combine multiple conditions in SQL queries, returning true only if all conditions are true., Examples: ["SELECT * FROM employees WHERE department = 'Sales' AND salary > 50000;", "SELECT * FROM orders WHERE order_date >= '2023-01-01' AND status = 'Pending';", "SELECT * FROM customers WHERE age > 21 AND city = 'New York';", "SELECT * FROM products WHERE category = 'Electronics' AND stock_quantity > 0;", "SELECT * FROM students WHERE grade = 'A' AND attendance_percentage > 90;"]
`OR`: The `ORDER BY` clause sorts query results based on specified columns or expressions, with optional ascending or descending order and handling of NULL values., Examples: ['SELECT * FROM addresses ORDER BY city;', 'SELECT * FROM addresses ORDER BY city DESC NULLS LAST;', 'SELECT * FROM addresses ORDER BY city, zip;', 'SELECT * FROM addresses ORDER BY city COLLATE DE;', 'SELECT * FROM addresses ORDER BY ALL;', 'SELECT * FROM addresses ORDER BY ALL DESC;']
`ON`: The `ON` keyword in SQL is used with various clauses and statements such as JOIN, UPDATE, DELETE, and ON CONFLICT to specify conditions for selecting, updating, or deleting rows that match specific criteria in relational databases., Examples: ['SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;', "UPDATE employees SET salary = salary * 1.1 ON department.id = employees.department_id WHERE department.name = 'Engineering';", "DELETE FROM orders WHERE orders.date < '2023-01-01' ON CONFLICT DO NOTHING;", "INSERT INTO products (id, name) VALUES (1, 'Widget') ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;"]
`NOT`: The `NOT` keyword in SQL is used to negate a condition, meaning it reverses the truth value of the condition it's used with., Examples: ['SELECT * FROM Students WHERE NOT (grade > 80);', 'SELECT * FROM Employees WHERE NOT EXISTS (SELECT * FROM Promotions WHERE Promotions.employee_id = Employees.id);', "SELECT * FROM Products WHERE NOT (name LIKE 'A%');", 'SELECT * FROM Orders WHERE order_date IS NOT NULL;', 'SELECT * FROM Library WHERE NOT (publication_year BETWEEN 1980 AND 2000);']
`DISTINCT`: The `DISTINCT` keyword is used in the SQL `SELECT` statement to ensure that only unique values are returned for specified columns, effectively removing duplicate rows from the result set., Examples: ['SELECT DISTINCT city FROM addresses;', 'SELECT DISTINCT ON(country) city, population FROM cities ORDER BY population DESC;']
`DESC`: The keyword `desc` is used in SQL to describe the DESCENDING order in which query results should be sorted, often associated with the `ORDER BY` clause., Examples: ['SELECT name FROM employees ORDER BY salary DESC;', 'CREATE TABLE example (id INTEGER, name VARCHAR);', 'DESCRIBE example;', 'DESCRIBE SELECT * FROM example WHERE id < 10 ORDER BY name DESC;']
`IS`: The "IS" keyword is used in SQL to perform tests on values to check for NULL values or to use as part of statements like IS DISTINCT FROM which can handle NULLs in equality comparisons., Examples: ['SELECT 4 IS DISTINCT FROM NULL;', 'SELECT 4 IS NOT DISTINCT FROM 4;', 'SELECT NULL IS NULL;', 'SELECT NULL IS NOT NULL;']
`IN`: The `IN` keyword is used in SQL to specify a list of discrete values for a column to match against, typically in a `WHERE` clause, allowing for multiple specific conditions to be evaluated at once., Examples: ["SELECT * FROM employees WHERE department IN ('HR', 'Engineering', 'Marketing');", 'SELECT id, name FROM students WHERE grade IN (10, 11, 12);', "DELETE FROM orders WHERE order_status IN ('Cancelled', 'Returned');", "UPDATE items SET status = 'Unavailable' WHERE item_id IN (1001, 1002, 1003);", "SELECT * FROM logs WHERE severity IN ('ERROR', 'CRITICAL') ORDER BY timestamp DESC;"]
`ALL`: The `ALL` keyword in SQL specifies that operations should retain all duplicate rows, as seen in commands like `UNION ALL`, `INTERSECT ALL`, and `EXCEPT ALL`, which follow bag semantics instead of eliminating duplicates., Examples: ['UNION ALL ```sql\nSELECT * FROM range(2) t1(x)\nUNION ALL\nSELECT * FROM range(3) t2(x);\n```\nThis example demonstrates using `UNION ALL` to combine rows from two queries without eliminating duplicates.', 'INTERSECT ALL ```sql\nSELECT unnest([5, 5, 6, 6, 6, 6, 7, 8]) AS x\nINTERSECT ALL\nSELECT unnest([5, 6, 6, 7, 7, 9]);\n```\nThis example shows using `INTERSECT ALL` to select rows that are present in both result sets, keeping duplicate values.', 'EXCEPT ALL ```sql\nSELECT unnest([5, 5, 6, 6, 6, 6, 7, 8]) AS x\nEXCEPT ALL\nSELECT unnest([5, 6, 6, 7, 7, 9]);\n```\nThis example illustrates `EXCEPT ALL`, which selects all rows present in the first query but not in the second, without removing duplicates.', 'ORDER BY ALL ```sql\nSELECT *\nFROM addresses\nORDER BY ALL;\n```\nThis SQL command uses `ORDER BY ALL` to sort the result set by all columns sequentially from left to right.']
`LIKE`: The `LIKE` expression is used to determine if a string matches a specified pattern, allowing wildcard characters such as `_` to represent any single character and `%` to match any sequence of characters., Examples: ["SELECT 'abc' LIKE 'abc'; -- true", "SELECT 'abc' LIKE 'a%'; -- true", "SELECT 'abc' LIKE '_b_'; -- true", "SELECT 'abc' LIKE 'c'; -- false", "SELECT 'abc' LIKE 'c%'; -- false", "SELECT 'abc' LIKE '%c'; -- true", "SELECT 'abc' NOT LIKE '%c'; -- false", "SELECT 'abc' ILIKE '%C'; -- true"]
`IF`: The `IF` keyword is used in conditional constructs, most commonly found in the `IF NOT EXISTS` or `IF EXISTS` clauses in SQL commands to prevent errors or skip operations, such as creating or dropping a database or table when certain conditions are met., Examples: ['CREATE DATABASE IF NOT EXISTS ducks_db;', 'CREATE TABLE IF NOT EXISTS t1 (i INTEGER, j INTEGER);', 'CREATE TABLE t1 (id INTEGER, PRIMARY KEY (id), UNIQUE (id)) IF NOT EXISTS;']
`EXISTS`: The `EXISTS` operator is used to determine if a subquery returns any rows, returning `true` if at least one row exists and `false` otherwise., Examples: ["SELECT EXISTS (FROM grades WHERE course = 'Math') AS math_grades_present;", "SELECT EXISTS (FROM grades WHERE course = 'History') AS history_grades_present;"]
`COLUMN`: The keyword 'column' is used to refer to the vertical entities in a table, defining the structure and type of data stored in a database table., Examples: ['ALTER TABLE integers ADD COLUMN k INTEGER;', 'ALTER TABLE integers DROP COLUMN k;', 'ALTER TABLE integers RENAME COLUMN i TO j;', 'SELECT * EXCLUDE (city) FROM addresses;', 'SELECT * REPLACE (lower(city) AS city) FROM addresses;', "SELECT COLUMNS(c -> c LIKE '%num%') FROM addresses;", "SELECT COLUMNS('(id|numbers?)') FROM numbers;"]
`INTO`: The `INSERT INTO` clause in SQL is used to add new records to a table, either by providing specific values or by inserting data from an existing query., Examples: ['INSERT INTO tbl VALUES (1), (2), (3);', 'INSERT INTO tbl SELECT * FROM other_tbl;', 'INSERT INTO tbl (i) VALUES (1), (2), (3);', 'INSERT INTO tbl (i) VALUES (1), (DEFAULT), (3);', 'INSERT OR IGNORE INTO tbl (i) VALUES (1);', 'INSERT OR REPLACE INTO tbl (i) VALUES (1);', 'INSERT INTO tbl RETURNING *;', 'INSERT INTO tbl (a, b) BY POSITION VALUES (5, 42);', 'INSERT INTO tbl BY NAME (SELECT 42 AS b, 32 AS a);']
`TO`: The `TO` keyword in SQL is used within the `COPY` statement to export data from a database table or query result to an external file, supporting various formats such as CSV, Parquet, JSON, and databases., Examples: ["COPY lineitem TO 'lineitem.csv' (FORMAT CSV, DELIMITER '|', HEADER);", 'COPY lineitem TO "lineitem.csv";', "COPY (SELECT l_orderkey, l_partkey FROM lineitem) TO 'lineitem.parquet' (COMPRESSION ZSTD);", "COPY lineitem TO 'lineitem.tsv' (DELIMITER '\\t', HEADER false);", "COPY lineitem(l_orderkey) TO 'orderkey.tbl' (DELIMITER '|');", "COPY (SELECT 42 AS a, 'hello' AS b) TO 'query.csv' (DELIMITER ',');", "COPY (SELECT 42 AS a, 'hello' AS b) TO 'query.parquet' (FORMAT PARQUET);", "COPY (SELECT 42 AS a, 'hello' AS b) TO 'query.ndjson' (FORMAT JSON);"]
`EXCLUDE`: The `EXCLUDE` keyword is used in SQL to exclude certain columns from being selected when using the star (`*`) expression, providing more control over the columns included in the query results., Examples: ['SELECT * EXCLUDE (city) FROM addresses;', 'SELECT COLUMNS(* EXCLUDE (number)) FROM numbers;', 'INSERT INTO tbl SELECT * EXCLUDE (id) FROM source_table;']
`YEAR`: The keyword `YEAR` is used in intervals to specify periods of time, specifically one year, and is stored as 12 months in database systems., Examples: ['SELECT INTERVAL 1 YEAR;', "SELECT INTERVAL '1.5' YEARS; -- Returns 12 months due to truncation", "SELECT DATE '2000-01-01' + INTERVAL 1 YEAR;", "SELECT datepart('year', INTERVAL 14 MONTHS); -- Returns 1", 'PIVOT Cities ON Year USING sum(Population);']
`USING`: The `USING` clause in SQL is used in conjunction with a `JOIN` to specify columns to compare for equality when joining tables, simplifying syntax when column names are the same in both tables., Examples: ['SELECT * FROM employees JOIN departments USING (department_id);', 'PIVOT sales_data ON month USING sum(sales) GROUP BY category;', 'SELECT * FROM cities JOIN countries USING (country_code);', 'FROM test.csv USING SAMPLE 10 PERCENT;', 'SELECT product_id, order_date, SUM(quantity) FROM orders JOIN order_items USING (order_id) GROUP BY product_id, order_date;']
`ADD`: The `ADD COLUMN` clause in the `ALTER TABLE` statement allows for the inclusion of a new column with a specified data type and optional default value into an existing table., Examples: ['ALTER TABLE integers ADD COLUMN k INTEGER;', 'ALTER TABLE integers ADD COLUMN l INTEGER DEFAULT 10;', "UPDATE my_db.movies SET summary = prompt('summarize review in one sentence' || overview);", 'ALTER TABLE my_db.movies ADD COLUMN summary VARCHAR;', 'ALTER TABLE my_db.movies ADD COLUMN metadata STRUCT(main_characters VARCHAR[], genre VARCHAR, action INTEGER);']
`TYPE`: The `CREATE TYPE` statement is used to define a new data type in the catalog, allowing users to define enums, structs, unions, and type aliases for better schema and type management in SQL., Examples: ["CREATE TYPE mood AS ENUM ('happy', 'sad', 'curious');", 'CREATE TYPE many_things AS STRUCT(k INTEGER, l VARCHAR);', 'CREATE TYPE one_thing AS UNION(number INTEGER, string VARCHAR);', 'CREATE TYPE x_index AS INTEGER;']
`PRAGMA`: The 'PRAGMA' statement is a SQL extension used to modify the internal state of the database engine and influence its execution or behavior., Examples: ['PRAGMA database_list; -- List all databases', 'PRAGMA show_tables; -- List all tables', "PRAGMA table_info('table_name'); -- Get information about a specific table's columns", 'PRAGMA database_size; -- Get file and memory size of each database', "PRAGMA storage_info('table_name'); -- Retrieve storage information for a specific table", 'PRAGMA show_databases; -- Equivalent to SHOW DATABASES statement', 'PRAGMA version; -- Show DuckDB version information', 'PRAGMA platform; -- Show platform identifier where DuckDB is running', 'PRAGMA enable_progress_bar; -- Enable progress bar for running queries', 'PRAGMA disable_progress_bar; -- Disable progress bar for running queries', 'PRAGMA enable_profiling; -- Enable query profiling', 'PRAGMA disable_profiling; -- Disable query profiling', 'PRAGMA verify_external; -- Enable verification of external operators', 'PRAGMA disable_verify_external; -- Disable verification of external operators', 'PRAGMA enable_object_cache; -- Enable caching of objects', 'PRAGMA disable_object_cache; -- Disable caching of objects', 'PRAGMA force_checkpoint; -- Force a checkpoint even when no changes are made', 'PRAGMA enable_checkpoint_on_shutdown; -- Run checkpoint on shutdown', 'PRAGMA disable_checkpoint_on_shutdown; -- Do not run checkpoint on shutdown']
`COLUMNS`: The `COLUMNS` expression in SQL is used to apply operations across multiple columns, allowing dynamic column selection through patterns, regular expressions, or lambda functions for querying or modifying datasets efficiently., Examples: ["SELECT COLUMNS(c -> c LIKE '%num%') FROM addresses;", "SELECT COLUMNS('number\\d+') FROM addresses;", 'SELECT COLUMNS(* EXCLUDE (empid, dept)) INTO NAME month VALUE sales UNPIVOT monthly_sales;', 'SELECT min(COLUMNS(* REPLACE (number + id AS number))), count(COLUMNS(* EXCLUDE (number))) FROM numbers;', "SELECT coalesce(*COLUMNS(['a', 'b', 'c'])) AS result FROM (SELECT NULL a, 42 b, true c);"]
`NAME`: The `INSERT` statement is used to add new rows of data into an existing table, either by specifying explicit values or by using results from a query., Examples: ['INSERT INTO tbl VALUES (1), (2), (3);', 'INSERT INTO tbl SELECT * FROM other_tbl;', 'INSERT INTO tbl (i) VALUES (1), (2), (3);', 'INSERT INTO tbl (i) VALUES (1), (DEFAULT), (3);', 'INSERT OR IGNORE INTO tbl (i) VALUES (1);', 'INSERT OR REPLACE INTO tbl (i) VALUES (1);']
`DATA`: The `CREATE DATABASE` statement allows you to create a new database, either by copying it from a local environment or zero-copy cloning within the same environment., Examples: ['CREATE DATABASE ducks;', 'CREATE OR REPLACE DATABASE ducks;', 'CREATE DATABASE IF NOT EXISTS ducks_db;', "CREATE DATABASE flying_ducks FROM './databases/local_ducks.db';"]
`DELIMITER`: The 'delimiter' keyword is used in SQL statements to specify the character that separates columns in a data representation, such as in CSV files, allowing for precise parsing of column values during data import or export operations., Examples: ["COPY lineitem FROM 'lineitem.csv' (DELIMITER '|');", "COPY ontime FROM 'flights.csv' (DELIMITER '|', HEADER);", "COPY lineitem TO 'lineitem.tbl' (DELIMITER '|', HEADER);", "COPY (SELECT * FROM ontime) TO 'ontime.csv' (DELIMITER ',', HEADER);", "EXPORT DATABASE 'directory' (FORMAT CSV, DELIMITER '|');"]

Here is the schema of the DuckDB database that the SQL query will run on:
{schema}

Question:
Here is the question or an instruction the user provided:
{question}

Write a DuckDB SQL query for the given question! Make sure to only response with the SQL query and wrap it in ```sql\n<ANSWER>``` markdown code tags.

Answer:
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterLlama(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """<|begin_of_text|>

Your task is to generate valid DuckDB SQL to answer the following question, given a DuckDB database schema.

## DuckDB SQL syntax specifics you should be aware of:

### Case Insensitivity and Quoting:

Identifiers (tables, columns): Case-insensitive, but DuckDB remembers the case you use. Use double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity.
```
CREATE TABLE "My Table" ("column_name" VARCHAR); -- Spaces and mixed case
SELECT "column_name" FROM "My Table";
```

### String Literals: Always use single quotes (') to define string literals.
```
SELECT 'This is a string' AS text;
```

### Direct File Querying: Query CSV, Parquet, and JSON files directly without loading them first.

```
SELECT * FROM 'data.csv';
SELECT * FROM 'data.parquet';
SELECT * FROM 'data.json';
```

### CREATE TABLE AS (CTAS): Create tables from query results.

```
CREATE TABLE squares AS SELECT i, i * i AS square FROM generate_series(1, 10) t(i);
```

### FROM-First Syntax (Optional SELECT): Start queries with FROM, and optionally omit SELECT *.

```
FROM my_table WHERE condition;  -- Equivalent to SELECT * FROM my_table WHERE condition
```

### SELECT without FROM: DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly.

```
SELECT 1 + 1 AS result;
```

### GROUP BY ALL/ORDER BY ALL:  Shorthand for grouping/ordering by all non-aggregated/all columns.

```
SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;
SELECT * FROM my_table ORDER BY ALL;
```

### SELECT COLUMNS(): Powerful way to select or transform multiple columns using patterns or functions.

```
-- Select columns matching a pattern
SELECT COLUMNS('sales_.*') FROM sales_data;

-- Transform multiple columns with a function
SELECT AVG(COLUMNS(*)) FROM sales_data;
```

### UNION BY NAME: Combine tables by matching column names, not just their positions.

```
SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;
```

### Implicit/Explicit Casting: DuckDB is generally more lenient with implicit type conversions, but you can always be explicit using ::

```
SELECT '42' + 1;  -- Implicit cast, result is 43
SELECT '42'::INTEGER + 1; -- Explicit cast, result is 43
```

### String/List Slicing: Extract parts of strings and lists using [start:end] or [start:end:step] syntax.

```
SELECT 'DuckDB'[1:4];  -- Returns 'Duck'
SELECT [1, 2, 3, 4][1:3]; -- Returns [1, 2, 3]
```

### Simple List/Struct/Map/Array Creation: Create complex types using intuitive syntax.

In a SELECT statement:
```
SELECT [1, 2, 3] AS my_list, {{'a': 1, 'b': 'text'}} AS my_struct, MAP([1,2],['one','two']) as my_map;
```

When creating a table:
```
CREATE TABLE data (
    my_list INTEGER[],
    my_struct STRUCT(a INTEGER, b TEXT),
    my_map MAP(INTEGER, VARCHAR),
    my_array INTEGER[3]
);
```

### Timestamp Conversions and Extraction: Convert between text and timestamps, and extract date parts.

```
SELECT strftime(NOW(), '%Y-%m-%d');  -- Current date as 'YYYY-MM-DD'
SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP; -- String to timestamp
SELECT EXTRACT(YEAR FROM DATE '2023-07-23'); -- Extract year
```

### Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses.

```
SELECT a + b AS total
FROM my_table
WHERE total > 10
GROUP BY total
HAVING total < 20;
```

### List Comprehensions:  Generate lists using expressions similar to Python list comprehensions.

```
SELECT [x*2 FOR x IN [1, 2, 3]];  -- Returns [2, 4, 6]
```

### Function Chaining: Chain multiple function calls together using the dot (.) operator.

```
SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB'
```

### Regular Expressions: DuckDB has built-in functions for regex matching, replacement, and extraction.

```
SELECT regexp_matches('DuckDB', 'Duck'); -- Returns true
SELECT regexp_replace('DuckDB', 'Duck', 'Goose'); -- Returns 'GooseDB'
SELECT regexp_extract('DuckDB', '(\w+)(DB)', 1); -- Returns 'Duck'
```

### Sampling: Quickly get a subset of your data with SAMPLE or TABLESAMPLE.

```
SELECT * FROM large_table USING SAMPLE 10%; -- Random 10% sample
SELECT * FROM large_table TABLESAMPLE BERNOULLI(10); -- Bernoulli sampling
```

### ATTACH and Access: Attach external databases and reference their objects using databasename.table_name syntax.

```
ATTACH 'my_database.duckdb' AS mydb;
SELECT * FROM mydb.my_table;
```

### SUMMARIZE: Get summary statistics (min, max, unique count, average, standard deviation, quartiles, and count) of a table.

```
SUMMARIZE table_name;
```

### DESCRIBE: Get schema of a table (column_name, column_type, null, key, default, extra).

```
DESCRIBE table_name;
```

Database Schema:
Here is the schema of the DuckDB database that the SQL query will run on:
{schema}

Question:
Here is the question or an instruction the user provided:
{question}

Task:
Write a DuckDB SQL query for the given question!

Here is the valid DuckDB SQL query:
```
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterGranite(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """System:

Your task is to generate valid DuckDB SQL to answer the following question, given a DuckDB database schema.

## DuckDB SQL syntax specifics you should be aware of:

### Case Insensitivity and Quoting:

Identifiers (tables, columns): Case-insensitive, but DuckDB remembers the case you use. Use double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity.
```
CREATE TABLE "My Table" ("column_name" VARCHAR); -- Spaces and mixed case
SELECT "column_name" FROM "My Table";
```

### String Literals: Always use single quotes (') to define string literals.
```
SELECT 'This is a string' AS text;
```

### Direct File Querying: Query CSV, Parquet, and JSON files directly without loading them first.

```
SELECT * FROM 'data.csv';
SELECT * FROM 'data.parquet';
SELECT * FROM 'data.json';
```

### CREATE TABLE AS (CTAS): Create tables from query results.

```
CREATE TABLE squares AS SELECT i, i * i AS square FROM generate_series(1, 10) t(i);
```

### FROM-First Syntax (Optional SELECT): Start queries with FROM, and optionally omit SELECT *.

```
FROM my_table WHERE condition;  -- Equivalent to SELECT * FROM my_table WHERE condition
```

### SELECT without FROM: DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly.

```
SELECT 1 + 1 AS result;
```

### GROUP BY ALL/ORDER BY ALL:  Shorthand for grouping/ordering by all non-aggregated/all columns.

```
SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;
SELECT * FROM my_table ORDER BY ALL;
```

### SELECT COLUMNS(): Powerful way to select or transform multiple columns using patterns or functions.

```
-- Select columns matching a pattern
SELECT COLUMNS('sales_.*') FROM sales_data;

-- Transform multiple columns with a function
SELECT AVG(COLUMNS(*)) FROM sales_data;
```

### UNION BY NAME: Combine tables by matching column names, not just their positions.

```
SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;
```

### Implicit/Explicit Casting: DuckDB is generally more lenient with implicit type conversions, but you can always be explicit using ::

```
SELECT '42' + 1;  -- Implicit cast, result is 43
SELECT '42'::INTEGER + 1; -- Explicit cast, result is 43
```

### String/List Slicing: Extract parts of strings and lists using [start:end] or [start:end:step] syntax.

```
SELECT 'DuckDB'[1:4];  -- Returns 'Duck'
SELECT [1, 2, 3, 4][1:3]; -- Returns [1, 2, 3]
```

### Simple List/Struct/Map/Array Creation: Create complex types using intuitive syntax.

In a SELECT statement:
```
SELECT [1, 2, 3] AS my_list, {{'a': 1, 'b': 'text'}} AS my_struct, MAP([1,2],['one','two']) as my_map;
```

When creating a table:
```
CREATE TABLE data (
    my_list INTEGER[],
    my_struct STRUCT(a INTEGER, b TEXT),
    my_map MAP(INTEGER, VARCHAR),
    my_array INTEGER[3]
);
```

### Timestamp Conversions and Extraction: Convert between text and timestamps, and extract date parts.

```
SELECT strftime(NOW(), '%Y-%m-%d');  -- Current date as 'YYYY-MM-DD'
SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP; -- String to timestamp
SELECT EXTRACT(YEAR FROM DATE '2023-07-23'); -- Extract year
```

### Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses.

```
SELECT a + b AS total
FROM my_table
WHERE total > 10
GROUP BY total
HAVING total < 20;
```

### List Comprehensions:  Generate lists using expressions similar to Python list comprehensions.

```
SELECT [x*2 FOR x IN [1, 2, 3]];  -- Returns [2, 4, 6]
```

### Function Chaining: Chain multiple function calls together using the dot (.) operator.

```
SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB'
```

### Regular Expressions: DuckDB has built-in functions for regex matching, replacement, and extraction.

```
SELECT regexp_matches('DuckDB', 'Duck'); -- Returns true
SELECT regexp_replace('DuckDB', 'Duck', 'Goose'); -- Returns 'GooseDB'
SELECT regexp_extract('DuckDB', '(\w+)(DB)', 1); -- Returns 'Duck'
```

### Sampling: Quickly get a subset of your data with SAMPLE or TABLESAMPLE.

```
SELECT * FROM large_table USING SAMPLE 10%; -- Random 10% sample
SELECT * FROM large_table TABLESAMPLE BERNOULLI(10); -- Bernoulli sampling
```

### ATTACH and Access: Attach external databases and reference their objects using databasename.table_name syntax.

```
ATTACH 'my_database.duckdb' AS mydb;
SELECT * FROM mydb.my_table;
```

### SUMMARIZE: Get summary statistics (min, max, unique count, average, standard deviation, quartiles, and count) of a table.

```
SUMMARIZE table_name;
```

### DESCRIBE: Get schema of a table (column_name, column_type, null, key, default, extra).

```
DESCRIBE table_name;
```

Here is the schema of the DuckDB database that the SQL query will run on:
{schema}

Question:
Here is the question or an instruction the user provided:
{question}

Please write a DuckDB SQL query that answers the user's question or instruction. Use DuckDB-specific syntax if possible.

Answer:
```
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterPhi(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """<|endoftext|><|user|>
Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.

Here are some DuckDB SQL syntax specifics you should be aware of:


- DuckDB use double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity and single quotes (') to define string literals
- DuckDB can query CSV, Parquet, and JSON directly without loading them first, e.g. `SELECT * FROM 'data.csv';`
- DuckDB supports CREATE TABLE AS (CTAS): `CREATE TABLE new_table AS SELECT * FROM old_table;`
- DuckDB queries can start with FROM, and optionally omit SELECT *, e.g. `FROM my_table WHERE condition;` is equivalent to `SELECT * FROM my_table WHERE condition;`
- DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly, e.g. `SELECT 1 + 1 AS result;`
- DuckDB supports attaching multiple databases, unsing the ATTACH statement: `ATTACH 'my_database.duckdb' AS mydb;`. Tables within attached databases can be accessed using the dot notation (.), e.g. `SELECT * FROM mydb.table_name syntax`. The default databases doesn't require the do notation to access tables. The default database can be changed with the USE statement, e.g. `USE my_db;`.
- DuckDB is generally more lenient with implicit type conversions (e.g. `SELECT '42' + 1;` - Implicit cast, result is 43), but you can always be explicit using `::`, e.g. `SELECT '42'::INTEGER + 1;`
- DuckDB can extract parts of strings and lists using [start:end] or [start:end:step] syntax. Indexes start at 1. String slicing: `SELECT 'DuckDB'[1:4];`. Array/List slicing: `SELECT [1, 2, 3, 4][1:3];`
- DuckDB has a powerful way to select or transform multiple columns using patterns or functions. You can select columns matching a pattern: `SELECT COLUMNS('sales_.*') FROM sales_data;` or transform multiple columns with a function: `SELECT AVG(COLUMNS('sales_.*')) FROM sales_data;`
- DuckDB an easy way to include/exclude or modify columns when selecting all: e.g. Exclude: `SELECT * EXCLUDE (sensitive_data) FROM users;` Replace: `SELECT * REPLACE (UPPER(name) AS name) FROM users;`
- DuckDB has a shorthand for grouping/ordering by all non-aggregated/all columns. e.g `SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;` and `SELECT * FROM my_table ORDER BY ALL;`
- DuckDB can combine tables by matching column names, not just their positions using UNION BY NAME. E.g. `SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;`
- DuckDB has an inutitive syntax to create List/Struct/Map and Array types. Create complex types using intuitive syntax. List: `SELECT [1, 2, 3] AS my_list;`, Struct: `{{'a': 1, 'b': 'text'}} AS my_struct;`, Map: `MAP([1,2],['one','two']) as my_map;`. All types can also be nested into each other. Array types are fixed size, while list types have variable size. Compared to Structs, MAPs do not need to have the same keys present for each row, but keys can only be of type Integer or Varchar. Example: `CREATE TABLE example (my_list INTEGER[], my_struct STRUCT(a INTEGER, b TEXT), my_map MAP(INTEGER, VARCHAR),  my_array INTEGER[3], my_nested_struct STRUCT(a INTEGER, b Integer[3]));`
- DuckDB has an inutive syntax to access struct fields using dot notation (.) or brackets ([]) with the field name. Maps fields can be accessed by brackets ([]).
- DuckDB's way of converting between text and timestamps, and extract date parts. Current date as 'YYYY-MM-DD': `SELECT strftime(NOW(), '%Y-%m-%d');` String to timestamp: `SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP;`, Extract Year from date: `SELECT EXTRACT(YEAR FROM DATE '2023-07-23');`
- Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses. E.g.: `SELECT a + b AS total FROM my_table WHERE total > 10 GROUP BY total HAVING total < 20;`
- DuckDB allows generating lists using expressions similar to Python list comprehensions. E.g. `SELECT [x*2 FOR x IN [1, 2, 3]];` Returns [2, 4, 6].
- DuckDB allows chaining multiple function calls together using the dot (.) operator. E.g.: `SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB';`
- DuckDB has a JSON data type. It supports selecting fields from the JSON with a JSON-Path expression using the arrow operator, -> (returns JSON) or ->> (returns text) with JSONPath expressions. For example: `SELECT data->'$.user.id' AS user_id, data->>'$.event_type' AS event_type FROM events;`
- DuckDB has built-in functions for regex regexp_matches(column, regex), regexp_replace(column, regex), and regexp_extract(column, regex).
- DuckDB has a way to quickly get a subset of your data with `SELECT * FROM large_table USING SAMPLE 10%;`

Here is the schema of the DuckDB database that the SQL query will run on:
{schema}

Question:
Here is the question or an instruction the user provided:
{question}

Write a DuckDB SQL query for the given question!<|end|>
<|assistant|>
```sql
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterGPTmini(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """Schema:
```sql
{schema}
```

Question:
{question}

Write a valid DuckDB SQL query to answer the question!
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstFormatterPhiAzure(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.

Here is the schema of the DuckDB database that the SQL query will run on:
{schema}

Question:
Here is the question or an instruction the user provided:
{question}

Write a DuckDB SQL query for the given question!
"""

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: str,
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction


class DuckDBInstFormatterCustom(RajkumarFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = ""

    @classmethod
    def format_retrieved_context(
            cls,
            context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n### Documentation:\n{context_str}\n"

    @classmethod
    def format_prompt(
            cls,
            instruction: str,
            table_text: str,
            context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        instruction = cls.PROMPT_TEMPLATE.format(
            schema=table_text,
            question=instruction
        )
        return instruction

class DuckDBInstNoShorthandFormatter(DuckDBInstFormatter):
    """DuckDB Inst class."""

    PROMPT_TEMPLATE = """### Instruction:\n{instruction}\n\n### Input:\n{input}{context}\n### Question:\n{question}\n\n### Response:\n"""
    INSTRUCTION_TEMPLATE = """Your task is to generate valid duckdb SQL to answer the following question{has_schema}"""  # noqa: E501


class DuckDBChat:
    """DuckDB Inst class."""

    table_sep: str = "\n\n"
    shuffle_table_order: bool = True
    _cache: dict[tuple[str, str, str], list[str]] = {}
    clean_whitespace = False
    model = None

    @classmethod
    def format_table(cls, table: Table) -> str:
        """Get table format."""
        table_fmt = []
        for col in table.columns or []:
            # This is technically an incorrect type, but it should be a catchall word
            table_fmt.append(f"    {col.name} {col.dtype or 'any'}")
        if table_fmt:
            all_cols = ",\n".join(table_fmt)
            create_tbl = f"CREATE TABLE {table.name} (\n{all_cols}\n)"
        else:
            create_tbl = f"CREATE TABLE {table.name}"
        return create_tbl

    @classmethod
    def format_all_tables(cls, tables: list[Table], instruction: str) -> list[dict]:
        """Get all tables format."""
        if not cls.model:
            cls.model = Manifest(
                engine="gpt-3.5-turbo",
                client_name="openaichat",
                cache_name="sqlite",
                cache_connection=".manifest.sqlite",
            )
        table_texts = [cls.format_table(table) for table in tables]
        full_schema = cls.table_sep.join(table_texts)
        prompt = f"""SQL schema of my database:
{full_schema}
Explain in a few sentences what the data is about:
        """
        messages = [
            {
                "role": "system",
                "content": "You are a helpful assistant that can generate an human redable summary of database content based on the schema.",
            },
            {"role": "user", "content": prompt},
        ]
        explanation = cls.model.run(messages, temperature=0)
        messages.append({"role": "assistant", "content": explanation})
        return messages[1:]

    @classmethod
    def format_retrieved_context(
        cls,
        context: list[str],
    ) -> str:
        """Format retrieved context."""
        context_str = "\n--------\n".join(context)
        return f"\n\nHere is additional documentation about DuckDB that could be useful.\n--------\n{context_str}\n--------\n"

    @classmethod
    def format_prompt(
        cls,
        instruction: str,
        table_text: list[dict],
        context_text: str,
    ) -> str | list[str]:
        """Get prompt format."""
        prompt = f"""Now output a single SQL query without any explanation and do not add anything
to the query that was not part of the question, also do not use markdown. Make sure to only
use information provided in the prompt, or tables and columns from the schema above and write a query to answer the question.{context_text}\n\nMy quesiton is \n`{instruction}`\n\nGenerate the DuckDB specific SQL query:"""  # noqa: E501
        messages = [
            {
                "role": "system",
                "content": "You are a helpful assistant that can generate DuckDB sql queries, which is a superset of Postgresql, based on the user input. You do not respond with any human readable text, only SQL code.",
            },
            *table_text,
            {"role": "user", "content": prompt},
        ]
        return messages

    @classmethod
    def format_model_output(cls, output_sql: str, prompt: str) -> str:
        """Format model output."""
        return output_sql

    @classmethod
    def format_gold_output(cls, output_sql: str) -> str:
        """Format gold output for demonstration."""
        return output_sql