Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/db/models/functions/window.py: 70%
69 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from django.db.models.expressions import Func
2from django.db.models.fields import FloatField, IntegerField
4__all__ = [
5 "CumeDist",
6 "DenseRank",
7 "FirstValue",
8 "Lag",
9 "LastValue",
10 "Lead",
11 "NthValue",
12 "Ntile",
13 "PercentRank",
14 "Rank",
15 "RowNumber",
16]
19class CumeDist(Func):
20 function = "CUME_DIST"
21 output_field = FloatField()
22 window_compatible = True
25class DenseRank(Func):
26 function = "DENSE_RANK"
27 output_field = IntegerField()
28 window_compatible = True
31class FirstValue(Func):
32 arity = 1
33 function = "FIRST_VALUE"
34 window_compatible = True
37class LagLeadFunction(Func):
38 window_compatible = True
40 def __init__(self, expression, offset=1, default=None, **extra):
41 if expression is None:
42 raise ValueError(
43 "%s requires a non-null source expression." % self.__class__.__name__
44 )
45 if offset is None or offset <= 0:
46 raise ValueError(
47 "%s requires a positive integer for the offset."
48 % self.__class__.__name__
49 )
50 args = (expression, offset)
51 if default is not None:
52 args += (default,)
53 super().__init__(*args, **extra)
55 def _resolve_output_field(self):
56 sources = self.get_source_expressions()
57 return sources[0].output_field
60class Lag(LagLeadFunction):
61 function = "LAG"
64class LastValue(Func):
65 arity = 1
66 function = "LAST_VALUE"
67 window_compatible = True
70class Lead(LagLeadFunction):
71 function = "LEAD"
74class NthValue(Func):
75 function = "NTH_VALUE"
76 window_compatible = True
78 def __init__(self, expression, nth=1, **extra):
79 if expression is None:
80 raise ValueError(
81 "%s requires a non-null source expression." % self.__class__.__name__
82 )
83 if nth is None or nth <= 0:
84 raise ValueError(
85 "%s requires a positive integer as for nth." % self.__class__.__name__
86 )
87 super().__init__(expression, nth, **extra)
89 def _resolve_output_field(self):
90 sources = self.get_source_expressions()
91 return sources[0].output_field
94class Ntile(Func):
95 function = "NTILE"
96 output_field = IntegerField()
97 window_compatible = True
99 def __init__(self, num_buckets=1, **extra):
100 if num_buckets <= 0:
101 raise ValueError("num_buckets must be greater than 0.")
102 super().__init__(num_buckets, **extra)
105class PercentRank(Func):
106 function = "PERCENT_RANK"
107 output_field = FloatField()
108 window_compatible = True
111class Rank(Func):
112 function = "RANK"
113 output_field = IntegerField()
114 window_compatible = True
117class RowNumber(Func):
118 function = "ROW_NUMBER"
119 output_field = IntegerField()
120 window_compatible = True