Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

window.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from django.db.models import FloatField, Func, IntegerField
  2. __all__ = [
  3. 'CumeDist', 'DenseRank', 'FirstValue', 'Lag', 'LastValue', 'Lead',
  4. 'NthValue', 'Ntile', 'PercentRank', 'Rank', 'RowNumber',
  5. ]
  6. class CumeDist(Func):
  7. function = 'CUME_DIST'
  8. name = 'CumeDist'
  9. output_field = FloatField()
  10. window_compatible = True
  11. class DenseRank(Func):
  12. function = 'DENSE_RANK'
  13. name = 'DenseRank'
  14. output_field = IntegerField()
  15. window_compatible = True
  16. class FirstValue(Func):
  17. arity = 1
  18. function = 'FIRST_VALUE'
  19. name = 'FirstValue'
  20. window_compatible = True
  21. class LagLeadFunction(Func):
  22. window_compatible = True
  23. def __init__(self, expression, offset=1, default=None, **extra):
  24. if expression is None:
  25. raise ValueError(
  26. '%s requires a non-null source expression.' %
  27. self.__class__.__name__
  28. )
  29. if offset is None or offset <= 0:
  30. raise ValueError(
  31. '%s requires a positive integer for the offset.' %
  32. self.__class__.__name__
  33. )
  34. args = (expression, offset)
  35. if default is not None:
  36. args += (default,)
  37. super().__init__(*args, **extra)
  38. def _resolve_output_field(self):
  39. sources = self.get_source_expressions()
  40. return sources[0].output_field
  41. class Lag(LagLeadFunction):
  42. function = 'LAG'
  43. name = 'Lag'
  44. class LastValue(Func):
  45. arity = 1
  46. function = 'LAST_VALUE'
  47. name = 'LastValue'
  48. window_compatible = True
  49. class Lead(LagLeadFunction):
  50. function = 'LEAD'
  51. name = 'Lead'
  52. class NthValue(Func):
  53. function = 'NTH_VALUE'
  54. name = 'NthValue'
  55. window_compatible = True
  56. def __init__(self, expression, nth=1, **extra):
  57. if expression is None:
  58. raise ValueError('%s requires a non-null source expression.' % self.__class__.__name__)
  59. if nth is None or nth <= 0:
  60. raise ValueError('%s requires a positive integer as for nth.' % self.__class__.__name__)
  61. super().__init__(expression, nth, **extra)
  62. def _resolve_output_field(self):
  63. sources = self.get_source_expressions()
  64. return sources[0].output_field
  65. class Ntile(Func):
  66. function = 'NTILE'
  67. name = 'Ntile'
  68. output_field = IntegerField()
  69. window_compatible = True
  70. def __init__(self, num_buckets=1, **extra):
  71. if num_buckets <= 0:
  72. raise ValueError('num_buckets must be greater than 0.')
  73. super().__init__(num_buckets, **extra)
  74. class PercentRank(Func):
  75. function = 'PERCENT_RANK'
  76. name = 'PercentRank'
  77. output_field = FloatField()
  78. window_compatible = True
  79. class Rank(Func):
  80. function = 'RANK'
  81. name = 'Rank'
  82. output_field = IntegerField()
  83. window_compatible = True
  84. class RowNumber(Func):
  85. function = 'ROW_NUMBER'
  86. name = 'RowNumber'
  87. output_field = IntegerField()
  88. window_compatible = True