File: test_relation_api.cpp

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (1168 lines) | stat: -rw-r--r-- 46,866 bytes parent folder | download | duplicates (3)
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
#include "catch.hpp"
#include "duckdb/common/file_system.hpp"
#include "duckdb/common/enums/joinref_type.hpp"
#include "duckdb/parser/expression/columnref_expression.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"
#include "duckdb/main/relation/value_relation.hpp"
#include "iostream"
#include "test_helpers.hpp"
#include "duckdb/main/relation/materialized_relation.hpp"

using namespace duckdb;
using namespace std;

TEST_CASE("Test simple relation API", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> tbl, filter, proj, proj2, v1, v2, v3;

	// create some tables
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3), (1), (2), (3)"));

	// simple projection
	REQUIRE_NOTHROW(tbl = con.Table("integers"));
	REQUIRE_NOTHROW(result = tbl->Project("i + 1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));

	REQUIRE_NOTHROW(result = tbl->Project(duckdb::vector<string> {"i + 1", "i + 2"})->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));
	REQUIRE(CHECK_COLUMN(result, 1, {3, 4, 5}));

	REQUIRE_NOTHROW(result = tbl->Project(duckdb::vector<string> {"i + 1"}, {"i"})->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));

	// we support * expressions
	REQUIRE_NOTHROW(result = tbl->Project("*")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));

	// we can also read the table directly
	REQUIRE_NOTHROW(result = tbl->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));

	// we can stack projections
	REQUIRE_NOTHROW(
	    result =
	        tbl->Project("i + 1 AS i")->Project("i + 1 AS i")->Project("i + 1 AS i")->Project("i + 1 AS i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {5, 6, 7}));

	// we can execute the same projection multiple times
	REQUIRE_NOTHROW(proj = tbl->Project("i + 1"));
	for (idx_t i = 0; i < 10; i++) {
		REQUIRE_NOTHROW(result = proj->Execute());
		REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));
	}

	result = con.Query("SELECT i+1 FROM (SELECT * FROM integers WHERE i <> 2) relation");
	REQUIRE(CHECK_COLUMN(result, 0, {2, 4}));
	// filter and projection
	REQUIRE_NOTHROW(filter = tbl->Filter("i <> 2"));
	REQUIRE_NOTHROW(proj = filter->Project("i + 1"));
	REQUIRE_NOTHROW(result = proj->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 4}));

	// multi filter
	REQUIRE_NOTHROW(result = tbl->Filter(duckdb::vector<string> {"i <> 2", "i <> 3"})->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1}));

	// we can reuse the same filter again and perform a different projection
	REQUIRE_NOTHROW(proj = filter->Project("i * 10"));
	REQUIRE_NOTHROW(result = proj->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {10, 30}));

	// add a limit
	REQUIRE_NOTHROW(result = proj->Limit(1)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {10}));

	// and an offset
	REQUIRE_NOTHROW(result = proj->Limit(1, 1)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {30}));

	// lets add some aliases
	REQUIRE_NOTHROW(proj = filter->Project("i + 1 AS a"));
	REQUIRE_NOTHROW(result = proj->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 4}));
	// we can check the column names
	REQUIRE(proj->Columns()[0].Name() == "a");
	REQUIRE(proj->Columns()[0].Type() == LogicalType::INTEGER);

	// we can also alias like this
	REQUIRE_NOTHROW(proj = filter->Project("i + 1", "a"));
	REQUIRE_NOTHROW(result = proj->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 4}));
	// we can check the column names
	REQUIRE(proj->Columns()[0].Name() == "a");
	REQUIRE(proj->Columns()[0].Type() == LogicalType::INTEGER);

	// now we can use that column to perform additional projections
	REQUIRE_NOTHROW(result = proj->Project("a + 1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {3, 5}));

	// we can also filter on this again
	REQUIRE_NOTHROW(result = proj->Filter("a=2")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2}));

	// filters can also contain conjunctions
	REQUIRE_NOTHROW(result = proj->Filter("a=2 OR a=4")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 2, 4, 4}));

	// alias
	REQUIRE_NOTHROW(result = proj->Project("a + 1")->Alias("bla")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {3, 5}));

	// now test ordering
	REQUIRE_NOTHROW(result = proj->Order("a DESC")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {4, 4, 2, 2}));
	REQUIRE_NOTHROW(result = proj->Order(duckdb::vector<string> {"a DESC", "a ASC"})->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {4, 4, 2, 2}));

	// top n
	REQUIRE_NOTHROW(result = proj->Order("a")->Limit(1)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2}));
	REQUIRE_NOTHROW(result = proj->Order("a DESC")->Limit(1)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {4}));

	// test set operations
	REQUIRE_NOTHROW(result = tbl->Union(tbl)->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3}));
	REQUIRE_NOTHROW(result = tbl->Except(tbl)->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {}));
	REQUIRE_NOTHROW(result = tbl->Intersect(tbl)->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 2, 2, 3, 3}));
	REQUIRE_NOTHROW(result = tbl->Except(tbl->Filter("i=2"))->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 3, 3}));
	REQUIRE_NOTHROW(result = tbl->Intersect(tbl->Filter("i=2"))->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 2}));

	// set operations with projections
	REQUIRE_NOTHROW(proj = tbl->Project("i::TINYINT AS i, i::SMALLINT, i::BIGINT, i::VARCHAR"));
	REQUIRE_NOTHROW(proj2 = tbl->Project("(i+10)::TINYINT, (i+10)::SMALLINT, (i+10)::BIGINT, (i+10)::VARCHAR"));
	REQUIRE_NOTHROW(result = proj->Union(proj2)->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 2, 2, 3, 3, 11, 11, 12, 12, 13, 13}));
	REQUIRE(CHECK_COLUMN(result, 1, {1, 1, 2, 2, 3, 3, 11, 11, 12, 12, 13, 13}));
	REQUIRE(CHECK_COLUMN(result, 2, {1, 1, 2, 2, 3, 3, 11, 11, 12, 12, 13, 13}));
	REQUIRE(CHECK_COLUMN(result, 3, {"1", "1", "2", "2", "3", "3", "11", "11", "12", "12", "13", "13"}));

	// distinct
	REQUIRE_NOTHROW(result = tbl->Union(tbl)->Union(tbl)->Distinct()->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));

	// join
	REQUIRE_NOTHROW(v1 = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"id", "j"}, "v1"));
	REQUIRE_NOTHROW(v2 = con.Values({{1, 27}, {2, 8}, {3, 20}}, {"id", "k"}, "v2"));
	REQUIRE_NOTHROW(v3 = con.Values({{1, 2}, {2, 6}, {3, 10}}, {"id", "k"}, "v3"));
	REQUIRE_NOTHROW(result = v1->Join(v2, "v1.id=v2.id")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
	REQUIRE(CHECK_COLUMN(result, 2, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 3, {27, 8, 20}));

	// asof join
	REQUIRE_NOTHROW(v1 = con.Values({{1, 10}, {6, 5}, {8, 4}, {10, 23}, {12, 12}, {15, 14}}, {"id", "j"}, "v1"));
	REQUIRE_NOTHROW(v2 = con.Values({{4, 27}, {8, 8}, {14, 20}}, {"id", "k"}, "v2"));
	REQUIRE_NOTHROW(result = v1->Join(v2, "v1.id>=v2.id", JoinType::INNER, JoinRefType::ASOF)->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {6, 8, 10, 12, 15}));
	REQUIRE(CHECK_COLUMN(result, 1, {5, 4, 23, 12, 14}));
	REQUIRE(CHECK_COLUMN(result, 2, {4, 8, 8, 8, 14}));
	REQUIRE(CHECK_COLUMN(result, 3, {27, 8, 8, 8, 20}));

	REQUIRE_NOTHROW(v1 = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"id", "j"}, "v1"));
	REQUIRE_NOTHROW(v2 = con.Values({{1, 27}, {2, 8}, {3, 20}}, {"id", "k"}, "v2"));
	REQUIRE_NOTHROW(v3 = con.Values({{1, 2}, {2, 6}, {3, 10}}, {"id", "k"}, "v3"));

	// projection after a join
	REQUIRE_NOTHROW(result = v1->Join(v2, "v1.id=v2.id")->Project("v1.id+v2.id, j+k")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 4, 6}));
	REQUIRE(CHECK_COLUMN(result, 1, {37, 13, 24}));

	// chain multiple joins
	auto multi_join = v1->Join(v2, "v1.id=v2.id")->Join(v3, "v1.id=v3.id");
	REQUIRE_NOTHROW(result = multi_join->Project("v1.id+v2.id+v3.id")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {3, 6, 9}));

	// multiple joins followed by a filter and a projection
	REQUIRE_NOTHROW(result = multi_join->Filter("v1.id=1")->Project("v1.id+v2.id+v3.id")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {3}));
	// multiple joins followed by multiple filters
	REQUIRE_NOTHROW(result = multi_join->Filter("v1.id>0")
	                             ->Filter("v2.id < 3")
	                             ->Filter("v3.id=2")
	                             ->Project("v1.id+v2.id+v3.id")
	                             ->Order("1")
	                             ->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {6}));

	// test explain
	REQUIRE_NO_FAIL(multi_join->Explain());

	// incorrect API usage
	REQUIRE_THROWS(tbl->Project(duckdb::vector<string> {})->Execute());
	REQUIRE_THROWS(tbl->Project(duckdb::vector<string> {"1, 2, 3"})->Execute());
	REQUIRE_THROWS(tbl->Project(duckdb::vector<string> {""})->Execute());
	REQUIRE_THROWS(tbl->Filter("i=1, i=2")->Execute());
	REQUIRE_THROWS(tbl->Filter("")->Execute());
	REQUIRE_THROWS(tbl->Filter(duckdb::vector<string> {})->Execute());
	REQUIRE_THROWS(tbl->Filter(duckdb::vector<string> {"1, 2, 3"})->Execute());
	REQUIRE_THROWS(tbl->Filter(duckdb::vector<string> {""})->Execute());
	REQUIRE_THROWS(tbl->Order(duckdb::vector<string> {})->Execute());
	REQUIRE_THROWS(tbl->Order(duckdb::vector<string> {"1, 2, 3"})->Execute());
	REQUIRE_THROWS(tbl->Order("1 LIMIT 3")->Execute());
	REQUIRE_THROWS(tbl->Order("1; SELECT 42")->Execute());
	REQUIRE_THROWS(tbl->Join(tbl, "")->Execute());
	REQUIRE_THROWS(tbl->Join(tbl, "a, a+1")->Execute());
	REQUIRE_THROWS(tbl->Join(tbl, "a, bla.bla")->Execute());
}

