-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
upsert
967 lines (907 loc) · 30.4 KB
/
upsert
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
# LogicTest: local
statement ok
CREATE TABLE kv (
k INT PRIMARY KEY,
v INT
)
# Use implicit target columns (which can use blind KV Put).
query T
EXPLAIN (VERBOSE) UPSERT INTO kv TABLE kv ORDER BY v DESC LIMIT 2
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: kv(k, v)
│ auto commit
│
└── • project
│ columns: (k, v, v)
│
└── • top-k
│ columns: (k, v)
│ ordering: -v
│ estimated row count: 2 (missing stats)
│ order: -v
│ k: 2
│
└── • scan
columns: (k, v)
estimated row count: 1,000 (missing stats)
table: kv@kv_pkey
spans: FULL SCAN
# Use explicit target columns (which can use blind KV Put).
query T
EXPLAIN (VERBOSE) UPSERT INTO kv (k, v) TABLE kv ORDER BY v DESC LIMIT 2
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: kv(k, v)
│ auto commit
│
└── • project
│ columns: (k, v, v)
│
└── • top-k
│ columns: (k, v)
│ ordering: -v
│ estimated row count: 2 (missing stats)
│ order: -v
│ k: 2
│
└── • scan
columns: (k, v)
estimated row count: 1,000 (missing stats)
table: kv@kv_pkey
spans: FULL SCAN
# Add RETURNING clause (should still use blind KV Put).
query T
EXPLAIN (VERBOSE) UPSERT INTO kv (k, v) TABLE kv ORDER BY v DESC LIMIT 2 RETURNING *
----
distribution: local
vectorized: true
·
• upsert
│ columns: (k, v)
│ estimated row count: 2 (missing stats)
│ into: kv(k, v)
│ auto commit
│
└── • project
│ columns: (k, v, v)
│
└── • top-k
│ columns: (k, v)
│ ordering: -v
│ estimated row count: 2 (missing stats)
│ order: -v
│ k: 2
│
└── • scan
columns: (k, v)
estimated row count: 1,000 (missing stats)
table: kv@kv_pkey
spans: FULL SCAN
# Use subset of explicit target columns (which cannot use blind KV Put).
query T
EXPLAIN (VERBOSE) UPSERT INTO kv (k) SELECT k FROM kv ORDER BY v DESC LIMIT 2
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: kv(k, v)
│ auto commit
│ arbiter indexes: kv_pkey
│
└── • lookup join (inner)
│ columns: (k, v_default, k)
│ estimated row count: 2 (missing stats)
│ table: kv@kv_pkey
│ equality: (k) = (k)
│ equality cols are key
│
└── • distinct
│ columns: (v_default, k)
│ estimated row count: 2 (missing stats)
│ distinct on: k
│ nulls are distinct
│ error on duplicate
│
└── • render
│ columns: (v_default, k)
│ render v_default: CAST(NULL AS INT8)
│ render k: k
│
└── • top-k
│ columns: (k, v)
│ ordering: -v
│ estimated row count: 2 (missing stats)
│ order: -v
│ k: 2
│
└── • scan
columns: (k, v)
estimated row count: 1,000 (missing stats)
table: kv@kv_pkey
spans: FULL SCAN
# Use Upsert with indexed table, default columns, computed columns, and check
# columns.
statement ok
CREATE TABLE indexed (
a INT PRIMARY KEY,
b INT,
c INT DEFAULT(10),
d INT AS (a + c) STORED,
FAMILY (a, b, c, d),
UNIQUE INDEX secondary (d, b),
CHECK (c > 0)
)
# Should fetch existing values since there is a secondary index.
query T
EXPLAIN (VERBOSE) UPSERT INTO indexed VALUES (1)
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey
│
└── • project
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d, b_default, c_default, d_comp, a, check1)
│
└── • render
│ columns: (check1, column1, b_default, c_default, d_comp, a, b, c, d)
│ render check1: c_default > 0
│ render column1: column1
│ render b_default: b_default
│ render c_default: c_default
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│
└── • cross join (left outer)
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d)
│ estimated row count: 1 (missing stats)
│
├── • values
│ columns: (column1, b_default, c_default, d_comp)
│ size: 4 columns, 1 row
│ row 0, expr 0: 1
│ row 0, expr 1: CAST(NULL AS INT8)
│ row 0, expr 2: 10
│ row 0, expr 3: 11
│
└── • scan
columns: (a, b, c, d)
estimated row count: 1 (missing stats)
table: indexed@indexed_pkey
spans: /1/0
locking strength: for update
query T
EXPLAIN (VERBOSE) UPSERT INTO indexed VALUES (1), (2), (3), (4)
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey
│
└── • project
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d, b_default, c_default, d_comp, a, check1)
│
└── • render
│ columns: (check1, column1, b_default, c_default, d_comp, a, b, c, d)
│ render check1: c_default > 0
│ render column1: column1
│ render b_default: b_default
│ render c_default: c_default
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│
└── • lookup join (left outer)
│ columns: (d_comp, b_default, c_default, column1, a, b, c, d)
│ estimated row count: 4 (missing stats)
│ table: indexed@indexed_pkey
│ equality: (column1) = (a)
│ equality cols are key
│ locking strength: for update
│
└── • render
│ columns: (d_comp, b_default, c_default, column1)
│ render d_comp: column1 + 10
│ render b_default: CAST(NULL AS INT8)
│ render c_default: 10
│ render column1: column1
│
└── • values
columns: (column1)
size: 1 column, 4 rows
row 0, expr 0: 1
row 1, expr 0: 2
row 2, expr 0: 3
row 3, expr 0: 4
query T
EXPLAIN (VERBOSE)
INSERT INTO indexed
VALUES (1, 2, 3)
ON CONFLICT (a)
DO UPDATE SET b = 2, c = 3
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey
│
└── • project
│ columns: (column1, column2, column3, d_comp, a, b, c, d, upsert_b, upsert_c, upsert_d, a, check1)
│
└── • render
│ columns: (check1, column1, column2, column3, d_comp, a, b, c, d, upsert_b, upsert_c, upsert_d)
│ render check1: upsert_c > 0
│ render column1: column1
│ render column2: column2
│ render column3: column3
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│ render upsert_b: upsert_b
│ render upsert_c: upsert_c
│ render upsert_d: upsert_d
│
└── • render
│ columns: (upsert_b, upsert_c, upsert_d, column1, column2, column3, d_comp, a, b, c, d)
│ render upsert_b: CASE WHEN a IS NULL THEN column2 ELSE 2 END
│ render upsert_c: CASE WHEN a IS NULL THEN column3 ELSE 3 END
│ render upsert_d: CASE WHEN a IS NULL THEN d_comp ELSE a + 3 END
│ render column1: column1
│ render column2: column2
│ render column3: column3
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│
└── • cross join (left outer)
│ columns: (column1, column2, column3, d_comp, a, b, c, d)
│ estimated row count: 1 (missing stats)
│
├── • values
│ columns: (column1, column2, column3, d_comp)
│ size: 4 columns, 1 row
│ row 0, expr 0: 1
│ row 0, expr 1: 2
│ row 0, expr 2: 3
│ row 0, expr 3: 4
│
└── • scan
columns: (a, b, c, d)
estimated row count: 1 (missing stats)
table: indexed@indexed_pkey
spans: /1/0
locking strength: for update
# Use index hints.
query T
EXPLAIN (VERBOSE) UPSERT INTO indexed@secondary VALUES (1)
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey
│
└── • project
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d, b_default, c_default, d_comp, a, check1)
│
└── • render
│ columns: (check1, column1, b_default, c_default, d_comp, a, b, c, d)
│ render check1: c_default > 0
│ render column1: column1
│ render b_default: b_default
│ render c_default: c_default
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│
└── • cross join (left outer)
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d)
│ estimated row count: 1 (missing stats)
│
├── • values
│ columns: (column1, b_default, c_default, d_comp)
│ size: 4 columns, 1 row
│ row 0, expr 0: 1
│ row 0, expr 1: CAST(NULL AS INT8)
│ row 0, expr 2: 10
│ row 0, expr 3: 11
│
└── • filter
│ columns: (a, b, c, d)
│ estimated row count: 1 (missing stats)
│ filter: a = 1
│
└── • index join
│ columns: (a, b, c, d)
│ estimated row count: 1,000 (missing stats)
│ table: indexed@indexed_pkey
│ key columns: a
│
└── • scan
columns: (a, b, d)
estimated row count: 1,000 (missing stats)
table: indexed@secondary
spans: FULL SCAN
query T
EXPLAIN (VERBOSE) UPSERT INTO indexed@{NO_FULL_SCAN} VALUES (1)
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey
│
└── • project
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d, b_default, c_default, d_comp, a, check1)
│
└── • render
│ columns: (check1, column1, b_default, c_default, d_comp, a, b, c, d)
│ render check1: c_default > 0
│ render column1: column1
│ render b_default: b_default
│ render c_default: c_default
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│
└── • cross join (left outer)
│ columns: (column1, b_default, c_default, d_comp, a, b, c, d)
│ estimated row count: 1 (missing stats)
│
├── • values
│ columns: (column1, b_default, c_default, d_comp)
│ size: 4 columns, 1 row
│ row 0, expr 0: 1
│ row 0, expr 1: CAST(NULL AS INT8)
│ row 0, expr 2: 10
│ row 0, expr 3: 11
│
└── • scan
columns: (a, b, c, d)
estimated row count: 1 (missing stats)
table: indexed@indexed_pkey
spans: /1/0
locking strength: for update
query error could not produce a query plan conforming to the NO_FULL_SCAN hint
EXPLAIN (VERBOSE) UPSERT INTO indexed@{FORCE_INDEX=secondary,NO_FULL_SCAN} VALUES (1)
query T
EXPLAIN (VERBOSE)
INSERT INTO indexed@indexed_pkey AS indexed_pk
VALUES (1, 2, 3)
ON CONFLICT (a)
DO UPDATE SET b = 2, c = 3
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey
│
└── • project
│ columns: (column1, column2, column3, d_comp, a, b, c, d, upsert_b, upsert_c, upsert_d, a, check1)
│
└── • render
│ columns: (check1, column1, column2, column3, d_comp, a, b, c, d, upsert_b, upsert_c, upsert_d)
│ render check1: upsert_c > 0
│ render column1: column1
│ render column2: column2
│ render column3: column3
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│ render upsert_b: upsert_b
│ render upsert_c: upsert_c
│ render upsert_d: upsert_d
│
└── • render
│ columns: (upsert_b, upsert_c, upsert_d, column1, column2, column3, d_comp, a, b, c, d)
│ render upsert_b: CASE WHEN a IS NULL THEN column2 ELSE 2 END
│ render upsert_c: CASE WHEN a IS NULL THEN column3 ELSE 3 END
│ render upsert_d: CASE WHEN a IS NULL THEN d_comp ELSE a + 3 END
│ render column1: column1
│ render column2: column2
│ render column3: column3
│ render d_comp: d_comp
│ render a: a
│ render b: b
│ render c: c
│ render d: d
│
└── • cross join (left outer)
│ columns: (column1, column2, column3, d_comp, a, b, c, d)
│ estimated row count: 1 (missing stats)
│
├── • values
│ columns: (column1, column2, column3, d_comp)
│ size: 4 columns, 1 row
│ row 0, expr 0: 1
│ row 0, expr 1: 2
│ row 0, expr 2: 3
│ row 0, expr 3: 4
│
└── • scan
columns: (a, b, c, d)
estimated row count: 1 (missing stats)
table: indexed@indexed_pkey
spans: /1/0
locking strength: for update
let $t_id
SELECT id FROM system.namespace WHERE name='indexed'
query T
EXPLAIN (VERBOSE)
INSERT INTO [$t_id AS t]@{NO_FULL_SCAN}
VALUES (1, 2, 3)
ON CONFLICT DO NOTHING
----
distribution: local
vectorized: true
·
• insert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: indexed(a, b, c, d)
│ auto commit
│ arbiter indexes: indexed_pkey, secondary
│
└── • render
│ columns: (column1, column2, column3, d_comp, check1)
│ render check1: column3 > 0
│ render column1: column1
│ render column2: column2
│ render column3: column3
│ render d_comp: d_comp
│
└── • lookup join (anti)
│ columns: (column1, column2, column3, d_comp)
│ estimated row count: 0 (missing stats)
│ table: indexed@secondary
│ equality: (d_comp, column2) = (d,b)
│ equality cols are key
│
└── • cross join (anti)
│ columns: (column1, column2, column3, d_comp)
│ estimated row count: 0 (missing stats)
│
├── • values
│ columns: (column1, column2, column3, d_comp)
│ size: 4 columns, 1 row
│ row 0, expr 0: 1
│ row 0, expr 1: 2
│ row 0, expr 2: 3
│ row 0, expr 3: 4
│
└── • scan
columns: (a)
estimated row count: 1 (missing stats)
table: indexed@indexed_pkey
spans: /1/0
# Drop index and verify that existing values no longer need to be fetched.
statement ok
DROP INDEX indexed@secondary CASCADE
query T
EXPLAIN (VERBOSE) UPSERT INTO indexed VALUES (1) RETURNING *
----
distribution: local
vectorized: true
·
• upsert
│ columns: (a, b, c, d)
│ estimated row count: 1
│ into: indexed(a, b, c, d)
│ auto commit
│
└── • project
│ columns: (column1, b_default, c_default, d_comp, b_default, c_default, d_comp, check1)
│
└── • values
columns: (column1, b_default, c_default, d_comp, check1)
size: 5 columns, 1 row
row 0, expr 0: 1
row 0, expr 1: CAST(NULL AS INT8)
row 0, expr 2: 10
row 0, expr 3: 11
row 0, expr 4: true
subtest regression_32473
statement ok
CREATE TABLE customers (
customer_id serial PRIMARY KEY,
name VARCHAR UNIQUE,
email VARCHAR NOT NULL
);
statement ok
INSERT INTO customers (name, email) VALUES ('bob', 'bob@email.com') ON CONFLICT (name)
DO UPDATE SET (name, email) = (
SELECT 'bob', 'otherbob@email.com'
)
query TT
SELECT name, email FROM customers
----
bob bob@email.com
# This statement only works with the optimizer enabled.
statement ok
INSERT INTO customers (name, email) VALUES ('bob', 'bob@email.com') ON CONFLICT (name)
DO UPDATE SET (name, email) = (
SELECT 'bob2', 'otherbob@email.com'
)
query TT
SELECT name, email FROM customers
----
bob2 otherbob@email.com
statement ok
DROP TABLE customers
# The CBO behaves differently than the HP and PG in this case. It only checks
# constraints if an insert or update actually occurs. In this case, the DO
# NOTHING clause skips the update, so there is no need to check the constraint.
statement ok
CREATE TABLE t5 (k INT PRIMARY KEY, a INT, b int CHECK (a > b))
statement ok
INSERT INTO t5 VALUES (1, 10, 9) ON CONFLICT (k) DO NOTHING
statement ok
INSERT INTO t5 VALUES (1, 10, 20) ON CONFLICT (k) DO NOTHING
# Regression test for #35564: make sure we use the Upsert's input required
# ordering for the internal projection.
statement ok
CREATE TABLE abc (a INT, b INT, c INT, INDEX(c) STORING(a,b))
statement ok
CREATE TABLE xyz (x INT, y INT, z INT)
query T
EXPLAIN (VERBOSE) SELECT * FROM [UPSERT INTO xyz SELECT a, b, c FROM abc RETURNING z] ORDER BY z
----
distribution: local
vectorized: true
·
• root
│ columns: (z)
│
├── • sort
│ │ columns: (z)
│ │ ordering: +z
│ │ estimated row count: 1,000 (missing stats)
│ │ order: +z
│ │
│ └── • scan buffer
│ columns: (z)
│ estimated row count: 1,000 (missing stats)
│ label: buffer 1
│
└── • subquery
│ id: @S1
│ original sql: UPSERT INTO xyz SELECT a, b, c FROM abc RETURNING z
│ exec mode: all rows
│
└── • buffer
│ columns: (z)
│ label: buffer 1
│
└── • project
│ columns: (z)
│
└── • upsert
│ columns: (z, rowid)
│ estimated row count: 1,000 (missing stats)
│ into: xyz(x, y, z, rowid)
│
└── • project
│ columns: (a, b, c, rowid_default, a, b, c)
│
└── • render
│ columns: (rowid_default, a, b, c)
│ render rowid_default: unique_rowid()
│ render a: a
│ render b: b
│ render c: c
│
└── • scan
columns: (a, b, c)
estimated row count: 1,000 (missing stats)
table: abc@abc_pkey
spans: FULL SCAN
# ------------------------------------------------------------------------------
# Regression for #35364. This tests behavior that is different between the CBO
# and the HP. The CBO will (deliberately) round any input columns *before*
# evaluating any computed columns, as well as rounding the output.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE t35364(
x DECIMAL(10,0) CHECK(round(x) = x) PRIMARY KEY,
y DECIMAL(10,0) DEFAULT (1.5),
z DECIMAL(10,0) AS (x+y+2.5) STORED CHECK(z >= 7)
)
query TTT
UPSERT INTO t35364 (x) VALUES (1.5) RETURNING *
----
2 2 7
query TTT
UPSERT INTO t35364 (x, y) VALUES (1.5, 2.5) RETURNING *
----
2 3 8
query TTT
INSERT INTO t35364 (x) VALUES (1.5) ON CONFLICT (x) DO UPDATE SET x=2.5 RETURNING *
----
3 3 9
statement error pq: failed to satisfy CHECK constraint \(z >= 7:::DECIMAL\)
UPSERT INTO t35364 (x) VALUES (0)
# ------------------------------------------------------------------------------
# Regression for #38627. Combined with the equivalent logic test, make sure that
# UPSERT in the presence of column mutations uses a lookup join without a
# problem.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE table38627 (a INT PRIMARY KEY, b INT, FAMILY (a, b)); INSERT INTO table38627 VALUES(1,1)
statement ok
BEGIN; ALTER TABLE table38627 ADD COLUMN c INT NOT NULL DEFAULT 5
query T
EXPLAIN (VERBOSE) UPSERT INTO table38627 SELECT * FROM table38627 WHERE a=1
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: table38627(a, b)
│ arbiter indexes: table38627_pkey
│
└── • project
│ columns: (a, b, a, b, c, b, a)
│
└── • lookup join (inner)
│ columns: (a, b, a, b, c)
│ estimated row count: 1 (missing stats)
│ table: table38627@table38627_pkey
│ equality: (a) = (a)
│ equality cols are key
│
└── • scan
columns: (a, b)
estimated row count: 1 (missing stats)
table: table38627@table38627_pkey
spans: /1/0
statement ok
COMMIT
# ------------------------------------------------------------------------------
# Show UPSERT plans with Distinct execution operator.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE tdup (x INT PRIMARY KEY, y INT, z INT, UNIQUE (y, z))
# Show unsorted upsert-distinct-on. Plan should not contain "order key".
# Ensure this test stays synchronized to the test in logic_test/upsert.
query T
EXPLAIN (VERBOSE)
INSERT INTO tdup VALUES (2, 2, 2), (3, 2, 2) ON CONFLICT (z, y) DO UPDATE SET z=1
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: tdup(x, y, z)
│ auto commit
│ arbiter indexes: tdup_y_z_key
│
└── • project
│ columns: (column1, column2, column3, x, y, z, upsert_z, x)
│
└── • render
│ columns: (upsert_z, column1, column2, column3, x, y, z)
│ render upsert_z: CASE WHEN x IS NULL THEN column3 ELSE 1 END
│ render column1: column1
│ render column2: column2
│ render column3: column3
│ render x: x
│ render y: y
│ render z: z
│
└── • lookup join (left outer)
│ columns: (column1, column2, column3, x, y, z)
│ estimated row count: 2 (missing stats)
│ table: tdup@tdup_y_z_key
│ equality: (column2, column3) = (y,z)
│ equality cols are key
│
└── • distinct
│ columns: (column1, column2, column3)
│ estimated row count: 2
│ distinct on: column2, column3
│ nulls are distinct
│ error on duplicate
│
└── • values
columns: (column1, column2, column3)
size: 3 columns, 2 rows
row 0, expr 0: 2
row 0, expr 1: 2
row 0, expr 2: 2
row 1, expr 0: 3
row 1, expr 1: 2
row 1, expr 2: 2
statement ok
CREATE TABLE target (a INT PRIMARY KEY, b INT, c INT, UNIQUE (b, c))
statement ok
CREATE TABLE source (x INT PRIMARY KEY, y INT, z INT, INDEX (y, z))
# Show sorted upsert-distinct-on. "order key = y, z" should be set below.
# Ensure this test stays synchronized to the test in logic_test/upsert.
query T
EXPLAIN (VERBOSE)
INSERT INTO target SELECT x, y, z FROM source WHERE (y IS NULL OR y > 0) AND x <> 1
ON CONFLICT (b, c) DO UPDATE SET b=5
----
distribution: local
vectorized: true
·
• upsert
│ columns: ()
│ estimated row count: 0 (missing stats)
│ into: target(a, b, c)
│ auto commit
│ arbiter indexes: target_b_c_key
│
└── • project
│ columns: (x, y, z, a, b, c, upsert_b, a)
│
└── • render
│ columns: (upsert_b, x, y, z, a, b, c)
│ render upsert_b: CASE WHEN a IS NULL THEN y ELSE 5 END
│ render x: x
│ render y: y
│ render z: z
│ render a: a
│ render b: b
│ render c: c
│
└── • merge join (right outer)
│ columns: (a, b, c, x, y, z)
│ estimated row count: 311 (missing stats)
│ equality: (b, c) = (y, z)
│ merge ordering: +"(b=y)",+"(c=z)"
│
├── • scan
│ columns: (a, b, c)
│ ordering: +b,+c
│ estimated row count: 1,000 (missing stats)
│ table: target@target_b_c_key
│ spans: FULL SCAN
│
└── • distinct
│ columns: (x, y, z)
│ ordering: +y,+z
│ estimated row count: 311 (missing stats)
│ distinct on: y, z
│ nulls are distinct
│ error on duplicate
│ order key: y, z
│
└── • filter
│ columns: (x, y, z)
│ ordering: +y,+z
│ estimated row count: 311 (missing stats)
│ filter: x != 1
│
└── • scan
columns: (x, y, z)
ordering: +y,+z
estimated row count: 333 (missing stats)
table: source@source_y_z_idx
spans: /NULL-/!NULL /1-
# Regression test for #25726.
# UPSERT over tables with column families, on the fast path, use the
# INSERT logic. This has special casing for column families of 1
# column, and another special casing for column families of 2+
# columns. The special casing is only for families that do not include
# the primary key. So we need a table with 3 families: 1 for the PK, 1
# with just 1 col, and 1 with 2+ cols.
statement ok
CREATE TABLE tu (a INT PRIMARY KEY, b INT, c INT, d INT, FAMILY (a), FAMILY (b), FAMILY (c,d));
INSERT INTO tu VALUES (1, 2, 3, 4)
# Force the leasing of the `tu` descriptor, otherwise the trace below is
# polluted.
statement ok
SELECT 1 FROM tu;
statement ok
SET tracing = on,kv,results; UPSERT INTO tu VALUES (1, NULL, NULL, NULL); SET tracing = off
query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
WHERE operation != 'dist sender send'
----
Put /Table/117/1/1/0 -> /TUPLE/
Del /Table/117/1/1/1/1
Del /Table/117/1/1/2/1
fast path completed
rows affected: 1
# KV operations.
statement ok
CREATE DATABASE t; CREATE TABLE t.kv(k INT PRIMARY KEY, v INT, FAMILY "primary" (k, v))
statement ok
CREATE UNIQUE INDEX woo ON t.kv(v)
statement ok
SET tracing = on,kv,results; UPSERT INTO t.kv(k, v) VALUES (2,3); SET tracing = off
query TT
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE operation != 'dist sender send' AND operation != 'kv.DistSender: sending partial batch'
----
colbatchscan Scan /Table/120/1/2/0
count CPut /Table/120/1/2/0 -> /TUPLE/2:2:Int/3
count InitPut /Table/120/2/3/0 -> /BYTES/0x8a
count fast path completed
sql query rows affected: 1
statement ok
SET tracing = on,kv,results; UPSERT INTO t.kv(k, v) VALUES (1,2); SET tracing = off
query TT
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE operation != 'dist sender send' AND operation != 'kv.DistSender: sending partial batch'
----
colbatchscan Scan /Table/120/1/1/0
count CPut /Table/120/1/1/0 -> /TUPLE/2:2:Int/2
count InitPut /Table/120/2/2/0 -> /BYTES/0x89
count fast path completed
sql query rows affected: 1
statement error duplicate key value
SET tracing = on,kv,results; UPSERT INTO t.kv(k, v) VALUES (2,2); SET tracing = off
query TT
set tracing=off;
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE operation != 'dist sender send' AND operation != 'kv.DistSender: sending partial batch'
----
colbatchscan Scan /Table/120/1/2/0
colbatchscan fetched: /kv/kv_pkey/2/v -> /3
count Put /Table/120/1/2/0 -> /TUPLE/2:2:Int/2
count Del /Table/120/2/3/0
count CPut /Table/120/2/2/0 -> /BYTES/0x8a (expecting does not exist)
sql query execution failed after 0 rows: duplicate key value violates unique constraint "woo"