-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_planner.py
More file actions
531 lines (464 loc) · 17.5 KB
/
test_planner.py
File metadata and controls
531 lines (464 loc) · 17.5 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#
# test_planner.py
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
class Predicates(object):
@staticmethod
def no_table_scan(explanation):
this_type = explanation['type']
if this_type == 'table scan':
return False
elif this_type == 'union':
return all([Predicates.no_table_scan(p) for p in explanation['plans']])
elif 'source_plan' in explanation:
return Predicates.no_table_scan(explanation['source_plan'])
else:
return True
@staticmethod
def only_index_named(index_name, explanation):
this_type = explanation['type']
if this_type == 'index scan' and explanation['index name'] == index_name:
return True
elif this_type == 'union':
return all([Predicates.only_index_named(index_name, p) for p in explanation['plans']])
elif 'source_plan' in explanation:
return Predicates.only_index_named(index_name, explanation['source_plan'])
else:
return False
#issue-17: Added decode_bytes and decode_key_part to update explain output with user readable keys
# To check begin and end keys in bounds in explanation
@staticmethod
def check_bound(index_name, begin, end, explanation):
this_type = explanation['type']
if this_type == 'union':
return all([Predicates.check_bound(index_name, begin, end, p) for p in explanation['plans']])
elif 'source_plan' in explanation:
return Predicates.check_bound(index_name, begin, end, explanation['source_plan'])
elif index_name in explanation['bounds']:
bound = explanation['bounds'][index_name]
if bound['begin'] == begin and bound['end'] == end:
return True
elif bound['begin'] == begin and bound['end'] == 'nan.0':
# This check needed because in MacOS decode byte '0x1f' returns 'nan.0' instead of 'inf.0'.
return True
else:
return False
else:
return False
@staticmethod
def pk_lookup(explanation):
this_type = explanation['type']
if this_type == 'PK lookup':
return True
elif this_type == 'union':
return all([Predicates.pk_lookup(p) for p in explanation['plans']])
elif 'source_plan' in explanation:
return Predicates.pk_lookup(explanation['source_plan'])
else:
return False
@staticmethod
def no_filter(explanation):
this_type = explanation['type']
if this_type == 'filter':
return False
elif this_type == 'union':
return all([Predicates.no_filter(p) for p in explanation['plans']])
elif 'source_plan' in explanation:
return Predicates.no_filter(explanation['source_plan'])
else:
return True
@staticmethod
def pk_lookup_no_filter(explanation):
return Predicates.pk_lookup(explanation) and Predicates.no_filter(explanation)
@staticmethod
def no_table_scan_no_filter(explanation):
return Predicates.no_table_scan(explanation) and Predicates.no_filter(explanation)
# PK Lookup Tests
def test_pk_lookup(fixture_collection):
ret = fixture_collection.find({'_id': 1}).explain()
assert Predicates.pk_lookup_no_filter(ret['explanation'])
def test_pk_scan(fixture_collection):
ret = fixture_collection.find({'_id': {'$gt': 1}}).explain()
assert Predicates.pk_lookup(ret['explanation'])
# Simple Index Tests
def test_simple_index_basic(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='index')
query = {'a': 1}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan_no_filter(ret['explanation'])
def test_simple_index_union(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='index1')
fixture_collection.create_index(keys=[('b', 1)], name='index2')
query = {
'$or': [{
"a": 1
}, {
"b": 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
def test_simple_index_compound_as_simple(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='compound')
query = {'a': 1}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan_no_filter(ret['explanation'])
def test_simple_index_prefer_simple_over_compound(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='compound')
fixture_collection.create_index(keys=[('a', 1)], name='simple')
query = {'a': 1}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('simple', ret['explanation'])
def test_simple_index_prefer_shorter_compound_index(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1)], name='long')
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='short')
query = {'a': 1}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('short', ret['explanation'])
def test_simple_index_greedy_plan(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='one')
fixture_collection.create_index(keys=[('b', 1)], name='two')
query = {
'$and': [{
'b': 1
}, {
'a': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('two', ret['explanation'])
def test_simple_index_scan(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='index')
query = {
"a": {
'$gt': 1
}
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
def test_simple_index_multi_union(fixture_collection):
fixture_collection.create_index(keys=[('d', 1)], name='index1')
fixture_collection.create_index(keys=[('c', 1)], name='index2')
fixture_collection.create_index(keys=[('b', 1)], name='index3')
fixture_collection.create_index(keys=[('a', 1)], name='index4')
query = {
'$or': [{
"a": {
'$gt': 1
}
}, {
"b": 1
}, {
"c": {
'$lte': 1
}
}, {
"d": 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
def test_simple_index_dotted_path(fixture_collection):
fixture_collection.create_index(keys=[('a.b', 1)], name='simple')
query = {'a.b': 'hello'}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
# Compound Index Tests
def test_compound_index_basic(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='compound')
query = {
'$and': [{
'a': 1
}, {
'b': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan_no_filter(ret['explanation'])
def test_compound_index_out_of_order(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='compound')
query = {
'$and': [{
'b': 1
}, {
'a': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
def test_compound_index_find_long_index(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1), ('d', 1), ('e', 1), ('f', 1), ('g', 1)], name='long')
query = {
'$and': [{
'g': 1
}, {
'f': 1
}, {
'e': 1
}, {
'd': 1
}, {
'c': 1
}, {
'b': 1
}, {
'a': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
def test_compound_index_union(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='compound')
fixture_collection.create_index(keys=[('d', 1), ('c', 1)], name='compound2')
query = {
'$or': [{
'$and': [{
'b': 1
}, {
'a': 1
}]
}, {
'$and': [{
'd': 1
}, {
'c': 1
}]
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.no_table_scan(ret['explanation'])
def test_compound_index_range_at_end(fixture_collection):
fixture_collection.create_index(keys=[('d', 1), ('b', 1), ('c', 1)], name='compound')
fixture_collection.create_index(keys=[('d', 1)], name='simple')
query = {
'$and': [{
'c': {
'$gt': 1
}
}, {
'b': 1
}, {
'd': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("compound", ret['explanation'])
def test_compound_index_range_at_start(fixture_collection):
fixture_collection.create_index(keys=[('d', 1), ('b', 1), ('c', 1)], name='compound')
fixture_collection.create_index(keys=[('d', 1)], name='simple')
query = {
'$and': [{
'd': {
'$gt': 1
}
}, {
'b': 1
}, {
'c': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("simple", ret['explanation'])
def test_compound_index_range_at_middle(fixture_collection):
fixture_collection.create_index(keys=[('d', 1), ('b', 1), ('c', 1)], name='compound')
fixture_collection.create_index(keys=[('d', 1)], name='simple')
query = {
'$and': [{
'b': {
'$gt': 1
}
}, {
'd': 1
}, {
'c': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("compound", ret['explanation'])
def test_compound_index_greedy_planner(fixture_collection):
fixture_collection.create_index(keys=[('d', 1), ('b', 1), ('c', 1)], name='long')
fixture_collection.create_index(keys=[('b', 1), ('c', 1)], name='short')
query = {
'$and': [{
'b': 1
}, {
'd': 1
}, {
'c': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("short", ret['explanation'])
def test_compound_index_match_exact_index(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='a')
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='ab')
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1)], name='abc')
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1), ('d', 1), ('e', 1)], name='abcde')
query = {
'$and': [{
'a': 1
}, {
'b': 1
}, {
'c': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("abc", ret['explanation']) and Predicates.no_table_scan_no_filter(ret['explanation'])
def test_compound_index_match_longest_prefix(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='a')
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='ab')
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1)], name='abc')
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1), ('d', 1), ('e', 1)], name='abcde')
query = {
'$and': [{
'a': 1
}, {
'b': 1
}, {
'c': 1
}, {
'd': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("abcde", ret['explanation']) and Predicates.no_table_scan_no_filter(ret['explanation'])
def test_compound_index_match_non_contiguous_prefix(fixture_collection):
fixture_collection.create_index(keys=[('a', 1)], name='a')
fixture_collection.create_index(keys=[('a', 1), ('b', 1)], name='ab')
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1)], name='abc')
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1), ('d', 1), ('e', 1)], name='abcde')
query = {
'$and': [{
'a': 1
}, {
'b': 1
}, {
'c': 1
}, {
'e': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named("abc", ret['explanation']) and Predicates.no_table_scan(ret['explanation'])
def test_compound_index_multi_match(fixture_collection):
fixture_collection.create_index(keys=[('a', 1), ('b', 1), ('c', 1), ('d', 1)], name='abcd')
fixture_collection.create_index(keys=[('a', 1), ('e', 1)], name='ae')
fixture_collection.create_index(keys=[('a', 1), ('d', 1)], name='ad')
fixture_collection.create_index(keys=[('d', 1), ('a', 1)], name='da')
fixture_collection.create_index(keys=[('e', 1)], name='e')
query = {
'$and': [{
'a': 1
}, {
'd': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('ad', ret['explanation'])
assert Predicates.no_table_scan(ret['explanation'])
assert Predicates.no_filter(ret['explanation'])
query = {
'$and': [{
'a': 1
}, {
'd': 1
}, {
'e': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('ad', ret['explanation'])
assert Predicates.no_table_scan(ret['explanation'])
query = {
'$and': [{
'd': 1
}, {
'a': 1
}]
}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('da', ret['explanation'])
assert Predicates.no_table_scan(ret['explanation'])
assert Predicates.no_filter(ret['explanation'])
query = {'d': 1}
ret = fixture_collection.find(query).explain()
assert Predicates.only_index_named('da', ret['explanation'])
assert Predicates.no_table_scan(ret['explanation'])
#issue-17: Added decode_bytes and decode_key_part to update explain output with user readable keys
# check bound range test
def test_bounds_in_basic_query_explain(fixture_collection):
fixture_collection.create_index([('a', 1), ('b', 1)])
query = {
'a' : 'A',
'b' : 64
}
ret = fixture_collection.find(query).explain()
# Predicates.check_bound(index_name, begin_key, end_key, explanation)
# Check whether, index name 'a', begin key 'A' and end key in 'A' are present in explanation
assert Predicates.check_bound('a', '"A"', '"A"', ret['explanation'])
# Check whether, index name 'b', begin key '64.0' and end key in '64.0' are present in explanation
assert Predicates.check_bound('b', '64.0', '64.0', ret['explanation'])
def test_bounds_in_operator_query_explain(fixture_collection):
fixture_collection.create_index(keys=[('d', 1), ('b', 1), ('c', 1)], name='compound')
fixture_collection.create_index(keys=[('d', 1)], name='simple')
query = {
'$and':
[{'d':
{'$gt': 1}
}
]}
ret = fixture_collection.find(query).explain()
explanation = ret['explanation']
# Predicates.check_bound(index_name, begin_key, end_key, explanation)
# Check whether, index name 'd', begin key '1.0' and end key in 'inf.0' are present in explanation
# For range 1.0 < x < inf.0
assert Predicates.check_bound('d', '1.0', 'inf.0', ret['explanation'])
query = {
'$and':
[{'d':
{'$lt': 1}
}
]}
ret = fixture_collection.find(query).explain()
explanation = ret['explanation']
# Check whether, index name 'd', begin key '-inf.0' and end key in '1.0' are present in explanation
# For range -inf.0 < x < 1.0
assert Predicates.check_bound('d', '-inf.0', '1.0', ret['explanation'])
query = {
'$and':
[{'d':
{'$lt': -5}
}
]}
ret = fixture_collection.find(query).explain()
explanation = ret['explanation']
# Check whether, index name 'd', begin key '-inf.0' and end key in '-5.0' are present in explanation
# For range -inf.0 < x < -5.0
assert Predicates.check_bound('d', '-inf.0', '-5.0', ret['explanation'])
query = {
'$and':
[{'d':
{'$gt': -5}
}
]}
ret = fixture_collection.find(query).explain()
explanation = ret['explanation']
# Check whether, index name 'd', begin key '-5.0' and end key in 'inf.0' are present in explanation
# For range -5.0 < x < inf.0
assert Predicates.check_bound('d', '-5.0', 'inf.0', ret['explanation'])