File: returning_insert.test

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 (208 lines) | stat: -rw-r--r-- 4,987 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
# name: test/sql/returning/returning_insert.test
# description: Test returning with top level INSERT statement
# group: [returning]

statement ok
CREATE TABLE table1 (a INTEGER DEFAULT -1, b INTEGER DEFAULT -2, c INTEGER DEFAULT -3);

# standard insert still works
query I
INSERT INTO table1 VALUES (1, 2, 3);
----
1

# returning 1 named column
query I
INSERT INTO table1 VALUES (1, 2, 3) RETURNING a;
----
1

# returning all columns with *
query III
INSERT INTO table1 VALUES (1, 2, 3) RETURNING *;
----
1	2	3

# returning with COLUMNS expression
query II
INSERT INTO table1 VALUES (1, 2, 3) RETURNING COLUMNS('a|c');
----
1	3

query II
INSERT INTO table1 VALUES (1, 2, 3) RETURNING COLUMNS('a|c') + 42;
----
43	45

# returning * expression with more named columns
query IIIIII
INSERT INTO table1 VALUES (10, 20, 30), (40, 50, 60), (70, 80, 90) RETURNING *, c, b, a;
----
10	20	30	30	20	10
40	50	60	60	50	40
70	80	90	90	80	70

# returning column names out of order
query III
INSERT INTO table1 VALUES (1, 2, 3) RETURNING c, a, b;
----
3	1	2

# inserting column names out of order, then returning them in order
query III
INSERT INTO table1 (c, b, a) VALUES (3, 2, 1) RETURNING a, b, c;
----
1	2	3

# returning using a column alias
query II
INSERT INTO table1 VALUES (1, 2, 3) RETURNING a AS alias1, b AS alias2;
----
1	2

# returning expression with aggregate function at top level is not allowed
statement error
INSERT INTO table1 VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3) RETURNING SUM(a);
----
<REGEX>:Binder Error.*Aggregate functions are not supported.*

# returning * while inserting only a subset of columns
query III
INSERT INTO table1(a) VALUES (10) RETURNING *;
----
10	-2	-3


# returning while insert uses a subquery
query III
INSERT INTO table1 (a, b, c) SELECT * from table1 WHERE a = 10 and b=-2 and c=-3 RETURNING *;
----
10	-2	-3

# Using a Window function to insert
query III
INSERT INTO table1 (SELECT row_number() OVER (ORDER BY a) as row_number, b, c FROM table1 LIMIT 1) RETURNING *;
----
1	2	3

# inserting empty subquery returns nothing
query III
INSERT INTO table1 (a, b, c) SELECT * from table1 WHERE a = 100000 and b = 10000 and c=100000 RETURNING a, b, c;
----

# returning subquery should result in error (test 2).
statement error
INSERT INTO table1 (a, b, c) VALUES (10, 1000, 1000) RETURNING a IN (SELECT a from table1 where b = -2);
----
<REGEX>:Binder Error.*SUBQUERY is not supported.*

# using case statement
query I
INSERT INTO table1 VALUES (1, 2, 3) RETURNING CASE WHEN b=2 THEN a ELSE b END;
----
1

# using case statement
query I
INSERT INTO table1 VALUES (1, 2, 3) RETURNING CASE WHEN b=3 THEN a ELSE b END;
----
2

# returning combination of columns
query I
INSERT INTO table1 VALUES (1, 1, -3) RETURNING a + b + c;
----
-1

# returning string literal
query I
INSERT INTO table1 VALUES (1, 2, 3) RETURNING 'hello';
----
hello

# returning a list
query I
INSERT INTO table1 VALUES (1, 2, 3) RETURNING [a, b, c];
----
[1, 2, 3]

# returning a structure
query I
INSERT INTO table1 VALUES (1, 2, 3) RETURNING {'a':a, 'b':b, 'c':c};
----
{'a': 1, 'b': 2, 'c': 3}

# uncorrelated scalar subqueries in returning statement
query II
INSERT INTO table1(a) (SELECT 42) RETURNING a, b;
----
42	-2

# insert VALUES (scalar) throws error
statement error
INSERT INTO table1(a) VALUES (SELECT 42) RETURNING a, b, (select 10);
----
<REGEX>:Parser Error.*syntax error.*

# unknown columns
statement error
INSERT INTO table1 VALUES (1,2,3) RETURNING d, e, f
----
<REGEX>:Binder Error.*Referenced column.*not found.*

# Top level statement cannot contain a window function in returning statement
statement error
INSERT INTO table1 SELECT * from table1 RETURNING row_number() OVER (ORDER BY a) as row_number, a, b FROM table1;
----
<REGEX>:Parser Error.*syntax error.*

statement error
INSERT INTO table1 VALUES (1, 2, 3) RETURNING [1, 2] IN (SELECT [a, b] from table1);
----
<REGEX>:Binder Error.*SUBQUERY is not supported.*

# scalar subquery (should fail since scalar is a subquery)
statement error
INSERT INTO table1(a, b) VALUES (42, 43) RETURNING (SELECT a), (SELECT b), NULL;
----
<REGEX>:Binder Error.*SUBQUERY is not supported.*

# Window function in subquery
statement error
INSERT INTO table1 VALUES (-10, -20, -30)
    RETURNING '-10.-20' IN (SELECT group_concat(a) OVER (ORDER BY b) as GC FROM table1);
----
<REGEX>:Binder Error.*SUBQUERY is not supported.*

# new table with multiple types
statement ok
CREATE TABLE table2 (a VARCHAR DEFAULT 'hello world', b INT);

query II
INSERT INTO table2(a,b) VALUES ('hello duckdb', 1) RETURNING b, a;
----
1	hello duckdb

query I
INSERT INTO table2(b) VALUES (97) RETURNING b::VARCHAR;
----
97

query I
INSERT INTO table2(a, b) VALUES ('duckdb', 97) RETURNING {'a': a, 'b': b};
----
{'a': duckdb, 'b': 97}


### SEQUENCES
statement ok
CREATE SEQUENCE seq;

statement ok
CREATE TABLE table3 (a INTEGER DEFAULT nextval('seq'), b INTEGER);

query II
INSERT INTO table3(b) VALUES (4), (5) RETURNING a, b;
----
1	4
2	5