/
functions.py
398 lines (318 loc) · 14.8 KB
/
functions.py
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
# Copyright 2020 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""Various math helper functions."""
import math
from django.db.models.expressions import Func, Value
from django.db.models.functions import (
Cast,
Chr,
ConcatPair,
Cot,
Degrees,
Left,
Log,
Ord,
Pi,
Radians,
Right,
StrIndex,
Substr,
)
class IfNull(Func):
"""Represent SQL `IFNULL` function."""
function = "IFNULL"
arity = 2
def cast(self, compiler, connection, **extra_context):
"""
A method to extend Django Cast class. Cast SQL query for given
parameters.
:type self: :class:`~django.db.models.functions.comparison.Cast`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
# Account for a field's max_length using SUBSTR.
max_length = getattr(self.output_field, "max_length", None)
if max_length is not None:
template = "SUBSTR(" + self.template + ", 0, %s)" % max_length
else:
template = self.template
return self.as_sql(
compiler, connection, template=template, **extra_context
)
def chr_(self, compiler, connection, **extra_context):
"""
A method to extend Django Chr class. Returns a SQL query where the code
points are displayed as a string.
:type self: :class:`~django.db.models.functions.text.Chr`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler,
connection,
template="CODE_POINTS_TO_STRING([%(expressions)s])",
**extra_context
)
def concatpair(self, compiler, connection, **extra_context):
"""
A method to extend Django ConcatPair class. Concatenates a SQL query
into the sequence of :class:`IfNull` objects.
:type self: :class:`~django.db.models.functions.text.ConcatPair`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
# Spanner's CONCAT function returns null if any of its arguments are null.
# Prevent that by converting null arguments to an empty string.
clone = self.copy()
clone.set_source_expressions(
IfNull(e, Value("")) for e in self.get_source_expressions()
)
return clone.as_sql(compiler, connection, **extra_context)
def cot(self, compiler, connection, **extra_context):
"""
A method to extend Django Cot class. Returns a SQL query of calculated
trigonometric cotangent function.
:type self: :class:`~django.db.models.functions.math.Cot`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler,
connection,
template="(1 / TAN(%(expressions)s))",
**extra_context
)
def degrees(self, compiler, connection, **extra_context):
"""
A method to extend Django Degress class. Returns a SQL query of the
angle converted to degrees.
:type self: :class:`~django.db.models.functions.math.Degrees`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler,
connection,
template="((%%(expressions)s) * 180 / %s)" % math.pi,
**extra_context
)
def left_and_right(self, compiler, connection, **extra_context):
"""A method to extend Django Left and Right classes.
:type self: :class:`~django.db.models.functions.text.Left` or
:class:`~django.db.models.functions.text.Right`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.get_substr().as_spanner(compiler, connection, **extra_context)
def log(self, compiler, connection, **extra_context):
"""
A method to extend Django Log class. Returns a SQL query of calculated
logarithm.
:type self: :class:`~django.db.models.functions.math.Log`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
# This function is usually Log(b, x) returning the logarithm of x to the
# base b, but on Spanner it's Log(x, b).
clone = self.copy()
clone.set_source_expressions(self.get_source_expressions()[::-1])
return clone.as_sql(compiler, connection, **extra_context)
def ord_(self, compiler, connection, **extra_context):
"""
A method to extend Django Ord class. Returns a SQL query of the
expression converted to ord.
:type self: :class:`~django.db.models.functions.text.Ord`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler,
connection,
template="TO_CODE_POINTS(%(expressions)s)[OFFSET(0)]",
**extra_context
)
def pi(self, compiler, connection, **extra_context):
"""
A method to extend Django Pi class. Returns a SQL query of the Pi
constant.
:type self: :class:`~django.db.models.functions.math.Pi`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler, connection, template=str(math.pi), **extra_context
)
def radians(self, compiler, connection, **extra_context):
"""
A method to extend Django Radians class. Returns a SQL query of the
angle converted to radians.
:type self: :class:`~django.db.models.functions.math.Radians`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler,
connection,
template="((%%(expressions)s) * %s / 180)" % math.pi,
**extra_context
)
def strindex(self, compiler, connection, **extra_context):
"""
A method to extend Django StrIndex class. Returns a SQL query of the
string position.
:type self: :class:`~django.db.models.functions.text.StrIndex`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler, connection, function="STRPOS", **extra_context
)
def substr(self, compiler, connection, **extra_context):
"""
A method to extend Django Substr class. Returns a SQL query of a
substring.
:type self: :class:`~django.db.models.functions.text.Substr`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple(str, list)
:returns: A tuple where `str` is a string containing ordered SQL parameters
to be replaced with the elements of the `list`.
"""
return self.as_sql(
compiler, connection, function="SUBSTR", **extra_context
)
def register_functions():
"""Register the above methods with the corersponding Django classes."""
Cast.as_spanner = cast
Chr.as_spanner = chr_
ConcatPair.as_spanner = concatpair
Cot.as_spanner = cot
Degrees.as_spanner = degrees
Left.as_spanner = left_and_right
Log.as_spanner = log
Ord.as_spanner = ord_
Pi.as_spanner = pi
Radians.as_spanner = radians
Right.as_spanner = left_and_right
StrIndex.as_spanner = strindex
Substr.as_spanner = substr