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(low-code cdk): add overwrite flag to dpath flatten #410

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

Conversation

lazebnyi
Copy link
Contributor

@lazebnyi lazebnyi commented Mar 12, 2025

Fixed: https://github.com/airbytehq/airbyte-internal-issues/issues/11937

Summary by CodeRabbit

  • New Features

    • Added an option to control record replacement during data flattening. When enabled, the transformation now fully replaces the original record with the flattened data, offering enhanced control over data processing.
  • Chores

    • Refreshed header information to reflect updated ownership details.

@Copilot Copilot bot review requested due to automatic review settings March 12, 2025 10:58
@lazebnyi
Copy link
Contributor Author

lazebnyi commented Mar 12, 2025

/autofix

Auto-Fix Job Info

This job attempts to auto-fix any linting or formating issues. If any fixes are made,
those changes will be automatically committed and pushed back to the PR.

Note: This job can only be run by maintainers. On PRs from forks, this command requires
that the PR author has enabled the Allow edits from maintainers option.

PR auto-fix job started... Check job output.

✅ Changes applied successfully.

@github-actions github-actions bot added the enhancement New feature or request label Mar 12, 2025
Copy link
Contributor

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

This PR adds a new replace_record property to the DpathFlattenFields definition in multiple parts of the code: YAML schema, model class, transformation logic, and corresponding tests. The new property allows conditional replacement of the original record during flattening. Additionally, minor cosmetic changes improve code formatting and update the copyright year.

Changes

File(s) Summary
airbyte_cdk/sources/declarative/declarative_component_schema.yaml
airbyte_cdk/sources/declarative/models/declarative_component_schema.py
Added replace_record to the DpathFlattenFields definition and model, allowing configuration of record replacement during flattening.
airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py Reformatted return statements in the create_add_fields and create_dpath_flatten_fields methods for better readability, including the new replace_record parameter.
airbyte_cdk/sources/declarative/transformations/dpath_flatten_fields.py Introduced a boolean attribute replace_record (default False) and modified the transform method to conditionally delete the original record if replace_record is true and extracted data is available.
unit_tests/sources/declarative/transformations/test_dpath_flatten_fields.py Updated test cases by parameterizing and validating the new replace_record functionality in the flattening process.
airbyte_cdk/sources/declarative/transformations/add_fields.py Updated the copyright year in the file header from 2023 to 2025.

Sequence Diagram(s)

sequenceDiagram
    participant T as DpathFlattenFields
    participant R as Record
    participant D as dpath

    Note over T: Begin transformation

    T->>T: Extract fields from R
    alt replace_record is True and extracted data exists
        T->>D: dpath.delete(R, "**")
        T->>T: Update R with extracted data
    else replace_record is False or no extracted data
        T->>T: Check for key conflicts
        alt No conflicts & delete_origin_value is True
            T->>D: dpath.delete(R, specified path)
            T->>T: Merge extracted data into R
        else
            T->>T: Merge extracted data into R without deletion
        end
    end
    Note over T: Return updated record
Loading

Possibly related PRs

Suggested reviewers

  • lazebnyi: Would you like to review these changes? wdyt?
  • maxi297: Could you please take a look at the modifications? wdyt?
  • aldogonzalez8: Would you be able to review this PR as well? wdyt?

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bad7b6f and ec241cf.

