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): added flatten_lists option to FlattenFields transformation #206

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

Conversation

darynaishchenko
Copy link
Contributor

@darynaishchenko darynaishchenko commented Jan 8, 2025

Problem

FlattenFields transformation updates array fields by default
from {"array": [1, 2, 3]}
to {"array.1": 1, "array.2": 2, "array.3": 3}
(for more examples see unit_tests/sources/declarative/transformations/test_flatten_fields.py)

Some connectors (e.g airtable) don't need such transformation of arrays, we just need to keep it as is.

Solution

Added flatten_lists to FlattenFields(default is True) to be able to set up needed behavior of the transformation.

Summary by CodeRabbit

  • New Features

    • Introduced an optional flatten_lists configuration to control list flattening behavior during data transformations.
    • Default behavior remains unchanged (lists will be flattened).
    • Users can now choose to preserve original list structures if needed.
  • Tests

    • Enhanced test cases to verify the new flatten_lists configuration option, including scenarios for both True and False settings.

@darynaishchenko darynaishchenko self-assigned this Jan 8, 2025
@github-actions github-actions bot added the enhancement New feature or request label Jan 8, 2025
Copy link
Contributor

coderabbitai bot commented Jan 8, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new flatten_lists property to the FlattenFields transformation in the Airbyte CDK. This property allows users to control whether list structures should be flattened during record processing. By default, lists will continue to be flattened (set to True), but users can now optionally preserve the original list structure by setting flatten_lists to False.

Changes

File Change Summary
airbyte_cdk/sources/declarative/declarative_component_schema.yaml Added flatten_lists boolean property to FlattenFields definition
airbyte_cdk/sources/declarative/models/declarative_component_schema.py Added optional flatten_lists field to FlattenFields class
airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py Updated create_flatten_fields method to pass flatten_lists parameter
airbyte_cdk/sources/declarative/transformations/flatten_fields.py Added flatten_lists attribute to control list flattening behavior
unit_tests/sources/declarative/transformations/test_flatten_fields.py Updated test cases to include flatten_lists parameter

Sequence Diagram

sequenceDiagram
    participant User
    participant FlattenFields
    participant Record

    User->>FlattenFields: Configure flatten_lists
    alt flatten_lists is True
        FlattenFields->>Record: Flatten list structures
    else flatten_lists is False
        FlattenFields->>Record: Preserve original list structure
    end
Loading

Possibly Related PRs

Suggested Reviewers

  • maxi297
  • aldogonzalez8
  • darynaishchenko

Hey there! 👋 I noticed you've added a really neat configuration option for list flattening. Quick question: have you considered adding some documentation about the new flatten_lists parameter to help users understand when they might want to set it to False? 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 ec5eb16 and 87bcbee.

📒 Files selected for processing (1)
  • airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • airbyte_cdk/sources/declarative/parsers/model_to_component_factory.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)

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: 1

🧹 Nitpick comments (2)
airbyte_cdk/sources/declarative/transformations/flatten_fields.py (1)

14-14: Consider adding docstring for the new parameter?

The new flatten_lists parameter could benefit from a docstring explaining its purpose and default behavior, wdyt? For example:

"""
:param flatten_lists: Whether to flatten list fields into separate fields with numeric indices.
                     If True (default), a list [1, 2, 3] becomes {"0": 1, "1": 2, "2": 3}.
                     If False, lists are preserved in their original form.
"""
airbyte_cdk/sources/declarative/declarative_component_schema.yaml (1)

1883-1887: The implementation looks good! Would you consider enhancing the documentation? wdyt?

The property is well-structured and maintains backward compatibility. Consider adding more detailed documentation with examples to help users understand the behavior:

       flatten_lists:
         title: Flatten Lists
-        description: Whether to flatten lists or leave it as is. Default is True.
+        description: |
+          Controls whether array fields should be flattened during transformation. When set to true (default),
+          arrays are flattened into dot-notation fields (e.g., {"array": [1, 2]} becomes {"array.1": 1, "array.2": 2}).
+          When set to false, arrays remain unchanged, which is useful for connectors like Airtable that require
+          preserving array structures.
+        examples:
+          - true   # Flattens arrays: {"array": [1, 2]} -> {"array.1": 1, "array.2": 2}
+          - false  # Preserves arrays: {"array": [1, 2]} -> {"array": [1, 2]}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3ee710d and f468844.

📒 Files selected for processing (5)
  • 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 (1 hunks)
  • airbyte_cdk/sources/declarative/transformations/flatten_fields.py (2 hunks)
  • unit_tests/sources/declarative/transformations/test_flatten_fields.py (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: Linters
airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

[error] 636-636: Argument "flatten_lists" to "FlattenFields" has incompatible type "bool | None"; expected "bool"

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
🔇 Additional comments (3)
airbyte_cdk/sources/declarative/transformations/flatten_fields.py (1)

44-44: LGTM! Clean implementation of the conditional list flattening

The addition of and self.flatten_lists condition is elegant and maintains backward compatibility.

unit_tests/sources/declarative/transformations/test_flatten_fields.py (1)

53-83: LGTM! Great test coverage

The new test cases thoroughly cover both simple and nested list scenarios when flatten_lists=False. Particularly good to see the mixed case where some fields are flattened while lists are preserved.

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

726-730: LGTM! Well-documented schema field

The flatten_lists field is properly defined with clear description and correct default value.

@darynaishchenko
Copy link
Contributor Author

darynaishchenko commented Jan 8, 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.

Copy link
Contributor

@maxi297 maxi297 left a comment

Choose a reason for hiding this comment

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

I really like the exhaustive test cases. I'll approve even regardless of some nit comments

[
({"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Doe"}),
({"123Number": 123, "456Another123": 456}, {"123Number": 123, "456Another123": 456}),
(True, {"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Doe"}),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really attached to this but I'm wondering what is your opinion on this. It is becoming harder and harder when we have a lot of cases to relate to which parameter is what. Do you think it would be overkill to have _FLATTEN_LISTS = True and _DO_NOT_FLATTEN_LISTS = False and use them here for extra clarity? Or who it be better to have ids for each test cases? Or should we do both?

@@ -11,6 +11,8 @@

@dataclass
class FlattenFields(RecordTransformation):
flatten_lists: bool = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make this a private fields by prefixing this by _?

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