TEST_CASE("Test combinations of set operations", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> values, v1, v2, v3;

	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}));

	// union between values
	auto vunion = values->Union(values);
	REQUIRE_NOTHROW(result = vunion->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 2, 2, 3, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 10, 5, 5, 4, 4}));

	// different ops after a union
	// order and limit
	REQUIRE_NOTHROW(result = vunion->Order("i")->Limit(1)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1}));
	REQUIRE(CHECK_COLUMN(result, 1, {10}));
	// multiple orders and limits
	REQUIRE_NOTHROW(result = vunion->Order("i")->Limit(4)->Order("j")->Limit(2)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {5, 5}));
	// filter
	REQUIRE_NOTHROW(result = vunion->Filter("i=1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 10}));
	// multiple filters
	REQUIRE_NOTHROW(result = vunion->Filter("i<3")->Filter("j=5")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {5, 5}));
	// distinct
	REQUIRE_NOTHROW(result = vunion->Distinct()->Order("j DESC")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
	// multiple distincts followed by a top-n
	REQUIRE_NOTHROW(result = vunion->Distinct()->Distinct()->Distinct()->Order("j DESC")->Limit(2)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5}));
	// top-n followed by multiple distincts
	REQUIRE_NOTHROW(result = vunion->Order("j DESC")->Limit(2)->Distinct()->Distinct()->Distinct()->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1}));
	REQUIRE(CHECK_COLUMN(result, 1, {10}));

	// multiple set ops
	REQUIRE_NOTHROW(result = vunion->Union(vunion)->Distinct()->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
	REQUIRE_NOTHROW(result = vunion->Intersect(vunion)->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 2, 2, 3, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 10, 5, 5, 4, 4}));
	REQUIRE_NOTHROW(result = vunion->Except(vunion)->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {}));
	REQUIRE(CHECK_COLUMN(result, 1, {}));

	// setops require the same amount of columns on both sides
	REQUIRE_NOTHROW(v1 = con.Values("(1, 2), (3, 4)"));
	REQUIRE_NOTHROW(v2 = con.Values("(1)"));
	REQUIRE_THROWS(v1->Union(v2)->Execute());

	// setops require the same types on both sides
	REQUIRE_NOTHROW(v1 = con.Values("(DATE '1992-01-01', 2)"));
	REQUIRE_NOTHROW(v2 = con.Values("(3.0, 'hello')"));
	REQUIRE_FAIL(v1->Union(v2)->Execute());
}

