-
Notifications
You must be signed in to change notification settings - Fork 44
/
collectors.py
153 lines (107 loc) · 4.12 KB
/
collectors.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
from pysparkling.sql.expressions.aggregate.aggregations import Aggregation
class CollectList(Aggregation):
def __init__(self, column):
super(CollectList, self).__init__(column)
self.column = column
self.items = []
def merge(self, row, schema):
self.items.append(self.column.eval(row, schema))
def mergeStats(self, other, schema):
self.items += other.items
def eval(self, row, schema):
return self.items
def __str__(self):
return "collect_list({0})".format(self.column)
class CollectSet(Aggregation):
def __init__(self, column):
super(CollectSet, self).__init__(column)
self.column = column
self.items = set()
def merge(self, row, schema):
self.items.add(self.column.eval(row, schema))
def mergeStats(self, other, schema):
self.items |= other.items
def eval(self, row, schema):
return list(self.items)
def __str__(self):
return "collect_set({0})".format(self.column)
class SumDistinct(Aggregation):
def __init__(self, column):
super(SumDistinct, self).__init__(column)
self.column = column
self.items = set()
def merge(self, row, schema):
self.items.add(self.column.eval(row, schema))
def mergeStats(self, other, schema):
self.items |= other.items
def eval(self, row, schema):
return sum(self.items)
def __str__(self):
return "sum_distinct({0})".format(self.column)
class First(Aggregation):
_sentinel = object()
def __init__(self, column, ignore_nulls):
super(First, self).__init__(column)
self.column = column
self.value = self._sentinel
self.ignore_nulls = ignore_nulls
def merge(self, row, schema):
if self.value is First._sentinel or (self.ignore_nulls and self.value is None):
self.value = self.column.eval(row, schema)
def mergeStats(self, other, schema):
if self.value is First._sentinel or (self.ignore_nulls and self.value is None):
self.value = other.value
def eval(self, row, schema):
return self.value if self.value is not First._sentinel else None
def __str__(self):
return "first({0}, {1})".format(self.column, str(self.ignore_nulls).lower())
class Last(Aggregation):
_sentinel = object()
def __init__(self, column, ignore_nulls):
super(Last, self).__init__(column)
self.column = column
self.value = None
self.ignore_nulls = ignore_nulls
def merge(self, row, schema):
new_value = self.column.eval(row, schema)
if not (self.ignore_nulls and new_value is None):
self.value = new_value
def mergeStats(self, other, schema):
if not (self.ignore_nulls and other.value is None):
self.value = other.value
def eval(self, row, schema):
return self.value
def __str__(self):
return "last({0}, {1})".format(self.column, str(self.ignore_nulls).lower())
class CountDistinct(Aggregation):
def __init__(self, columns):
super(CountDistinct, self).__init__(columns)
self.columns = columns
self.items = set()
def merge(self, row, schema):
self.items.add(tuple(
col.eval(row, schema) for col in self.columns
))
def mergeStats(self, other, schema):
self.items += other.items
def eval(self, row, schema):
return len(self.items)
def __str__(self):
return "count(DISTINCT {0})".format(",".join(self.columns))
class ApproxCountDistinct(Aggregation):
def __init__(self, column):
super(ApproxCountDistinct, self).__init__(column)
self.column = column
self.items = set()
def merge(self, row, schema):
self.items.add(self.column.eval(row, schema))
def mergeStats(self, other, schema):
self.items += other.items
def eval(self, row, schema):
return len(self.items)
def __str__(self):
return "approx_count_distinct({0})".format(self.column)
__all__ = [
"SumDistinct", "ApproxCountDistinct", "CollectList", "CollectSet",
"First", "CountDistinct", "Last"
]