Skip to content

Commit d7516ec

Browse files
maxi297octavia-squidington-iii
andauthored
fix: substreampartitionrouter state migration from child when it is empty (#444)
Co-authored-by: octavia-squidington-iii <[email protected]>
1 parent 837913f commit d7516ec

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ def _migrate_child_state_to_parent_state(self, stream_state: StreamState) -> Str
374374
# Ignore per-partition states or invalid formats.
375375
if isinstance(substream_state, (list, dict)) or len(substream_state_values) != 1:
376376
# If a global state is present under the key "state", use its first value.
377-
if "state" in stream_state and isinstance(stream_state["state"], dict):
377+
if (
378+
"state" in stream_state
379+
and isinstance(stream_state["state"], dict)
380+
and stream_state["state"] != {}
381+
):
378382
substream_state = list(stream_state["state"].values())[0]
379383
else:
380384
return {}

unit_tests/sources/declarative/partition_routers/test_substream_partition_router.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ def test_substream_partition_router_invalid_parent_record_type():
473473
},
474474
{"parent_stream_cursor": "2023-05-27T00:00:00Z"},
475475
),
476+
# Case 7: Migrate child state to parent state but child state is empty
477+
(
478+
{
479+
"state": {},
480+
"states": [],
481+
"parent_state": {"posts": {}},
482+
"lookback_window": 1,
483+
"use_global_cursor": False,
484+
},
485+
{},
486+
),
476487
],
477488
ids=[
478489
"empty_initial_state",
@@ -481,6 +492,7 @@ def test_substream_partition_router_invalid_parent_record_type():
481492
"initial_state_no_parent_per_partition_state",
482493
"initial_state_with_parent_state",
483494
"initial_state_no_parent_global_state_declarative",
495+
"initial_state_no_parent_and_no_child",
484496
],
485497
)
486498
def test_set_initial_state(initial_state, expected_parent_state):

0 commit comments

Comments
 (0)