TEST_CASE("Test combinations of joins", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> values, vjoin;

	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}));

	auto v1 = values->Alias("v1");
	auto v2 = values->Alias("v2");

	// join on explicit join condition
	vjoin = v1->Join(v2, "v1.i=v2.i");
	REQUIRE_NOTHROW(result = vjoin->Order("v1.i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
	REQUIRE(CHECK_COLUMN(result, 2, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 3, {10, 5, 4}));

	// aggregate on EXPLICIT join
	REQUIRE_NOTHROW(result = vjoin->Aggregate("SUM(v1.i) + SUM(v2.i), SUM(v1.j) + SUM(v2.j)")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {12}));
	REQUIRE(CHECK_COLUMN(result, 1, {38}));

	// implicit join
	vjoin = v1->Join(v2, "i");
	REQUIRE_NOTHROW(result = vjoin->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
	REQUIRE(CHECK_COLUMN(result, 2, {10, 5, 4}));

	// implicit join on multiple columns
	vjoin = v1->Join(v2, "i, j");
	REQUIRE_NOTHROW(result = vjoin->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	// aggregate on USING join
	REQUIRE_NOTHROW(result = vjoin->Aggregate("SUM(i), SUM(j)")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {6}));
	REQUIRE(CHECK_COLUMN(result, 1, {19}));

	// joining on a column that doesn't exist results in an error
	REQUIRE_THROWS(v1->Join(v2, "blabla"));
	// also with explicit join condition
	REQUIRE_THROWS(v1->Join(v2, "v1.i=v2.blabla"));

	// set ops involving joins
	REQUIRE_NOTHROW(result = vjoin->Union(vjoin)->Distinct()->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	// do a bunch of joins in a loop
	auto v1tmp = v1;
	auto v2tmp = v2;
	for (idx_t i = 0; i < 4; i++) {
		REQUIRE_NOTHROW(v1tmp = v1tmp->Join(v2tmp->Alias(to_string(i)), "i, j"));
	}
	REQUIRE_NOTHROW(result = v1tmp->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	// now add on some projections and such
	auto complex_join = v1tmp->Order("i")->Limit(2)->Order("i DESC")->Limit(1)->Project("i+1, j+1");
	for (idx_t i = 0; i < 3; i++) {
		REQUIRE_NOTHROW(result = complex_join->Execute());
		REQUIRE(CHECK_COLUMN(result, 0, {3}));
		REQUIRE(CHECK_COLUMN(result, 1, {6}));
	}

	// create and query a view
	REQUIRE_NOTHROW(complex_join->CreateView("test123"));
	result = con.Query("SELECT * FROM test123");
	REQUIRE(CHECK_COLUMN(result, 0, {3}));
	REQUIRE(CHECK_COLUMN(result, 1, {6}));

	// joins of tables that have output modifiers attached to them (limit, order by, distinct)
	auto v1_modified = v1->Limit(100)->Order("1")->Distinct()->Filter("i<1000");
	auto v2_modified = v2->Limit(100)->Order("1")->Distinct()->Filter("i<1000");
	vjoin = v1_modified->Join(v2_modified, "i, j");
	REQUIRE_NOTHROW(result = vjoin->Order("i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	REQUIRE_NO_FAIL(con.Query("SELECT * FROM sqlite_master"));
}

TEST_CASE("Test crossproduct relation", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> values, vcross;

	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}), "v1");
	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}), "v2");

	auto v1 = values->Alias("v1");
	auto v2 = values->Alias("v2");

	// run cross product
	vcross = v1->CrossProduct(v2);
	REQUIRE_NOTHROW(result = vcross->Order("v1.i, v2.i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 1, 2, 2, 2, 3, 3, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 10, 10, 5, 5, 5, 4, 4, 4}));
	REQUIRE(CHECK_COLUMN(result, 2, {1, 2, 3, 1, 2, 3, 1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 3, {10, 5, 4, 10, 5, 4, 10, 5, 4}));

	// run a positional cross product
	auto join_ref_type = JoinRefType::POSITIONAL;
	vcross = v1->CrossProduct(v2, join_ref_type);
	REQUIRE_NOTHROW(result = vcross->Order("v1.i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
	REQUIRE(CHECK_COLUMN(result, 2, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 3, {10, 5, 4}));
}

TEST_CASE("Test view creation of relations", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> tbl, filter, proj, proj2;

	// create some tables
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NOTHROW(con.Table("integers")->Insert({{Value::INTEGER(1)}, {Value::INTEGER(2)}, {Value::INTEGER(3)}}));

	// insertion failure because of primary key
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE tbl_pk(i INTEGER PRIMARY KEY)"));
	REQUIRE_NOTHROW(con.Table("tbl_pk")->Insert({{Value::INTEGER(1)}, {Value::INTEGER(2)}, {Value::INTEGER(3)}}));
	REQUIRE_THROWS(con.Table("tbl_pk")->Insert({{Value::INTEGER(1)}, {Value::INTEGER(2)}, {Value::INTEGER(3)}}));

	// simple view creation
	REQUIRE_NOTHROW(tbl = con.Table("integers"));
	REQUIRE_NOTHROW(result = tbl->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));

	duckdb::vector<duckdb::unique_ptr<ParsedExpression>> expressions;
	expressions.push_back(duckdb::make_uniq<duckdb::ColumnRefExpression>("i"));
	duckdb::vector<duckdb::string> aliases;
	aliases.push_back("j");

	REQUIRE_NOTHROW(result = tbl->Project(std::move(expressions), aliases)->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	// add a projection
	REQUIRE_NOTHROW(result = tbl->Project("i + 1")->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));

	// multiple projections
	proj = tbl->Project("i + 1", "i");
	for (idx_t i = 0; i < 10; i++) {
		proj = proj->Project("i + 1", "i");
	}
	REQUIRE_NOTHROW(result = proj->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {12, 13, 14}));
	REQUIRE_NOTHROW(result = proj->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {12, 13, 14}));

	// we can also use more complex SQL
	REQUIRE_NOTHROW(result = proj->Query("test", "SELECT SUM(t1.i) FROM test t1 JOIN test t2 ON t1.i=t2.i"));
	REQUIRE(CHECK_COLUMN(result, 0, {39}));

	// limit
	REQUIRE_NOTHROW(result = tbl->Limit(1)->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {1}));
	// order
	REQUIRE_NOTHROW(result = tbl->Order("i DESC")->Limit(1)->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {3}));
	// union
	auto node = tbl->Order("i DESC")->Limit(1);
	REQUIRE_NOTHROW(result = node->Union(node)->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {3, 3}));
	// distinct
	REQUIRE_NOTHROW(result = node->Union(node)->Distinct()->Query("test", "SELECT * FROM test"));
	REQUIRE(CHECK_COLUMN(result, 0, {3}));

	// manually create views and query from them
	result = con.Query("SELECT i+1 FROM integers UNION SELECT i+10 FROM integers ORDER BY 1");
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4, 11, 12, 13}));

	REQUIRE_NOTHROW(tbl->Project("i + 1")->CreateView("test1"));
	REQUIRE_NOTHROW(tbl->Project("i + 10")->CreateView("test2"));
	result = con.Query("SELECT * FROM test1 UNION SELECT * FROM test2 ORDER BY 1");
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4, 11, 12, 13}));

	// project <> alias column count mismatch
	REQUIRE_THROWS(proj->Project("i + 1, i + 2", "i"));
	// view already exists
	REQUIRE_THROWS(tbl->Project("i + 10")->CreateView("test2", false));
	// table already exists
	REQUIRE_THROWS(tbl->Project("i + 10")->Create("test2"));
}

