-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Source S3: maintain backwards compatibility between V3 & V4 state mes…
…sages
- Loading branch information
Showing
13 changed files
with
426 additions
and
12 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
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
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
18 changes: 18 additions & 0 deletions
18
airbyte-integrations/connectors/source-s3/integration_tests/v4_abnormal_state.json
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,18 @@ | ||
[ | ||
{ | ||
"type": "STREAM", | ||
"stream": { | ||
"stream_state": { | ||
"history": { | ||
"simple_test.csv": "2021-07-25T15:33:04.000000Z", | ||
"simple_test_2.csv": "2021-12-20T12:35:05.000000Z", | ||
"redshift_result.csv": "2022-05-26T09:17:47.000000Z", | ||
"redshift_result_3.csv": "2022-05-26T09:55:15.000000Z", | ||
"redshift_result_2.csv": "2022-05-26T09:55:16.000000Z" | ||
}, | ||
"_ab_source_file_last_modified": "2999-01-01T00:00:00.000000Z" | ||
}, | ||
"stream_descriptor": { "name": "test" } | ||
} | ||
} | ||
] |
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
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
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
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
68 changes: 68 additions & 0 deletions
68
airbyte-integrations/connectors/source-s3/source_s3/v4/cursor.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,68 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from datetime import datetime | ||
from typing import MutableMapping | ||
|
||
from airbyte_cdk.sources.file_based.stream.cursor import DefaultFileBasedCursor | ||
from airbyte_cdk.sources.file_based.types import StreamState | ||
|
||
|
||
class Cursor(DefaultFileBasedCursor): | ||
DATE_FORMAT = "%Y-%m-%d" | ||
CURSOR_FIELD = "_ab_source_file_last_modified" | ||
|
||
def set_initial_state(self, value: StreamState) -> None: | ||
if self._is_legacy_state(value): | ||
value = self._convert_legacy_state(value) | ||
super().set_initial_state(value) | ||
|
||
@staticmethod | ||
def _is_legacy_state(value: StreamState) -> bool: | ||
if not value: | ||
return False | ||
item = list(value.get("history", {}).keys())[0] | ||
try: | ||
datetime.strptime(item, Cursor.DATE_FORMAT) | ||
except ValueError: | ||
return False | ||
return True | ||
|
||
@staticmethod | ||
def _convert_legacy_state(legacy_state: StreamState) -> MutableMapping[str, str]: | ||
""" | ||
Transform the history from the old state message format to the new. | ||
e.g. | ||
{ | ||
"2022-05-26": ["simple_test.csv.csv", "simple_test_2.csv"], | ||
"2022-05-27": ["simple_test_2.csv", "redshift_result.csv"], | ||
... | ||
} | ||
=> | ||
{ | ||
"simple_test.csv": "2022-05-26T00:00:00.000000Z", | ||
"simple_test_2.csv": "2022-05-27T00:00:00.000000Z", | ||
"redshift_result.csv": "2022-05-27T00:00:00.000000Z", | ||
... | ||
} | ||
""" | ||
converted_history = {} | ||
|
||
for date_str, filenames in legacy_state.get("history", {}).items(): | ||
date_obj = datetime.strptime(date_str, Cursor.DATE_FORMAT) | ||
|
||
for filename in filenames: | ||
if filename in converted_history: | ||
if date_obj > datetime.strptime(converted_history[filename], DefaultFileBasedCursor.DATE_TIME_FORMAT): | ||
converted_history[filename] = date_obj.strftime(DefaultFileBasedCursor.DATE_TIME_FORMAT) | ||
else: | ||
converted_history[filename] = date_obj.strftime(DefaultFileBasedCursor.DATE_TIME_FORMAT) | ||
|
||
if converted_history: | ||
filename, timestamp = max(converted_history.items(), key=lambda x: (x[1], x[0])) | ||
cursor = f"{timestamp}_{filename}" | ||
else: | ||
cursor = None | ||
return {"history": converted_history, "_ab_source_file_last_modified": cursor} |
Oops, something went wrong.