-
Notifications
You must be signed in to change notification settings - Fork 587
/
analytic.py
242 lines (167 loc) · 4.43 KB
/
analytic.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
from __future__ import annotations
from public import public
import ibis.expr.datatypes as dt
import ibis.expr.rules as rlz
from ibis.common.annotations import attribute
from ibis.expr.operations.core import Value
from ibis.expr.window import propagate_down_window
@public
class Window(Value):
expr = rlz.analytic
window = rlz.window_from(rlz.base_table_of(rlz.ref("expr"), strict=False))
output_dtype = rlz.dtype_like("expr")
output_shape = rlz.Shape.COLUMNAR
def __init__(self, expr, window):
expr = propagate_down_window(expr, window)
super().__init__(expr=expr, window=window)
def over(self, window):
new_window = self.window.combine(window)
return Window(self.expr, new_window)
@property
def name(self):
return self.expr.name
@public
class Analytic(Value):
output_shape = rlz.Shape.COLUMNAR
@public
class ShiftBase(Analytic):
arg = rlz.column(rlz.any)
offset = rlz.optional(rlz.one_of((rlz.integer, rlz.interval)))
default = rlz.optional(rlz.any)
output_dtype = rlz.dtype_like("arg")
@public
class Lag(ShiftBase):
pass
@public
class Lead(ShiftBase):
pass
@public
class RankBase(Analytic):
output_dtype = dt.int64
@public
class MinRank(RankBase):
"""Compute position of first element within each equal-value group in
sorted order. Equivalent to SQL RANK().
Examples
--------
values ranks
1 0
1 0
2 2
2 2
2 2
3 5
Returns
-------
Int64Column
The min rank
"""
arg = rlz.column(rlz.any)
@public
class DenseRank(RankBase):
"""Compute position of first element within each equal-value group in
sorted order, ignoring duplicate values. Equivalent to SQL DENSE_RANK().
Examples
--------
values ranks
1 0
1 0
2 1
2 1
2 1
3 2
Returns
-------
IntegerColumn
The rank
"""
arg = rlz.column(rlz.any)
@public
class RowNumber(RankBase):
"""Compute row number starting from 0 after sorting by column expression.
Equivalent to SQL ROW_NUMBER().
Examples
--------
>>> import ibis
>>> t = ibis.table([('values', dt.int64)])
>>> w = ibis.window(order_by=t.values)
>>> row_num = ibis.row_number().over(w)
>>> result = t[t.values, row_num.name('row_num')]
Returns
-------
IntegerColumn
Row number
"""
@public
class CumulativeOp(Analytic):
pass
@public
class CumulativeSum(CumulativeOp):
"""Cumulative sum.
Requires an ordering window.
"""
arg = rlz.column(rlz.numeric)
@attribute.default
def output_dtype(self):
return dt.higher_precedence(self.arg.output_dtype.largest, dt.int64)
@public
class CumulativeMean(CumulativeOp):
"""Cumulative mean.
Requires an order window.
"""
arg = rlz.column(rlz.numeric)
@attribute.default
def output_dtype(self):
return dt.higher_precedence(self.arg.output_dtype.largest, dt.float64)
@public
class CumulativeMax(CumulativeOp):
"""Cumulative max.
Requires an order window.
"""
arg = rlz.column(rlz.any)
output_dtype = rlz.dtype_like("arg")
@public
class CumulativeMin(CumulativeOp):
"""Cumulative min.
Requires an order window.
"""
arg = rlz.column(rlz.any)
output_dtype = rlz.dtype_like("arg")
@public
class CumulativeAny(CumulativeOp):
arg = rlz.column(rlz.boolean)
output_dtype = rlz.dtype_like("arg")
@public
class CumulativeAll(CumulativeOp):
arg = rlz.column(rlz.boolean)
output_dtype = rlz.dtype_like("arg")
@public
class PercentRank(Analytic):
arg = rlz.column(rlz.any)
output_dtype = dt.double
@public
class CumeDist(Analytic):
arg = rlz.column(rlz.any)
output_dtype = dt.double
@public
class NTile(Analytic):
arg = rlz.column(rlz.any)
buckets = rlz.scalar(rlz.integer)
output_dtype = dt.int64
@public
class FirstValue(Analytic):
"""Retrieve the first element."""
arg = rlz.column(rlz.any)
output_dtype = rlz.dtype_like("arg")
@public
class LastValue(Analytic):
"""Retrieve the last element."""
arg = rlz.column(rlz.any)
output_dtype = rlz.dtype_like("arg")
@public
class NthValue(Analytic):
"""Retrieve the Nth element."""
arg = rlz.column(rlz.any)
nth = rlz.integer
output_dtype = rlz.dtype_like("arg")
public(WindowOp=Window, AnalyticOp=Analytic)