TEST_CASE("Test table creations using the relation API", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> values;

	// create a table from a Values statement
	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}));
	REQUIRE_NOTHROW(values->Create("integers"));

	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	// insert from a set of values
	REQUIRE_NOTHROW(con.Values({{4, 7}, {5, 8}})->Insert("integers"));

	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3, 4, 5}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4, 7, 8}));

	// create a table from a query
	REQUIRE_NOTHROW(
	    con.Table("integers")->Filter("i BETWEEN 3 AND 4")->Project("i + 1 AS k, 'hello' AS l")->Create("new_values"));

	result = con.Query("SELECT * FROM new_values ORDER BY k");
	REQUIRE(CHECK_COLUMN(result, 0, {4, 5}));
	REQUIRE(CHECK_COLUMN(result, 1, {"hello", "hello"}));

	// create a table in an attached db and insert values
	auto test_dir = TestDirectoryPath();
	string db_path = test_dir + "/my_db.db";
	REQUIRE_NO_FAIL(con.Query("ATTACH '" + db_path + "' AS my_db;"));
	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}));
	REQUIRE_NOTHROW(values->Create(std::string("my_db"), std::string(), std::string("integers")));
	result = con.Query("SELECT * FROM my_db.integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));
}

TEST_CASE("Test table creations with on_create_conflict using the relation API", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> values, values1, proj;

	// create a table from a Values statement
	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 5}, {3, 4}}, {"i", "j"}));
	REQUIRE_NOTHROW(values->Create("integers"));

	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	REQUIRE_NOTHROW(values1 = con.Values({{4, 14}, {5, 15}, {6, 16}}, {"i", "j"}));
	REQUIRE_THROWS(values1->Create("integers"));
	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	REQUIRE_THROWS(values1->Create("integers", false, OnCreateConflict::ERROR_ON_CONFLICT));
	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	REQUIRE_NOTHROW(values1->Create("integers", false, OnCreateConflict::IGNORE_ON_CONFLICT));
	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 5, 4}));

	REQUIRE_NOTHROW(values1->Create("integers", false, OnCreateConflict::REPLACE_ON_CONFLICT));
	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {4, 5, 6}));
	REQUIRE(CHECK_COLUMN(result, 1, {14, 15, 16}));
}

