Skip to content

Commit 00ef745

Browse files
committed
Revert "rename has_any and has_all queries to any and all"
This reverts commit 38218c6.
1 parent 604dc3c commit 00ef745

File tree

4 files changed

+102
-80
lines changed

4 files changed

+102
-80
lines changed

django_enum/query.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Specialized any and all query lookups for flag enumerations.
2+
Specialized has_any and has_all query lookups for flag enumerations.
33
"""
44

55
# from django.core.exceptions import FieldError
@@ -15,7 +15,7 @@ class HasAllFlagsLookup(Exact):
1515
to the lookup value.
1616
"""
1717

18-
lookup_name = "all"
18+
lookup_name = "has_all"
1919

2020
def process_lhs(self, compiler, connection, lhs=None):
2121
lhs_sql, lhs_params = super().process_lhs(compiler, connection, lhs)
@@ -49,7 +49,7 @@ def get_rhs_op(self, connection, rhs):
4949
# HasAllFlagsLookup
5050
# ):
5151
# """
52-
# Support for bitwise all lookup on extra big integers (>64 bits)
52+
# Support for bitwise has_all lookup on extra big integers (>64 bits)
5353
# stored as binary columns.
5454
#
5555
# get_bit(, 0) AND get_bit(, 7) = 1;
@@ -82,7 +82,7 @@ class HasAnyFlagsLookup(HasAllFlagsLookup):
8282
than zero.
8383
"""
8484

85-
lookup_name = "any"
85+
lookup_name = "has_any"
8686

8787
def process_rhs(self, compiler, connection):
8888
rhs_sql, rhs_params = super().process_rhs(compiler, connection)
@@ -101,7 +101,7 @@ def get_rhs_op(self, connection, rhs):
101101
# HasAnyFlagsLookup
102102
# ):
103103
# """
104-
# Support for bitwise any lookup on extra big integers (>64 bits)
104+
# Support for bitwise has_any lookup on extra big integers (>64 bits)
105105
# stored as binary columns.
106106
# """
107107
#

tests/benchmarks.py

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,15 @@ def test_size_benchmark(self):
461461
bf.write(json.dumps(data, indent=4))
462462

463463
def test_query_performance(self):
464-
any_flag_count = {}
465-
all_flag_count = {}
466-
any_flag_load = {}
467-
all_flag_load = {}
464+
has_any_flag_count = {}
465+
has_all_flag_count = {}
466+
has_any_flag_load = {}
467+
has_all_flag_load = {}
468468

469-
any_bool_count = {}
470-
all_bool_count = {}
471-
any_bool_load = {}
472-
all_bool_load = {}
469+
has_any_bool_count = {}
470+
has_all_bool_count = {}
471+
has_any_bool_load = {}
472+
has_all_bool_load = {}
473473

474474
with connection.cursor() as cursor:
475475
for FlagModel, BoolModel in zip(self.FLAG_MODELS, self.BOOL_MODELS):
@@ -480,8 +480,8 @@ def test_query_performance(self):
480480
mask = 1
481481
mask_en = FlagModel._meta.get_field("flags").enum(mask)
482482

483-
flag_any_q = FlagModel.objects.filter(flags__any=mask_en)
484-
flag_all_q = FlagModel.objects.filter(flags__all=mask_en)
483+
flag_any_q = FlagModel.objects.filter(flags__has_any=mask_en)
484+
flag_all_q = FlagModel.objects.filter(flags__has_all=mask_en)
485485

