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

feat: add CustomSchemaNormalization #194

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

artem1205
Copy link
Contributor

@artem1205 artem1205 commented Dec 31, 2024

What

  • add CustomSchemaNormalization to declarative schema

Summary by CodeRabbit

  • New Features

    • Introduced a custom schema normalization strategy for record selection.
    • Enhanced flexibility in handling schema normalization types.
  • Improvements

    • Updated the RecordSelector to support custom normalization approaches.
    • Expanded schema transformation capabilities with more dynamic configuration options.
  • Technical Updates

    • Refactored schema normalization handling in the component factory.
    • Simplified schema transformer type mappings.
    • Added an abstract base class for type transformation logic.

Signed-off-by: Artem Inzhyyants <[email protected]>
Signed-off-by: Artem Inzhyyants <[email protected]>
@artem1205 artem1205 self-assigned this Dec 31, 2024
@github-actions github-actions bot added the enhancement New feature or request label Dec 31, 2024
@artem1205 artem1205 changed the title feat: add CustomSchemaNormalization feat: add CustomSchemaNormalization Dec 31, 2024
Signed-off-by: Artem Inzhyyants <[email protected]>
Signed-off-by: Artem Inzhyyants <[email protected]>
Signed-off-by: Artem Inzhyyants <[email protected]>
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

a few nits and one question, otherwise seems pretty straightforward!

schema_normalization = (
TypeTransformer(SCHEMA_TRANSFORMER_TYPE_MAPPING[model.schema_normalization])
if isinstance(model.schema_normalization, SchemaNormalizationModel)
else self._create_component_from_model(model.schema_normalization, config=config) # type: ignore[arg-type] # custom normalization model expected here
Copy link
Contributor

Choose a reason for hiding this comment

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

Presumably this CustomNormalization component would need to already adhere or inherit from the TypeTransformer interface/class?

And that's why we don't need to invoke the TypeTransformer constructor?

Copy link
Contributor Author

@artem1205 artem1205 Jan 2, 2025

Choose a reason for hiding this comment

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

yes, most of the time we use customtypetransformer by registration some custom function, but generally speaking we need only transform interface, I can add abstract class for this

self, records: Iterable[Mapping[str, Any]], schema: Optional[Mapping[str, Any]]
) -> Iterable[Mapping[str, Any]]:
if schema:
# record has type Mapping[str, Any], but dict[str, Any] expected
for record in records:
normalized_record = dict(record)
self.schema_normalization.transform(normalized_record, schema)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think we need to create an interface in the declarative package for this. I don't think we want users to be re-implementing TypeTransformer for a couple reasons:

  • It looks like it has a lot of method we don't actually care about
  • We want to avoid the pattern of customers re-implementing concrete classes since it couples our internal implementation to customer usage w/ unexpected breaking changes

So we should just create a new TypeTransformer interface in low-code (i'll leave naming to you), and then I think RecordSelector.schema_normalization will need to be changed to a union of these two classes interfaces. Once we have these added will 👍 the PR. thanks for making the other adjustments!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Signed-off-by: Artem Inzhyyants <[email protected]>
Signed-off-by: Artem Inzhyyants <[email protected]>
Signed-off-by: Artem Inzhyyants <[email protected]>
@artem1205 artem1205 requested a review from brianjlai January 2, 2025 14:24
@artem1205 artem1205 marked this pull request as ready for review January 2, 2025 14:32
Copy link
Contributor

coderabbitai bot commented Jan 2, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new CustomSchemaNormalization component to the Airbyte CDK's declarative source framework. The change enhances the schema normalization capabilities by allowing custom normalization strategies alongside the existing standard approach. The modifications span multiple files in the declarative source implementation, updating the component schema, record selector, and model-to-component factory to support this new flexible normalization mechanism.

Changes

File Change Summary
airbyte_cdk/sources/declarative/declarative_component_schema.yaml Added CustomSchemaNormalization component definition and updated RecordSelector to support it.
airbyte_cdk/sources/declarative/extractors/record_selector.py Removed SCHEMA_TRANSFORMER_TYPE_MAPPING and TransformConfig import; updated schema_normalization type to include AbstractTypeTransformer.
airbyte_cdk/sources/declarative/models/declarative_component_schema.py Added CustomSchemaNormalization class and updated RecordSelector schema normalization type.
airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py Updated import statements and schema normalization handling to support new component.
airbyte_cdk/sources/declarative/extractors/__init__.py Added import for AbstractTypeTransformer and updated __all__ list.
airbyte_cdk/sources/declarative/extractors/type_transformer.py Introduced AbstractTypeTransformer class with an abstract transform method.

Sequence Diagram

sequenceDiagram
    participant RecordSelector
    participant SchemaTransformer
    participant CustomNormalization
    
    RecordSelector->>SchemaTransformer: Check normalization type
    alt Standard Normalization
        SchemaTransformer-->>RecordSelector: Apply standard normalization
    else Custom Normalization
        RecordSelector->>CustomNormalization: Instantiate custom normalization
        CustomNormalization-->>RecordSelector: Apply custom normalization strategy
    end
Loading

Possibly Related PRs

Suggested Reviewers

  • darynaishchenko
  • maxi297
  • aldogonzalez8

Hey there! 👋 I noticed a few things that might be worth discussing:

  • The removal of SCHEMA_TRANSFORMER_TYPE_MAPPING seems like a significant change. Wdyt about adding a comment explaining the rationale?
  • The new CustomSchemaNormalization looks flexible. Have you considered adding some example usage documentation?
  • The sequence diagram is a high-level representation - would you be interested in expanding on the specific interaction details?

Feel free to share your thoughts on these suggestions! 🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (8)
airbyte_cdk/sources/declarative/extractors/record_selector.py (2)

17-17: Consider clarifying the import usage.

You’re importing TypeTransformer here but it’s not referenced until much later. Would consolidating related imports help readability and keep them closer to the usage site, wdyt?


Line range hint 2012-2015: Add a safety check for unsupported modes.

In this conditional expression, we assume model.schema_normalization must be either SchemaNormalizationModel or a custom type. Would it help to explicitly handle unknown modes (e.g., raising a descriptive exception), to avoid silent misconfigurations, wdyt?

airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2)