TEST_CASE("Test table create from query with on_create_conflict using the relation API", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> values, values1, proj;

	REQUIRE_NOTHROW(values = con.Values({{1, 10}, {2, 20}, {3, 30}, {4, 40}}, {"i", "j"}));
	REQUIRE_NOTHROW(values->Create("integers"));

	result = con.Query("SELECT * FROM integers ORDER BY i");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3, 4}));
	REQUIRE(CHECK_COLUMN(result, 1, {10, 20, 30, 40}));

	REQUIRE_NOTHROW(con.Table("integers")
	                    ->Filter("i BETWEEN 2 AND 3")
	                    ->Project("i + 100 AS k, 'hello' AS l")
	                    ->Create("new_values"));

	result = con.Query("SELECT * FROM new_values ORDER BY k");
	REQUIRE(CHECK_COLUMN(result, 0, {102, 103}));
	REQUIRE(CHECK_COLUMN(result, 1, {"hello", "hello"}));

	REQUIRE_NOTHROW(proj = con.Table("integers")->Filter("i BETWEEN 1 AND 2")->Project("i + 200 AS k, 'hi' AS l"));
	REQUIRE_THROWS(proj->Create("new_values"));
	REQUIRE_THROWS(proj->Create("new_values", false, OnCreateConflict::ERROR_ON_CONFLICT));
	REQUIRE_NOTHROW(proj->Create("new_values", false, OnCreateConflict::IGNORE_ON_CONFLICT));

	result = con.Query("SELECT * FROM new_values ORDER BY k");
	REQUIRE(CHECK_COLUMN(result, 0, {102, 103}));
	REQUIRE(CHECK_COLUMN(result, 1, {"hello", "hello"}));

	REQUIRE_NOTHROW(proj->Create("new_values", false, OnCreateConflict::REPLACE_ON_CONFLICT));
	result = con.Query("SELECT * FROM new_values ORDER BY k");
	REQUIRE(CHECK_COLUMN(result, 0, {201, 202}));
	REQUIRE(CHECK_COLUMN(result, 1, {"hi", "hi"}));
}

TEST_CASE("Test table deletions and updates", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;

	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));

	auto tbl = con.Table("integers");

	// update
	tbl->Update("i=i+10", "i=2");

	result = con.Query("SELECT * FROM integers ORDER BY 1");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 3, 12}));

	// we can only have a single expression in the condition liset
	REQUIRE_THROWS(tbl->Update("i=1", "i=3,i<100"));

	tbl->Delete("i=3");

	result = con.Query("SELECT * FROM integers ORDER BY 1");
	REQUIRE(CHECK_COLUMN(result, 0, {1, 12}));

	// delete without condition
	tbl->Delete();

	result = con.Query("SELECT * FROM integers ORDER BY 1");
	REQUIRE(CHECK_COLUMN(result, 0, {}));

	// we cannot run update/delete on anything but base table relations
	REQUIRE_THROWS(tbl->Limit(1)->Delete());
	REQUIRE_THROWS(tbl->Limit(1)->Update("i=1"));
}

TEST_CASE("Test aggregates in relation API", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;

	// create a table
	REQUIRE_NOTHROW(con.Values("(1, 5), (2, 6), (1, 7)", {"i", "j"})->Create("integers"));

	// perform some aggregates
	auto tbl = con.Table("integers");

	// ungrouped aggregate
	REQUIRE_NOTHROW(result = tbl->Aggregate("SUM(i), SUM(j)")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {4}));
	REQUIRE(CHECK_COLUMN(result, 1, {18}));

	REQUIRE_NOTHROW(result = tbl->Aggregate(duckdb::vector<string> {"SUM(i)", "SUM(j)"})->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {4}));
	REQUIRE(CHECK_COLUMN(result, 1, {18}));

	// we cannot put aggregates in a Project clause
	REQUIRE_THROWS(result = tbl->Project("SUM(i), SUM(j)")->Execute());
	REQUIRE_THROWS(result = tbl->Project("i, SUM(j)")->Execute());
	// implicitly grouped aggregate
	REQUIRE_NOTHROW(result = tbl->Aggregate("i, SUM(j)")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {12, 6}));
	REQUIRE_NOTHROW(result = tbl->Aggregate("SUM(j), i")->Order("2")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {12, 6}));
	REQUIRE(CHECK_COLUMN(result, 1, {1, 2}));
	// explicitly grouped aggregate
	REQUIRE_NOTHROW(
	    result =
	        tbl->Aggregate(duckdb::vector<string> {"SUM(j)"}, duckdb::vector<string> {"i"})->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {6, 12}));

	// grouped aggregates can be expressions
	REQUIRE_NOTHROW(result = tbl->Aggregate("i+1 AS i, SUM(j)")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3}));
	REQUIRE(CHECK_COLUMN(result, 1, {12, 6}));
	// they can also involve multiple columns
	REQUIRE_NOTHROW(result = tbl->Aggregate("i+i AS i, SUM(j)")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 4}));
	REQUIRE(CHECK_COLUMN(result, 1, {12, 6}));
	// we cannot combine non-aggregates with aggregates
	REQUIRE_THROWS(result = tbl->Aggregate("i + SUM(j) AS i")->Order("1")->Execute());
	REQUIRE_THROWS(result = tbl->Aggregate("i, i + SUM(j)")->Order("1")->Execute());
	// group by multiple columns
	REQUIRE_NOTHROW(result = tbl->Aggregate("i, j, SUM(i + j)")->Order("1, 2")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {5, 7, 6}));
	REQUIRE(CHECK_COLUMN(result, 2, {6, 8, 8}));
	// subqueries as groups
	REQUIRE_NOTHROW(result = tbl->Aggregate("(SELECT i), SUM(i + j)")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {14, 8}));
	// subqueries as aggregates
	REQUIRE_NOTHROW(result = tbl->Aggregate("(SELECT i), (SELECT SUM(i + j))")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {14, 8}));
	// constants without a grouping column
	REQUIRE_NOTHROW(result = tbl->Aggregate("'hello', SUM(i + j)")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {"hello"}));
	REQUIRE(CHECK_COLUMN(result, 1, {22}));
	// constants with a grouping column
	REQUIRE_NOTHROW(result = tbl->Aggregate("i, 'hello', SUM(i + j)")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {"hello", "hello"}));
	REQUIRE(CHECK_COLUMN(result, 2, {14, 8}));
	// aggregate with only non-aggregate columns becomes a distinct
	REQUIRE_NOTHROW(result = tbl->Aggregate("i")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE_NOTHROW(result = tbl->Aggregate("i, j")->Order("1, 2")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {5, 7, 6}));

	// now test aggregates with explicit groups
	REQUIRE_NOTHROW(result = tbl->Aggregate("i", "i")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE_NOTHROW(result = tbl->Aggregate("i, SUM(j)", "i")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {12, 6}));
	// explicit groups can be combined with aggregates
	REQUIRE_NOTHROW(result = tbl->Aggregate("i, i+SUM(j)", "i")->Order("1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2}));
	REQUIRE(CHECK_COLUMN(result, 1, {13, 8}));
	// when using explicit groups, we cannot have non-explicit groups
	REQUIRE_THROWS(tbl->Aggregate("j, i+SUM(j)", "i")->Order("1")->Execute());

	// Coverage: Groups expressions can not create multiple statements
	REQUIRE_THROWS(tbl->Aggregate("i", "i; select 42")->Execute());

	// project -> aggregate -> project -> aggregate
	// SUM(j) = 18 -> 18 + 1 = 19 -> 19 * 2 = 38
	result = tbl->Aggregate("SUM(j) AS k")->Project("k+1 AS l")->Aggregate("SUM(l) AS m")->Project("m*2")->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {38}));

	// aggregate after output modifiers
	result = tbl->Order("i")->Limit(100)->Aggregate("SUM(j) AS k")->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {18}));
}

