math.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import math
  2. from django.db.models.expressions import Func
  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, NumericOutputFieldMixin,
  7. )
  8. from django.db.models.lookups import Transform
  9. class Abs(Transform):
  10. function = 'ABS'
  11. lookup_name = 'abs'
  12. class ACos(NumericOutputFieldMixin, Transform):
  13. function = 'ACOS'
  14. lookup_name = 'acos'
  15. class ASin(NumericOutputFieldMixin, Transform):
  16. function = 'ASIN'
  17. lookup_name = 'asin'
  18. class ATan(NumericOutputFieldMixin, Transform):
  19. function = 'ATAN'
  20. lookup_name = 'atan'
  21. class ATan2(NumericOutputFieldMixin, Func):
  22. function = 'ATAN2'
  23. arity = 2
  24. def as_sqlite(self, compiler, connection, **extra_context):
  25. if not getattr(connection.ops, 'spatialite', False) or connection.ops.spatial_version >= (5, 0, 0):
  26. return self.as_sql(compiler, connection)
  27. # This function is usually ATan2(y, x), returning the inverse tangent
  28. # of y / x, but it's ATan2(x, y) on SpatiaLite < 5.0.0.
  29. # Cast integers to float to avoid inconsistent/buggy behavior if the
  30. # arguments are mixed between integer and float or decimal.
  31. # https://www.gaia-gis.it/fossil/libspatialite/tktview?name=0f72cca3a2
  32. clone = self.copy()
  33. clone.set_source_expressions([
  34. Cast(expression, FloatField()) if isinstance(expression.output_field, IntegerField)
  35. else expression for expression in self.get_source_expressions()[::-1]
  36. ])
  37. return clone.as_sql(compiler, connection, **extra_context)
  38. class Ceil(Transform):
  39. function = 'CEILING'
  40. lookup_name = 'ceil'
  41. def as_oracle(self, compiler, connection, **extra_context):
  42. return super().as_sql(compiler, connection, function='CEIL', **extra_context)
  43. class Cos(NumericOutputFieldMixin, Transform):
  44. function = 'COS'
  45. lookup_name = 'cos'
  46. class Cot(NumericOutputFieldMixin, Transform):
  47. function = 'COT'
  48. lookup_name = 'cot'
  49. def as_oracle(self, compiler, connection, **extra_context):
  50. return super().as_sql(compiler, connection, template='(1 / TAN(%(expressions)s))', **extra_context)
  51. class Degrees(NumericOutputFieldMixin, Transform):
  52. function = 'DEGREES'
  53. lookup_name = 'degrees'
  54. def as_oracle(self, compiler, connection, **extra_context):
  55. return super().as_sql(
  56. compiler, connection,
  57. template='((%%(expressions)s) * 180 / %s)' % math.pi,
  58. **extra_context
  59. )
  60. class Exp(NumericOutputFieldMixin, Transform):
  61. function = 'EXP'
  62. lookup_name = 'exp'
  63. class Floor(Transform):
  64. function = 'FLOOR'
  65. lookup_name = 'floor'
  66. class Ln(NumericOutputFieldMixin, Transform):
  67. function = 'LN'
  68. lookup_name = 'ln'
  69. class Log(FixDecimalInputMixin, NumericOutputFieldMixin, Func):
  70. function = 'LOG'
  71. arity = 2
  72. def as_sqlite(self, compiler, connection, **extra_context):
  73. if not getattr(connection.ops, 'spatialite', False):
  74. return self.as_sql(compiler, connection)
  75. # This function is usually Log(b, x) returning the logarithm of x to
  76. # the base b, but on SpatiaLite it's Log(x, b).
  77. clone = self.copy()
  78. clone.set_source_expressions(self.get_source_expressions()[::-1])
  79. return clone.as_sql(compiler, connection, **extra_context)
  80. class Mod(FixDecimalInputMixin, NumericOutputFieldMixin, Func):
  81. function = 'MOD'
  82. arity = 2
  83. class Pi(NumericOutputFieldMixin, Func):
  84. function = 'PI'
  85. arity = 0
  86. def as_oracle(self, compiler, connection, **extra_context):
  87. return super().as_sql(compiler, connection, template=str(math.pi), **extra_context)
  88. class Power(NumericOutputFieldMixin, Func):
  89. function = 'POWER'
  90. arity = 2
  91. class Radians(NumericOutputFieldMixin, Transform):
  92. function = 'RADIANS'
  93. lookup_name = 'radians'
  94. def as_oracle(self, compiler, connection, **extra_context):
  95. return super().as_sql(
  96. compiler, connection,
  97. template='((%%(expressions)s) * %s / 180)' % math.pi,
  98. **extra_context
  99. )
  100. class Round(Transform):
  101. function = 'ROUND'
  102. lookup_name = 'round'
  103. class Sign(Transform):
  104. function = 'SIGN'
  105. lookup_name = 'sign'
  106. class Sin(NumericOutputFieldMixin, Transform):
  107. function = 'SIN'
  108. lookup_name = 'sin'
  109. class Sqrt(NumericOutputFieldMixin, Transform):
  110. function = 'SQRT'
  111. lookup_name = 'sqrt'
  112. class Tan(NumericOutputFieldMixin, Transform):
  113. function = 'TAN'
  114. lookup_name = 'tan'