-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathupdate_from
168 lines (137 loc) · 3.46 KB
/
update_from
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
# LogicTest: local-opt fakedist-opt
statement ok
CREATE TABLE abc (a int primary key, b int, c int)
statement ok
INSERT INTO abc VALUES (1, 20, 300), (2, 30, 400)
# Updating using self join.
statement ok
UPDATE abc SET b = other.b + 1, c = other.c + 1 FROM abc AS other WHERE abc.a = other.a
query III rowsort
SELECT * FROM abc
----
1 21 301
2 31 401
# Update only some columns.
statement ok
UPDATE abc SET b = other.b + 1 FROM abc AS other WHERE abc.a = other.a
query III rowsort
SELECT * FROM abc
----
1 22 301
2 32 401
# Update only some rows.
statement ok
UPDATE abc SET b = other.b + 1 FROM abc AS other WHERE abc.a = other.a AND abc.a = 1
query III rowsort
SELECT * FROM abc
----
1 23 301
2 32 401
# Update from another table.
statement ok
CREATE TABLE new_abc (a int, b int, c int)
statement ok
INSERT INTO new_abc VALUES (1, 2, 3), (2, 3, 4)
statement ok
UPDATE abc SET b = new_abc.b, c = new_abc.c FROM new_abc WHERE abc.a = new_abc.a
query III rowsort
SELECT * FROM abc
----
1 2 3
2 3 4
# Multiple matching values for a given row. When this happens, we pick
# the first matching value for the row (this is arbitrary). This behavior
# is consistent with Postgres.
statement ok
INSERT INTO new_abc VALUES (1, 1, 1)
statement ok
UPDATE abc SET b = new_abc.b, c = new_abc.c FROM new_abc WHERE abc.a = new_abc.a
query III rowsort
SELECT * FROM abc
----
1 2 3
2 3 4
# Returning old values.
query IIIII colnames
UPDATE abc
SET
b = old.b + 1, c = old.c + 2
FROM
abc AS old
WHERE
abc.a = old.a
RETURNING
abc.a, abc.b AS new_b, old.b as old_b, abc.c as new_c, old.c as old_c
----
a new_b old_b new_c old_c
1 3 2 5 3
2 4 3 6 4
# Check if RETURNING * returns everything
query IIIIII colnames rowsort
UPDATE abc SET b = old.b + 1, c = old.c + 2 FROM abc AS old WHERE abc.a = old.a RETURNING *
----
a b c a b c
1 4 7 1 3 5
2 5 8 2 4 6
# Make sure UPDATE FROM works properly in the presence of check columns.
statement ok
CREATE TABLE abc_check (a int primary key, b int, c int, check (a > 0), check (b > 0 AND b < 10))
statement ok
INSERT INTO abc_check VALUES (1, 2, 3), (2, 3, 4)
query III colnames
UPDATE abc_check
SET
b = other.b, c = other.c
FROM
abc AS other
WHERE
abc_check.a = other.a
RETURNING
abc_check.a, abc_check.b, abc_check.c
----
a b c
1 4 7
2 5 8
# Update values of table from values expression
statement ok
UPDATE abc SET b = other.b, c = other.c FROM (values (1, 2, 3), (2, 3, 4)) as other ("a", "b", "c") WHERE abc.a = other.a
query III rowsort
SELECT * FROM abc
----
1 2 3
2 3 4
# Check if UPDATE ... FROM works with multiple tables.
statement ok
CREATE TABLE ab (a INT, b INT)
statement ok
CREATE TABLE ac (a INT, c INT)
statement ok
INSERT INTO ab VALUES (1, 200), (2, 300)
statement ok
INSERT INTO ac VALUES (1, 300), (2, 400)
statement ok
UPDATE abc SET b = ab.b, c = ac.c FROM ab, ac WHERE abc.a = ab.a AND abc.a = ac.a
query III rowsort
SELECT * FROM abc
----
1 200 300
2 300 400
# Make sure UPDATE ... FROM works with LATERAL.
query IIIIIII colnames rowsort
UPDATE abc
SET
b=ab.b, c = other.c
FROM
ab, LATERAL
(SELECT * FROM ac WHERE ab.a=ac.a) AS other
WHERE
abc.a=ab.a
RETURNING
*
----
a b c a b a c
1 200 300 1 200 1 300
2 300 400 2 300 2 400
# Make sure the FROM clause cannot reference the target table.
statement error no data source matches prefix: abc
UPDATE abc SET a = other.a FROM (SELECT abc.a from abc as x) as other WHERE abc.a=other.a