TEST_CASE("Test interaction of relations with transactions", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con1(db), con2(db);
	duckdb::unique_ptr<QueryResult> result;

	con1.BeginTransaction();
	con2.BeginTransaction();

	// create a table in con1
	REQUIRE_NOTHROW(con1.Values("(1), (2), (3)")->Create("integers"));

	// con1 can see it, but con2 can't see it yet
	REQUIRE_NOTHROW(con1.Table("integers"));
	REQUIRE_THROWS(con2.Table("integers"));

	// we can also rollback
	con1.Rollback();

	REQUIRE_THROWS(con1.Table("integers"));
	REQUIRE_THROWS(con2.Table("integers"));

	// recreate the table, this time in auto-commit mode
	REQUIRE_NOTHROW(con1.Values("(1), (2), (3)")->Create("integers"));

	// con2 still can't see it, because it is in its own transaction
	REQUIRE_NOTHROW(con1.Table("integers"));
	REQUIRE_THROWS(con2.Table("integers"));

	// after con2 commits, both can see the table
	con2.Commit();

	REQUIRE_NOTHROW(con1.Table("integers"));
	REQUIRE_NOTHROW(con2.Table("integers"));
}

TEST_CASE("Test interaction of relations with schema changes", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	duckdb::unique_ptr<QueryResult> result;

	// create some tables
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));

	// create a table scan of the integers table
	auto tbl_scan = con.Table("integers")->Project("i+1")->Order("1");
	REQUIRE_NOTHROW(result = tbl_scan->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));

	// now drop the table
	REQUIRE_NO_FAIL(con.Query("DROP TABLE integers"));

	// the scan now fails, because the table is dropped!
	REQUIRE_FAIL(tbl_scan->Execute());

	// if we recreate the table, it works again
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));

	REQUIRE_NOTHROW(result = tbl_scan->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));

	// but what if we recreate an incompatible table?
	REQUIRE_NO_FAIL(con.Query("DROP TABLE integers"));
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i VARCHAR, j INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES ('hello', 3)"));

	// this results in a binding error!
	REQUIRE_FAIL(tbl_scan->Execute());

	// now what if we run a query that still binds successfully, but changes result?
	auto tbl = con.Table("integers");
	REQUIRE_NO_FAIL(tbl->Execute());

	// add extra columns
	REQUIRE_NO_FAIL(con.Query("DROP TABLE integers"));
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i VARCHAR, j VARCHAR)"));
	REQUIRE_FAIL(tbl->Execute());

	// change type of column
	REQUIRE_NO_FAIL(con.Query("DROP TABLE integers"));
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i DATE, j INTEGER)"));
	REQUIRE_FAIL(tbl->Execute());

	// different name also results in an error
	REQUIRE_NO_FAIL(con.Query("DROP TABLE integers"));
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(k VARCHAR, j INTEGER)"));
	REQUIRE_FAIL(tbl->Execute());

	// but once we go back to the original table it works again!
	REQUIRE_NO_FAIL(con.Query("DROP TABLE integers"));
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i VARCHAR, j INTEGER)"));
	REQUIRE_NO_FAIL(tbl->Execute());
}

TEST_CASE("Test junk SQL in expressions", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	duckdb::unique_ptr<QueryResult> result;

	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));

	auto tbl = con.Table("integers");
	REQUIRE_THROWS(tbl->Filter("1=1; DELETE FROM tables;"));
	REQUIRE_THROWS(tbl->Filter("1=1 UNION SELECT 42"));
	REQUIRE_THROWS(tbl->Order("1 DESC; DELETE FROM tables;"));
	REQUIRE_THROWS(tbl->Update("i=1; DELETE FROM TABLES"));
	REQUIRE_THROWS(con.Values("(1, 1); SELECT 42"));
	REQUIRE_THROWS(con.Values("(1, 1) UNION SELECT 42"));
}

TEST_CASE("We cannot mix statements from multiple databases", "[relation_api]") {
	DuckDB db(nullptr), db2(nullptr);
	Connection con(db), con2(db2);
	duckdb::unique_ptr<QueryResult> result;

	// create some tables
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));
	REQUIRE_NO_FAIL(con2.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con2.Query("INSERT INTO integers VALUES (4)"));

	auto i1 = con.Table("integers");
	auto i2 = con2.Table("integers");

	// we cannot mix statements from different connections without a wrapper!
	REQUIRE_THROWS(i2->Union(i1));
	REQUIRE_THROWS(i2->Except(i1));
	REQUIRE_THROWS(i2->Intersect(i1));
	REQUIRE_THROWS(i2->Join(i1, "i"));
	REQUIRE_THROWS(i2->Join(i1, "i1.i=i2.i"));

	// FIXME: what about a wrapper to scan data from other databases/connections?
}

