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

Merged
merged 14 commits into from
Jan 9, 2025

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.
    • Added an abstract base class for type transformation logic.
    • Made TypeTransformer available for external use in the module interface.

Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
@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 <artem.inzhyyants@gmail.com>
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
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!

Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
@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 Union[TypeTransformer, DeclarativeTypeTransformer].
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 TypeTransformer and updated __all__ list.
airbyte_cdk/sources/declarative/extractors/type_transformer.py Introduced TypeTransformer 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! 🚀

Finishing Touches

  • 📝 Generate Docstrings

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 <artem.inzhyyants@gmail.com>
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?

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.

sorry about the late approval and review. just a few small things but nothing to block on

Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
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 (3)

2-2: Update the copyright year

Hey! I noticed the copyright year is set to 2025. Should we update it to 2024 since that's when this code was written? wdyt? 🤔

- # Copyright (c) 2025 Airbyte, Inc., all rights reserved.
+ # Copyright (c) 2024 Airbyte, Inc., all rights reserved.

12-33: Enhance the docstring with examples

The docstring is great! Would you mind if we added a concrete example to make it even more helpful for developers? Also, since we're using @DataClass but not defining any fields, maybe we could clarify why? Here's what I'm thinking:

     """
     Abstract base class for implementing type transformation logic.

     This class provides a blueprint for defining custom transformations
     on data records based on a provided schema. Implementing classes
     must override the `transform` method to specify the transformation
     logic.

     Attributes:
         None explicitly defined, as this is a dataclass intended to be
         subclassed.

     Methods:
         transform(record: Dict[str, Any], schema: Mapping[str, Any]) -> None:
             Abstract method that must be implemented by subclasses.
             It performs a transformation on a given data record based
             on the provided schema.

     Usage:
         To use this class, create a subclass that implements the
         `transform` method with the desired transformation logic.
+
+    Example:
+        ```python
+        @dataclass
+        class MyTransformer(TypeTransformer):
+            def transform(self, record: Dict[str, Any], schema: Mapping[str, Any]) -> None:
+                # Transform string fields to uppercase
+                for field, value in record.items():
+                    if schema.get(field, {}).get("type") == "string":
+                        record[field] = value.upper()
+        ```
+
+    Note:
+        This class is marked as a dataclass to maintain consistency with other
+        components in the declarative framework, allowing for future extension
+        with configuration fields if needed.
     """

What do you think about these additions? 🤔


41-55: Clarify in-place modification in docstring

The method docstring looks good! Would it be helpful to explicitly mention that the transformation happens in-place on the record? Something like:

         """
-        Perform a transformation on a data record based on a given schema.
+        Perform an in-place transformation on a data record based on a given schema.

         Args:
             record (Dict[str, Any]): The data record to be transformed.
             schema (Mapping[str, Any]): The schema that dictates how
                 the record should be transformed.

         Returns:
-            None
+            None: The record is modified in-place.

         Raises:
             NotImplementedError: If the method is not implemented
                 by a subclass.
         """

What are your thoughts on making this more explicit? 🤔

📜 Review details

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

📥 Commits

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

📒 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)
🚧 Files skipped from review as they are similar to previous changes (2)
  • airbyte_cdk/sources/declarative/extractors/init.py
  • airbyte_cdk/sources/declarative/extractors/record_selector.py
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Analyze (python)
🔇 Additional comments (1)
airbyte_cdk/sources/declarative/extractors/type_transformer.py (1)

10-55: Solid implementation! 👍

Really nice work on this abstract base class! The implementation is clean, well-documented, and provides a solid foundation for custom type transformers. The use of dataclass, abstract method, and type hints makes it very developer-friendly.

artem1205 and others added 2 commits January 8, 2025 21:58
Signed-off-by: Artem Inzhyyants <artem.inzhyyants@gmail.com>
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 (1)
airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1)

688-688: Consider using a more generic example for the class_name?

The current example is specific to Amazon Seller Partner. For better documentation, we could use a more generic example like source_<name>.components.CustomTypeTransformer, wdyt?

-          - "source_amazon_seller_partner.components.LedgerDetailedViewReportsTypeTransformer"
+          - "source_<name>.components.CustomTypeTransformer"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f9d2212 and 049370d.

📒 Files selected for processing (3)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (2 hunks)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py (2 hunks)
  • airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py (6 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Pytest (Fast)
🔇 Additional comments (4)
airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1)

2625-2629: LGTM! Clean integration with existing schema normalization.

The schema changes for RecordSelector are well-structured, maintaining backward compatibility while adding support for custom normalization.

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

100-102: LGTM! Well-organized imports.

The new imports are properly organized and follow the existing pattern.

Also applies to: 187-189, 316-318


453-456: LGTM! Clear and concise mapping.

The schema transformer mapping is straightforward and handles both normalization cases appropriately.


2031-2034: LGTM! Implementation aligns with the discussed requirements.

The schema normalization handling correctly implements both standard and custom normalization cases, following the pattern discussed in the previous reviews.

@artem1205 artem1205 merged commit 3c76ef3 into main Jan 9, 2025
16 of 20 checks passed
@artem1205 artem1205 deleted the artem1205/add-custom-type-transformer branch January 9, 2025 14:03
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