Skip to content
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

Airbyte CDK: add filter to RemoveFields #35326

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,8 @@ definitions:
type:
type: string
enum: [RemoveFields]
condition:
artem1205 marked this conversation as resolved.
Show resolved Hide resolved
type: string
field_pointers:
title: Field Paths
description: Array of paths defining the field to remove. Each item is an array whose field describe the path of a field to remove.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ class RemoveFields(BaseModel):
examples=[['tags'], [['content', 'html'], ['content', 'plain_text']]],
title='Field Paths',
)
condition: Optional[str] = Field(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this file modified manually? we should run gradle :airbyte-cdk:python:build or airbyte-cdk/python/bin/generate-component-manifest-files.sh to generate it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, btw.

'',
description='The predicate to filter a property value. Property will be removed if evaluated to True.',
examples=[
"{{ property|string == '' }}",
],
)


class RequestPath(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def create_record_selector(

@staticmethod
def create_remove_fields(model: RemoveFieldsModel, config: Config, **kwargs: Any) -> RemoveFields:
return RemoveFields(field_pointers=model.field_pointers, parameters={})
return RemoveFields(field_pointers=model.field_pointers, condition=model.condition or "", parameters={})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a unit test where condition is "" to verify nothing is filtered out by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added


def create_selective_authenticator(self, model: SelectiveAuthenticatorModel, config: Config, **kwargs: Any) -> DeclarativeAuthenticator:
authenticators = {name: self._create_component_from_model(model=auth, config=config) for name, auth in model.authenticators.items()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import dpath.exceptions
import dpath.util
from airbyte_cdk.sources.declarative.interpolation.interpolated_boolean import InterpolatedBoolean
from airbyte_cdk.sources.declarative.transformations import RecordTransformation
from airbyte_cdk.sources.declarative.types import Config, FieldPointer, StreamSlice, StreamState

Expand Down Expand Up @@ -40,6 +41,10 @@ class RemoveFields(RecordTransformation):

field_pointers: List[FieldPointer]
parameters: InitVar[Mapping[str, Any]]
condition: str = ""

def __post_init__(self, parameters: Mapping[str, Any]) -> None:
self._filter_interpolator = InterpolatedBoolean(condition=self.condition, parameters=parameters)

def transform(
self,
Expand All @@ -55,7 +60,11 @@ def transform(
for pointer in self.field_pointers:
# the dpath library by default doesn't delete fields from arrays
try:
dpath.util.delete(record, pointer)
dpath.util.delete(
artem1205 marked this conversation as resolved.
Show resolved Hide resolved
record,
pointer,
afilter=(lambda x: self._filter_interpolator.eval(config or {}, property=x)) if self.condition else None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is afilter a typo, should it be filter? I don't fully understand the details, so I might be wrong on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's just what the parameter is called in the dpath library https://pypi.org/project/dpath/

)
except dpath.exceptions.PathNotFound:
# if the (potentially nested) property does not exist, silently skip
pass
Expand Down
Loading