TEST_CASE("Test view relations", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;

	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));
	REQUIRE_NO_FAIL(con.Query("CREATE VIEW v1 AS SELECT i+1 AS i, i+2 FROM integers"));
	REQUIRE_NO_FAIL(con.Query("CREATE VIEW v2(a,b) AS SELECT i+1, i+2 FROM integers"));

	auto i1 = con.View("v1");
	result = i1->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));
	REQUIRE(CHECK_COLUMN(result, 1, {3, 4, 5}));

	result = con.View("v2")->Project("a+b")->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {5, 7, 9}));

	// non-existant view
	REQUIRE_THROWS(con.View("blabla"));

	// combining views
	result = con.View("v1")->Join(con.View("v2"), "v1.i=v2.a")->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3, 4}));
	REQUIRE(CHECK_COLUMN(result, 1, {3, 4, 5}));
	REQUIRE(CHECK_COLUMN(result, 2, {2, 3, 4}));
	REQUIRE(CHECK_COLUMN(result, 3, {3, 4, 5}));
}

TEST_CASE("Test table function relations", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;

	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));

	auto i1 = con.TableFunction("duckdb_tables");
	result = i1->Execute();
	REQUIRE(CHECK_COLUMN(result, 2, {"main"}));

	// function with parameters
	auto i2 = con.TableFunction("pragma_table_info", {"integers"});
	result = i2->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {0}));
	REQUIRE(CHECK_COLUMN(result, 1, {"i"}));
	REQUIRE(CHECK_COLUMN(result, 2, {"INTEGER"}));

	// we can do ops on table functions
	result = i2->Filter("cid=0")->Project("concat(name, ' ', type), length(name), reverse(lower(type))")->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {"i INTEGER"}));
	REQUIRE(CHECK_COLUMN(result, 1, {1}));
	REQUIRE(CHECK_COLUMN(result, 2, {"regetni"}));

	// table function that takes a relation as input
	auto values = con.Values("(42)", {"i"});
	auto summary = values->TableFunction("summary", duckdb::vector<Value> {});
	result = summary->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {"[42]"}));
	REQUIRE(CHECK_COLUMN(result, 1, {"42"}));

	// non-existant table function
	REQUIRE_THROWS(con.TableFunction("blabla"));
}

TEST_CASE("Test CSV Relation with union by name", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	named_parameter_map_t options;
	options["union_by_name"] = Value(true);
	duckdb::vector<string> paths {"data/csv/sample-0.csv", "data/csv/sample-0.csv"};
	auto csv_scan = con.ReadCSV(paths, std::move(options));
	auto result = csv_scan->Execute();
	REQUIRE(!result->HasError());
	REQUIRE(CHECK_COLUMN(result, 0, {Value::DATE(2024, 1, 2), Value::DATE(2024, 1, 2), Value::DATE(2024, 1, 2)}));

	options["union_by_name"] = Value(true);
	paths = {"data/csv/union-by-name/ubn1.csv", "data/csv/union-by-name/ubn2.csv"};
	csv_scan = con.ReadCSV(paths, std::move(options));
	result = csv_scan->Execute();
	REQUIRE(!result->HasError());
}

TEST_CASE("Test CSV reading/writing from relations", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;

	// write a bunch of values to a CSV
	auto csv_file = TestCreatePath("relationtest.csv");

	case_insensitive_map_t<duckdb::vector<Value>> options;
	options["header"] = {duckdb::Value(0)};
	con.Values("(1), (2), (3)", {"i"})->WriteCSV(csv_file, options);
	REQUIRE_THROWS(con.Values("(1), (2), (3)", {"i"})->WriteCSV("//fef//gw/g/bla/bla", options));

	// now scan the CSV file
	auto csv_scan = con.ReadCSV(csv_file, {"i INTEGER"});
	result = csv_scan->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));

	// with auto detect
	auto auto_csv_scan = con.ReadCSV(csv_file);
	result = auto_csv_scan->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {1, 2, 3}));

	REQUIRE_THROWS(con.ReadCSV(csv_file, {"i INTEGER); SELECT 42;--"}));
	REQUIRE_THROWS(con.ReadCSV(csv_file, {"i INTEGER, j INTEGER"}));
}

TEST_CASE("Test CSV reading from weather.csv", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;

	auto auto_csv_scan = con.ReadCSV("data/csv/weather.csv")->Limit(1);
	result = auto_csv_scan->Execute();
	REQUIRE(CHECK_COLUMN(result, 0, {"2016-01-01"}));
}

TEST_CASE("Test query relation", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> tbl;

	// create some tables
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));
	REQUIRE_NO_FAIL(con.Query("INSERT INTO integers VALUES (1), (2), (3)"));

	REQUIRE_NOTHROW(tbl = con.RelationFromQuery("SELECT * FROM integers WHERE i >= 2"));
	REQUIRE_NOTHROW(result = tbl->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2, 3}));
	REQUIRE_NOTHROW(result = tbl->Project("i + 1")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {3, 4}));

	REQUIRE_NOTHROW(result = tbl->Alias("q1")->Join(tbl->Alias("q2")->Filter("i=3"), "q1.i=q2.i")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {3}));

	REQUIRE_THROWS(tbl->Project("k+1"));
	// multiple queries
	REQUIRE_THROWS(con.RelationFromQuery("SELECT 42; SELECT 84"));
	// not a select statement
	REQUIRE_THROWS(con.RelationFromQuery("DELETE FROM tbl"));
}

