-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmytap-partition.sql
424 lines (336 loc) · 12.5 KB
/
mytap-partition.sql
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
-- PARTITIONS
-- ==========
-- Table level tests on partitioning
USE tap;
DELIMITER //
/************************************************************************************/
-- _has_partition( schema, table, partition, description )
DROP FUNCTION IF EXISTS _has_partition //
CREATE FUNCTION _has_partition(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64))
RETURNS BOOLEAN
DETERMINISTIC
BEGIN
DECLARE ret BOOLEAN;
SELECT 1 INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `partition_name` = part
LIMIT 1;
RETURN COALESCE(ret, 0);
END //
-- has_partition( schema, table, partition, description )
DROP FUNCTION IF EXISTS has_partition //
CREATE FUNCTION has_partition(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('Partition ', quote_ident(tname), '.', quote_ident(part),
' should exist');
END IF;
IF NOT _has_table(sname, tname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' does not exist')));
END IF;
RETURN ok(_has_partition(sname, tname, part), description);
END //
-- hasnt_partition( schema, table, partition, description )
DROP FUNCTION IF EXISTS hasnt_partition //
CREATE FUNCTION hasnt_partition(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('Partition ', quote_ident(tname), '.', quote_ident(part),
' should not exist');
END IF;
IF NOT _has_table(sname, tname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' does not exist')));
END IF;
RETURN ok(NOT _has_partition(sname, tname, part), description);
END //
/************************************************************************/
-- SUBPARTITION
-- _has_subpartition( schema, table, sub, description )
DROP FUNCTION IF EXISTS _has_subpartition //
CREATE FUNCTION _has_subpartition(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64))
RETURNS BOOLEAN
DETERMINISTIC
BEGIN
DECLARE ret BOOLEAN;
SELECT 1 INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `subpartition_name` = subp;
RETURN COALESCE(ret, 0);
END //
-- has_subpartition( schema, table, subpartition, description )
DROP FUNCTION IF EXISTS has_subpartition //
CREATE FUNCTION has_subpartition(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('Subpartition ', quote_ident(tname),
'.' , quote_ident(subp), ' should exist');
END IF;
IF NOT _has_table(sname, tname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' does not exist')));
END IF;
RETURN ok(_has_subpartition(sname, tname, subp), description);
END //
-- hasnt_subpartition( schema, table, subpartition, description )
DROP FUNCTION IF EXISTS hasnt_subpartition //
CREATE FUNCTION hasnt_subpartition(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('Subpartition ', quote_ident(tname),
'.', quote_ident(subp), ' should not exist');
END IF;
IF NOT _has_table(sname, tname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' does not exist')));
END IF;
RETURN ok(NOT _has_subpartition(sname, tname, subp), description);
END //
/****************************************************************************/
-- PARTITION EXPRESSION
DROP FUNCTION IF EXISTS _partition_expression //
CREATE FUNCTION _partition_expression(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64))
RETURNS LONGTEXT
DETERMINISTIC
BEGIN
DECLARE ret LONGTEXT;
SELECT TRIM(`partition_expression`) INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `partition_name` = part
LIMIT 1;
RETURN COALESCE(ret, NULL);
END //
DROP FUNCTION IF EXISTS partition_expression_is//
CREATE FUNCTION partition_expression_is(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64), expr LONGTEXT, description TEXT)
RETURNS LONGTEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = concat('Partition ', quote_ident(tname), '.', quote_ident(part),
' should have Partition Expression ', qv(TRIM(expr)));
END IF;
IF NOT _has_partition(sname, tname, part) THEN
RETURN CONCAT(ok( FALSE, description), '\n',
diag(CONCAT('Partition ', quote_ident(tname),'.', quote_ident(part),
' does not exist')));
END IF;
RETURN eq(_partition_expression(sname, tname, part), TRIM(expr), description);
END //
/****************************************************************************/
-- PARTITION EXPRESSION
DROP FUNCTION IF EXISTS _subpartition_expression //
CREATE FUNCTION _subpartition_expression(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64))
RETURNS LONGTEXT
DETERMINISTIC
BEGIN
DECLARE ret LONGTEXT;
SELECT TRIM(`subpartition_expression`) INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `subpartition_name` = subp;
RETURN COALESCE(ret, NULL);
END //
DROP FUNCTION IF EXISTS subpartition_expression_is//
CREATE FUNCTION subpartition_expression_is(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64), expr LONGTEXT, description TEXT)
RETURNS LONGTEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = concat('Subpartition ', quote_ident(tname), '.', quote_ident(subp),
' should have Subpartition Expression ', qv(TRIM(expr)));
END IF;
IF NOT _has_subpartition(sname, tname, subp) THEN
RETURN CONCAT(ok( FALSE, description), '\n',
diag(CONCAT('Subpartition ', quote_ident(tname), '.', quote_ident(subp),
' does not exist')));
END IF;
RETURN eq(_subpartition_expression(sname, tname, subp), TRIM(expr), description);
END //
/****************************************************************************/
-- PARTITION METHOD
DROP FUNCTION IF EXISTS _partition_method //
CREATE FUNCTION _partition_method(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64))
RETURNS VARCHAR(18)
DETERMINISTIC
BEGIN
DECLARE ret VARCHAR(18);
SELECT `partition_method` INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `partition_name` = part
LIMIT 1;
RETURN COALESCE(ret, NULL);
END //
DROP FUNCTION IF EXISTS partition_method_is//
CREATE FUNCTION partition_method_is(sname VARCHAR(64), tname VARCHAR(64), part VARCHAR(64), pmeth VARCHAR(18), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE valid ENUM('RANGE', 'LIST', 'HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY');
DECLARE EXIT HANDLER FOR 1265
RETURN CONCAT(ok(FALSE, description), '\n',
diag('Partitioning Method must be { RANGE | LIST | HASH | LINEAR HASH | KEY | LINEAR KEY }'));
IF description = '' THEN
SET description = CONCAT('Partition ', quote_ident(tname), '.', quote_ident(part),
' should have Partition Method ', qv(pmeth));
END IF;
SET valid = pmeth;
IF NOT _has_partition(sname, tname, part) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Partition ', quote_ident(tname),'.', quote_ident(part),
' does not exist')));
END IF;
RETURN eq(_partition_method(sname, tname, part), pmeth, description);
END //
/****************************************************************************/
-- SUBPARTITION METHOD
DROP FUNCTION IF EXISTS _subpartition_method //
CREATE FUNCTION _subpartition_method(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64))
RETURNS VARCHAR(12)
DETERMINISTIC
BEGIN
DECLARE ret VARCHAR(12);
SELECT `subpartition_method` INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `subpartition_name` = subp;
RETURN COALESCE(ret, NULL);
END //
DROP FUNCTION IF EXISTS subpartition_method_is//
CREATE FUNCTION subpartition_method_is(sname VARCHAR(64), tname VARCHAR(64), subp VARCHAR(64), smeth VARCHAR(18), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE valid ENUM('HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY');
DECLARE EXIT HANDLER FOR 1265
RETURN CONCAT(ok(FALSE, description), '\n',
diag('Subpartition Method must be { HASH | LINEAR HASH | KEY | LINEAR KEY }'));
IF description = '' THEN
SET description = CONCAT('Subpartition ', quote_ident(tname), '.', quote_ident(subp),
' should have SubPartition Method ', qv(smeth));
END IF;
SET valid = smeth;
IF NOT _has_subpartition(sname, tname, subp) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Subpartition ', quote_ident(tname),'.', quote_ident(subp),
' does not exist')));
END IF;
RETURN eq(_subpartition_method(sname, tname, subp), smeth, description);
END //
/****************************************************************************/
-- Number of PARTITIONS and SUBPARTITIONS defined for a table
-- might be more suitable test if the partition names are subject to change
-- NON NDB can have 8196
DROP FUNCTION IF EXISTS _partition_count //
CREATE FUNCTION _partition_count(sname VARCHAR(64), tname VARCHAR(64))
RETURNS SMALLINT
DETERMINISTIC
BEGIN
DECLARE ret SMALLINT;
SELECT COUNT(*) INTO ret
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname
AND `partition_name` IS NOT NULL;
RETURN COALESCE(ret, 0);
END //
DROP FUNCTION IF EXISTS partition_count_is//
CREATE FUNCTION partition_count_is(sname VARCHAR(64), tname VARCHAR(64), cnt SMALLINT, description TEXT)
RETURNS LONGTEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' should have a Partition Count of ', qv(cnt));
END IF;
IF NOT _has_table(sname, tname) THEN
RETURN CONCAT(ok( FALSE, description), '\n',
diag(CONCAT('Table ', quote_ident(sname),'.', quote_ident(tname),
' does not exist')));
END IF;
RETURN eq(_partition_count(sname, tname), cnt, description);
END //
/****************************************************************************/
-- Check that the proper partitions are defined
DROP FUNCTION IF EXISTS partitions_are //
CREATE FUNCTION partitions_are(sname VARCHAR(64), tname VARCHAR(64), want TEXT, description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
SET @want = want;
SET @have = (SELECT GROUP_CONCAT('`', COALESCE(`subpartition_name`, `partition_name`) ,'`')
FROM `information_schema`.`partitions`
WHERE `table_schema` = sname
AND `table_name` = tname);
IF description = '' THEN
SET description = CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' should have the correct partitions');
END IF;
IF NOT _has_table(sname,tname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Table ', quote_ident(sname), '.', quote_ident(tname),
' does not exist')));
END IF;
CALL _populate_want(@want);
CALL _populate_have(@have);
SET @missing = (SELECT _missing(@have));
SET @extras = (SELECT _extra(@want));
RETURN _are('partitions', @extras, @missing, description);
END //
/*****************************************************************************/
-- Version 8.0.11 does not have generic partitioning, it is now
-- included in the individual engines
-- This test is therefore redundant since the test for INNODB
-- will satify the test
-- partitioning enabled
DROP FUNCTION IF EXISTS _has_partitioning //
CREATE FUNCTION _has_partitioning()
RETURNS BOOLEAN
DETERMINISTIC
BEGIN
DECLARE ret BOOLEAN;
SELECT 1 INTO ret
FROM `information_schema`.`plugins`
WHERE `plugin_type`='STORAGE ENGINE'
AND `plugin_name` = 'partition'
AND `plugin_status` = 'active';
RETURN COALESCE(ret, 0);
END //
-- enabled and active
DROP FUNCTION IF EXISTS has_partitioning //
CREATE FUNCTION has_partitioning(description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = 'Partitioning should be active';
END IF;
IF tap.mysql_version() >= 800011 THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag('Partitioning support is part of specific ENGINE post 8.0.11'));
END IF;
RETURN ok(_has_partitioning(), description);
END //
DELIMITER ;