-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(airbyte-cdk): CSV decoder #47131
Open
natikgadzhi
wants to merge
2
commits into
master
Choose a base branch
from
ng/csv-decoder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−15
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions
46
airbyte-cdk/python/airbyte_cdk/sources/declarative/decoders/csv_decoder.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,46 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import csv | ||
import io | ||
import logging | ||
from dataclasses import InitVar, dataclass | ||
from typing import Any, Generator, Mapping, MutableMapping | ||
|
||
import requests | ||
from airbyte_cdk.sources.declarative.decoders.decoder import Decoder | ||
|
||
logger = logging.getLogger("airbyte") | ||
|
||
|
||
@dataclass | ||
class CsvDecoder(Decoder): | ||
""" | ||
CsvDecoder is a decoder strategy that parses the CSV content of the response, and converts it to a stream of dicts. | ||
""" | ||
|
||
parameters: InitVar[Mapping[str, Any]] | ||
|
||
def __post_init__(self, parameters: Mapping[str, Any]): | ||
self.delimiter = parameters.get("delimiter", ",") | ||
|
||
def is_stream_response(self) -> bool: | ||
return False | ||
|
||
def decode(self, response: requests.Response) -> Generator[MutableMapping[str, Any], None, None]: | ||
try: | ||
csv_content = io.StringIO(response.text) | ||
reader = csv.reader(csv_content, delimiter=self.delimiter) | ||
Comment on lines
+28
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨This is a potential OOM🚨
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !!111 you're right! Thank you kindly. |
||
header = next(reader) | ||
|
||
for row in reader: | ||
record = {} | ||
for i, value in enumerate(row): | ||
if i < len(header): | ||
record[header[i]] = value | ||
yield record | ||
|
||
except Exception as exc: | ||
logger.warning(f"Response cannot be parsed from CSV: {response.status_code=}, {response.text=}, {exc=}") | ||
yield {} |
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
35 changes: 35 additions & 0 deletions
35
airbyte-cdk/python/unit_tests/sources/declarative/decoders/test_csv_decoder.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,35 @@ | ||
# | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import pytest | ||
import requests | ||
from airbyte_cdk.sources.declarative.decoders import CsvDecoder | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"response_body, parameters, expected", | ||
[ | ||
( | ||
"col1,col2\nval1,val2", | ||
{}, | ||
[{"col1": "val1", "col2": "val2"}], | ||
), | ||
( | ||
"col1;col2\nval1;val2", | ||
{"delimiter": ";"}, | ||
[{"col1": "val1", "col2": "val2"}], | ||
), | ||
( | ||
"malformed csv", | ||
{}, | ||
[], # Expect an empty dict on error | ||
), | ||
], | ||
ids=["with_header", "custom_delimiter", "malformed_csv"], | ||
) | ||
def test_csv_decoder(requests_mock, response_body, parameters, expected): | ||
requests_mock.register_uri("GET", "https://airbyte.io/", text=response_body) | ||
response = requests.get("https://airbyte.io/") | ||
results = list(CsvDecoder(parameters=parameters).decode(response)) | ||
assert results == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a benefit to injecting the delimiter as a parameter property vs directly injecting as an argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know! Give me a suggestion here, I have no idea how this is done or if it will actually work when instantiated from an actual manifest. It could easily be that this is not the right way.