TEST_CASE("Test TopK relation", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);
	con.EnableQueryVerification();
	duckdb::unique_ptr<QueryResult> result;
	duckdb::shared_ptr<Relation> tbl;

	REQUIRE_NO_FAIL(con.Query("CREATE TABLE test (i integer,j VARCHAR, k varchar )"));
	REQUIRE_NO_FAIL(con.Query("insert into test values (10,'a','a'), (20,'a','b')"));

	REQUIRE_NOTHROW(tbl = con.Table("test"));
	REQUIRE_NOTHROW(tbl = tbl->Filter("k < 'f' and k is not null")
	                          ->Project("#3,#2,#1")
	                          ->Project("#2,#3")
	                          ->Order("(#2-10)::UTINYINT ASC")
	                          ->Limit(1));
}

TEST_CASE("Test Relation Pending Query API", "[relation_api]") {
	DuckDB db;
	Connection con(db);

	SECTION("Materialized result") {
		auto tbl = con.TableFunction("range", {Value(1000000)});
		auto aggr = tbl->Aggregate("SUM(range)");
		auto pending_query = con.context->PendingQuery(aggr, false);
		REQUIRE(!pending_query->HasError());
		auto result = pending_query->Execute();
		REQUIRE(CHECK_COLUMN(result, 0, {Value::BIGINT(499999500000)}));

		// cannot fetch twice from the same pending query
		REQUIRE_THROWS(pending_query->Execute());
		REQUIRE_THROWS(pending_query->Execute());

		// query the connection as normal after
		result = con.Query("SELECT 42");
		REQUIRE(CHECK_COLUMN(result, 0, {42}));
	}
	SECTION("Runtime error in pending query (materialized)") {
		auto tbl = con.TableFunction("range", {Value(1000000)});
		auto aggr = tbl->Aggregate("SUM(range) AS s")->Project("concat(s::varchar, 'hello')::int");
		// this succeeds initially
		auto pending_query = con.context->PendingQuery(aggr, false);
		REQUIRE(!pending_query->HasError());
		// we only encounter the failure later on as we are executing the query
		auto result = pending_query->Execute();
		REQUIRE_FAIL(result);

		// query the connection as normal after
		result = con.Query("SELECT 42");
		REQUIRE(CHECK_COLUMN(result, 0, {42}));
	}
}

TEST_CASE("Test Relation Query setting query", "[relation_api]") {
	DuckDB db;
	Connection con(db);

	auto query = con.RelationFromQuery("SELECT current_query()");
	auto result = query->Limit(1)->Execute();
	REQUIRE(!result->Fetch()->GetValue(0, 0).ToString().empty());
}

TEST_CASE("Construct ValueRelation with RelationContextWrapper and operate on it", "[relation_api][txn][wrapper]") {
	DuckDB db;
	Connection con(db);
	con.EnableQueryVerification();

	// Build expressions to force the "expressions" overload (not the constants path)
	duckdb::vector<duckdb::vector<duckdb::unique_ptr<duckdb::ParsedExpression>>> expressions;
	{
		duckdb::vector<duckdb::unique_ptr<duckdb::ParsedExpression>> row;

		{
			duckdb::ConstantExpression ce1(duckdb::Value::INTEGER(1));
			row.push_back(ce1.Copy());
		}
		{
			duckdb::ConstantExpression ce2(duckdb::Value::INTEGER(2));
			row.push_back(ce2.Copy());
		}
		expressions.push_back(std::move(row));
	}

	// Explicitly create a RelationContextWrapper from the client's context
	auto rcw = duckdb::make_shared_ptr<duckdb::RelationContextWrapper>(con.context);

	// Manually construct a ValueRelation using the RelationContextWrapper + expressions ctor
	duckdb::shared_ptr<duckdb::Relation> rel;
	REQUIRE_NOTHROW(rel = duckdb::make_shared_ptr<duckdb::ValueRelation>(rcw, std::move(expressions),
	                                                                     duckdb::vector<std::string> {}, "vr"));

	// Base relation should execute (1 row, 2 columns)
	duckdb::unique_ptr<QueryResult> result;
	REQUIRE_NOTHROW(result = rel->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1}));
	REQUIRE(CHECK_COLUMN(result, 1, {2}));

	// Project on top — should bind & execute under a valid transaction
	REQUIRE_NOTHROW(result = rel->Project("1+1, 2+2")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {2}));
	REQUIRE(CHECK_COLUMN(result, 1, {4}));

	// Aggregate on top — also must bind & execute cleanly
	REQUIRE_NOTHROW(result = rel->Aggregate("count(*)")->Execute());
	REQUIRE(CHECK_COLUMN(result, 0, {1}));
}

TEST_CASE("Test materialized relations", "[relation_api]") {
	auto db_path = TestCreatePath("relational_api_materialized_view.db");
	{
		DuckDB db(db_path);
		Connection con(db);
		con.EnableQueryVerification();

		REQUIRE_NO_FAIL(con.Query("create table tbl(a varchar);"));

		auto result = con.Query("insert into tbl values ('test') returning *");
		auto &materialized_result = result->Cast<MaterializedQueryResult>();
		auto materialized_relation = make_shared_ptr<MaterializedRelation>(
		    con.context, materialized_result.TakeCollection(), result->names, "vw");
		materialized_relation->CreateView("vw");
		materialized_relation.reset();

		result = con.Query("SELECT * FROM vw");
		REQUIRE(CHECK_COLUMN(result, 0, {"test"}));
	}
	// reset and query again
	{
		DuckDB db(db_path);
		Connection con(db);
		auto result = con.Query("SELECT * FROM vw");
		REQUIRE(CHECK_COLUMN(result, 0, {"test"}));
	}
}

TEST_CASE("Test create table with empty name", "[relation_api]") {
	DuckDB db(nullptr);
	Connection con(db);

	duckdb::unique_ptr<QueryResult> result = con.Query("CREATE TABLE '' AS SELECT 42;");
	REQUIRE_FAIL(result);

	auto values = con.Values("(42)");
	REQUIRE_THROWS_AS(values->Create(""), ParserException);
}