Source code

Download source archive ↓

src/piflow_portable.py

1"""Arrow stream adapters for portable PiFlow components."""
2
3import bisect
4from itertools import islice
5import json
6import math
7import re
8import statistics
9from xml.etree import ElementTree
10
11import pyarrow as pa
12import pyarrow.compute as pc
13import piflow_documents as documents
14import piflow_collections as collections
15import piflow_tabular as tabular
16import piflow_rdf as rdf
17import piflow_workbooks as workbooks
18from xstream_operator import descriptor, preview_process, records_schema, result, run_operator
19
20MAX_INPUT = 8 * 1024 * 1024
21HALF_WIDTH = str.maketrans({**{c: c - 65248 for c in range(0xff01, 0xff5f)}, 0x3000: 32})
22PROVINCES = dict(zip(
23    "beijing tianjin shanghai chongqing anhui fujian gansu guangdong guizhou hainan hebei heilongjiang "
24    "henan hubei hunan jiangsu jiangxi jilin liaoning qinghai shaanxi shanxi shandong sichuan yunnan "
25    "zhejiang inner-mongolia ningxia guangxi xinjiang tibet".split(),
26    "北京 天津 上海 重庆 安徽 福建 甘肃 广东 贵州 海南 河北 黑龙江 河南 湖北 湖南 江苏 江西 吉林 辽宁 "
27    "青海 陕西 山西 山东 四川 云南 浙江 内蒙古自治区 宁夏回族自治区 广西壮族自治区 新疆维吾尔自治区 西藏自治区".split(),
28))
29CLEANERS = {"email_clean", "phone_number_clean", "identity_number_clean", "province_clean", "title_clean"}
30SCALERS = {"max_min_normalization", "scope_normalization", "z_score", "discretization"}
31
32
33def clean(value, operation):
34    if value is None:
35        return "" if operation == "province_clean" else None
36    if not isinstance(value, str):
37        raise ValueError("cleaning columns must contain strings")
38    value = value.translate(HALF_WIDTH)
39    if operation == "title_clean":
40        return value
41    if operation == "email_clean":
42        value = value.lower()
43        return value if re.fullmatch(r"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z0-9-]+", value) else ""
44    if operation == "phone_number_clean":
45        value = re.sub("[^0-9]", "", value)
46        return value if re.fullmatch(r"1[34578][0-9]{9}", value) else None
47    if operation == "identity_number_clean":
48        value = re.sub("[^0-9X]", "", value.upper()).lstrip("0")
49        if re.fullmatch("[0-9]{15}", value):
50            return value
51        if re.fullmatch("[0-9]{17}[0-9X]", value):
52            weights = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
53            checksum = sum(int(digit) * weight for digit, weight in zip(value, weights))
54            if value[-1] == "10X98765432"[checksum % 11]:
55                return value
56        return None
57    normalized = value.strip().casefold().replace(" ", "-")
58    for pinyin, name in PROVINCES.items():
59        if normalized in {pinyin, name.casefold(), name + "省", name + "市"}:
60            return name
61        if name.endswith("自治区") and normalized in {"内蒙古", "宁夏", "广西", "新疆", "西藏"} and name.startswith(normalized):
62            return name
63    return value
64
65
66def names(text):
67    values = [value.strip() for value in text.split(",")]
68    if not all(values) or len(values) != len(set(values)):
69        raise ValueError("column names must be unique and nonempty")
70    return values
71
72
73def field_indices(schema, columns):
74    indices = [schema.get_field_index(name) for name in columns]
75    if -1 in indices:
76        raise ValueError("input includes an unknown or ambiguous column")
77    return indices
78
79
80def scale_columns(operation, params):
81    if operation == "z_score":
82        ins, outs = names(params["inputCols"]), names(params["outputCols"])
83        if len(ins) != len(outs):
84            raise ValueError("inputCols and outputCols must contain the same number of names")
85        return list(zip(ins, outs))
86    return [(params["inputCol"], params["outputCol"])]
87
88
89def scale(values, operation, params):
90    available = [float(value) for value in values if value is not None]
91    if not all(map(math.isfinite, available)):
92        raise ValueError("scaling requires finite numeric input")
93    if not available:
94        return values
95    if operation == "discretization":
96        count = params["numBins"]
97        if not 2 <= count <= 1000:
98            raise ValueError("numBins must be from 2 to 1000")
99        if params["method"] == "EqualWidth":
100            if any(value < 0 or value > count for value in available):
101                raise ValueError("EqualWidth accepts values from zero through numBins")
102            apply = lambda value: min(int(value), count - 1)
103        elif params["method"] == "EqualFrequency":
104            ordered = sorted(available)
105            cuts = sorted({ordered[min(len(ordered) - 1, math.ceil(len(ordered) * i / count) - 1)]
106                           for i in range(1, count)} - {ordered[0], ordered[-1]})
107            apply = lambda value: bisect.bisect_right(cuts, value)
108        else:
109            raise ValueError("method must be EqualWidth or EqualFrequency")
110    else:
111        if operation == "z_score":
112            origin = statistics.mean(available)
113            spread = statistics.stdev(available) if len(available) > 1 else 0
114        else:
115            origin = min(available)
116            spread = max(available) - origin
117        low, high = params["range"] if operation == "scope_normalization" else (0, 1)
118        if not math.isfinite(low) or not math.isfinite(high) or low >= high:
119            raise ValueError("range must have two increasing finite bounds")
120        apply = lambda value: (value - origin) / spread * (high - low) + low if spread else None
121    return [None if value is None else apply(value) for value in values]
122
123
124def replace_column(table, name, values):
125    index = table.schema.get_field_index(name)
126    return table.append_column(name, values) if index < 0 else table.set_column(index, name, values)
127
128
129def collect(chunks):
130    data = bytearray()
131    for chunk in chunks:
132        data.extend(chunk)
133        if len(data) > MAX_INPUT:
134            raise ValueError("input exceeds 8 MiB")
135    return bytes(data)
136
137
138def collect_records(port, schema):
139    batches, size, rows = [], 0, 0
140    for batch in port.records():
141        size += batch.nbytes
142        rows += batch.num_rows
143        if size > MAX_INPUT or rows > 100_000:
144            raise ValueError("batch operation accepts at most 8 MiB and 100,000 records")
145        batches.append(batch)
146    return pa.Table.from_batches(batches, schema=schema)
147
148
149def json_rows(data):
150    def invalid_number(value):
151        raise ValueError("non-finite JSON number: " + value)
152
153    root = json.loads(data, parse_constant=invalid_number)
154    rows = root if isinstance(root, list) else [root]
155    if not all(isinstance(row, dict) for row in rows):
156        raise ValueError("JSON input must be an object or an array of objects")
157    # Arrow's Python row inference reads its columns from the first row.
158    columns = dict.fromkeys(key for row in rows for key in row)
159    return pa.table({column: [row.get(column) for row in rows] for column in columns})
160
161
162class XmlBuilder(ElementTree.TreeBuilder):
163    def doctype(self, name, pubid, system):
164        raise ValueError("XML document types are unsupported")
165
166
167def xml_root(data):
168    if len(data) > MAX_INPUT:
169        raise ValueError("XML exceeds 8 MiB")
170    return ElementTree.fromstring(data, parser=ElementTree.XMLParser(target=XmlBuilder()))
171
172
173def xml_rows(root, operation, params):
174    if operation == "xml_parser":
175        selected = list(root.iter(params["rowTag"]))
176        columns = list(dict.fromkeys(child.tag for row in selected for child in row))
177    else:
178        tags = names(params["label"])
179        if root.tag != tags[0] or len(tags) < 2:
180            raise ValueError("label must start with the root and name a repeated child tag")
181        selected = root.findall("/".join(tags[1:]))
182        columns = names(params["schema"])
183    return pa.table({column: pa.array([row.findtext(column) or None for row in selected], type=pa.string())
184                     for column in columns})
185
186
187def xml_model(node, depth=0):
188    if depth >= 32:
189        raise ValueError("XML nesting exceeds 32 levels")
190    if not node.attrib and not len(node):
191        return node.text
192    model = {"@" + name: value for name, value in node.attrib.items()}
193    if node.text and node.text.strip():
194        model["#text"] = node.text
195    groups = {}
196    for child in node:
197        groups.setdefault(child.tag, []).append(xml_model(child, depth + 1))
198    model.update({key: values[0] if len(values) == 1 else values for key, values in groups.items()})
199    return model
200
201
202def parse_xml_column(value):
203    if value is None:
204        return None
205    root = xml_root(value)
206    return json.dumps({root.tag: xml_model(root)}, ensure_ascii=False)
207
208
209def put_xml_row(root, row):
210    target = ElementTree.SubElement(root, "ROW")
211    for name, value in row.items():
212        if not re.fullmatch(r"[a-zA-Z_][a-zA-Z0-9_.-]*", name):
213            raise ValueError("record column is not an XML name: " + name)
214        if isinstance(value, (list, dict)):
215            raise ValueError("XML serialization accepts scalar columns")
216        if value is not None:
217            ElementTree.SubElement(target, name).text = str(value)
218
219
220def emit(ctx, table, port="result"):
221    out = ctx.records_output(port, table.schema)
222    for batch in table.to_batches(max_chunksize=1024):
223        out.write(batch)
224
225
226def emit_tables(ctx, tables):
227    outputs = {name: ctx.records_output(name, table.schema) for name, table in tables.items()}
228    for name, table in tables.items():
229        for batch in table.to_batches(max_chunksize=1024):
230            outputs[name].write(batch)
231
232
233def emit_graph(ctx, edges, vertices):
234    emit_tables(ctx, {
235        "edges": pa.Table.from_pylist(edges, schema=collections.EDGE_SCHEMA),
236        "vertex": pa.Table.from_pylist(vertices, schema=collections.VERTEX_SCHEMA),
237    })
238
239
240def inferred_schema(operation, schema, params):
241    if operation == "word_spliter":
242        return pa.schema([("words", pa.string())])
243    if operation == "average_temperature":
244        return tabular.SENSOR_SCHEMA
245    if operation in {"data_sort", "complement_by_memcache"}:
246        return schema
247    if operation == "field_type_conversion" and schema is not None:
248        return tabular.numeric_schema(schema, params, names)
249    if operation == "all_fields_clean_neo4j" and schema is not None:
250        return tabular.string_schema(schema)
251    if operation == "person_id_expansion" and schema is not None:
252        removed = [params["expandIDField"], params["expandIDType"]]
253        field_indices(schema, removed)
254        retained = [field for field in schema if field.name not in removed]
255        added = [*collections.ID_FIELDS, "source", "uuid", "id_hash"]
256        if len(set(removed)) != 2 or any(field.name in added for field in retained):
257            raise ValueError("input names conflict with generated identity fields")
258        return pa.schema(retained + [pa.field(name, pa.string()) for name in added], metadata=schema.metadata)
259    if operation == "folder_csv_parser":
260        return pa.schema([(name, pa.string()) for name in names(params["schema"])])
261    if operation == "select_files_by_name":
262        return pa.schema([("path", pa.string())])
263    if operation == "card_code_clean" and schema is not None:
264        field = schema.field(field_indices(schema, [params["cardCodeField"], params["cardTypeField"]])[0])
265        if not pa.types.is_string(field.type):
266            raise ValueError("cardCodeField must be a string column")
267        return schema
268    if operation == "regex_text_process" and schema is not None:
269        column = schema.field(field_indices(schema, [params["columnName"]])[0])
270        if not pa.types.is_string(column.type) or column.name + "_new" in schema.names:
271            raise ValueError("input must be a string column with an unused _new field name")
272        return schema.append(pa.field(column.name + "_new", pa.string()))
273    if operation == "pdf_parser":
274        return pa.schema([("page", pa.int64()), ("text", pa.string())])
275    if operation in {"docx_parser", "html_parser"}:
276        return documents.RECORDS_TEXT
277    if operation == "pptx_parser":
278        return documents.RECORDS_TEXT.append(pa.field("slide", pa.int64()))
279    if operation == "xml_parser_generate_new_field" and schema is not None:
280        field_indices(schema, [params["decoderField"]])
281        name = params["newField"]
282        if not name or name in schema.names:
283            raise ValueError("newField must be nonempty and absent from the input")
284        return schema.append(pa.field(name, pa.string()))
285    if operation in CLEANERS | {"xml_parser_columns"} and schema is not None:
286        columns = params["xmlColumns"] if operation == "xml_parser_columns" else params["columnName"]
287        indices = field_indices(schema, names(columns))
288        if any(not pa.types.is_string(schema.field(i).type) and not pa.types.is_large_string(schema.field(i).type)
289               for i in indices):
290            raise ValueError("selected columns must be strings")
291        return pa.schema([pa.field(field.name, pa.string()) if i in indices else field
292                          for i, field in enumerate(schema)], metadata=schema.metadata)
293    if operation in SCALERS and schema is not None:
294        for source, target in scale_columns(operation, params):
295            field = schema.field(field_indices(schema, [source])[0])
296            if not (pa.types.is_integer(field.type) or pa.types.is_floating(field.type)):
297                raise ValueError("scaling requires integer or floating-point columns")
298        for source, target in scale_columns(operation, params):
299            field = pa.field(target, pa.float64())
300            index = schema.get_field_index(target)
301            schema = schema.append(field) if index == -1 else schema.set(index, field)
302    if operation == "json_string_parser":
303        return json_rows(params["jsonString"]).schema
304    if operation == "xml_string_parser":
305        return pa.schema([(column, pa.string()) for column in names(params["schema"])])
306    return schema if operation in CLEANERS | SCALERS | {"xml_parser_columns"} else None
307
308
309def process(ctx, operation):
310    params = ctx.params
311    if operation in {"json_string_parser", "xml_string_parser"}:
312        if operation == "json_string_parser":
313            emit(ctx, json_rows(params["jsonString"]))
314        else:
315            emit(ctx, xml_rows(xml_root(params["XmlString"]), operation, params))
316        return
317    if operation == "excel_parser":
318        emit(ctx, workbooks.first_sheet_collection(ctx, collect))
319        return
320    if operation in collections.FOLDER_OPERATORS:
321        emit(ctx, collections.folder_tables(ctx, operation, params, collect, json_rows, xml_root, xml_rows, names))
322        return
323    if operation == "complement_by_memcache":
324        lookup = ctx.input("lookup")
325        materialized = collect_records(lookup, lookup.schema)
326        tabular.enrich_records(ctx, materialized, ctx.input("source").schema, names)
327        return
328    if operation == "excel_write_multiple_sheets":
329        collections.excel_workbook(ctx, collect_records)
330        return
331    if operation == "label_propagation":
332        edges = ctx.input("edges")
333        edge_rows = collect_records(edges, edges.schema).to_pylist()
334        vertex = ctx.input("vertex")
335        vertex_rows = collect_records(vertex, vertex.schema).to_pylist()
336        labels = collections.label_propagation(edge_rows, vertex_rows, params["maxIter"])
337        emit_graph(ctx, edge_rows, labels)
338        return
339    source = ctx.input("source")
340    if operation == "word_spliter":
341        import jieba
342        import unicodedata
343
344        value = collect(source.bytes()).decode("utf-8-sig")
345        text = "".join(char for char in value if not char.isspace()
346                       and not unicodedata.category(char).startswith(("P", "S")))
347        words = ({"words": word} for word in jieba.cut_for_search(text, HMM=False))
348        out = ctx.records_output("result", inferred_schema(operation, None, params))
349        for rows in iter(lambda: list(islice(words, 1024)), []):
350            out.write(pa.RecordBatch.from_pylist(rows, schema=inferred_schema(operation, None, params)))
351    elif operation == "rdf_to_df":
352        emit_tables(ctx, rdf.parse_rdf(collect(source.bytes()), params["syntax"]))
353    elif operation == "excel_write_sort":
354        table = collect_records(source, source.schema)
355        workbooks.write_sorted(ctx, tabular.sorted_table(table, params, names))
356    elif operation == "load_graph":
357        edges, vertices = collections.load_graph(collect(source.bytes()))
358        emit_graph(ctx, edges, vertices)
359    elif operation == "select_files_by_name":
360        schema = inferred_schema(operation, source.schema, params)
361        index = field_indices(source.schema, [params["pathColumn"]])[0]
362        pattern = params["selectionConditions"]
363        if not isinstance(pattern, str) or len(pattern) > 256:
364            raise ValueError("selectionConditions must be a glob up to 256 characters")
365        out = ctx.records_output("result", schema)
366        for batch in source.records():
367            matching = [value for value in batch.column(index).to_pylist() if collections.file_matches(value, pattern)]
368            out.write(pa.RecordBatch.from_arrays([pa.array(matching, type=pa.string())], schema=schema))
369    elif operation == "regex_text_process":
370        schema = inferred_schema(operation, source.schema, params)
371        index = field_indices(schema, [params["columnName"]])[0]
372        pattern, replacement = params["regex"], params["replaceStr"]
373        if len(pattern) > 1024 or len(replacement) > 2048:
374            raise ValueError("regular expression or replacement exceeds its size limit")
375        out = ctx.records_output("result", schema)
376        for batch in source.records():
377            # RE2 executes without backtracking. Limit input and expansion before calling Arrow.
378            column = batch.column(index)
379            widths = pc.binary_length(column).to_pylist()
380            max_bytes = sum((width + 1) * (len(replacement.encode()) + 1) for width in widths if width is not None)
381            if max_bytes > 32 * 1024 * 1024:
382                raise ValueError("potential regex expansion exceeds 32 MiB in one batch")
383            changed = pc.replace_substring_regex(column, pattern, replacement)
384            out.write(pa.RecordBatch.from_arrays([*batch.columns, changed], schema=schema))
385    elif operation in {"data_sort", "average_temperature"}:
386        table = collect_records(source, source.schema)
387        result = tabular.average_temperature(table.to_pylist()) if operation == "average_temperature" else tabular.sorted_table(table, params, names)
388        emit(ctx, result)
389    elif operation in {"field_type_conversion", "all_fields_clean_neo4j"}:
390        schema = inferred_schema(operation, source.schema, params)
391        out = ctx.records_output("result", schema)
392        for batch in source.records():
393            columns = []
394            for index, field in enumerate(schema):
395                column = pc.cast(batch.column(index), field.type)
396                if operation == "all_fields_clean_neo4j":
397                    column = pc.replace_substring(column, '"', '')
398                columns.append(column)
399            out.write(pa.RecordBatch.from_arrays(columns, schema=schema))
400    elif operation in {"card_code_clean", "person_id_expansion"}:
401        schema = inferred_schema(operation, source.schema, params)
402        out = ctx.records_output("result", schema)
403        for batch in source.records():
404            rows = batch.to_pylist()
405            if operation == "card_code_clean":
406                for row in rows:
407                    if str(row[params["cardTypeField"]]) == "1":
408                        key = params["cardCodeField"]
409                        row[key] = clean(row[key], "identity_number_clean")
410            else:
411                rows = [collections.expand_id(row, params) for row in rows]
412            out.write(pa.RecordBatch.from_pylist(rows, schema=schema))
413    elif operation in {"docx_parser", "pptx_parser", "html_parser", "pdf_parser", "flatten_xml_parser",
414                       "evaluate_json_path", "decompress_gzip"}:
415        data = collect(source.bytes())
416        if operation == "decompress_gzip":
417            out = ctx.bytes_output("result")
418            for chunk in documents.decompress(data):
419                out.write(chunk)
420            return
421        if operation == "html_parser":
422            table = documents.html_text(data)
423        elif operation == "pdf_parser":
424            table = documents.pdf_text(data, inferred_schema(operation, None, params))
425        elif operation in {"docx_parser", "pptx_parser"}:
426            table = documents.office_text(data, operation, xml_root)
427        elif operation == "evaluate_json_path":
428            table = documents.json_field(data, params["tag"], json_rows)
429        else:
430            table = documents.flatten_xml(xml_root(data), params)
431        emit(ctx, table)
432    elif operation == "xml_parser_generate_new_field":
433        schema = inferred_schema(operation, source.schema, params)
434        source_index = schema.get_field_index(params["decoderField"])
435        out = ctx.records_output("result", schema)
436        for batch in source.records():
437            values = [documents.xml_value(value, params["tagXPath"], xml_root)
438                      for value in batch.column(source_index).to_pylist()]
439            out.write(pa.RecordBatch.from_arrays([*batch.columns, pa.array(values, type=pa.string())], schema=schema))
440    elif operation in {"json_parser", "xml_parser"}:
441        data = collect(source.bytes())
442        emit(ctx, json_rows(data) if operation == "json_parser" else xml_rows(xml_root(data), operation, params))
443    elif operation == "json_save":
444        out = ctx.bytes_output("result")
445        for batch in source.records():
446            for row in batch.to_pylist():
447                out.write((json.dumps(row, ensure_ascii=False, allow_nan=False) + "\n").encode())
448    elif operation == "xml_save":
449        out = ctx.bytes_output("result")
450        out.write(b"<ROWS>")
451        for batch in source.records():
452            for row in batch.to_pylist():
453                temp = ElementTree.Element("ROWS")
454                put_xml_row(temp, row)
455                out.write(ElementTree.tostring(temp[0], encoding="utf-8"))
456        out.write(b"</ROWS>")
457    elif operation in SCALERS:
458        schema = source.schema
459        inferred_schema(operation, schema, params)
460        original = collect_records(source, schema)
461        table = original
462        for origin, target in scale_columns(operation, params):
463            values = scale(original[origin].to_pylist(), operation, params)
464            table = replace_column(table, target, pa.array(values, type=pa.float64()))
465        emit(ctx, table)
466    else:
467        schema = inferred_schema(operation, source.schema, params)
468        selected = names(params["xmlColumns"] if operation == "xml_parser_columns" else params["columnName"])
469        indices = field_indices(source.schema, selected)
470        out = ctx.records_output("result", schema)
471        for batch in source.records():
472            arrays = list(batch.columns)
473            for index in indices:
474                values = arrays[index].to_pylist()
475                cleaned = (parse_xml_column(value) if operation == "xml_parser_columns" else clean(value, operation)
476                           for value in values)
477                arrays[index] = pa.array(cleaned, type=pa.string())
478            out.write(pa.RecordBatch.from_arrays(arrays, schema=schema))
479
480
481def start(operation, output_view):
482    output_views = output_view if isinstance(output_view, dict) else {"result": output_view}
483    def execute(ctx):
484        process(ctx, operation)
485
486    def infer(inputs, params):
487        outputs = {}
488        for port, view in output_views.items():
489            schema = None
490            if view == "records":
491                if operation == "rdf_to_df":
492                    schema = rdf.ENTITY if port == "entityOut" else rdf.RELATION
493                elif operation in {"load_graph", "label_propagation"}:
494                    schema = collections.EDGE_SCHEMA if port == "edges" else collections.VERTEX_SCHEMA
495                else:
496                    source = inputs.get("source")
497                    schema = inferred_schema(operation, records_schema(source) if source else None, params)
498            formats = ["arrow"] if view == "records" else ["xlsx"] if operation in {"excel_write_multiple_sheets", "excel_write_sort"} else ["binary"]
499            outputs[port] = descriptor([view], formats, schema,
500                                       certainty="known" if schema is not None or view == "bytes" else "unknown")
501        return result(outputs)
502
503    def preview(inputs, params, limits):
504        if operation in collections.FOLDER_OPERATORS or operation in {"excel_parser", "excel_write_multiple_sheets", "excel_write_sort"}:
505            return {}
506        return preview_process(execute, inputs, params, limits)
507
508    run_operator(execute, infer, preview_outputs=preview)