src/dacp_piflow_execute_sql_stop.py
1"""PiFlow ExecuteSQLStop adapted to a bounded, read-only SQLite relation."""
2import pyarrow as pa
3from dacp_piflow_support import params, materialize, database, write_table, MAX_BYTES
4
5
6def process(ctx):
7 p = params(ctx, ['sql', 'viewName'])
8 if not isinstance(p['sql'], str) or not isinstance(p['viewName'], str) or not p['viewName']:
9 raise ValueError('sql and viewName must be strings')
10 table = materialize(ctx.input('source'))
11 connection = database(table, p['viewName'])
12 try:
13 cursor = connection.execute(p['sql'])
14 names = [column[0] for column in cursor.description]
15 if len(names) != len(set(names)):
16 raise ValueError('query must return unique column names')
17 rows, size = [], 0
18 for values in cursor:
19 row = dict(zip(names, values))
20 size += sum(len(v) if isinstance(v, (str, bytes)) else 32 for v in values) + 128
21 if size > MAX_BYTES:
22 raise ValueError('SQL output exceeds 128 MiB')
23 rows.append(row)
24 if rows:
25 result = pa.Table.from_pylist(rows)
26 else:
27 # SQLite has no static output type metadata; retain known source types.
28 result = pa.Table.from_batches([], schema=pa.schema([
29 table.schema.field(n) if n in table.column_names else pa.field(n, pa.null()) for n in names]))
30 write_table(ctx, result)
31 finally:
32 connection.close()
33
34
35def infer_outputs(inputs, params):
36 import pyarrow as pa
37 from dacp_operator import descriptor, result, records_schema
38 expected = {'source'}
39 if set(inputs) != expected:
40 raise ValueError('unexpected input ports')
41 outputs = {'result': {'representations': ['records'], 'formats': ['arrow'], 'certainty': 'unknown'}}
42 return result(outputs)
43
44
45def preview_outputs(inputs, params, limits):
46 from dacp_operator import preview_process
47 return preview_process(process, inputs, params, limits)
48
49
50if __name__ == '__main__':
51 from dacp_operator import run_operator
52 run_operator(process, infer_outputs, preview_outputs=preview_outputs)