-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathpgcatalog_test.go
More file actions
6177 lines (6018 loc) · 206 KB
/
pgcatalog_test.go
File metadata and controls
6177 lines (6018 loc) · 206 KB
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
package _go
import (
"testing"
"github.com/dolthub/go-mysql-server/sql"
)
func TestPgAggregate(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_aggregate",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_aggregate";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_aggregate";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_aggregate";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT aggfnoid FROM PG_catalog.pg_AGGREGATE ORDER BY aggfnoid;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgAm(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_am",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_am";`,
Expected: []sql.Row{
{2, "heap", "heap_tableam_handler", "t"},
{403, "btree", "bthandler", "i"},
{405, "hash", "hashhandler", "i"},
{783, "gist", "gisthandler", "i"},
{2742, "gin", "ginhandler", "i"},
{4000, "spgist", "spghandler", "i"},
{3580, "brin", "brinhandler", "i"},
},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_am";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_am";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT amname FROM PG_catalog.pg_AM ORDER BY amname;",
Expected: []sql.Row{{"brin"}, {"btree"}, {"gin"}, {"gist"}, {"hash"}, {"heap"}, {"spgist"}},
},
},
},
})
}
func TestPgAmop(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_amop",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_amop";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_amop";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_amop";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT oid FROM PG_catalog.pg_AMOP ORDER BY oid;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgAmproc(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_amproc",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_amproc";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_amproc";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_amproc";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT oid FROM PG_catalog.pg_AMPROC ORDER BY oid;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgAttribute(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_attribute",
SetUpScript: []string{
`CREATE SCHEMA testschema;`,
`SET search_path TO testschema;`,
`CREATE TABLE test (pk INT primary key, v1 TEXT DEFAULT 'hey');`,
`CREATE TABLE test2 (pk INT primary key, pktesting INT REFERENCES test(pk), v1 TEXT);`,
// Should show attributes for all schemas
`CREATE SCHEMA testschema2;`,
`SET search_path TO testschema2;`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_attribute" WHERE attname='pk' AND attrelid='testschema.test'::regclass;`,
Expected: []sql.Row{{2502341994, "pk", 23, 0, 1, -1, -1, 0, "f", "i", "p", "", "t", "f", "f", "", "", "f", "t", 0, -1, 0, nil, nil, nil, nil}},
},
{
Query: `SELECT * FROM "pg_catalog"."pg_attribute" WHERE attname='v1' AND attrelid='testschema.test'::regclass;`,
Expected: []sql.Row{{2502341994, "v1", 25, 0, 2, -1, -1, 0, "f", "i", "p", "", "f", "t", "f", "", "", "f", "t", 0, -1, 0, nil, nil, nil, nil}},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_attribute";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_attribute";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT attname FROM PG_catalog.pg_ATTRIBUTE ORDER BY attname LIMIT 3;",
Expected: []sql.Row{
{"ACTION_CONDITION"},
{"ACTION_ORDER"},
{"ACTION_ORIENTATION"},
},
},
{
Query: `SELECT attname FROM "pg_catalog"."pg_attribute" a
JOIN "pg_catalog"."pg_class" c ON a.attrelid = c.oid
WHERE c.relname = 'test';`,
Expected: []sql.Row{
{"pk"},
{"v1"},
},
},
{
Query: `SELECT count(*) FROM pg_attribute as a1
WHERE a1.attrelid = 0 OR a1.atttypid = 0 OR a1.attnum = 0 OR
a1.attcacheoff != -1 OR a1.attinhcount < 0 OR
(a1.attinhcount = 0 AND NOT a1.attislocal);`,
Expected: []sql.Row{{0}},
},
{
Query: `SELECT "con"."conname" AS "constraint_name",
"con"."nspname" AS "table_schema",
"con"."relname" AS "table_name",
"con"."confdeltype" AS "on_delete",
"con"."confupdtype" AS "on_update",
"con"."condeferrable" AS "deferrable",
"con"."condeferred" AS "deferred",
"con"."parent" as "parent",
"con"."child" as "child"
FROM
( SELECT UNNEST ("con1"."conkey") AS "parent",
UNNEST ("con1"."confkey") AS "child",
"con1"."confrelid",
"con1"."conrelid",
"con1"."conname",
"con1"."contype",
"ns"."nspname",
"cl"."relname",
"con1"."condeferrable",
CASE
WHEN "con1"."condeferred" THEN 'INITIALLY DEFERRED'
ELSE 'INITIALLY IMMEDIATE'
END as condeferred,
CASE "con1"."confdeltype"
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
END as "confdeltype",
CASE "con1"."confupdtype"
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
END as "confupdtype"
FROM "pg_class" "cl"
INNER JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid"
INNER JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid"
WHERE "con1"."contype" = 'f'
AND (("ns"."nspname" = 'testschema' AND "cl"."relname" = 'test2')) ) "con" order by 1`,
Expected: []sql.Row{
{"test2_pktesting_fkey", "testschema", "test2", "NO ACTION", "NO ACTION", "f", "INITIALLY IMMEDIATE", 2, 1},
},
},
{
Query: `SELECT "con"."conname" AS "constraint_name",
"con"."nspname" AS "table_schema",
"con"."relname" AS "table_name",
"att2"."attname" AS "column_name",
"ns"."nspname" AS "referenced_table_schema",
"cl"."relname" AS "referenced_table_name",
"att"."attname" AS "referenced_column_name",
"con"."confdeltype" AS "on_delete",
"con"."confupdtype" AS "on_update",
"con"."condeferrable" AS "deferrable",
"con"."condeferred" AS "deferred"
FROM
( SELECT UNNEST ("con1"."conkey") AS "parent",
UNNEST ("con1"."confkey") AS "child",
"con1"."confrelid",
"con1"."conrelid",
"con1"."conname",
"con1"."contype",
"ns"."nspname",
"cl"."relname",
"con1"."condeferrable",
CASE
WHEN "con1"."condeferred" THEN 'INITIALLY DEFERRED'
ELSE 'INITIALLY IMMEDIATE'
END as condeferred,
CASE "con1"."confdeltype"
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
END as "confdeltype",
CASE "con1"."confupdtype"
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
END as "confupdtype"
FROM "pg_class" "cl"
INNER JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid"
INNER JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid"
WHERE "con1"."contype" = 'f'
AND (("ns"."nspname" = 'testschema' AND "cl"."relname" = 'test2')) ) "con"
INNER JOIN "pg_attribute" "att" ON "att"."attrelid" = "con"."confrelid" AND "att"."attnum" = "con"."child"
INNER JOIN "pg_class" "cl" ON "cl"."oid" = "con"."confrelid" AND "cl"."relispartition" = 'f'
INNER JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid"
INNER JOIN "pg_attribute" "att2" ON "att2"."attrelid" = "con"."conrelid" AND "att2"."attnum" = "con"."parent"
order by 1,2;`,
Expected: []sql.Row{
{"test2_pktesting_fkey", "testschema", "test2", "pktesting", "testschema", "test", "pk", "NO ACTION", "NO ACTION", "f", "INITIALLY IMMEDIATE"},
},
},
},
},
})
}
func TestPgAttrdef(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_attrdef",
SetUpScript: []string{
`CREATE SCHEMA testschema;`,
`SET search_path TO testschema;`,
`CREATE TABLE test (pk INT primary key, v1 TEXT DEFAULT 'hey');`,
// Should show attributes for all schemas
`CREATE SCHEMA testschema2;`,
`SET search_path TO testschema2;`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_attrdef" WHERE adrelid='testschema.test'::regclass;`,
Expected: []sql.Row{
{597021512, 2502341994, 2, nil},
},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_attrdef";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_attrdef";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT oid FROM PG_catalog.pg_ATTRDEF ORDER BY oid;",
Expected: []sql.Row{
{597021512},
},
},
},
},
})
}
func TestPgAuthMembers(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_auth_members",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_auth_members";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_auth_members";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_auth_members";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT member FROM PG_catalog.pg_AUTH_MEMBERS ORDER BY member;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgAuthid(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_authid",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_authid";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_authid";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_authid";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT rolname FROM PG_catalog.pg_AUTHID ORDER BY rolname;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgAvailableExtensionVersions(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_available_extension_versions",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_available_extension_versions";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_available_extension_versions";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_available_extension_versions";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT name FROM PG_catalog.pg_AVAILABLE_EXTENSION_VERSIONS ORDER BY name;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgAvailableExtensions(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_available_extensions",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_available_extensions";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_available_extensions";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_available_extensions";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT name FROM PG_catalog.pg_AVAILABLE_EXTENSIONS ORDER BY name;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgBackendMemoryContexts(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_backend_memory_contexts",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_backend_memory_contexts";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_backend_memory_contexts";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_backend_memory_contexts";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT name FROM PG_catalog.pg_BACKEND_MEMORY_CONTEXTS ORDER BY name;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgCast(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_cast",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_cast";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_cast";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_cast";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT oid FROM PG_catalog.pg_CAST ORDER BY oid;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgClass(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_class",
SetUpScript: []string{
`CREATE SCHEMA testschema;`,
`SET search_path TO testschema;`,
`CREATE TABLE testing (pk INT primary key, v1 INT UNIQUE);`,
`CREATE VIEW testview AS SELECT * FROM testing LIMIT 1;`,
// Should show classes for all schemas
`CREATE SCHEMA testschema2;`,
`SET search_path TO testschema2;`,
},
Assertions: []ScriptTestAssertion{
// Table
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE relname='testing' order by 1;`,
Expected: []sql.Row{
{3120782595, "testing", 2638679668, 0, 0, 0, 2, 0, 0, 0, float32(0), 0, 0, "t", "f", "p", "r", 0, 0, "f", "f", "f", "f", "f", "t", "d", "f", 0, 0, 0, nil, nil, nil},
},
},
// Index
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE relname='testing_pkey';`,
Expected: []sql.Row{
{1067629180, "testing_pkey", 2638679668, 0, 0, 0, 403, 0, 0, 0, float32(0), 0, 0, "f", "f", "p", "i", 0, 0, "f", "f", "f", "f", "f", "t", "d", "f", 0, 0, 0, nil, nil, nil},
},
},
// View
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE relname='testview';`,
Expected: []sql.Row{
{887295443, "testview", 2638679668, 0, 0, 0, 0, 0, 0, 0, float32(0), 0, 0, "f", "f", "p", "v", 0, 0, "f", "f", "f", "f", "f", "t", "d", "f", 0, 0, 0, nil, nil, nil},
},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_class";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_class";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT relname FROM PG_catalog.pg_CLASS where relnamespace not in (select oid from pg_namespace where nspname = 'dolt') ORDER BY relname ASC LIMIT 3;",
Expected: []sql.Row{
{"administrable_role_authorizations"},
{"applicable_roles"},
{"character_sets"},
},
},
{
Query: "SELECT relname from pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = 'testschema' and left(relname, 5) <> 'dolt_' ORDER BY relname;",
Expected: []sql.Row{
{"testing"},
{"testing_pkey"},
{"testview"},
{"v1"},
},
},
{
Query: "SELECT relname from pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = 'pg_catalog' LIMIT 3;",
Expected: []sql.Row{
{"pg_aggregate"},
{"pg_am"},
{"pg_amop"},
},
},
{
Query: `SELECT relname FROM "pg_class" WHERE relname='testing';`,
Expected: []sql.Row{
{"testing"},
},
},
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE oid=1234`,
Expected: []sql.Row{},
},
},
},
{
Name: "pg_class with regclass",
SetUpScript: []string{
`CREATE SCHEMA testschema;`,
`SET search_path TO testschema;`,
`CREATE TABLE testing (pk INT primary key, v1 INT UNIQUE);`,
`CREATE VIEW testview AS SELECT * FROM testing LIMIT 1;`,
`CREATE SCHEMA testschema2;`,
`SET search_path TO testschema2;`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE oid='testing'::regclass;`,
ExpectedErr: "does not exist",
},
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE oid='testschema.testing'::regclass;`,
Expected: []sql.Row{
{3120782595, "testing", 2638679668, 0, 0, 0, 2, 0, 0, 0, float32(0), 0, 0, "t", "f", "p", "r", 0, 0, "f", "f", "f", "f", "f", "t", "d", "f", 0, 0, 0, nil, nil, nil},
},
},
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE oid='testschema.testing_pkey'::regclass;`,
Expected: []sql.Row{
{1067629180, "testing_pkey", 2638679668, 0, 0, 0, 403, 0, 0, 0, float32(0), 0, 0, "f", "f", "p", "i", 0, 0, "f", "f", "f", "f", "f", "t", "d", "f", 0, 0, 0, nil, nil, nil},
},
},
{
Query: `SELECT * FROM "pg_catalog"."pg_class" WHERE oid='testschema.testview'::regclass;`,
Expected: []sql.Row{
{887295443, "testview", 2638679668, 0, 0, 0, 0, 0, 0, 0, float32(0), 0, 0, "f", "f", "p", "v", 0, 0, "f", "f", "f", "f", "f", "t", "d", "f", 0, 0, 0, nil, nil, nil},
},
},
},
},
{
Name: "pg_class joined with other pg_catalog tables to retrieve indexes",
SetUpScript: []string{
`CREATE TABLE foo (a INTEGER NOT NULL PRIMARY KEY, b INTEGER NULL);`,
`CREATE INDEX ON foo ( b ASC ) NULLS NOT DISTINCT;`,
`CREATE INDEX ON foo ( b ASC , a DESC ) NULLS NOT DISTINCT;`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT ix.relname AS index_name, upper(am.amname) AS index_algorithm FROM pg_index i
JOIN pg_class t ON t.oid = i.indrelid
JOIN pg_class ix ON ix.oid = i.indexrelid
JOIN pg_namespace n ON t.relnamespace = n.oid
JOIN pg_am AS am ON ix.relam = am.oid WHERE t.relname = 'foo' AND n.nspname = 'public';`,
Expected: []sql.Row{{"foo_pkey", "BTREE"}, {"b", "BTREE"}, {"b_2", "BTREE"}}, // TODO: should follow Postgres index naming convention: "foo_pkey", "foo_b_idx", "foo_b_a_idx"
},
},
},
})
}
func TestPgCollation(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_collation",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_collation";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_collation";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_collation";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT collname FROM PG_catalog.pg_COLLATION ORDER BY collname;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgConfig(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_config",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_config";`,
Expected: []sql.Row{},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_config";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_config";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT name FROM PG_catalog.pg_CONFIG ORDER BY name;",
Expected: []sql.Row{},
},
},
},
})
}
func TestPgConstraint(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_constraint",
SetUpScript: []string{
`CREATE TABLE testing (pk INT primary key, v1 INT UNIQUE);`,
`CREATE TABLE testing2 (pk INT primary key, pktesting INT REFERENCES testing(pk), v1 TEXT);`,
`ALTER TABLE testing2 ADD CONSTRAINT v1_check CHECK (v1 != '')`,
},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM "pg_catalog"."pg_constraint" WHERE conrelid='testing2'::regclass OR conrelid='testing'::regclass order by 1`,
Expected: []sql.Row{
{1719906648, "testing2_pktesting_fkey", 2200, "f", "f", "f", "t", 2694106299, 0, 1719906648, 0, 2147906242, "a", "a", "s", "t", 0, "t", "{2}", "{1}", nil, nil, nil, nil, nil, nil},
{2068729390, "testing2_pkey", 2200, "p", "f", "f", "t", 2694106299, 0, 2068729390, 0, 0, "", "", "", "t", 0, "t", "{1}", nil, nil, nil, nil, nil, nil, nil},
{3050361446, "v1", 2200, "u", "f", "f", "t", 2147906242, 0, 3050361446, 0, 0, "", "", "", "t", 0, "t", "{2}", nil, nil, nil, nil, nil, nil, nil},
{3259318326, "v1_check", 2200, "c", "f", "f", "t", 2694106299, 0, 0, 0, 0, "", "", "", "t", 0, "t", nil, nil, nil, nil, nil, nil, nil, nil},
{3757635986, "testing_pkey", 2200, "p", "f", "f", "t", 2147906242, 0, 3757635986, 0, 0, "", "", "", "t", 0, "t", "{1}", nil, nil, nil, nil, nil, nil, nil},
},
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "PG_catalog"."pg_constraint";`,
ExpectedErr: "not",
},
{ // Different cases and quoted, so it fails
Query: `SELECT * FROM "pg_catalog"."PG_constraint";`,
ExpectedErr: "not",
},
{ // Different cases but non-quoted, so it works
Query: "SELECT conname FROM PG_catalog.pg_CONSTRAINT ORDER BY conname;",
Expected: []sql.Row{
{"testing2_pkey"},
{"testing2_pktesting_fkey"},
{"testing_pkey"},
{"v1"},
{"v1_check"},
},
},
{
Query: "SELECT co.oid, co.conname, co.conrelid, cl.relname FROM pg_catalog.pg_constraint co JOIN pg_catalog.pg_class cl ON co.conrelid = cl.oid WHERE cl.relname = 'testing2';",
Expected: []sql.Row{
{2068729390, "testing2_pkey", 2694106299, "testing2"},
{1719906648, "testing2_pktesting_fkey", 2694106299, "testing2"},
{3259318326, "v1_check", 2694106299, "testing2"},
},
},
},
},
})
}
func TestPgConstraintIndexes(t *testing.T) {
RunScripts(t, []ScriptTest{
{
Name: "pg_constraint",
SetUpScript: []string{
`CREATE TABLE testing (pk INT primary key, v1 INT UNIQUE);`,
`CREATE TABLE testing2 (pk INT primary key, pktesting INT REFERENCES testing(pk), v1 TEXT);`,
`ALTER TABLE testing2 ADD CONSTRAINT v1_check CHECK (v1 != '')`,
`CREATE DOMAIN mydomain AS INT CHECK (VALUE > 0);`,
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT oid, conname FROM pg_catalog.pg_constraint WHERE oid = 2068729390;",
Expected: []sql.Row{
{2068729390, "testing2_pkey"},
},
},
{
Query: "explain SELECT oid, conname FROM pg_catalog.pg_constraint WHERE oid = 2068729390 order by 1",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.oid, pg_constraint.conname]"},
{" └─ Filter"},
{" ├─ pg_constraint.oid = 2068729390"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.oid]"},
{" └─ filters: [{[{Index:[\"public\",\"testing2\",\"PRIMARY\"]}, {Index:[\"public\",\"testing2\",\"PRIMARY\"]}]}]"},
},
},
{
Query: "SELECT oid, conname FROM pg_catalog.pg_constraint WHERE conrelid = 2694106299 ORDER BY conname;",
Expected: []sql.Row{
{2068729390, "testing2_pkey"},
{1719906648, "testing2_pktesting_fkey"},
{3259318326, "v1_check"},
},
},
{
Query: "SELECT oid, conname FROM pg_catalog.pg_constraint WHERE conname = 'testing_pkey' AND connamespace = 2200;",
Expected: []sql.Row{
{3757635986, "testing_pkey"},
},
},
{
Query: "SELECT oid, conname FROM pg_catalog.pg_constraint WHERE conname >= 'testing' AND conname < 'testingz' ORDER BY conname;",
Expected: []sql.Row{
{2068729390, "testing2_pkey"},
{1719906648, "testing2_pktesting_fkey"},
{3757635986, "testing_pkey"},
},
},
},
},
{
Name: "pg_constraint comprehensive index tests",
SetUpScript: []string{
`CREATE TABLE test_table1 (pk INT primary key, val1 INT UNIQUE, val2 TEXT);`,
`CREATE TABLE test_table2 (id INT primary key, fk_col INT REFERENCES test_table1(pk), name TEXT UNIQUE);`,
`CREATE TABLE test_table3 (pk1 INT, pk2 INT, val TEXT, PRIMARY KEY(pk1, pk2));`,
`ALTER TABLE test_table2 ADD CONSTRAINT name_check CHECK (name != '');`,
`ALTER TABLE test_table1 ADD CONSTRAINT val2_check CHECK (val2 IS NOT NULL);`,
`CREATE DOMAIN test_domain AS INT CHECK (VALUE > 0);`,
`CREATE DOMAIN test_domain2 AS TEXT CHECK (LENGTH(VALUE) > 2);`,
},
Assertions: []ScriptTestAssertion{
// Primary key index tests (pg_constraint_oid_index)
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE oid = (SELECT oid FROM pg_catalog.pg_constraint WHERE conname = 'test_table1_pkey' LIMIT 1);",
Expected: []sql.Row{
{"test_table1_pkey"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE oid IN (SELECT oid FROM pg_catalog.pg_constraint WHERE conname LIKE '%_pkey' ORDER BY conname LIMIT 3);",
Expected: []sql.Row{
{"test_table1_pkey"},
{"test_table2_pkey"},
{"test_table3_pkey"},
},
},
// conname + connamespace index tests (pg_constraint_conname_nsp_index)
{
Query: "SELECT conname, connamespace FROM pg_catalog.pg_constraint WHERE conname = 'test_table1_pkey' AND connamespace = 2200;",
Expected: []sql.Row{
{"test_table1_pkey", uint32(2200)},
},
},
{
Query: "explain SELECT conname, connamespace FROM pg_catalog.pg_constraint WHERE conname = 'test_table1_pkey' AND connamespace = 2200 order by 1",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname, pg_constraint.connamespace]"},
{" └─ Filter"},
{" ├─ (pg_constraint.conname = 'test_table1_pkey' AND pg_constraint.connamespace = 2200)"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.conname,pg_constraint.connamespace]"},
{" └─ filters: [{[test_table1_pkey, test_table1_pkey], [{Namespace:[\"public\"]}, {Namespace:[\"public\"]}]}]"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conname IN ('test_table1_pkey', 'test_table2_pkey', 'name_check') AND connamespace = 2200 ORDER BY conname;",
Expected: []sql.Row{
{"name_check"},
{"test_table1_pkey"},
{"test_table2_pkey"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conname >= 'name_' AND conname < 'name_z' AND connamespace = 2200 ORDER BY conname;",
Expected: []sql.Row{
{"name_check"},
},
},
// conrelid + contypid + conname index tests (pg_constraint_conrelid_contypid_conname_index)
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = 3645786842 AND contypid = 0 ORDER BY conname;",
Expected: []sql.Row{
{"test_table1_pkey"},
{"val1"}, // TODO: postgres names this "test_table1_val1_key"
{"val2_check"},
},
},
{
Query: "explain SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = 3645786842 AND contypid = 0 ORDER BY conname;",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname]"},
{" └─ Sort(pg_constraint.conname ASC)"},
{" └─ Filter"},
{" ├─ (pg_constraint.conrelid = 3645786842 AND pg_constraint.contypid = 0)"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.conrelid,pg_constraint.contypid,pg_constraint.conname]"},
{" └─ filters: [{[{Table:[\"public\",\"test_table1\"]}, {Table:[\"public\",\"test_table1\"]}], [{OID:[\"0\"]}, {OID:[\"0\"]}], [NULL, ∞)}]"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'test_table1') AND contypid = 0 ORDER BY conname;",
Expected: []sql.Row{
{"test_table1_pkey"},
{"val1"}, // TODO: postgres names this "test_table1_val1_key"
{"val2_check"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid IN (SELECT oid FROM pg_catalog.pg_class WHERE relname IN ('test_table1', 'test_table2')) AND contypid = 0 ORDER BY conname;",
Expected: []sql.Row{
{"name"}, // should be test_table2_name_key
{"name_check"},
{"test_table1_pkey"},
{"test_table2_fk_col_fkey"},
{"test_table2_pkey"},
{"val1"}, // should be test_table1_val1_key
{"val2_check"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'test_table2') AND contypid = 0 AND conname = 'name_check';",
Expected: []sql.Row{
{"name_check"},
},
},
// contypid index tests (pg_constraint_contypid_index)
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE contypid = (SELECT oid FROM pg_catalog.pg_type WHERE typname = 'test_domain') ORDER BY conname;",
Expected: []sql.Row{
{"test_domain_check"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE contypid = 1309307140 ORDER BY conname;",
Expected: []sql.Row{
{"test_domain_check"},
},
},
{
Query: "explain SELECT conname FROM pg_catalog.pg_constraint WHERE contypid = 1309307140 ORDER BY conname;",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname]"},
{" └─ Sort(pg_constraint.conname ASC)"},
{" └─ Filter"},
{" ├─ pg_constraint.contypid = 1309307140"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.contypid]"},
{" └─ filters: [{[{Type:[\"public\",\"test_domain\"]}, {Type:[\"public\",\"test_domain\"]}]}]"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE contypid IN (SELECT oid FROM pg_catalog.pg_type WHERE typname LIKE 'test_domain%') ORDER BY conname;",
Expected: []sql.Row{
{"test_domain2_check"},
{"test_domain_check"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE contypid > 0 ORDER BY conname;",
Expected: []sql.Row{
{"test_domain2_check"},
{"test_domain_check"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid >= (SELECT MIN(oid) FROM pg_catalog.pg_class WHERE relname LIKE 'test_%') AND conrelid <= (SELECT MAX(oid) FROM pg_catalog.pg_class WHERE relname LIKE 'test_%') AND contypid = 0 ORDER BY conname;",
Expected: []sql.Row{
{"name"}, // should be test_table2_name_key
{"name_check"},
{"test_table1_pkey"},
{"test_table2_fk_col_fkey"},
{"test_table2_pkey"},
{"test_table3_pkey"},
{"val1"}, // should be test_table1_val1_key
{"val2_check"},
},
},
{
Query: "explain SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid >= (SELECT MIN(oid) FROM pg_catalog.pg_class WHERE relname LIKE 'test_%') AND conrelid <= (SELECT MAX(oid) FROM pg_catalog.pg_class WHERE relname LIKE 'test_%') AND contypid = 0 ORDER BY conname;",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname]"},
{" └─ Sort(pg_constraint.conname ASC)"},
{" └─ Filter"},
{" ├─ ((pg_constraint.conrelid >= Subquery((select min(oid) from pg_class where relname like 'test_%')) AND pg_constraint.conrelid <= Subquery((select min max(oid) from pg_class where relname like 'test_%'))) AND pg_constraint.contypid = 0)"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.contypid]"},
{" └─ filters: [{[{OID:[\"0\"]}, {OID:[\"0\"]}]}]"},
},
},
// Prefix match on 3-column index
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'test_table1') ORDER BY conname;",
Expected: []sql.Row{
{"test_table1_pkey"},
{"val1"}, // should be test_table1_val1_key
{"val2_check"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = 3645786842 ORDER BY conname;",
Expected: []sql.Row{
{"test_table1_pkey"},
{"val1"}, // should be test_table1_val1_key
{"val2_check"},
},
},
{
Query: "explain SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = 3645786842 ORDER BY conname;",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname]"},
{" └─ Sort(pg_constraint.conname ASC)"},
{" └─ Filter"},
{" ├─ pg_constraint.conrelid = 3645786842"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.conrelid,pg_constraint.contypid,pg_constraint.conname]"},
{" └─ filters: [{[{Table:[\"public\",\"test_table1\"]}, {Table:[\"public\",\"test_table1\"]}], [NULL, ∞), [NULL, ∞)}]"},
},
},
{
Query: "SELECT conname FROM pg_catalog.pg_constraint WHERE (conname LIKE '%_pkey' OR conname LIKE '%_key') AND connamespace = 2200 ORDER BY conname;",
Expected: []sql.Row{
{"test_table1_pkey"},
{"test_table2_fk_col_fkey"},
{"test_table2_pkey"},
{"test_table3_pkey"},
},
},
{
Query: "EXPLAIN SELECT conname FROM pg_catalog.pg_constraint WHERE conname = 'test_table1_pkey' AND connamespace = 2200;",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname]"},
{" └─ Filter"},
{" ├─ (pg_constraint.conname = 'test_table1_pkey' AND pg_constraint.connamespace = 2200)"},
{" └─ IndexedTableAccess(pg_constraint)"},
{" ├─ index: [pg_constraint.conname,pg_constraint.connamespace]"},
{" └─ filters: [{[test_table1_pkey, test_table1_pkey], [{Namespace:[\"public\"]}, {Namespace:[\"public\"]}]}]"},
},
},
{
Query: "EXPLAIN SELECT conname FROM pg_catalog.pg_constraint WHERE conrelid = 3645786842 AND contypid > 0 order by 1;",
Expected: []sql.Row{
{"Project"},
{" ├─ columns: [pg_constraint.conname]"},