486486
bool_q = [
487487
Q(**{f"flg_{flg}": bool(mask & (1 << flg) != 0)})
@@ -502,11 +502,11 @@ def test_query_performance(self):
502502

503503
start = perf_counter()
504504
flag_any_count = flag_any_q.count()
505-
any_flag_count[FlagModel.num_flags] = perf_counter() - start
505+
has_any_flag_count[FlagModel.num_flags] = perf_counter() - start
506506

507507
start = perf_counter()
508508
bool_any_count = bool_any_q.count()
509-
any_bool_count[BoolModel.num_flags] = perf_counter() - start
509+
has_any_bool_count[BoolModel.num_flags] = perf_counter() - start
510510

511511
try:
512512
# make sure our queries are equivalent
@@ -518,75 +518,83 @@ def test_query_performance(self):
518518

519519
start = perf_counter()
520520
flag_all_count = flag_all_q.count()
521-
all_flag_count[FlagModel.num_flags] = perf_counter() - start
521+
has_all_flag_count[FlagModel.num_flags] = perf_counter() - start
522522

523523
start = perf_counter()
524524
bool_all_count = bool_all_q.count()
525-
all_bool_count[BoolModel.num_flags] = perf_counter() - start
525+
has_all_bool_count[BoolModel.num_flags] = perf_counter() - start
526526

527527
# make sure our queries are equivalent
528528
self.assertEqual(flag_all_count, bool_all_count)
529529

530530
start = perf_counter()
531531
flag_any_list = list(flag_any_q.all())
532-
any_flag_load[FlagModel.num_flags] = perf_counter() - start
532+
has_any_flag_load[FlagModel.num_flags] = perf_counter() - start
533533

534534
start = perf_counter()
535535
bool_any_list = list(bool_any_q.all())
536-
any_bool_load[BoolModel.num_flags] = perf_counter() - start
536+
has_any_bool_load[BoolModel.num_flags] = perf_counter() - start
537537

538538
# make sure our queries are equivalent
539539
self.assertEqual(len(flag_any_list), len(bool_any_list))
540540

541541
start = perf_counter()
542542
flag_all_list = list(flag_all_q.all())
543-
all_flag_load[FlagModel.num_flags] = perf_counter() - start
543+
has_all_flag_load[FlagModel.num_flags] = perf_counter() - start
544544

545545
start = perf_counter()
546546
bool_all_list = list(bool_all_q.all())
547-
all_bool_load[BoolModel.num_flags] = perf_counter() - start
547+
has_all_bool_load[BoolModel.num_flags] = perf_counter() - start
548548

549549
# make sure our queries are equivalent
550550
self.assertEqual(len(flag_all_list), len(bool_all_list))
551551

552-
num_flags = sorted(any_flag_count.keys())
552+
num_flags = sorted(has_any_flag_count.keys())
553553

554-
any_count_diff = [
555-
any_bool_count[flg] - any_flag_count[flg] for flg in num_flags
554+
has_any_count_diff = [
555+
has_any_bool_count[flg] - has_any_flag_count[flg] for flg in num_flags
556556
]
557-
all_count_diff = [
558-
all_bool_count[flg] - all_flag_count[flg] for flg in num_flags
557+
has_all_count_diff = [
558+
has_all_bool_count[flg] - has_all_flag_count[flg] for flg in num_flags
559559
]
560560

561-
any_load_diff = [any_bool_load[flg] - any_flag_load[flg] for flg in num_flags]
562-
all_load_diff = [all_bool_load[flg] - all_flag_load[flg] for flg in num_flags]
561+
has_any_load_diff = [
562+
has_any_bool_load[flg] - has_any_flag_load[flg] for flg in num_flags
563+
]
564+
has_all_load_diff = [
565+
has_all_bool_load[flg] - has_all_flag_load[flg] for flg in num_flags
566+
]
563567

564-
# print(any_count_diff)
568+
# print(has_any_count_diff)
565569
# print('--------------------------------')
566-
# print(all_count_diff)
570+
# print(has_all_count_diff)
567571
# print('--------------------------------')
568-
# print(any_load_diff)
572+
# print(has_any_load_diff)
569573
# print('--------------------------------')
570-
# print(all_load_diff)
574+
# print(has_all_load_diff)
571575

572-
any_count_tpl = [
573-
(any_bool_count[flg], any_flag_count[flg]) for flg in num_flags
576+
has_any_count_tpl = [
577+
(has_any_bool_count[flg], has_any_flag_count[flg]) for flg in num_flags
574578
]
575-
all_count_tpl = [
576-
(all_bool_count[flg], all_flag_count[flg]) for flg in num_flags
579+
has_all_count_tpl = [
580+
(has_all_bool_count[flg], has_all_flag_count[flg]) for flg in num_flags
577581
]
578582

579-
any_load_tpl = [(any_bool_load[flg], any_flag_load[flg]) for flg in num_flags]
580-
all_load_tpl = [(all_bool_load[flg], all_flag_load[flg]) for flg in num_flags]
583+
has_any_load_tpl = [
584+
(has_any_bool_load[flg], has_any_flag_load[flg]) for flg in num_flags
585+
]
586+
has_all_load_tpl = [
587+
(has_all_bool_load[flg], has_all_flag_load[flg]) for flg in num_flags
588+
]
581589

582-
print("------------ any_cnt ----------------")
583-
print(any_count_tpl)
584-
print("------------ all_cnt ----------------")
585-
print(all_count_tpl)
586-
print("------------ any_load ---------------")
587-
print(any_load_tpl)
588-
print("------------ all_load ---------------")
589-
print(all_load_tpl)
590+
print("------------ has_any_cnt ----------------")
591+
print(has_any_count_tpl)
592+
print("------------ has_all_cnt ----------------")
593+
print(has_all_count_tpl)
594+
print("------------ has_any_load ---------------")
595+
print(has_any_load_tpl)
596+
print("------------ has_all_load ---------------")
597+
print(has_all_load_tpl)
590598

591599

592600
class CreateRowMixin(BulkCreateMixin):
@@ -709,11 +717,11 @@ def do_flag_query(self, masks):
709717
# dont change query order
710718

711719
start = perf_counter()
712-
flg_all.append(self.FlagModel.objects.filter(flags__all=mask).count())
720+
flg_all.append(self.FlagModel.objects.filter(flags__has_all=mask).count())
713721
flg_all_time += perf_counter() - start
714722

715723
all_explanation = json.loads(
716-
self.FlagModel.objects.filter(flags__all=mask).explain(
724+
self.FlagModel.objects.filter(flags__has_all=mask).explain(
717725
format="json",
718726
analyze=True,
719727
buffers=True,
@@ -728,11 +736,11 @@ def do_flag_query(self, masks):
728736
flg_all_ftr_time += all_explanation["Execution Time"]
729737

730738
start = perf_counter()
731-
flg_any.append(self.FlagModel.objects.filter(flags__any=mask).count())
739+
flg_any.append(self.FlagModel.objects.filter(flags__has_any=mask).count())
732740
flg_any_time += perf_counter() - start
733741

734742
any_explanation = json.loads(
735-
self.FlagModel.objects.filter(flags__any=mask).explain(
743+
self.FlagModel.objects.filter(flags__has_any=mask).explain(
736744
format="json",
737745
analyze=True,
738746
buffers=True,
@@ -826,7 +834,7 @@ def get_all_q(set_bits):
826834

827835
bq_all = reduce(and_, get_all_q(set_bits))
828836

829-
# todo there is not a better way to formulate a any query
837+
# todo there is not a better way to formulate a has_any query
830838
# that will use the index ??
831839

832840
# bq_any = reduce(
@@ -1008,9 +1016,11 @@ def optimize():
10081016
any_ftr_time,
10091017
exact_ftr_time,
10101018
) = query(masks)
1011-
queries[f"{name} all"] = ctx.captured_queries[0]["sql"]
1012-
queries[f"{name} any"] = ctx.captured_queries[1]["sql"]
1013-
queries[f"{name} exact"] = ctx.captured_queries[2]["sql"]
1019+
queries[f"{name} has_all"] = ctx.captured_queries[0]["sql"]
1020+
queries[f"{name} has_any"] = ctx.captured_queries[1]["sql"]
1021+
queries[f"{name} has_exact"] = ctx.captured_queries[2][
1022+
"sql"
1023+
]
10141024

10151025
pbar.refresh()
10161026

tests/test_field_types_ep.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,23 @@ def test_large_bitfields(self):
9797
bit_field_large_neg=None,
9898
)
9999

100-
# any and all are not supported on ExtraLarge bit fields
100+
# has_any and has_all are not supported on ExtraLarge bit fields
101101
with self.assertRaises(FieldError):
102-
BitFieldModel.objects.filter(bit_field_large__any=LargeBitField.ONE)
102+
BitFieldModel.objects.filter(bit_field_large__has_any=LargeBitField.ONE)
103103

104104
with self.assertRaises(FieldError):
105105
BitFieldModel.objects.filter(
106-
bit_field_large__all=LargeBitField.ONE | LargeBitField.TWO
106+
bit_field_large__has_all=LargeBitField.ONE | LargeBitField.TWO
107107
)
108108

109109
with self.assertRaises(FieldError):
110110
BitFieldModel.objects.filter(
111-
bit_field_large_neg__any=LargeNegativeField.NEG_ONE
111+
bit_field_large_neg__has_any=LargeNegativeField.NEG_ONE
112112
)
113113

114114
with self.assertRaises(FieldError):
115115
BitFieldModel.objects.filter(
116-
bit_field_large_neg__all=LargeNegativeField.NEG_ONE
116+
bit_field_large_neg__has_all=LargeNegativeField.NEG_ONE
117117
| LargeNegativeField.ZERO
118118
)
119119

0 commit comments

Comments
 (0)