-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to edge cases of CheckStream (#21404)
* Add test for failure case * Except StopIteration - make test pass * Don't attempt to connect to a stream if we get no stream slices * Make helper method for getting first record for a slice * Add comments and exit early if stream to check isn't in list of source streams * move helpers to helper module * Clarify what it means when StopIteration is returned by helper methods
- Loading branch information
1 parent
55a4715
commit d378294
Showing
3 changed files
with
95 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
airbyte-cdk/python/airbyte_cdk/sources/streams/utils/stream_helper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from typing import Any, Mapping, Optional | ||
|
||
from airbyte_cdk.models import SyncMode | ||
from airbyte_cdk.sources.streams.core import Stream, StreamData | ||
|
||
|
||
def get_first_stream_slice(stream) -> Optional[Mapping[str, Any]]: | ||
""" | ||
Gets the first stream_slice from a given stream's stream_slices. | ||
:param stream: stream | ||
:raises StopIteration: if there is no first slice to return (the stream_slices generator is empty) | ||
:return: first stream slice from 'stream_slices' generator (`None` is a valid stream slice) | ||
""" | ||
# We wrap the return output of stream_slices() because some implementations return types that are iterable, | ||
# but not iterators such as lists or tuples | ||
slices = iter( | ||
stream.stream_slices( | ||
cursor_field=stream.cursor_field, | ||
sync_mode=SyncMode.full_refresh, | ||
) | ||
) | ||
return next(slices) | ||
|
||
|
||
def get_first_record_for_slice(stream: Stream, stream_slice: Optional[Mapping[str, Any]]) -> StreamData: | ||
""" | ||
Gets the first record for a stream_slice of a stream. | ||
:param stream: stream | ||
:param stream_slice: stream_slice | ||
:raises StopIteration: if there is no first record to return (the read_records generator is empty) | ||
:return: StreamData containing the first record in the slice | ||
""" | ||
records_for_slice = stream.read_records(sync_mode=SyncMode.full_refresh, stream_slice=stream_slice) | ||
return next(records_for_slice) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters