forked from cockroachdb/cockroach
/
interleaved
403 lines (314 loc) · 9.75 KB
/
interleaved
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
# LogicTest: local local-vec fakedist fakedist-metadata
# Grandparent table
statement ok
CREATE TABLE p2 (i INT PRIMARY KEY, s STRING)
# Two tables interleaved at the same level
statement ok
CREATE TABLE p1_0 (
i INT,
s1 STRING,
s2 STRING,
d DECIMAL,
PRIMARY KEY (i, s1),
FAMILY (i, s1, s2),
FAMILY (d)
) INTERLEAVE IN PARENT p2 (i)
statement ok
CREATE TABLE p1_1 (
i INT PRIMARY KEY,
s1 STRING,
s2 STRING,
d DECIMAL
) INTERLEAVE IN PARENT p2 (i)
# Two level deep interleave
statement ok
CREATE TABLE p0 (
i INT,
s1 STRING,
s2 STRING,
d DECIMAL,
PRIMARY KEY (i, s1, s2)
) INTERLEAVE IN PARENT p1_0 (i, s1)
statement ok
INSERT INTO p2 VALUES (2, '2'), (3, '3'), (5, '5'), (7, '7')
statement ok
INSERT INTO p1_0 VALUES (2, '2', '2.01', 2), (3, '3', '3.01', 3), (5, '5', NULL, NULL)
statement ok
INSERT INTO p1_1 VALUES (2, '2', '2.11', 2), (3, '3', '3.11', 3)
statement ok
INSERT INTO p0 VALUES (2, '2', '2.0', 2), (3, '3', '3.0', 3), (5, '5', '5.0', 5)
query IT rowsort
SELECT * FROM p2
----
2 2
3 3
5 5
7 7
query ITTT rowsort
SELECT * FROM p1_0
----
2 2 2.01 2
3 3 3.01 3
5 5 NULL NULL
query ITTT rowsort
SELECT * FROM p1_1
----
2 2 2.11 2
3 3 3.11 3
query ITTT rowsort
SELECT * FROM p0
----
2 2 2.0 2
3 3 3.0 3
5 5 5.0 5
statement ok
CREATE INDEX p0i ON p0 (i) INTERLEAVE IN PARENT p1_1 (i)
statement ok
CREATE INDEX p1_id ON p1_1 (i, d) INTERLEAVE IN PARENT p1_1 (i)
query ITTT rowsort
SELECT * FROM p0@p0i WHERE i BETWEEN 2 AND 4
----
2 2 2.0 2
3 3 3.0 3
query ITTT rowsort
SELECT * FROM p1_1@p1_id WHERE d BETWEEN 1.5 AND 4.0
----
2 2 2.11 2
3 3 3.11 3
statement ok
DELETE FROM p1_0 WHERE i = 3
statement ok
INSERT INTO p1_0 (i, s1) VALUES (5, '5') ON CONFLICT (i, s1) DO UPDATE SET i = 7, s2 = '7.01', d = 7.0
statement ok
DELETE FROM p2 WHERE i = 2 OR i = 7
query IT rowsort
SELECT * FROM p2
----
3 3
5 5
# Check that we're skipping first/last row of a block of interleaved data correctly
query ITTT rowsort
SELECT * FROM p0
----
2 2 2.0 2
3 3 3.0 3
5 5 5.0 5
# check that a column backfill on an interleaved table works well.
statement ok
ALTER TABLE p0 ADD e INT DEFAULT 7
query ITTTI rowsort
SELECT * FROM p0
----
2 2 2.0 2 7
3 3 3.0 3 7
5 5 5.0 5 7
# Check that fast delete is disabled when run on a table with child interleaves
statement ok
DELETE FROM p2
query IT rowsort
SELECT * FROM p2
----
query ITTT rowsort
SELECT * FROM p1_0
----
2 2 2.01 2
7 5 7.01 7.0
# check that a column backfill on an interleaved table works well.
statement ok
ALTER TABLE p1_0 ADD e INT DEFAULT 7
query ITTTI rowsort
SELECT * FROM p1_0
----
2 2 2.01 2 7
7 5 7.01 7.0 7
query ITTTI rowsort
SELECT * FROM p0
----
2 2 2.0 2 7
3 3 3.0 3 7
5 5 5.0 5 7
statement ok
DROP INDEX p0@p0i
query ITTTI rowsort
SELECT * FROM p0
----
2 2 2.0 2 7
3 3 3.0 3 7
5 5 5.0 5 7
statement ok
DROP TABLE p0
query ITTTI rowsort
SELECT * FROM p1_0
----
2 2 2.01 2 7
7 5 7.01 7.0 7
statement error "p2" is interleaved by table "p1_0"
TRUNCATE TABLE p2
statement ok
TRUNCATE TABLE p2 CASCADE
statement error unimplemented
DROP TABLE p2
statement ok
CREATE INDEX p1_s2 ON p1_1 (s2)
# p1_0 is truncated when p2 is truncated.
query ITTTI rowsort
SELECT * FROM p1_0
----
statement ok
DROP TABLE p2 CASCADE
statement error pgcode 42P01 relation "p0" does not exist
SELECT * FROM p0
# Validation and descriptor bookkeeping
# TODO(dan): Interleave these two indexes once we support the syntax.
statement ok
CREATE TABLE all_interleaves (
b INT PRIMARY KEY,
c INT,
d INT,
INDEX (c),
UNIQUE INDEX (d)
) INTERLEAVE IN PARENT p1_1 (b)
statement ok
CREATE INDEX ON all_interleaves (c, d) INTERLEAVE IN PARENT p1_1 (c)
statement ok
CREATE UNIQUE INDEX ON all_interleaves (d, c) INTERLEAVE IN PARENT p1_1 (d)
query TT
SHOW CREATE TABLE all_interleaves
----
all_interleaves CREATE TABLE all_interleaves (
b INT8 NOT NULL,
c INT8 NULL,
d INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (b ASC),
INDEX all_interleaves_c_idx (c ASC),
UNIQUE INDEX all_interleaves_d_key (d ASC),
FAMILY "primary" (b, c, d)
) INTERLEAVE IN PARENT p1_1 (b)
statement error pgcode 42P01 relation "missing" does not exist
CREATE TABLE err (f FLOAT PRIMARY KEY) INTERLEAVE IN PARENT missing (f)
# Check that interleaved columns match in length to parent's primary columns.
statement error pq: declared interleaved columns \(s1, d\) must match the parent's primary index \(i\)
CREATE INDEX ON p1_0 (s1, d) INTERLEAVE IN PARENT p1_1 (s1, d)
# Check that interleaved columns are prefix of index's columns.
statement error pq: declared interleaved columns \(i, s1\) must be a prefix of the primary key columns being interleaved \(i\)
CREATE TABLE err (i INT PRIMARY KEY) INTERLEAVE IN PARENT p1_0 (i, s1)
statement error pq: declared interleaved columns \(i, s1\) must be a prefix of the index columns being interleaved \(i\)
CREATE INDEX ON p1_1 (i) INTERLEAVE IN PARENT p1_0 (i, s1)
# Check that interleaved columns are referencing a valid prefix of names
# of the index's columns.
statement error pq: declared interleaved columns \(j\) must refer to a prefix of the primary key column names being interleaved \(i, j\)
CREATE TABLE err (i INT, j INT, PRIMARY KEY (i, j)) INTERLEAVE IN PARENT p1_1 (j)
statement error pq: declared interleaved columns \(i\) must refer to a prefix of the index column names being interleaved \(d\)
CREATE INDEX ON p1_0 (d) INTERLEAVE IN PARENT p1_1 (i)
# Check that interleaved columns are of the same type AND direction as parent's
# primary columns.
statement error pq: declared interleaved columns \(f\) must match type and sort direction of the parent's primary index \(i\)
CREATE TABLE err (f FLOAT PRIMARY KEY) INTERLEAVE IN PARENT p1_1 (f)
statement error pq: declared interleaved columns \(d\) must match type and sort direction of the parent's primary index \(i\)
CREATE INDEX ON p1_0 (d) INTERLEAVE IN PARENT p1_1 (d)
statement error pq: declared interleaved columns \(i\) must match type and sort direction of the parent's primary index \(i\)
CREATE INDEX ON p1_0 (i DESC) INTERLEAVE IN PARENT p1_1 (i)
statement error unimplemented
CREATE TABLE err (i INT PRIMARY KEY, INDEX (i) INTERLEAVE IN PARENT p1_1 (i))
statement error unimplemented
CREATE TABLE err (i INT PRIMARY KEY, UNIQUE INDEX (i) INTERLEAVE IN PARENT p1_1 (i))
statement error unimplemented: unsupported shorthand CASCADE
CREATE TABLE err (i INT PRIMARY KEY) INTERLEAVE IN PARENT p1_1 (i) CASCADE
statement error unimplemented: unsupported shorthand RESTRICT
CREATE TABLE err (i INT PRIMARY KEY) INTERLEAVE IN PARENT p1_1 (i) RESTRICT
# Regression test for #13451
statement ok
CREATE TABLE customers (
id INT PRIMARY KEY,
name STRING (50)
)
statement ok
CREATE TABLE orders (
customer INT,
id INT,
total DECIMAL (20, 5),
PRIMARY KEY (customer, id),
CONSTRAINT fk_customer FOREIGN KEY (customer) REFERENCES customers
) INTERLEAVE IN PARENT customers (customer)
statement ok
INSERT INTO customers
(id, name) VALUES
(1, 'Ha-Yun'),
(2, 'Emanuela')
statement ok
INSERT INTO orders
(customer, id, total) VALUES
(1, 1000, 100.00),
(2, 1001, 90.00),
(1, 1002, 80.00),
(2, 1003, 70.00)
query IIR
SELECT * FROM orders WHERE customer = 1 AND id = 1000
----
1 1000 100.00000
# Check that interleaving can occur across databases
statement ok
CREATE DATABASE other; CREATE TABLE other.foo(x INT PRIMARY KEY);
CREATE TABLE interdb (x INT PRIMARY KEY) INTERLEAVE IN PARENT other.foo (x)
query TT
SHOW CREATE TABLE interdb
----
interdb CREATE TABLE interdb (
x INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (x ASC),
FAMILY "primary" (x)
) INTERLEAVE IN PARENT other.public.foo (x)
statement ok
CREATE TABLE t1 (id1 INT PRIMARY KEY, id2 INT, id3 INT);
statement ok
CREATE INDEX c on t1 (id2)
STORING (id3)
INTERLEAVE in PARENT t1 (id2);
statement ok
DROP INDEX t1@c;
statement ok
DROP TABLE t1
# Regression test for #20067.
statement ok
CREATE TABLE p20067 (
p_id INT PRIMARY KEY,
name STRING NOT NULL
)
statement ok
CREATE TABLE c20067 (
p_id INT,
c_id INT,
name STRING NOT NULL,
PRIMARY KEY (p_id, c_id),
CONSTRAINT uq_name UNIQUE(name)
) INTERLEAVE IN PARENT p20067 (p_id)
statement ok
BEGIN;
INSERT INTO p20067 VALUES (1, 'John Doe');
INSERT INTO c20067 VALUES (1, 1, 'John Doe Junior');
COMMIT;
statement error duplicate key value \(name\)=\('John Doe Junior'\) violates unique constraint "uq_name"
INSERT INTO c20067 VALUES (2, 1, 'John Doe Junior')
statement error duplicate key value \(name\)=\('John Doe Junior'\) violates unique constraint "uq_name"
BEGIN; INSERT INTO p20067 VALUES (2, 'John Doe'); INSERT INTO c20067 VALUES (2, 1, 'John Doe Junior'); END;
# End the last transaction.
statement ok
END
statement error duplicate key value \(p_id,c_id\)=\(1,1\) violates unique constraint "primary"
INSERT INTO c20067 VALUES (1, 1, 'John Doe')
# Regression test for #26756: ensure that interleaved table joins don't get
# planned incorrectly given a merge join ordering caused by a constant value
# constraint on a non-interleaved column.
subtest interleaved_join_on_other_columns
statement ok
CREATE TABLE users (id INT PRIMARY KEY)
statement ok
CREATE TABLE documents (id INT PRIMARY KEY, user_id INT NOT NULL) INTERLEAVE IN PARENT users (id)
statement ok
INSERT INTO users(id) VALUES(1)
statement ok
INSERT INTO documents(id, user_id) VALUES (0, 1)
query I
SELECT count(*) FROM users JOIN documents ON users.id=documents.user_id WHERE documents.id=0
----
1