-
Notifications
You must be signed in to change notification settings - Fork 45
feat(low-code): add DpathFlattenFields #227
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
Merged
Daryna Ishchenko (darynaishchenko)
merged 5 commits into
main
from
daryna/low-code/add-dpath-flatten-fields
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
5 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 hidden or 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 hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
airbyte_cdk/sources/declarative/transformations/dpath_flatten_fields.py
This file contains hidden or 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,55 @@ | ||
| from dataclasses import InitVar, dataclass | ||
| from typing import Any, Dict, List, Mapping, Optional, Union | ||
|
|
||
| import dpath | ||
|
|
||
| from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString | ||
| from airbyte_cdk.sources.declarative.transformations import RecordTransformation | ||
| from airbyte_cdk.sources.types import Config, StreamSlice, StreamState | ||
|
|
||
|
|
||
| @dataclass | ||
| class DpathFlattenFields(RecordTransformation): | ||
| """ | ||
| Flatten fields only for provided path. | ||
|
|
||
| field_path: List[Union[InterpolatedString, str]] path to the field to flatten. | ||
| delete_origin_value: bool = False whether to delete origin field or keep it. Default is False. | ||
|
|
||
| """ | ||
|
|
||
| config: Config | ||
| field_path: List[Union[InterpolatedString, str]] | ||
| parameters: InitVar[Mapping[str, Any]] | ||
| delete_origin_value: bool = False | ||
|
|
||
| def __post_init__(self, parameters: Mapping[str, Any]) -> None: | ||
| self._field_path = [ | ||
| InterpolatedString.create(path, parameters=parameters) for path in self.field_path | ||
| ] | ||
| for path_index in range(len(self.field_path)): | ||
| if isinstance(self.field_path[path_index], str): | ||
| self._field_path[path_index] = InterpolatedString.create( | ||
| self.field_path[path_index], parameters=parameters | ||
| ) | ||
|
|
||
| def transform( | ||
| self, | ||
| record: Dict[str, Any], | ||
| config: Optional[Config] = None, | ||
| stream_state: Optional[StreamState] = None, | ||
| stream_slice: Optional[StreamSlice] = None, | ||
| ) -> None: | ||
| path = [path.eval(self.config) for path in self._field_path] | ||
| if "*" in path: | ||
| matched = dpath.values(record, path) | ||
| extracted = matched[0] if matched else None | ||
| else: | ||
| extracted = dpath.get(record, path, default=[]) | ||
|
|
||
|
darynaishchenko marked this conversation as resolved.
|
||
| if isinstance(extracted, dict): | ||
| conflicts = set(extracted.keys()) & set(record.keys()) | ||
| if not conflicts: | ||
| if self.delete_origin_value: | ||
| dpath.delete(record, path) | ||
| record.update(extracted) | ||
110 changes: 110 additions & 0 deletions
110
unit_tests/sources/declarative/transformations/test_dpath_flatten_fields.py
This file contains hidden or 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,110 @@ | ||
| import pytest | ||
|
|
||
| from airbyte_cdk.sources.declarative.transformations.dpath_flatten_fields import DpathFlattenFields | ||
|
|
||
| _ANY_VALUE = -1 | ||
| _DELETE_ORIGIN_VALUE = True | ||
| _DO_NOT_DELETE_ORIGIN_VALUE = False | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| [ | ||
| "input_record", | ||
| "config", | ||
| "field_path", | ||
| "delete_origin_value", | ||
| "expected_record", | ||
| ], | ||
| [ | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| {}, | ||
| ["field2"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}, "field3": _ANY_VALUE}, | ||
| id="flatten by dpath, don't delete origin value", | ||
| ), | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| {}, | ||
| ["field2"], | ||
| _DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field3": _ANY_VALUE}, | ||
| id="flatten by dpath, delete origin value", | ||
| ), | ||
| pytest.param( | ||
| { | ||
| "field1": _ANY_VALUE, | ||
| "field2": {"field3": {"field4": {"field5": _ANY_VALUE}}}, | ||
| }, | ||
| {}, | ||
| ["field2", "*", "field4"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| { | ||
| "field1": _ANY_VALUE, | ||
| "field2": {"field3": {"field4": {"field5": _ANY_VALUE}}}, | ||
| "field5": _ANY_VALUE, | ||
| }, | ||
| id="flatten by dpath with *, don't delete origin value", | ||
| ), | ||
| pytest.param( | ||
| { | ||
| "field1": _ANY_VALUE, | ||
| "field2": {"field3": {"field4": {"field5": _ANY_VALUE}}}, | ||
| }, | ||
| {}, | ||
| ["field2", "*", "field4"], | ||
| _DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": {}}, "field5": _ANY_VALUE}, | ||
| id="flatten by dpath with *, delete origin value", | ||
| ), | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| {"field_path": "field2"}, | ||
| ["{{ config['field_path'] }}"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}, "field3": _ANY_VALUE}, | ||
| id="flatten by dpath from config, don't delete origin value", | ||
| ), | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| {}, | ||
| ["non-existing-field"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| id="flatten by non-existing dpath, don't delete origin value", | ||
| ), | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| {}, | ||
| ["*", "non-existing-field"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}}, | ||
| id="flatten by non-existing dpath with *, don't delete origin value", | ||
| ), | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}, "field3": _ANY_VALUE}, | ||
| {}, | ||
| ["field2"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}, "field3": _ANY_VALUE}, | ||
| id="flatten by dpath, not to update when record has field conflicts, don't delete origin value", | ||
| ), | ||
| pytest.param( | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}, "field3": _ANY_VALUE}, | ||
| {}, | ||
| ["field2"], | ||
| _DO_NOT_DELETE_ORIGIN_VALUE, | ||
| {"field1": _ANY_VALUE, "field2": {"field3": _ANY_VALUE}, "field3": _ANY_VALUE}, | ||
| id="flatten by dpath, not to update when record has field conflicts, delete origin value", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_dpath_flatten_lists( | ||
| input_record, config, field_path, delete_origin_value, expected_record | ||
| ): | ||
| flattener = DpathFlattenFields( | ||
| field_path=field_path, parameters={}, config=config, delete_origin_value=delete_origin_value | ||
| ) | ||
| flattener.transform(input_record) | ||
| assert input_record == expected_record |
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.
Uh oh!
There was an error while loading. Please reload this page.