Line range hint 187-189: Confirm the naming alignment.

We’re aliasing CustomSchemaNormalization to CustomSchemaNormalizationModel here. Would it be clearer if we kept consistent naming across all imports, e.g., dropping “Model” to preserve brevity and clarity, wdyt?


1532-1536: Question about the default.

The Field default for schema_normalization is set to SchemaNormalization.None_. Did you intend to freely toggle between None and a custom transformer? If so, do you think an explicit None might be clearer, wdyt?

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py (2)

500-500: Request more clarify around custom component creation.

You’re adding CustomSchemaNormalizationModel to create_custom_component. Could we ensure it enforces an interface like TypeTransformer? This might reduce future confusion, wdyt?


2012-2015: Evaluate future extension for merging transformations.

TypeTransformer or a CustomSchemaNormalizationModel is chosen. One day we might combine standard transformations with custom transformations. Is that something you’d consider supporting in your logic, wdyt?

airbyte_cdk/sources/declarative/declarative_component_schema.yaml (2)

670-691: Alphabetical order for consistency.

We introduced CustomSchemaNormalization near line 670. Could we keep alphabetical ordering (like other custom components) to make searching simpler, wdyt?


2580-2584: Improve clarity around “anyOf” usage.

We allow either a standard SchemaNormalization or CustomSchemaNormalization in RecordSelector. Would clarifying in the schema that “None” is the baseline default help reduce confusion for integrators, wdyt?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8cb659 and e7a6c0c.

📒 Files selected for processing (4)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (2 hunks)
  • airbyte_cdk/sources/declarative/extractors/record_selector.py (1 hunks)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2 hunks)
  • airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py (6 hunks)
🔇 Additional comments (2)
airbyte_cdk/sources/declarative/models/declarative_component_schema.py (1)

Line range hint 100-102: Validate usage of CustomStateMigration.

Would it be useful to verify that CustomStateMigration indeed inherits from a recognized migration interface or base class to prevent runtime errors, wdyt?

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py (1)

447-451: Suggest verifying mapping coverage.

You’ve reintroduced SCHEMA_TRANSFORMER_TYPE_MAPPING for SchemaNormalizationModel. Would it be safer to confirm all possible enum values in SchemaNormalizationModel are covered here to avoid mismatches, wdyt?

Signed-off-by: Artem Inzhyyants <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
airbyte_cdk/sources/declarative/extractors/type_transformer.py (2)

10-11: Using @DataClass on an abstract class
Seems elegant. Are you sure you need dataclass features for an ABC that currently has no fields? If a future extension is planned, this is fine, otherwise a simple ABC might suffice. wdyt?


35-55: Potential return of the transformed record
Currently, the method does not return a record, but modifies it in place. Would returning a new record be clearer, or is in-place mutation the intended design? wdyt?

airbyte_cdk/sources/declarative/extractors/record_selector.py (1)

13-13: Importing AbstractTypeTransformer
Great to see the usage of AbstractTypeTransformer. Any thoughts on adding a brief mention in the class docstring to clarify that both TypeTransformer and AbstractTypeTransformer can be provided? wdyt?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e7a6c0c and f787b6d.

📒 Files selected for processing (3)
  • airbyte_cdk/sources/declarative/extractors/__init__.py (1 hunks)
  • airbyte_cdk/sources/declarative/extractors/record_selector.py (2 hunks)
  • airbyte_cdk/sources/declarative/extractors/type_transformer.py (1 hunks)
🔇 Additional comments (3)
airbyte_cdk/sources/declarative/extractors/__init__.py (2)

12-12: Good addition of AbstractTypeTransformer import.
Nice step to unify type transformation strategies. Would it be helpful to add a short comment here indicating its primary usage? wdyt?


15-15: Exporting AbstractTypeTransformer in all
This makes it publicly available, which is great. Might be worth ensuring that external users are guided toward this new abstraction. wdyt?

airbyte_cdk/sources/declarative/extractors/record_selector.py (1)

37-37: Union type for schema_normalization
Allowing both TypeTransformer and AbstractTypeTransformer is flexible. Are there any potential pitfalls with inconsistent method signatures between them? Maybe reinforcing type hints or documentation could help. wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants