Skip to content

Commit 5d8ee16

Browse files
committed
feat: ClickHouse hash functions
1 parent 80d3f10 commit 5d8ee16

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from .datetime import * # noqa: F401,F403
22
from .datetime import __all__ as datetime_all
3+
from .hashes import * # noqa: F401,F403
4+
from .hashes import __all__ as hashes_all
35
from .other import * # noqa: F401,F403
46
from .other import __all__ as other_all
57
from .random import * # noqa: F401,F403
68
from .random import __all__ as random_all
79
from .tuples import * # noqa: F401,F403
810
from .tuples import __all__ as tuples_all
911

10-
__all__ = datetime_all + other_all + random_all + tuples_all
12+
__all__ = datetime_all + hashes_all + other_all + random_all + tuples_all
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
from django.db.models.expressions import Value
2+
3+
from clickhouse_backend.models import fields
4+
5+
from .base import Func
6+
from .tuples import Tuple
7+
8+
__all__ = [
9+
"halfMD5",
10+
"MD4",
11+
"MD5",
12+
"sipHash64",
13+
"sipHash64Keyed",
14+
"sipHash128",
15+
"sipHash128Keyed",
16+
"sipHash128Reference",
17+
"sipHash128ReferenceKeyed",
18+
"cityHash64",
19+
"intHash32",
20+
"intHash64",
21+
"SHA1",
22+
"SHA224",
23+
"SHA256",
24+
"SHA512",
25+
"SHA512_256",
26+
"BLAKE3",
27+
"URLHash",
28+
"farmFingerprint64",
29+
"farmHash64",
30+
]
31+
32+
33+
class halfMD5(Func):
34+
output_field = fields.UInt64Field()
35+
36+
37+
class MD4(Func):
38+
arity = 1
39+
output_field = fields.FixedStringField(max_bytes=16)
40+
41+
42+
class MD5(Func):
43+
arity = 1
44+
output_field = fields.FixedStringField(max_bytes=16)
45+
46+
47+
class sipHash64(Func):
48+
output_field = fields.UInt64Field()
49+
50+
51+
class sipHash64Keyed(Func):
52+
template = "%(function)s((%(k0)s, %(k1)s), %(expressions)s)"
53+
output_field = fields.UInt64Field()
54+
55+
def __init__(self, k0, k1, *expressions):
56+
super().__init__(Tuple(k0, k1), *expressions)
57+
58+
59+
class sipHash128(Func):
60+
output_field = fields.FixedStringField(max_bytes=16)
61+
62+
63+
class sipHash128Keyed(sipHash64Keyed):
64+
output_field = fields.FixedStringField(max_bytes=16)
65+
66+
67+
class sipHash128Reference(Func):
68+
output_field = fields.FixedStringField(max_bytes=16)
69+
70+
71+
class sipHash128ReferenceKeyed(sipHash64Keyed):
72+
output_field = fields.FixedStringField(max_bytes=16)
73+
74+
75+
class cityHash64(Func):
76+
output_field = fields.UInt64Field()
77+
78+
79+
class intHash32(Func):
80+
arity = 1
81+
output_field = fields.UInt32Field()
82+
83+
84+
class intHash64(Func):
85+
arity = 1
86+
output_field = fields.UInt64Field()
87+
88+
89+
class SHA1(Func):
90+
arity = 1
91+
output_field = fields.FixedStringField(max_bytes=20)
92+
93+
94+
class SHA224(Func):
95+
arity = 1
96+
output_field = fields.FixedStringField(max_bytes=28)
97+
98+
99+
class SHA256(Func):
100+
arity = 1
101+
output_field = fields.FixedStringField(max_bytes=32)
102+
103+
104+
class SHA512(Func):
105+
arity = 1
106+
output_field = fields.FixedStringField(max_bytes=64)
107+
108+
109+
class SHA512_256(Func):
110+
arity = 1
111+
output_field = fields.FixedStringField(max_bytes=64)
112+
113+
114+
class BLAKE3(Func):
115+
arity = 1
116+
output_field = fields.FixedStringField(max_bytes=32)
117+
118+
119+
class URLHash(Func):
120+
output_field = fields.UInt64Field()
121+
122+
def __init__(self, *expressions):
123+
if len(expressions) == 1:
124+
pass
125+
elif len(expressions) == 2:
126+
expressions = (expressions[0], Value(expressions[1]))
127+
else:
128+
raise TypeError(
129+
"'%s' takes 1 or 2 arguments (%s given)"
130+
% (
131+
self.__class__.__name__,
132+
len(expressions),
133+
)
134+
)
135+
super().__init__(*expressions)
136+
137+
138+
class farmFingerprint64(Func):
139+
output_field = fields.UInt64Field()
140+
141+
142+
class farmHash64(Func):
143+
output_field = fields.UInt64Field()

0 commit comments

Comments
 (0)