math.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import math
  2. from django.db.models.expressions import Func, Value
  3. from django.db.models.fields import FloatField, IntegerField
  4. from django.db.models.functions import Cast
  5. from django.db.models.functions.mixins import (
  6. FixDecimalInputMixin,
  7. NumericOutputFieldMixin,
  8. )
  9. from django.db.models.lookups import Transform
  10. class Abs(Transform):
  11. function = "ABS"
  12. lookup_name = "abs"
  13. class ACos(NumericOutputFieldMixin, Transform):
  14. function = "ACOS"
  15. lookup_name = "acos"
  16. class ASin(NumericOutputFieldMixin, Transform):
  17. function = "ASIN"
  18. lookup_name = "asin"
  19. class ATan(NumericOutputFieldMixin, Transform):
  20. function = "ATAN"
  21. lookup_name = "atan"
  22. class ATan2(NumericOutputFieldMixin, Func):
  23. function = "ATAN2"
  24. arity = 2
  25. def as_sqlite(self, compiler, connection, **extra_context):
  26. if not getattr(
  27. connection.ops, "spatialite", False
  28. ) or connection.ops.spatial_version >= (5, 0, 0):
  29. return self.as_sql(compiler, connection)
  30. # This function is usually ATan2(y, x), returning the inverse tangent
  31. # of y / x, but it's ATan2(x, y) on SpatiaLite < 5.0.0.
  32. # Cast integers to float to avoid inconsistent/buggy behavior if the
  33. # arguments are mixed between integer and float or decimal.
  34. # https://www.gaia-gis.it/fossil/libspatialite/tktview?name=0f72cca3a2
  35. clone = self.copy()
  36. clone.set_source_expressions(
  37. [
  38. Cast(expression, FloatField())
  39. if isinstance(expression.output_field, IntegerField)
  40. else expression
  41. for expression in self.get_source_expressions()[::-1]
  42. ]
  43. )
  44. return clone.as_sql(compiler, connection, **extra_context)
  45. class Ceil(Transform):
  46. function = "CEILING"
  47. lookup_name = "ceil"
  48. def as_oracle(self, compiler, connection, **extra_context):
  49. return super().as_sql(compiler, connection, function="CEIL", **extra_context)
  50. class Cos(NumericOutputFieldMixin, Transform):
  51. function = "COS"
  52. lookup_name = "cos"
  53. class Cot(NumericOutputFieldMixin, Transform):
  54. function = "COT"
  55. lookup_name = "cot"
  56. def as_oracle(self, compiler, connection, **extra_context):
  57. return super().as_sql(
  58. compiler, connection, template="(1 / TAN(%(expressions)s))", **extra_context
  59. )
  60. class Degrees(NumericOutputFieldMixin, Transform):
  61. function = "DEGREES"
  62. lookup_name = "degrees"
  63. def as_oracle(self, compiler, connection, **extra_context):
  64. return super().as_sql(
  65. compiler,
  66. connection,
  67. template="((%%(expressions)s) * 180 / %s)" % math.pi,
  68. **extra_context,
  69. )
  70. class Exp(NumericOutputFieldMixin, Transform):
  71. function = "EXP"
  72. lookup_name = "exp"
  73. class Floor(Transform):
  74. function = "FLOOR"
  75. lookup_name = "floor"
  76. class Ln(NumericOutputFieldMixin, Transform):
  77. function = "LN"
  78. lookup_name = "ln"
  79. class Log(FixDecimalInputMixin, NumericOutputFieldMixin, Func):
  80. function = "LOG"
  81. arity = 2
  82. def as_sqlite(self, compiler, connection, **extra_context):
  83. if not getattr(connection.ops, "spatialite", False):
  84. return self.as_sql(compiler, connection)
  85. # This function is usually Log(b, x) returning the logarithm of x to
  86. # the base b, but on SpatiaLite it's Log(x, b).
  87. clone = self.copy()
  88. clone.set_source_expressions(self.get_source_expressions()[::-1])
  89. return clone.as_sql(compiler, connection, **extra_context)
  90. class Mod(FixDecimalInputMixin, NumericOutputFieldMixin, Func):
  91. function = "MOD"
  92. arity = 2
  93. class Pi(NumericOutputFieldMixin, Func):
  94. function = "PI"
  95. arity = 0
  96. def as_oracle(self, compiler, connection, **extra_context):
  97. return super().as_sql(
  98. compiler, connection, template=str(math.pi), **extra_context
  99. )
  100. class Power(NumericOutputFieldMixin, Func):
  101. function = "POWER"
  102. arity = 2
  103. class Radians(NumericOutputFieldMixin, Transform):
  104. function = "RADIANS"
  105. lookup_name = "radians"
  106. def as_oracle(self, compiler, connection, **extra_context):
  107. return super().as_sql(
  108. compiler,
  109. connection,
  110. template="((%%(expressions)s) * %s / 180)" % math.pi,
  111. **extra_context,
  112. )
  113. class Random(NumericOutputFieldMixin, Func):
  114. function = "RANDOM"
  115. arity = 0
  116. def as_mysql(self, compiler, connection, **extra_context):
  117. return super().as_sql(compiler, connection, function="RAND", **extra_context)
  118. def as_oracle(self, compiler, connection, **extra_context):
  119. return super().as_sql(
  120. compiler, connection, function="DBMS_RANDOM.VALUE", **extra_context
  121. )
  122. def as_sqlite(self, compiler, connection, **extra_context):
  123. return super().as_sql(compiler, connection, function="RAND", **extra_context)
  124. def get_group_by_cols(self):
  125. return []
  126. class Round(FixDecimalInputMixin, Transform):
  127. function = "ROUND"
  128. lookup_name = "round"
  129. arity = None # Override Transform's arity=1 to enable passing precision.
  130. def __init__(self, expression, precision=0, **extra):
  131. super().__init__(expression, precision, **extra)
  132. def as_sqlite(self, compiler, connection, **extra_context):
  133. precision = self.get_source_expressions()[1]
  134. if isinstance(precision, Value) and precision.value < 0:
  135. raise ValueError("SQLite does not support negative precision.")
  136. return super().as_sqlite(compiler, connection, **extra_context)
  137. def _resolve_output_field(self):
  138. source = self.get_source_expressions()[0]
  139. return source.output_field
  140. class Sign(Transform):
  141. function = "SIGN"
  142. lookup_name = "sign"
  143. class Sin(NumericOutputFieldMixin, Transform):
  144. function = "SIN"
  145. lookup_name = "sin"
  146. class Sqrt(NumericOutputFieldMixin, Transform):
  147. function = "SQRT"
  148. lookup_name = "sqrt"
  149. class Tan(NumericOutputFieldMixin, Transform):
  150. function = "TAN"
  151. lookup_name = "tan"