-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
232 lines (198 loc) · 7.54 KB
/
filter.py
File metadata and controls
232 lines (198 loc) · 7.54 KB
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
from typing import Any, Dict
import pyarrow as pa
import pyarrow.compute
from cjwmodule import i18n
from cjwmodule.arrow.condition import ConditionError, condition_to_mask
def _filter_column(column: pa.ChunkedArray, mask: pa.ChunkedArray) -> pa.ChunkedArray:
result = pa.compute.filter(column, mask)
if pa.types.is_dictionary(result.type):
# Re-encode dictionary, so no extra values are there
#
# TODO optimize!
result = pa.compute.cast(result, pa.utf8()).dictionary_encode()
return result
def _filter_table(arrow_table: pa.Table, params: Dict[str, Any]) -> pa.Table:
if not params["condition"]:
return arrow_table
if params["keep"]:
condition = params["condition"]
else:
condition = {"operation": "not", "condition": params["condition"]}
mask = condition_to_mask(arrow_table, condition) # or raise ConditionError
return pa.table(
{
name: _filter_column(column, mask)
for name, column in zip(arrow_table.column_names, arrow_table.itercolumns())
}
)
def render(arrow_table: pa.Table, params, output_path, **kwargs):
try:
output_table = _filter_table(arrow_table, params)
except ConditionError as err:
return [
i18n.trans(
"regexParseError.message",
"Regex parse error: {error}",
{"error": e.msg},
)
for e in err.errors
]
with pa.ipc.RecordBatchFileWriter(output_path, output_table.schema) as writer:
writer.write_table(output_table)
return []
def _migrate_params_v0_to_v1(params: Dict[str, Any]) -> Dict[str, Any]:
is_regex = params["regex"]
condition = params["condition"]
# v0:
# Select|| (0,1)
# Text contains|Text does not contain|Text is exactly|| (2, 3, 4, 5)
# Cell is empty|Cell is not empty|| (6, 7, 8)
# Equals|Greater than|Greater than or equals|Less than|Less than or
# equals|| (9, 10, 11, 12, 13, 14)
# Date is|Date is before|Date is after (15, 16, 17)
#
# v1:
# Select|| (0,1)
# Text contains|Text does not contain|Text is exactly|| (2, 3, 4, 5)
# Text contains regex|Text does not contain regex|Text matches regex
# exactly|| (6, 7, 8, 9)
# Cell is empty|Cell is not empty|| (10, 11, 12)
# Equals|Greater than|Greater than or equals|Less than|Less than or
# equals|| (13, 14, 15, 16, 17, 18)
# Date is|Date is before|Date is after (19, 20, 21)
if is_regex and condition in (2, 3, 4, 5):
condition += 4 # 2 => 6, 3 => 7, ...
elif condition > 5:
condition += 4
ret = dict(params)
del ret["regex"]
ret["condition"] = condition
return ret
def _migrate_params_v1_to_v2(params: Dict[str, Any]) -> Dict[str, Any]:
# v1 condition _was_ number pointing into menu:
# Select|| (0,1)
# Text contains|Text does not contain|Text is exactly|| (2, 3, 4, 5)
# Text contains regex|Text does not contain regex|Text matches regex
# exactly|| (6, 7, 8, 9)
# Cell is empty|Cell is not empty|| (10, 11, 12)
# Equals|Greater than|Greater than or equals|Less than|Less than or
# equals|| (13, 14, 15, 16, 17, 18)
# Date is|Date is before|Date is after (19, 20, 21)
try:
condition = [
"",
"",
"text_contains",
"text_does_not_contain",
"text_is_exactly",
"",
"text_contains_regex",
"text_does_not_contain_regex",
"text_is_exactly_regex",
"",
"cell_is_empty",
"cell_is_not_empty",
"",
"number_equals",
"number_is_greater_than",
"number_is_greater_than_or_equals",
"number_is_less_than",
"number_is_less_than_or_equals",
"",
"date_is",
"date_is_before",
"date_is_after",
][params.get("condition", 0)]
except IndexError:
condition = ""
return {
"keep": params.get("keep", 0),
"filters": {
"operator": "and",
"filters": [
{
"operator": "and",
"subfilters": [
{
"colname": params["column"],
"condition": condition,
"value": params.get("value", ""),
"case_sensitive": params.get("casesensitive", False),
}
],
}
],
},
}
def _migrate_params_v2_to_v3(params):
# v2: params['keep'] is 0 (True) or 1 (False)
#
# v3: params['keep'] is bool
return {**params, "keep": params["keep"] == 0}
def _migrate_params_v3_to_v4(params):
# v3: "operator+filters", each filter "operator+subfilters", snake_case names
# v4: nested operations, renamed, regex is boolean option
filters = params["filters"]
operations_map = {
"": "",
"text_contains": "text_contains",
"text_does_not_contain": "text_does_not_contain",
"text_is_exactly": "text_is",
"text_is_not_exactly": "text_is_not",
"text_contains_regex": "text_contains",
"text_does_not_contain_regex": "text_does_not_contain",
"text_is_exactly_regex": "text_is",
"cell_is_empty": "cell_is_null",
"cell_is_not_empty": "cell_is_not_null",
"cell_is_empty_str_or_null": "cell_is_empty",
"cell_is_not_empty_str_or_null": "cell_is_not_empty",
"number_equals": "number_is",
"number_does_not_equal": "number_is_not",
"number_is_greater_than": "number_is_greater_than",
"number_is_greater_than_or_equals": "number_is_greater_than_or_equals",
"number_is_less_than": "number_is_less_than",
"number_is_less_than_or_equals": "number_is_less_than_or_equals",
"date_is": "timestamp_is",
"date_is_not": "timestamp_is_not",
"date_is_before": "timestamp_is_before",
"date_is_after": "timestamp_is_after",
}
def migrate_subfilter(subfilter):
return dict(
operation=operations_map[subfilter["condition"]],
column=subfilter["colname"],
value=subfilter["value"],
isCaseSensitive=subfilter["case_sensitive"],
isRegex=subfilter["condition"].endswith("_regex"),
)
return {
"keep": params["keep"],
"condition": {
# [2020-11-13] in v3, "condition" is always 2 levels deep
"operation": filters["operator"], # and|or
"conditions": [
{
"operation": filter["operator"],
"conditions": [
migrate_subfilter(sf) for sf in filter["subfilters"]
],
}
for filter in filters["filters"]
],
},
}
def migrate_params(params: Dict[str, Any]):
# v0: 'regex' is a checkbox. Migrate it to a menu entry.
if "regex" in params:
params = _migrate_params_v0_to_v1(params)
# v1: just one condition. v2: op+filters, each containing op+subfilters
if "column" in params:
params = _migrate_params_v1_to_v2(params)
# v2: 'keep' is an integer (not a boolean)
# Don't use `isinstance(params['keep'], int)` because bool is a subclass of
# int (!)
if not isinstance(params["keep"], bool):
params = _migrate_params_v2_to_v3(params)
if "filters" in params:
params = _migrate_params_v3_to_v4(params)
return params