1--source include/have_debug.inc
2--source include/have_rocksdb.inc
3
4# Basic TTL test
5CREATE TABLE t1 (
6`a` binary(8) NOT NULL,
7`b` varbinary(64) NOT NULL,
8`c` varbinary(256) NOT NULL,
9`ts` bigint(20) UNSIGNED NOT NULL,
10`value` mediumblob NOT NULL,
11PRIMARY KEY (`b`,`a`,`c`)
12) ENGINE=ROCKSDB DEFAULT CHARSET=latin1
13COMMENT='ttl_duration=1;ttl_col=ts;';
14
15set global rocksdb_debug_ttl_rec_ts = -100;
16INSERT INTO t1 values ('a', 'b', 'c', UNIX_TIMESTAMP(), 'd');
17INSERT INTO t1 values ('d', 'e', 'f', UNIX_TIMESTAMP(), 'g');
18set global rocksdb_debug_ttl_rec_ts = 0;
19SELECT COUNT(*) FROM t1;
20set global rocksdb_force_flush_memtable_now=1;
21set global rocksdb_compact_cf='default';
22
23# should have filtered the rows out since ttl is passed in compaction filter
24SELECT COUNT(*) FROM t1;
25DROP TABLE t1;
26
27# column before TTL in value
28CREATE TABLE t1 (
29  a bigint(20) NOT NULL,
30  b int NOT NULL,
31  ts bigint(20) UNSIGNED NOT NULL,
32  c int NOT NULL,
33  PRIMARY KEY (a)
34) ENGINE=rocksdb
35COMMENT='ttl_duration=1;ttl_col=ts;';
36
37set global rocksdb_debug_ttl_rec_ts = -100;
38INSERT INTO t1 values (1, 3, UNIX_TIMESTAMP(), 5);
39INSERT INTO t1 values (2, 4, UNIX_TIMESTAMP(), 6);
40set global rocksdb_debug_ttl_rec_ts = 0;
41SELECT COUNT(*) FROM t1;
42
43set global rocksdb_force_flush_memtable_now=1;
44set global rocksdb_compact_cf='default';
45
46# should have filtered the rows out since ttl is passed in compaction filter
47SELECT COUNT(*) FROM t1;
48DROP TABLE t1;
49
50# multi-part PK w/ TTL
51CREATE TABLE t1 (
52  a bigint(20) NOT NULL,
53  b int NOT NULL,
54  c int NOT NULL,
55  ts bigint(20) UNSIGNED NOT NULL,
56  PRIMARY KEY (a,c)
57) ENGINE=rocksdb
58COMMENT='ttl_duration=1;ttl_col=ts;';
59
60set global rocksdb_debug_ttl_rec_ts = -100;
61INSERT INTO t1 values (1, 3, 5, UNIX_TIMESTAMP());
62INSERT INTO t1 values (2, 4, 6, UNIX_TIMESTAMP());
63set global rocksdb_debug_ttl_rec_ts = 0;
64SELECT COUNT(*) FROM t1;
65
66set global rocksdb_force_flush_memtable_now=1;
67set global rocksdb_compact_cf='default';
68
69# should have filtered the rows out since ttl is passed in compaction filter
70SELECT COUNT(*) FROM t1;
71DROP TABLE t1;
72
73# multi-part PK w/ TTL
74CREATE TABLE t1 (
75  a bigint(20) NOT NULL,
76  b int NOT NULL,
77  c int NOT NULL,
78  ts bigint(20) UNSIGNED NOT NULL,
79  PRIMARY KEY (a,c)
80) ENGINE=rocksdb
81COMMENT='ttl_duration=1;ttl_col=ts;';
82
83set global rocksdb_debug_ttl_rec_ts = -100;
84INSERT INTO t1 values (1, 3, 5, UNIX_TIMESTAMP());
85INSERT INTO t1 values (2, 4, 6, UNIX_TIMESTAMP());
86set global rocksdb_debug_ttl_rec_ts = 0;
87SELECT COUNT(*) FROM t1;
88
89set global rocksdb_force_flush_memtable_now=1;
90set global rocksdb_compact_cf='default';
91
92# should have filtered the rows out since ttl is passed in compaction filter
93SELECT COUNT(*) FROM t1;
94DROP TABLE t1;
95
96# nullable column(s) before TTL
97CREATE TABLE t1 (
98  a bigint(20) NOT NULL,
99  b int,
100  c int,
101  ts bigint(20) UNSIGNED NOT NULL,
102  PRIMARY KEY (a)
103) ENGINE=rocksdb
104COMMENT='ttl_duration=1;ttl_col=ts;';
105
106set global rocksdb_debug_ttl_rec_ts = -100;
107INSERT INTO t1 values (1, NULL, NULL, UNIX_TIMESTAMP());
108INSERT INTO t1 values (2, NULL, NULL, UNIX_TIMESTAMP());
109set global rocksdb_debug_ttl_rec_ts = 0;
110SELECT COUNT(*) FROM t1;
111
112set global rocksdb_force_flush_memtable_now=1;
113set global rocksdb_compact_cf='default';
114
115# should have filtered the rows out since ttl is passed in compaction filter
116SELECT COUNT(*) FROM t1;
117DROP TABLE t1;
118
119# variable len columns + null column(s) before TTL
120CREATE TABLE t1 (
121`a` binary(8) NOT NULL,
122`b` varbinary(64),
123`c` varbinary(256),
124`ts` bigint(20) UNSIGNED NOT NULL,
125`value` mediumblob NOT NULL,
126PRIMARY KEY (`a`)
127) ENGINE=ROCKSDB DEFAULT CHARSET=latin1
128COMMENT='ttl_duration=1;ttl_col=ts;';
129
130set global rocksdb_debug_ttl_rec_ts = -100;
131INSERT INTO t1 values ('a', NULL, 'bc', UNIX_TIMESTAMP(), 'd');
132INSERT INTO t1 values ('d', 'efghijk', NULL, UNIX_TIMESTAMP(), 'l');
133set global rocksdb_debug_ttl_rec_ts = 0;
134SELECT COUNT(*) FROM t1;
135
136set global rocksdb_force_flush_memtable_now=1;
137set global rocksdb_compact_cf='default';
138
139# should have filtered the rows out since ttl is passed in compaction filter
140SELECT COUNT(*) FROM t1;
141DROP TABLE t1;
142
143# TTL implicitly generated (no ttl column)
144CREATE TABLE t1 (
145  a bigint(20) NOT NULL,
146  b int NOT NULL,
147  c int NOT NULL,
148  PRIMARY KEY (a)
149) ENGINE=rocksdb
150COMMENT='ttl_duration=1;';
151
152set global rocksdb_debug_ttl_rec_ts = -100;
153INSERT INTO t1 values (1, 3, 5);
154INSERT INTO t1 values (2, 4, 6);
155set global rocksdb_debug_ttl_rec_ts = 0;
156SELECT COUNT(*) FROM t1;
157
158set global rocksdb_force_flush_memtable_now=1;
159set global rocksdb_compact_cf='default';
160
161# should have filtered the rows out since ttl is passed in compaction filter
162SELECT COUNT(*) FROM t1;
163DROP TABLE t1;
164
165# TTL field as the PK
166CREATE TABLE t1 (
167	a int,
168  ts bigint(20) UNSIGNED NOT NULL,
169  PRIMARY KEY (a, ts)
170) ENGINE=rocksdb
171COMMENT='ttl_duration=5;ttl_col=ts;';
172
173INSERT INTO t1 values (1, UNIX_TIMESTAMP());
174INSERT INTO t1 values (2, UNIX_TIMESTAMP());
175SELECT COUNT(*) FROM t1;
176
177set global rocksdb_debug_ttl_snapshot_ts = -10;
178set global rocksdb_force_flush_memtable_now=1;
179set global rocksdb_compact_cf='default';
180set global rocksdb_debug_ttl_snapshot_ts = 0;
181# should all still be there..
182SELECT COUNT(*) FROM t1;
183
184set global rocksdb_debug_ttl_snapshot_ts = 10;
185set global rocksdb_compact_cf='default';
186set global rocksdb_debug_ttl_snapshot_ts = 0;
187# should have filtered the rows out since ttl is passed in compaction filter
188SELECT COUNT(*) FROM t1;
189DROP TABLE t1;
190
191
192# TTL field inside multi-part pk
193CREATE TABLE t1 (
194  a bigint(20) NOT NULL,
195  b int NOT NULL,
196  ts bigint(20) UNSIGNED NOT NULL,
197  c int NOT NULL,
198  PRIMARY KEY (a, ts)
199) ENGINE=rocksdb
200COMMENT='ttl_duration=1;ttl_col=ts;';
201
202set global rocksdb_debug_ttl_rec_ts = -100;
203INSERT INTO t1 values (1, 3, UNIX_TIMESTAMP(), 5);
204INSERT INTO t1 values (2, 4, UNIX_TIMESTAMP(), 6);
205set global rocksdb_debug_ttl_rec_ts = 0;
206SELECT COUNT(*) FROM t1;
207
208set global rocksdb_force_flush_memtable_now=1;
209set global rocksdb_compact_cf='default';
210
211# should have filtered the rows out since ttl is passed in compaction filter
212SELECT COUNT(*) FROM t1;
213DROP TABLE t1;
214
215# TTL field inside key with variable length things..
216CREATE TABLE t1 (
217`a` binary(8) NOT NULL,
218`b` varbinary(64),
219`c` varbinary(256),
220`ts` bigint(20) UNSIGNED NOT NULL,
221`value` mediumblob NOT NULL,
222PRIMARY KEY (`a`, `ts`)
223) ENGINE=ROCKSDB DEFAULT CHARSET=latin1
224COMMENT='ttl_duration=1;ttl_col=ts;';
225
226set global rocksdb_debug_ttl_rec_ts = -100;
227INSERT INTO t1 values ('a', NULL, 'bc', UNIX_TIMESTAMP(), 'd');
228INSERT INTO t1 values ('de', 'fghijk', NULL, UNIX_TIMESTAMP(), 'l');
229set global rocksdb_debug_ttl_rec_ts = 0;
230SELECT COUNT(*) FROM t1;
231
232set global rocksdb_force_flush_memtable_now=1;
233set global rocksdb_compact_cf='default';
234
235# should have filtered the rows out since ttl is passed in compaction filter
236SELECT COUNT(*) FROM t1;
237DROP TABLE t1;
238
239# TTL test where you compact (values still exist), real_sleep, then compact again,
240# values should now be gone.
241CREATE TABLE t1 (
242a INT NOT NULL,
243b varbinary(64) NOT NULL,
244c varbinary(256) NOT NULL,
245ts bigint(20) UNSIGNED NOT NULL,
246value mediumblob NOT NULL,
247PRIMARY KEY (b,a,c)
248) ENGINE=ROCKSDB DEFAULT CHARSET=latin1
249COMMENT='ttl_duration=10;ttl_col=ts;';
250
251set global rocksdb_debug_ttl_rec_ts = -300;
252INSERT INTO t1 values (1, 'b', 'c', UNIX_TIMESTAMP(), 'd');
253INSERT INTO t1 values (2, 'e', 'f', UNIX_TIMESTAMP(), 'g');
254set global rocksdb_debug_ttl_rec_ts = 300;
255INSERT INTO t1 values (3, 'i', 'j', UNIX_TIMESTAMP(), 'k');
256INSERT INTO t1 values (4, 'm', 'n', UNIX_TIMESTAMP(), 'o');
257set global rocksdb_debug_ttl_rec_ts = 0;
258
259# Nothing should get removed here.
260set global rocksdb_debug_ttl_snapshot_ts = -3600;
261set global rocksdb_force_flush_memtable_now=1;
262set global rocksdb_compact_cf='default';
263set global rocksdb_debug_ttl_snapshot_ts = 0;
264--sorted_result
265SELECT a FROM t1;
266
267# 1 and 2 should get removed here.
268set global rocksdb_compact_cf='default';
269--sorted_result
270SELECT a FROM t1;
271
272# 3 and 4 should get removed here.
273set global rocksdb_debug_ttl_snapshot_ts = 3600;
274set global rocksdb_compact_cf='default';
275set global rocksdb_debug_ttl_snapshot_ts = 0;
276--sorted_result
277SELECT a FROM t1;
278
279DROP TABLE t1;
280
281# TTL field with nullable ttl column (should fail)
282--error ER_RDB_TTL_COL_FORMAT
283CREATE TABLE t1 (
284  a bigint(20) UNSIGNED NOT NULL,
285  b int NOT NULL,
286  c int NOT NULL,
287  ts bigint(20),
288  PRIMARY KEY (a,c)
289) ENGINE=rocksdb
290COMMENT='ttl_duration=1;ttl_col=ts;';
291
292# TTL field with non 8-bit integer column (should fail)
293--error ER_RDB_TTL_COL_FORMAT
294CREATE TABLE t1 (
295  a bigint(20) UNSIGNED NOT NULL,
296  b int NOT NULL,
297  c int NOT NULL,
298  ts int,
299  PRIMARY KEY (a,c)
300) ENGINE=rocksdb
301COMMENT='ttl_duration=1;ttl_col=ts;';
302
303# TTL duration as some random garbage value
304--error ER_RDB_TTL_DURATION_FORMAT
305CREATE TABLE t1 (
306  a bigint(20) UNSIGNED NOT NULL,
307  b int NOT NULL,
308  c int NOT NULL,
309  PRIMARY KEY (a,c)
310) ENGINE=rocksdb
311COMMENT='ttl_duration=abc;';
312
313# TTL col is some column outside of the table
314--error ER_RDB_TTL_COL_FORMAT
315CREATE TABLE t1 (
316  a bigint(20) UNSIGNED NOT NULL,
317  b int NOT NULL,
318  c int NOT NULL,
319  PRIMARY KEY (a,c)
320) ENGINE=rocksdb
321COMMENT='ttl_duration=1;ttl_col=abc;';
322
323# TTL col must have accompanying duration
324--error ER_RDB_TTL_COL_FORMAT
325CREATE TABLE t1 (
326  a bigint(20) UNSIGNED NOT NULL,
327  b int NOT NULL,
328  c int NOT NULL,
329  PRIMARY KEY (a,c)
330) ENGINE=rocksdb
331COMMENT='ttl_col=abc;';
332
333# Make sure it doesn't filter out things early
334CREATE TABLE t1 (
335  a bigint(20) NOT NULL,
336  PRIMARY KEY (a)
337) ENGINE=rocksdb
338COMMENT='ttl_duration=500;';
339
340INSERT INTO t1 values (1);
341SELECT COUNT(*) FROM t1;
342set global rocksdb_force_flush_memtable_now=1;
343set global rocksdb_compact_cf='default';
344
345SELECT COUNT(*) FROM t1;
346DROP TABLE t1;
347
348# Testing altering table comment with updated TTL duration
349# This should trigger a rebuild of the table
350CREATE TABLE t1 (
351  a INT PRIMARY KEY
352) ENGINE=rocksdb
353COMMENT='ttl_duration=100;';
354
355INSERT INTO t1 values (1);
356SELECT * FROM t1;
357
358set global rocksdb_debug_ttl_rec_ts = -300;
359ALTER TABLE t1 COMMENT = 'ttl_duration=1';
360set global rocksdb_debug_ttl_rec_ts = 0;
361SHOW CREATE TABLE t1;
362set global rocksdb_force_flush_memtable_now=1;
363set global rocksdb_compact_cf='default';
364
365SELECT COUNT(*) FROM t1;
366DROP TABLE t1;
367
368# Tables with hidden PK and SK disabled
369CREATE TABLE t1 (
370  a INT PRIMARY KEY,
371  b INT
372) ENGINE=rocksdb
373COMMENT='ttl_duration=100;';
374
375--error ER_RDB_TTL_UNSUPPORTED
376ALTER TABLE t1 DROP PRIMARY KEY;
377
378DROP TABLE t1;
379
380# Test replacing PK, ttl should still work after
381CREATE TABLE t1 (
382  a INT PRIMARY KEY,
383  b INT
384) ENGINE=rocksdb
385COMMENT='ttl_duration=5;';
386
387INSERT INTO t1 VALUES (1,1);
388INSERT INTO t1 VALUES (2,2);
389
390ALTER TABLE t1 DROP PRIMARY KEY, ADD PRIMARY KEY(b);
391set global rocksdb_debug_ttl_snapshot_ts = -3600;
392set global rocksdb_force_flush_memtable_now=1;
393set @@global.rocksdb_compact_cf = 'default';
394set global rocksdb_debug_ttl_snapshot_ts = 0;
395
396--sorted_result
397SELECT COUNT(*) FROM t1;
398
399set global rocksdb_debug_ttl_snapshot_ts = 3600;
400set @@global.rocksdb_compact_cf = 'default';
401set global rocksdb_debug_ttl_snapshot_ts = 0;
402
403--sorted_result
404SELECT COUNT(*) FROM t1;
405
406DROP TABLE t1;
407
408# Make sure table comment filled with other text before/after will work
409# (basically, it needs semicolon before and after)
410CREATE TABLE t1 (
411  a bigint(20) UNSIGNED NOT NULL,
412  b int,
413  PRIMARY KEY (a,b)
414) ENGINE=rocksdb
415COMMENT='asdadfasdfsadfadf ;ttl_duration=1; asfasdfasdfadfa';
416INSERT INTO t1 values (UNIX_TIMESTAMP(), 1);
417SELECT COUNT(*) FROM t1;
418
419set global rocksdb_debug_ttl_snapshot_ts = 3600;
420set global rocksdb_force_flush_memtable_now=1;
421set global rocksdb_compact_cf='default';
422set global rocksdb_debug_ttl_snapshot_ts = 0;
423
424SELECT COUNT(*) FROM t1;
425
426ALTER TABLE t1 COMMENT = 'adsf;;ttl_duration=5;asfasdfa;ttl_col=a;asdfasdf;';
427
428set global rocksdb_debug_ttl_rec_ts = 300;
429INSERT INTO t1 values (UNIX_TIMESTAMP(), 2);
430set global rocksdb_debug_ttl_rec_ts = 0;
431set global rocksdb_force_flush_memtable_now=1;
432
433# nothing removed here
434set global rocksdb_compact_cf='default';
435SELECT COUNT(*) FROM t1;
436
437# all removed here
438set global rocksdb_debug_ttl_snapshot_ts = 3600;
439set global rocksdb_compact_cf='default';
440set global rocksdb_debug_ttl_snapshot_ts = 0;
441SELECT COUNT(*) FROM t1;
442
443DROP TABLE t1;
444
445# Test to make sure that TTL retains original timestamp during update
446CREATE TABLE t1 (
447  a bigint(20) NOT NULL,
448  PRIMARY KEY (a)
449) ENGINE=rocksdb
450COMMENT='ttl_duration=5;';
451
452set global rocksdb_debug_ttl_rec_ts = -300;
453INSERT INTO t1 values (1);
454INSERT INTO t1 values (3);
455INSERT INTO t1 values (5);
456set global rocksdb_debug_ttl_rec_ts = 300;
457INSERT INTO t1 values (7);
458INSERT INTO t1 values (9);
459set global rocksdb_debug_ttl_rec_ts = 0;
460
461UPDATE t1 SET a=a+1;
462--sorted_result
463SELECT * FROM t1;
464
465set global rocksdb_force_flush_memtable_now=1;
466set global rocksdb_compact_cf='default';
467
468# 1,3,5 should be dropped
469--sorted_result
470SELECT * FROM t1;
471DROP TABLE t1;
472
473# test behaviour on update with TTL column, TTL time can be updated here.
474CREATE TABLE t1 (
475  a INT,
476  b bigint(20) UNSIGNED NOT NULL,
477  PRIMARY KEY (a)
478) ENGINE=rocksdb
479COMMENT='ttl_duration=5;ttl_col=b;';
480
481set global rocksdb_debug_ttl_rec_ts = -300;
482INSERT INTO t1 values (1, UNIX_TIMESTAMP());
483INSERT INTO t1 values (3, UNIX_TIMESTAMP());
484INSERT INTO t1 values (5, UNIX_TIMESTAMP());
485INSERT INTO t1 values (7, UNIX_TIMESTAMP());
486
487set global rocksdb_debug_ttl_rec_ts = 300;
488UPDATE t1 SET b=UNIX_TIMESTAMP() WHERE a < 4;
489set global rocksdb_debug_ttl_rec_ts = 0;
490
491--sorted_result
492SELECT a FROM t1;
493
494set global rocksdb_force_flush_memtable_now=1;
495set global rocksdb_compact_cf='default';
496
497# 5 and 7 should be gone here
498--sorted_result
499SELECT a FROM t1;
500DROP TABLE t1;
501
502# Test rows expired stat variable and disable ttl variable
503CREATE TABLE t1 (
504  a bigint(20) NOT NULL,
505  PRIMARY KEY (a)
506) ENGINE=rocksdb
507COMMENT='ttl_duration=1;';
508
509set global rocksdb_debug_ttl_rec_ts = -100;
510INSERT INTO t1 values (1);
511INSERT INTO t1 values (2);
512INSERT INTO t1 values (3);
513set global rocksdb_debug_ttl_rec_ts = 0;
514
515set global rocksdb_enable_ttl=0;
516set global rocksdb_force_flush_memtable_now=1;
517set global rocksdb_compact_cf='default';
518
519select variable_value into @c from information_schema.global_status where variable_name='rocksdb_rows_expired';
520set global rocksdb_enable_ttl=1;
521set global rocksdb_compact_cf='default';
522
523select variable_value-@c from information_schema.global_status where variable_name='rocksdb_rows_expired';
524SELECT COUNT(*) FROM t1;
525DROP TABLE t1;
526
527
528# Table with TTL won't increment rows expired when no records have been
529# compacted
530CREATE TABLE t1 (
531  a bigint(20) NOT NULL,
532  PRIMARY KEY (a)
533) ENGINE=rocksdb
534COMMENT='ttl_duration=100;';
535
536INSERT INTO t1 values (1);
537INSERT INTO t1 values (2);
538INSERT INTO t1 values (3);
539
540select variable_value into @c from information_schema.global_status where variable_name='rocksdb_rows_expired';
541set global rocksdb_force_flush_memtable_now=1;
542set global rocksdb_compact_cf='default';
543select variable_value-@c from information_schema.global_status where variable_name='rocksdb_rows_expired';
544
545DROP TABLE t1;
546