-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathvirtual_columns
1266 lines (1014 loc) · 25.5 KB
/
virtual_columns
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
# Test that we don't allow FAMILY constraints with virtual columns.
statement error virtual computed column "v" cannot be part of a family
CREATE TABLE t (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL,
FAMILY (a, b, v)
)
statement error virtual computed column "v" cannot be part of a family
CREATE TABLE t (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL,
FAMILY (a),
FAMILY (b),
FAMILY (v)
)
statement error index "t_b_idx" cannot store virtual column "v"
CREATE TABLE t (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL,
INDEX (b) STORING (v)
)
statement ok
CREATE TABLE t (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL
)
statement error pgcode 42611 column "v" is not a stored computed column
ALTER TABLE t ALTER COLUMN v DROP STORED
statement ok
INSERT INTO t VALUES (1, 1)
statement ok
INSERT INTO t(a,b) VALUES (2, 2)
statement error cannot write directly to computed column
INSERT INTO t(a,b,v) VALUES (2, 2, 0)
statement error cannot write directly to computed column
INSERT INTO t VALUES (2, 2, 0)
# Ensure that the virtual column is produced.
query III colnames,rowsort
SELECT * FROM t
----
a b v
1 1 2
2 2 4
statement ok
DELETE FROM t WHERE a > 0
statement ok
INSERT INTO t VALUES (1, 10), (2, 20), (3, 30), (4, 40)
query I rowsort
DELETE FROM t WHERE a = 1 RETURNING v
----
11
query III colnames,rowsort
SELECT * FROM t
----
a b v
2 20 22
3 30 33
4 40 44
statement ok
DELETE FROM t WHERE v = 33
query III colnames,rowsort
SELECT * FROM t
----
a b v
2 20 22
4 40 44
statement error cannot write directly to computed column
UPDATE t SET v=1
statement ok
UPDATE t SET a=a+1
query III colnames,rowsort
SELECT * FROM t
----
a b v
3 20 23
5 40 45
query III colnames,rowsort
UPDATE t SET b=b+1 WHERE v=45 RETURNING a,b,v
----
a b v
5 41 46
# Tests with an index on the virtual column.
statement ok
CREATE TABLE t_idx (
a INT PRIMARY KEY,
b INT,
c INT,
v INT AS (a+b) VIRTUAL,
w INT AS (c+1) VIRTUAL,
INDEX (v),
UNIQUE (w)
)
statement ok
INSERT INTO t_idx VALUES (1, 1, 1), (2, 8, 2), (3, 3, 3), (4, 6, 4), (5, 0, 5)
statement error duplicate key value violates unique constraint
INSERT INTO t_idx VALUES (10, 10, 1)
# Queries which should use the index on v. Note that there are corresponding
# execbuilder tests which verify the query plans.
query I rowsort
SELECT a FROM t_idx WHERE a+b=10
----
2
4
query I rowsort
SELECT a FROM t_idx WHERE v=10
----
2
4
query I rowsort
SELECT a FROM t_idx WHERE w IN (4,6)
----
3
5
# Covering lookup join.
query II
SELECT v, x FROM (VALUES (1), (2), (10), (5)) AS u(x) INNER LOOKUP JOIN t_idx@t_idx_v_idx ON u.x = t_idx.v
----
2 2
5 5
10 10
10 10
# Non-covering lookup join that requires a second join on the primary index.
query IIII
SELECT a, b, v, x FROM (VALUES (1), (2), (10), (5)) AS u(x) INNER LOOKUP JOIN t_idx@t_idx_v_idx ON u.x = t_idx.v
----
1 1 2 2
2 8 10 10
4 6 10 10
5 0 5 5
statement ok
DELETE FROM t_idx WHERE v = 6
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
1 1 1 2 2
2 8 2 10 3
4 6 4 10 5
5 0 5 5 6
statement ok
DELETE FROM t_idx WHERE a+b = 10
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
1 1 1 2 2
5 0 5 5 6
# Update PK; ensure indexes are updated.
statement ok
UPDATE t_idx SET a=a+1
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
2 1 1 3 2
6 0 5 6 6
query I
SELECT a FROM t_idx WHERE v=3
----
2
query I
SELECT a FROM t_idx WHERE w=2
----
2
# Update b, which affects only v.
statement ok
UPDATE t_idx SET b=b+1
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
2 2 1 4 2
6 1 5 7 6
query I
SELECT a FROM t_idx WHERE v=4
----
2
query I
SELECT a FROM t_idx WHERE w=2
----
2
# Update c, which affects only w.
statement ok
UPDATE t_idx SET c=c+1
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
2 2 2 4 3
6 1 6 7 7
query I
SELECT a FROM t_idx WHERE v=4
----
2
query I
SELECT a FROM t_idx WHERE w=3
----
2
statement error duplicate key value violates unique constraint
UPDATE t_idx SET c=6 WHERE a=2
# Test UPDATE .. RETURNING.
query III colnames,rowsort
UPDATE t_idx SET a=a+1 RETURNING a,v,w
----
a v w
3 5 3
7 8 7
query I rowsort
UPDATE t_idx SET b=b+1 RETURNING w
----
3
7
# Upsert tests on t.
statement ok
TRUNCATE t
statement error cannot write directly to computed column
UPSERT INTO t(a,b,v) VALUES (1, 1, 1)
statement error cannot write directly to computed column
UPSERT INTO t VALUES (1, 1, 1)
statement ok
UPSERT INTO t VALUES (1, 10), (2, 20), (3, 30), (4, 40)
query III colnames,rowsort
SELECT * FROM t
----
a b v
1 10 11
2 20 22
3 30 33
4 40 44
query I colnames,rowsort
UPSERT INTO t VALUES (3, 31), (5, 50) RETURNING v
----
v
34
55
query I colnames,rowsort
INSERT INTO t VALUES (5, 51), (6, 60) ON CONFLICT DO NOTHING RETURNING v
----
v
66
query III colnames,rowsort
SELECT * FROM t
----
a b v
1 10 11
2 20 22
3 31 34
4 40 44
5 50 55
6 60 66
statement ok
INSERT INTO t VALUES (4, 100), (6, 100), (7, 100) ON CONFLICT (a) DO UPDATE SET b = t.v
query III colnames,rowsort
SELECT * FROM t
----
a b v
1 10 11
2 20 22
3 31 34
4 44 48
5 50 55
6 66 72
7 100 107
statement ok
INSERT INTO t VALUES (2, 100), (5, 100), (8, 100) ON CONFLICT (a) DO UPDATE SET b = excluded.v
query III colnames,rowsort
SELECT * FROM t
----
a b v
1 10 11
2 102 104
3 31 34
4 44 48
5 105 110
6 66 72
7 100 107
8 100 108
# Upsert tests on t_idx.
statement ok
TRUNCATE t_idx
statement error cannot write directly to computed column
UPSERT INTO t_idx(a,b,v) VALUES (1, 1, 1)
statement error cannot write directly to computed column
UPSERT INTO t_idx VALUES (1, 1, 1, 1)
statement ok
UPSERT INTO t_idx VALUES (1, 10, 100), (2, 20, 200), (3, 30, 300), (4, 40, 400)
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
1 10 100 11 101
2 20 200 22 201
3 30 300 33 301
4 40 400 44 401
query III colnames,rowsort
UPSERT INTO t_idx VALUES (3, 31, 301), (5, 50, 500) RETURNING a, v, w
----
a v w
3 34 302
5 55 501
# Some rows conflict on the PK, some on w.
query I colnames,rowsort
INSERT INTO t_idx VALUES (4, 41, 301), (6, 60, 600), (7, 70, 100) ON CONFLICT DO NOTHING RETURNING w
----
w
601
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
1 10 100 11 101
2 20 200 22 201
3 31 301 34 302
4 40 400 44 401
5 50 500 55 501
6 60 600 66 601
# Conflict on a.
statement error violates unique constraint
INSERT INTO t_idx VALUES (1, 80, 900) ON CONFLICT (w) DO NOTHING
# Conflict on w.
statement error violates unique constraint
INSERT INTO t_idx VALUES (8, 80, 100) ON CONFLICT (a) DO NOTHING
# Conflict on w.
statement error violates unique constraint
INSERT INTO t_idx VALUES (4, 10, 100), (6, 10, 100), (7, 70, 700) ON CONFLICT (a) DO UPDATE SET c = 0
query IIIII colnames,rowsort
INSERT INTO t_idx VALUES (4, 10, 100), (6, 10, 100), (7, 70, 700) ON CONFLICT (a) DO UPDATE SET c = t_idx.w RETURNING a, b, c, v, w
----
a b c v w
4 40 401 44 402
6 60 601 66 602
7 70 700 77 701
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
1 10 100 11 101
2 20 200 22 201
3 31 301 34 302
4 40 401 44 402
5 50 500 55 501
6 60 601 66 602
7 70 700 77 701
statement ok
INSERT INTO t_idx VALUES (8, 80, 800), (10, 100, 700) ON CONFLICT (w) DO UPDATE SET a = excluded.a, c = excluded.v
query IIIII colnames,rowsort
SELECT * FROM t_idx
----
a b c v w
1 10 100 11 101
2 20 200 22 201
3 31 301 34 302
4 40 401 44 402
5 50 500 55 501
6 60 601 66 602
8 80 800 88 801
10 70 110 80 111
# Verify that FK relations on virtual columns are disallowed.
statement ok
CREATE TABLE fk (
a INT PRIMARY KEY,
b INT,
c INT,
u INT UNIQUE AS (b+c) VIRTUAL
)
statement error virtual column "u" cannot be referenced by a foreign key
CREATE TABLE fk2 (
p INT PRIMARY KEY,
c INT REFERENCES fk(u)
)
statement error virtual column "c" cannot reference a foreign key
CREATE TABLE fk2 (
p INT PRIMARY KEY,
c INT AS (p+1) VIRTUAL REFERENCES fk(a)
)
statement error virtual column "u" cannot be referenced by a foreign key
CREATE TABLE fk2 (
p INT PRIMARY KEY,
q INT,
r INT,
CONSTRAINT fk FOREIGN KEY (q,r) REFERENCES fk(a,u)
)
statement ok
CREATE TABLE fk2 (
x INT PRIMARY KEY,
y INT,
v INT AS (x+y) VIRTUAL
)
statement error virtual column "u" cannot be referenced by a foreign key
ALTER TABLE fk2 ADD CONSTRAINT foo FOREIGN KEY (x) REFERENCES fk(u)
statement error virtual column "v" cannot reference a foreign key
ALTER TABLE fk2 ADD CONSTRAINT foo FOREIGN KEY (v) REFERENCES fk(a)
# Tests with not null virtual columns.
subtest NotNull
statement ok
CREATE TABLE n (
a INT PRIMARY KEY,
b INT,
v INT NOT NULL AS (a+b) VIRTUAL
)
statement error null value in column "v" violates not-null constraint
INSERT INTO n VALUES (1, NULL)
statement ok
INSERT INTO n VALUES (1, 1), (2, 2)
statement error null value in column "v" violates not-null constraint
UPDATE n SET b = NULL WHERE a > 0
statement error null value in column "v" violates not-null constraint
UPSERT INTO n VALUES (1, NULL)
statement error null value in column "v" violates not-null constraint
UPSERT INTO n VALUES (3, NULL)
statement ok
INSERT INTO n VALUES (1, NULL) ON CONFLICT DO NOTHING
statement error null value in column "v" violates not-null constraint
INSERT INTO n VALUES (3, NULL) ON CONFLICT DO NOTHING
statement error null value in column "v" violates not-null constraint
INSERT INTO n VALUES (1, 10) ON CONFLICT (a) DO UPDATE SET b = NULL
statement error null value in column "v" violates not-null constraint
INSERT INTO n VALUES (3, NULL) ON CONFLICT (a) DO UPDATE SET b = NULL
# Tests with check constraints on virtual columns.
subtest Checks
statement ok
CREATE TABLE t_check (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL CHECK (v >= 10),
w INT AS (a*b) VIRTUAL,
CHECK (v < w)
)
statement error failed to satisfy CHECK constraint
INSERT INTO t_check VALUES (1,1), (5,5)
statement ok
INSERT INTO t_check VALUES (5,5), (6,6)
statement error failed to satisfy CHECK constraint
UPDATE t_check SET b=b-1
statement ok
UPDATE t_check SET b=b+1
query IIII colnames,rowsort
SELECT * FROM t_check
----
a b v w
5 6 11 30
6 7 13 42
statement error failed to satisfy CHECK constraint
UPSERT INTO t_check VALUES (5, 2), (8, 8)
statement error failed to satisfy CHECK constraint
UPSERT INTO t_check VALUES (5, 10), (8, 1)
statement ok
UPSERT INTO t_check VALUES (5, 10), (8, 8)
query IIII colnames,rowsort
SELECT * FROM t_check
----
a b v w
5 10 15 50
6 7 13 42
8 8 16 64
statement error failed to satisfy CHECK constraint
INSERT INTO t_check VALUES (5, 1) ON CONFLICT (a) DO UPDATE SET b=3
statement ok
INSERT INTO t_check VALUES (5, 1) ON CONFLICT (a) DO UPDATE SET b=5
query IIII colnames,rowsort
SELECT * FROM t_check
----
a b v w
5 5 10 25
6 7 13 42
8 8 16 64
# Tests with unique indexes and constraints involving virtual columns.
subtest NotNull
statement ok
CREATE TABLE uniq_simple (
a INT PRIMARY KEY,
b INT,
v INT UNIQUE AS (a+b) VIRTUAL
)
statement ok
INSERT INTO uniq_simple VALUES (1, 10), (2, 20)
statement error duplicate key value violates unique constraint
INSERT INTO uniq_simple VALUES (3, 8)
statement error duplicate key value violates unique constraint
UPDATE uniq_simple SET b=b+11 WHERE a < 2
statement error duplicate key value violates unique constraint
UPSERT INTO uniq_simple VALUES (2, 30), (5, 6)
statement ok
INSERT INTO uniq_simple VALUES (5, 6) ON CONFLICT (v) DO UPDATE SET b=15
query III colnames,rowsort
SELECT * FROM uniq_simple
----
a b v
1 15 16
2 20 22
statement ok
CREATE TABLE uniq_partial (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL,
UNIQUE INDEX (v) WHERE b > 10
)
statement ok
INSERT INTO uniq_partial VALUES (1, 10), (2, 20)
statement error duplicate key value violates unique constraint
INSERT INTO uniq_partial VALUES (3, 19)
statement ok
INSERT INTO uniq_partial VALUES (4, 7)
query III colnames,rowsort
SELECT * FROM uniq_partial
----
a b v
1 10 11
2 20 22
4 7 11
statement error duplicate key value violates unique constraint
UPDATE uniq_partial SET b = 30-a
statement ok
UPDATE uniq_partial SET b = 10-a
query III colnames,rowsort
SELECT * FROM uniq_partial
----
a b v
1 9 10
2 8 10
4 6 10
statement ok
UPSERT INTO uniq_partial VALUES (3, 7), (20, 20)
statement error duplicate key value violates unique constraint
UPSERT INTO uniq_partial VALUES (15, 25)
statement ok
CREATE TABLE uniq_partial_pred (
a INT PRIMARY KEY,
b INT,
c INT,
v INT AS (a+b) VIRTUAL,
UNIQUE INDEX (c) WHERE v > 10
)
statement ok
INSERT INTO uniq_partial_pred VALUES (1, 1, 1), (2, 4, 2), (3, 3, 2), (10, 10, 1)
statement error duplicate key value violates unique constraint
INSERT INTO uniq_partial_pred VALUES (11, 9, 1)
statement error duplicate key value violates unique constraint
UPDATE uniq_partial_pred SET b=20-a
statement ok
UPDATE uniq_partial_pred SET b=10-a
statement ok
CREATE TABLE uniq_partial_multi (
a INT PRIMARY KEY,
b INT,
c INT,
v INT AS (a+b) VIRTUAL,
UNIQUE INDEX (c, v) WHERE (v > 10)
)
statement ok
INSERT INTO uniq_partial_multi VALUES (1, 1, 1), (2, 4, 2), (3, 3, 2), (10, 10, 1)
statement error duplicate key value violates unique constraint
INSERT INTO uniq_partial_multi VALUES (15, 5, 1)
statement ok
UPSERT INTO uniq_partial_multi VALUES (4, 2, 2)
statement error duplicate key value violates unique constraint
UPSERT INTO uniq_partial_multi VALUES (4, 16, 1)
statement ok
SET experimental_enable_unique_without_index_constraints = true
statement ok
CREATE TABLE uniq_no_index (
a INT PRIMARY KEY,
b INT,
v INT AS (a+b) VIRTUAL,
UNIQUE WITHOUT INDEX (v)
)
statement ok
INSERT INTO uniq_no_index VALUES (1, 10), (2, 20)
statement error duplicate key value violates unique constraint
INSERT INTO uniq_no_index VALUES (3, 8)
statement error duplicate key value violates unique constraint
UPDATE uniq_no_index SET b=b+11 WHERE a < 2
statement error duplicate key value violates unique constraint
UPSERT INTO uniq_no_index VALUES (2, 30), (5, 6)
statement ok
INSERT INTO uniq_no_index VALUES (5, 6) ON CONFLICT (v) DO UPDATE SET b=15
query III colnames,rowsort
SELECT * FROM uniq_no_index
----
a b v
1 15 16
2 20 22
statement ok
CREATE TABLE uniq_no_index_multi (
a INT PRIMARY KEY,
b INT,
c INT,
v INT AS (a+b) VIRTUAL,
UNIQUE WITHOUT INDEX (v, c)
)
statement ok
INSERT INTO uniq_no_index_multi VALUES (1, 1, 1), (2, 4, 2), (3, 3, 3)
statement error duplicate key value violates unique constraint
INSERT INTO uniq_no_index_multi VALUES (4, 2, 2)
statement error duplicate key value violates unique constraint
UPDATE uniq_no_index_multi SET c=2 WHERE a=3
statement ok
UPSERT INTO uniq_no_index_multi VALUES (3, 3, 10)
statement error duplicate key value violates unique constraint
UPSERT INTO uniq_no_index_multi VALUES (3, 3, 2)
# TODO(radu): add a test with a partial unique without index constraint.
# Test schema changes with virtual columns.
subtest SchemaChanges
statement ok
CREATE TABLE sc (a INT PRIMARY KEY, b INT)
statement ok
INSERT INTO sc VALUES (1, 10), (2, 20), (3, 30);
statement ok
ALTER TABLE sc ADD COLUMN v INT AS (a+b) VIRTUAL
query III rowsort,colnames
SELECT * FROM sc
----
a b v
1 10 11
2 20 22
3 30 33
statement ok
ALTER TABLE sc ADD COLUMN x INT AS (a+1) VIRTUAL, ADD COLUMN y INT AS (b+1) VIRTUAL, ADD COLUMN z INT AS (a+b) VIRTUAL
query IIIIII rowsort,colnames
SELECT * FROM sc
----
a b v x y z
1 10 11 2 11 11
2 20 22 3 21 22
3 30 33 4 31 33
statement error computed column expression cannot reference computed columns
ALTER TABLE sc ADD COLUMN u INT AS (a+v) VIRTUAL
statement ok
ALTER TABLE sc DROP COLUMN z
query IIIII rowsort,colnames
SELECT * FROM sc
----
a b v x y
1 10 11 2 11
2 20 22 3 21
3 30 33 4 31
statement ok
ALTER TABLE sc DROP COLUMN x, DROP COLUMN y
query III rowsort,colnames
SELECT * FROM sc
----
a b v
1 10 11
2 20 22
3 30 33
# Add virtual columns inside an explicit transactions.
statement ok
BEGIN
statement ok
ALTER TABLE sc ADD COLUMN w1 INT AS (a*b) VIRTUAL
statement ok
ALTER TABLE sc ADD COLUMN w2 INT AS (b*2) VIRTUAL
statement ok
COMMIT
query IIIII rowsort,colnames
SELECT * FROM sc
----
a b v w1 w2
1 10 11 10 20
2 20 22 40 40
3 30 33 90 60
statement ok
ALTER TABLE sc DROP COLUMN w1, DROP COLUMN w2
query III rowsort,colnames
SELECT * FROM sc
----
a b v
1 10 11
2 20 22
3 30 33
# Create an index on the virtual column and check that it works.
statement ok
CREATE INDEX v_idx ON sc(v)
query I rowsort
SELECT a FROM sc@v_idx
----
1
2
3
query I rowsort
SELECT a FROM sc WHERE v>20 AND v<40
----
2
3
statement ok
DROP INDEX v_idx
statement ok
ALTER TABLE sc DROP COLUMN v
# Add a column and an index on that column in the same transaction.
statement ok
BEGIN
statement ok
ALTER TABLE sc ADD COLUMN v INT AS (a+b) VIRTUAL
statement ok
CREATE INDEX v_idx ON sc(v)
statement ok
END
query I rowsort
SELECT a FROM sc@v_idx
----
1
2
3
statement ok
DROP INDEX v_idx
statement ok
ALTER TABLE sc DROP COLUMN v
# Add a column and a partial index using that column in the predicate in the
# same transaction.
statement ok
BEGIN
statement ok
ALTER TABLE sc ADD COLUMN v INT AS (a+b) VIRTUAL
statement ok
CREATE INDEX partial_idx ON sc(b) WHERE v > 20
statement ok
END
query I rowsort
SELECT a FROM sc@partial_idx WHERE v > 20
----
2
3
statement ok
DROP INDEX partial_idx
# Create a partial index on the virtual column and which uses the virtual column in the predicate.
statement ok
CREATE INDEX v_partial_idx ON sc(v) WHERE v > 20
query I rowsort
SELECT a FROM sc@v_partial_idx WHERE v > 20
----
2
3
statement ok
INSERT INTO sc VALUES (10, 10), (11, 9)
query III rowsort,colnames
SELECT * FROM sc
----
a b v
1 10 11
2 20 22
3 30 33
10 10 20
11 9 20
# Create a partial unique index on v which should fail.
statement error violates unique constraint
CREATE UNIQUE INDEX v_partial_idx2 ON sc(v) WHERE v > 10
# Create a check constraint which should fail.
statement error validation.*failed on row
ALTER TABLE sc ADD CONSTRAINT c CHECK (v < 30)
statement ok
ALTER TABLE sc ADD CONSTRAINT c CHECK (v < 40)
statement error failed to satisfy CHECK constraint
UPDATE sc SET b=b+10
# Add a virtual column with a check constraint.
statement error validation.*failed on row
ALTER TABLE sc ADD COLUMN w INT AS (a*b) VIRTUAL CHECK (w < 100)
statement ok
ALTER TABLE sc ADD COLUMN w INT AS (a*b) VIRTUAL CHECK (w <= 100)
statement error failed to satisfy CHECK constraint
INSERT INTO sc VALUES (20, 20)
# Test inverted indexes on virtual columns.
subtest InvertedIndexes
statement ok
CREATE TABLE inv (
k INT PRIMARY KEY,
i INT,
j JSON,
iv INT AS (i + 10) VIRTUAL,
jv JSON AS (j->'a') VIRTUAL,
INVERTED INDEX jv_idx (jv),
INVERTED INDEX i_jv_idx (i, jv),
INVERTED INDEX iv_j_idx (iv, j),
INVERTED INDEX iv_jv_idx (iv, jv)
)
statement ok
INSERT INTO inv VALUES
(1, 10, NULL),
(2, 10, '1'),
(3, 10, '"a"'),
(4, 10, 'true'),
(5, 10, 'null'),
(6, 10, '{}'),
(7, 10, '[]'),
(8, 10, '{"a": "b"}'),
(9, 10, '{"a": "b", "c": "d"}'),
(10, 10, '{"a": {}, "b": "c"}'),
(11, 10, '{"a": {"b": "c"}, "d": "e"}'),
(12, 10, '{"a": {"b": "c", "d": "e"}}'),
(13, 10, '{"a": [], "d": "e"}'),
(14, 10, '{"a": ["b", "c"], "d": "e"}'),
(15, 10, '["a"]'),
(16, 10, '["a", "b", "c"]'),
(17, 10, '[{"a": "b"}, "c"]')
statement ok