-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathsql_context.py
More file actions
456 lines (330 loc) · 14.9 KB
/
Copy pathsql_context.py
File metadata and controls
456 lines (330 loc) · 14.9 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import time
import pandas
import re
from typing import Optional, Dict, Callable
from typing_extensions import Self
from queue import Queue
from feldera import FelderaClient
from feldera.rest.program import Program
from feldera.rest.pipeline import Pipeline
from feldera.rest.connector import Connector
from feldera._sql_table import SQLTable
from feldera.sql_schema import SQLSchema
from feldera.output_handler import OutputHandler
from feldera._callback_runner import CallbackRunner, _CallbackRunnerInstruction
from enum import Enum
class BuildMode(Enum):
CREATE = 1
GET = 2
GET_OR_CREATE = 3
def _table_name_from_sql(ddl: str) -> str:
return re.findall(r"[\w']+", ddl)[2]
class SQLContext:
"""
.. _SQLContext:
The SQLContext is the main entry point for the Feldera SQL API.
Abstracts the interaction with the Feldera API and provides a high-level interface for SQL pipelines.
"""
def __init__(
self,
pipeline_name: str,
client: FelderaClient,
pipeline_description: str = None,
program_name: str = None,
program_description: str = None,
):
self.build_mode: Optional[BuildMode] = None
self.is_pipeline_running: bool = False
self.ddl: str = ""
# In the SQL DDL declaration, the order of the tables and views is important.
# From python 3.7 onwards, the order of insertion is preserved in dictionaries.
# https://softwaremaniacs.org/blog/2020/02/05/dicts-ordered/en/
self.views: Dict[str, str] = {}
self.tables: Dict[str, SQLTable] = {}
# TODO: to be used for schema inference
self.todo_tables: Dict[str, Optional[SQLTable]] = {}
self.http_input_buffer: list[Dict[str, dict | list[dict] | str]] = []
# buffer that stores all input connectors to be created
# this is a Mapping[table_name -> list[Connector]]
self.input_connectors_buffer: Dict[str, list[Connector]] = {}
# buffer that stores all output connectors to be created
# this is a Mapping[view_name -> list[Connector]]
self.output_connectors_buffer: Dict[str, list[Connector]] = {}
self.views_tx: list[Dict[str, Queue]] = []
self.client: FelderaClient = client
self.pipeline_name: str = pipeline_name
self.pipeline_description: str = pipeline_description or ""
self.program_name: str = program_name or pipeline_name
self.program_description: str = program_description or ""
def __build_ddl(self):
"""
Internal function used to create the DDL from the registered tables and views.
"""
tables = "\n".join([tbl.build_ddl() for tbl in self.tables.values()])
views = "\n".join([view for view in self.views.values()])
self.ddl = tables + "\n" + views
def __setup_pipeline(self):
"""
Internal function used to setup the pipeline and program on the Feldera API.
:meta private:
"""
self.__build_ddl()
# TODO: handle different build modes
program = Program(self.program_name, self.ddl, self.program_description)
self.client.compile_program(program)
attached_cons = []
for tbl_name, conns in self.input_connectors_buffer.items():
for conn in conns:
self.client.create_connector(conn)
attached_con = conn.attach_relation(tbl_name, True)
attached_cons.append(attached_con)
for view_name, conns in self.output_connectors_buffer.items():
for con in conns:
self.client.create_connector(con)
attached_con = con.attach_relation(view_name, False)
attached_cons.append(attached_con)
pipeline = Pipeline(
self.pipeline_name,
self.program_name,
self.pipeline_description,
attached_connectors=attached_cons
)
self.client.create_pipeline(pipeline)
def __setup_output_listeners(self):
"""
Internal function used to setup the output listeners.
:meta private:
"""
for view_queue in self.views_tx:
for view_name, queue in view_queue.items():
# sends a message to the callback runner to start listening
queue.put(_CallbackRunnerInstruction.PipelineStarted)
# block until the callback runner is ready
queue.join()
def __push_http_inputs(self):
"""
Internal function used to push the input data to the pipeline.
:meta private:
"""
for input_buffer in self.http_input_buffer:
for tbl_name, data in input_buffer.items():
self.client.push_to_pipeline(self.pipeline_name, tbl_name, "json", data, array=True)
self.http_input_buffer.clear()
def create(self) -> Self:
"""
Sets the build mode to CREATE, meaning that the pipeline will be created from scratch.
"""
self.build_mode = BuildMode.CREATE
return self
def get(self) -> Self:
"""
Sets the build mode to GET, meaning that an existing pipeline will be used.
"""
self.build_mode = BuildMode.GET
return self
def get_or_create(self) -> Self:
"""
Sets the build mode to GET_OR_CREATE, meaning that an existing pipeline will be used if it exists,
else a new one will be created.
"""
self.build_mode = BuildMode.GET_OR_CREATE
return self
def register_table(self, table_name: str, schema: Optional[SQLSchema] = None, ddl: str = None):
"""
Registers a table with the SQLContext. The table can be registered with a schema or with the SQL DDL.
One of the two must be provided, but not both.
Auto inserts the trailing semicolon if not present.
In the future, schema will be inferred from the data provided from applicable sources.
:param table_name: The name of the table.
:param schema: The schema of the table.
:param ddl: The SQL DDL of the table.
"""
if not schema and not ddl:
raise ValueError("Schema inference isn't supported yet, either provide a schema or the SQL DDL")
if schema and ddl:
raise ValueError("Provide either a schema or the SQL DDL, not both")
if ddl:
self.register_table_from_sql(ddl)
return
if schema:
self.tables[table_name] = SQLTable(table_name, schema=schema)
else:
self.todo_tables[table_name] = None
def register_table_from_sql(self, ddl: str):
"""
Registers a table with the provided SQL DDL.
Auto inserts the trailing semicolon if not present.
:param ddl: The SQL DDL of the table.
"""
if ddl[-1] != ';':
ddl += ';'
name = _table_name_from_sql(ddl)
self.tables[name] = SQLTable(name, ddl)
def connect_source_pandas(self, table_name: str, df: pandas.DataFrame):
"""
Adds a pandas DataFrame to the input buffer of the SQLContext, to be pushed to the pipeline.
:param table_name: The name of the table.
:param df: The pandas DataFrame to be pushed to the pipeline.
"""
tbl = self.tables.get(table_name)
if tbl:
# tbl.validate_schema(df) TODO: something like this would be nice
self.http_input_buffer.append({tbl.name: df.to_dict('records')})
return
tbl = self.todo_tables.get(table_name)
if not tbl:
raise ValueError(f"Table {table_name} not registered")
# tbl.infer_schema(df) TODO: support schema inference
self.tables[table_name] = tbl
self.todo_tables.pop(table_name)
self.http_input_buffer.append({tbl.name: df.to_dict('records')})
def register_view(self, name: str, query: str):
"""
Registers a Feldera View based on the provided query.
Auto inserts the trailing semicolon if not present.
:param name: The name of the view.
:param query: The query to be used to create the view.
"""
if query[-1] != ';':
query += ';'
self.views[name] = f"CREATE VIEW {name} AS {query}"
def listen(self, view_name: str) -> OutputHandler:
"""
Listens to the output of the provided view so that it is available in the notebook / python code.
:param view_name: The name of the view to listen to.
.. note::
- This method must be called before calling :meth:`.run_to_completion`, or :meth:`.start`.
"""
queue = Queue(maxsize=1)
self.views_tx.append({view_name: queue})
handler = OutputHandler(self.client, self.pipeline_name, view_name, queue)
handler.start()
return handler
def connect_source_delta_table(self, table_name: str, connector_name: str, config: dict):
"""
Tells feldera to read the data from the specified delta table.
:param table_name: The name of the table.
:param connector_name: The unique name for this connector.
:param config: The configuration for the delta table.
"""
if config.get("uri") is None:
raise ValueError("uri is required in the config")
if config.get("mode") is None:
raise ValueError("mode is required in the config, valid modes: snapshot, follow, snapshot_and_follow")
if config.get("mode") not in ["snapshot", "follow", "snapshot_and_follow"]:
raise ValueError("mode must be one of snapshot, follow, snapshot_and_follow")
connector = Connector(name=connector_name,
config={
"transport": {
"name": "delta_table_input",
"config": config,
}
})
if table_name in self.input_connectors_buffer:
self.input_connectors_buffer[table_name].append(connector)
else:
self.input_connectors_buffer[table_name] = [connector]
def connect_sink_delta_table(self, view_name: str, connector_name: str, config: dict):
"""
Tells feldera to write the data to the specified delta table.
:param view_name: The name of the view whose output is sent to delta table.
:param connector_name: The unique name for this connector.
:param config: The configuration for the delta table connector.
"""
if config.get("uri") is None:
raise ValueError("uri is required in the config")
connector = Connector(name=connector_name,
config={
"transport": {
"name": "delta_table_output",
"config": config,
},
"enable_output_buffer": True,
"max_output_buffer_time_millis": 10000,
})
if view_name in self.output_connectors_buffer:
self.output_connectors_buffer[view_name].append(connector)
else:
self.output_connectors_buffer[view_name] = [connector]
def foreach_chunk(self, view_name: str, callback: Callable[[pandas.DataFrame, int], None]):
"""
Runs the given callback on each chunk of the output of the specified view.
:param view_name: The name of the view.
:param callback: The callback to run on each chunk. The callback should take two arguments:
- **chunk** -> The chunk as a pandas DataFrame
- **seq_no** -> The sequence number. The sequence number is a monotonically increasing integer that
starts from 0. Note that the sequence number is unique for each chunk, but not necessarily contiguous.
Please note that the callback is run in a separate thread, so it should be thread-safe.
Please note that the callback should not block for a long time, as by default, backpressure is enabled and
will block the pipeline.
.. note::
- The callback must be thread-safe as it will be run in a separate thread.
- This method must be called before calling :meth:`.run_to_completion`, or :meth:`.start`.
"""
queue: Optional[Queue] = None
if not self.is_pipeline_running:
queue = Queue(maxsize=1)
self.views_tx.append({view_name: queue})
handler = CallbackRunner(self.client, self.pipeline_name, view_name, callback, queue)
handler.start()
def run_to_completion(self):
"""
.. _run_to_completion:
Runs the pipeline to completion, waiting for all input records to be processed.
:raises RuntimeError: If the pipeline returns unknown metrics.
"""
self.__setup_pipeline()
# start the pipeline in the paused state
# so that we can start listening to the output
# before the pipeline consumes input
# ensuring that we don't miss any output
self.pause()
# set up the output listeners
self.__setup_output_listeners()
# resume the pipeline operations
self.resume()
self.__push_http_inputs()
while True:
metrics: dict = self.client.get_pipeline_stats(self.pipeline_name).get("global_metrics")
pipeline_complete: bool = metrics.get("pipeline_complete")
if pipeline_complete is None:
raise RuntimeError("received unknown metrics from the pipeline, pipeline_complete is None")
if pipeline_complete:
break
time.sleep(1)
for view_queue in self.views_tx:
for view_name, queue in view_queue.items():
# sends a message to the callback runner to stop listening
queue.put(_CallbackRunnerInstruction.RanToCompletion)
# block until the callback runner has been stopped
queue.join()
self.shutdown()
def start(self):
"""
.. _start:
Starts the pipeline.
:raises RuntimeError: If the pipeline returns unknown metrics.
"""
self.__setup_pipeline()
self.pause()
self.__setup_output_listeners()
self.resume()
self.__push_http_inputs()
def pause(self):
"""
Pauses the pipeline.
"""
self.client.pause_pipeline(self.pipeline_name)
self.is_pipeline_running = False
def shutdown(self):
"""
Shuts down the pipeline.
"""
self.client.shutdown_pipeline(self.pipeline_name)
self.is_pipeline_running = False
def resume(self):
"""
Resumes the pipeline.
"""
self.client.start_pipeline(self.pipeline_name)
self.is_pipeline_running = True