Skip to content

Commit 8e23230

Browse files
authored
py: allow specifying update format in SQLContext.input_json (feldera#2144)
Fixes: feldera#2143 Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
1 parent 8fe9a3f commit 8e23230

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

python/feldera/sql_context.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,31 @@ def input_pandas(self, table_name: str, df: pandas.DataFrame, force: bool = Fals
283283
)
284284
return
285285

286-
def input_json(self, table_name: str, data: Dict | list, force: bool = False):
286+
def input_json(self, table_name: str, data: Dict | list, update_format: str = "raw", force: bool = False):
287287
"""
288288
Push this JSON data to the specified table of the pipeline.
289289
290290
:param table_name: The name of the table to push data into.
291291
:param data: The JSON encoded data to be pushed to the pipeline. The data should be in the form:
292292
`{'col1': 'val1', 'col2': 'val2'}` or `[{'col1': 'val1', 'col2': 'val2'}, {'col1': 'val1', 'col2': 'val2'}]`
293+
:param update_format: The update format of the JSON data to be pushed to the pipeline. Must be one of:
294+
"raw", "insert_delete". <https://www.feldera.com/docs/api/json#the-insertdelete-format>
293295
:param force: `True` to push data even if the pipeline is paused. `False` by default.
294296
"""
295297

298+
if update_format not in ["raw", "insert_delete"]:
299+
ValueError("update_format must be one of raw or insert_delete")
300+
296301
array = True if isinstance(data, list) else False
297-
self.client.push_to_pipeline(self.pipeline_name, table_name, "json", data, array=array, force=force)
302+
self.client.push_to_pipeline(
303+
self.pipeline_name,
304+
table_name,
305+
"json",
306+
data,
307+
update_format=update_format,
308+
array=array,
309+
force=force
310+
)
298311

299312
def register_local_view(self, name: str, query: str):
300313
"""

python/tests/test_wireframes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,18 +430,18 @@ def test_input_json0(self):
430430
sql.register_table(TBL_NAME, SQLSchema({"id": "INT", "name": "STRING"}))
431431
sql.register_materialized_view(VIEW_NAME, f"SELECT * FROM {TBL_NAME}")
432432

433-
data = {'id': 1, 'name': 'a'}
433+
data = {"insert": {'id': 1, 'name': 'a'}}
434434

435435
out = sql.listen(VIEW_NAME)
436436

437437
sql.start()
438-
sql.input_json(TBL_NAME, data)
438+
sql.input_json(TBL_NAME, data, update_format="insert_delete")
439439
sql.wait_for_completion(True)
440440

441441
out_data = out.to_dict()
442442

443-
data.update({"insert_delete": 1})
444-
assert out_data == [data]
443+
data["insert"].update({"insert_delete": 1})
444+
assert out_data == [data["insert"]]
445445

446446
def test_input_json1(self):
447447
sql = SQLContext("test_input_json", TEST_CLIENT).get_or_create()

0 commit comments

Comments
 (0)