📒 Files selected for processing (4)
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1 hunks)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py (1 hunks)
  • airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py (2 hunks)
  • airbyte_cdk/sources/declarative/transformations/add_fields.py (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • airbyte_cdk/sources/declarative/transformations/add_fields.py
  • airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • airbyte_cdk/sources/declarative/models/declarative_component_schema.py
  • airbyte_cdk/sources/declarative/declarative_component_schema.yaml
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-amplitude' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: SDM Docker Image Build

🪧 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.
  • @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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5d82e14 and bad7b6f.

📒 Files selected for processing (7)
  • 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 (2 hunks)
  • airbyte_cdk/sources/declarative/transformations/add_fields.py (3 hunks)
  • airbyte_cdk/sources/declarative/transformations/dpath_flatten_fields.py (2 hunks)
  • unit_tests/sources/declarative/transformations/test_add_fields.py (16 hunks)
  • unit_tests/sources/declarative/transformations/test_dpath_flatten_fields.py (11 hunks)
🧰 Additional context used
🪛 GitHub Actions: Linters
airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

[error] 711-711: Argument "condition" to "AddFields" has incompatible type "str | None"; expected "str"

⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-amplitude' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: SDM Docker Image Build
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
🔇 Additional comments (19)
airbyte_cdk/sources/declarative/transformations/dpath_flatten_fields.py (3)

18-18: Documentation update aligns with new feature - nice!

I see you've added documentation for the new replace_record parameter in the class docstring. This clearly explains the purpose and default value of the parameter, which is essential for developers using this class.


26-26: Parameter declaration looks good

The replace_record parameter is properly initialized with a default value of False, which ensures backward compatibility with existing code that doesn't specify this parameter.


53-61:

❓ Verification inconclusive

The record replacement logic looks clear and efficient

The implementation handles the record replacement nicely. I like how you've:

  1. First checked if replace_record is enabled and extracted has content
  2. Used dpath.delete(record, "**") to clear all fields in the record
  3. Preserved the existing conflict-checking logic for the non-replacement case

I'm curious though - have you considered handling potential edge cases where extracted might contain unexpected types? For instance, what if extracted is a list or a primitive value instead of a dict? The code assumes it's a dict in the non-replacement case.


🏁 Script executed:

#!/bin/bash
# Let's check if there are any callers that might pass non-dict values to extracted
rg -A 2 "extracted\s*=" airbyte_cdk/sources

Length of output: 1513


Record replacement logic verified – type checks are in place!

I ran a grep search and noted that before reaching this snippet, the code already ensures that extracted is a dict (using an isinstance(extracted, dict) check) before performing any update operations. It looks like returning a list or a primitive value (via defaults such as [] or from other code paths) is handled differently in the extractor logic, so the record update flow here is safe.

  • The previous check guarantees that only dicts reach this update logic.
  • The default value and guard ensure non-dict types are processed elsewhere.

Would you consider documenting this intentional safeguard for clarity, or do you feel additional checks aren’t necessary? wdyt?

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

2275-2278: Schema definition for new property is well structured

The replace_record property has been properly defined in the schema with:

  • Clear title
  • Detailed description that matches the code documentation
  • Correct type (boolean)
  • Default value that matches the code implementation

This ensures consistency between the schema and implementation, which is excellent for maintainability.

airbyte_cdk/sources/declarative/transformations/add_fields.py (3)

90-90: New condition parameter looks good

I see you've added a condition string parameter with an empty default value. This is a nice enhancement that allows for conditional field addition, and the empty default maintains backward compatibility.


96-98: InterpolatedBoolean initialization looks correct

Creating the _filter_interpolator in __post_init__ matches the pattern used elsewhere in the codebase. The instance is correctly initialized with the condition and parameters.


141-143: Condition evaluation logic is clean and efficient

The implementation:

  1. First checks if the condition is empty (fast path)
  2. Only evaluates the interpolator when needed
  3. Passes the necessary context (config, value, and **kwargs)

This approach is efficient and maintains the expected behavior for existing code. Good work!

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

880-884: Great addition of the replace_record option to enhance flattening control!

This new flag gives users more flexibility in how they handle data transformation. The default value of None (which I assume defaults to False in the implementation) ensures backward compatibility with existing configurations.


1468-1477: Nice implementation of conditional field addition!

The condition parameter will make the AddFields transformation much more flexible. The examples provided in the description are helpful for users to understand how to use this feature. The default empty string value maintains backward compatibility with existing configurations.

unit_tests/sources/declarative/transformations/test_add_fields.py (4)

14-15: Good update to include condition parameter in the test signature.

You've properly updated the test parameter list to include the new condition parameter. This ensures the tests will validate the new functionality.


161-178: Excellent test cases for the new condition functionality!

These test cases clearly validate both the positive and negative conditions, ensuring that fields are only added when the condition evaluates to True. This provides good coverage for the new feature.


185-186: Good addition of condition parameter to the test function signature.

The function signature is properly updated to accept the new parameter.


193-195: Properly passing the condition parameter to the AddFields transformation.

You've correctly updated the AddFields initialization to include the new condition parameter, ensuring it's passed through to the transformation logic.

unit_tests/sources/declarative/transformations/test_dpath_flatten_fields.py (5)

7-9: Good addition of constants for the replace_record functionality.

Adding these constants makes the test cases more readable and maintainable. They clearly indicate the purpose of the boolean values.


18-19: Well-structured update to include replace_record in the parameter list.

You've properly updated the parameter list to include the new functionality.


113-130: Great test cases to validate the replace_record functionality!

These new test cases specifically test the behavior when replace_record is True, ensuring that:

  1. When replace_record is True, only the flattened fields remain in the output
  2. The value of delete_origin_value doesn't affect the result when replace_record is True

This comprehensively covers the new functionality.


134-135: Good update to the test function signature.

You've correctly updated the function signature to include the new replace_record parameter.


136-142: Properly passing the replace_record parameter to DpathFlattenFields.

The instantiation of DpathFlattenFields now includes the new parameter, ensuring it's used in the transformation process.

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

739-750: Good implementation of the replace_record feature

The new replace_record parameter is properly implemented with a default value when it's None. This parameter aligns with the PR objective of adding an "overwrite" flag to the dpath flatten functionality.

@lazebnyi
Copy link
Contributor Author

lazebnyi commented Mar 12, 2025

/autofix

Auto-Fix Job Info

This job attempts to auto-fix any linting or formating issues. If any fixes are made,
those changes will be automatically committed and pushed back to the PR.

Note: This job can only be run by maintainers. On PRs from forks, this command requires
that the PR author has enabled the Allow edits from maintainers option.

PR auto-fix job started... Check job output.

✅ Changes applied successfully.

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.

1 participant