Skip to content

Conversation

lazebnyi
Copy link
Contributor

@lazebnyi lazebnyi commented Mar 12, 2025

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

This PR updates set_initial_state to handle cases where child-to-parent state migration is skipped when the parent state looks like:

"parent_state": {
    "parent_stream_name": {
        "parent_stream_cursor": ""
    }
}

Summary by CodeRabbit

  • Refactor
    • Streamlined the process for managing initialization and migration of state data, resulting in a more robust and efficient workflow.

@Copilot Copilot AI review requested due to automatic review settings March 12, 2025 15:55
@github-actions github-actions bot added the bug Something isn't working label Mar 12, 2025
Copy link
Contributor

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

This pull request modifies the set_initial_state method in the SubstreamPartitionRouter class to simplify the logic for migrating child stream state to parent stream state format. The previous implementation had separate checks for the absence of parent_state and incremental dependencies. The updated logic consolidates these checks within the loop iterating over parent_stream_configs, allowing for direct migration when the parent_state is absent and an incremental dependency exists. No public or exported entities have been altered.

Changes

File(s) Change Summary
airbyte_cdk/.../substream_partition_router.py Updated set_initial_state: removed the early return check; now directly migrates child state to parent state format within the loop when a parent state is missing with incremental dependency

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Caller
    participant Router as SubstreamPartitionRouter
    participant ParentConf as ParentStreamConfig
    Caller->>Router: set_initial_state(child_state)
    Router->>ParentConf: Iterate over parent_stream_configs
    alt Parent state is absent and stream has incremental dependency
        Router->>Router: Migrate child state to parent state
    else
        Router-->>ParentConf: Skip migration
    end
    Router->>Caller: Return updated state
Loading

Possibly related PRs

Suggested reviewers

  • darynaishchenko – Would you like to add darynaishchenko as a reviewer, wdyt?
  • aldogonzalez8 – Would you also like to include aldogonzalez8 in the review, 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 c4ca314 and 530a787.

📒 Files selected for processing (1)
  • airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • 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 (All, Python 3.10, Ubuntu)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: SDM Docker Image Build
  • GitHub Check: Analyze (python)

🪧 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: 0

🔭 Outside diff range comments (1)
airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py (1)

1-376: 💡 Verification agent

🧩 Analysis chain

Format code using Ruff

The pipeline is failing because of a formatting issue. Please run poetry run ruff format to fix code style issues in this file.


🏁 Script executed:

#!/bin/bash
# Just displaying the linter error for reference
echo "Ruff formatting check failed. 1 file would be reformatted."
echo "Please run 'poetry run ruff format' to fix code style issues in this file."

Length of output: 291


Fix code formatting using Ruff

It looks like the linter output confirms that one file would be reformatted. Could you please run poetry run ruff format on the file:

  • airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py (lines 1-376)

to resolve the formatting issue in the pipeline? wdyt?

🧰 Tools
🪛 GitHub Actions: Linters

[error] 1-1: Ruff formatting check failed. 1 file would be reformatted. Please run 'poetry run ruff format' to fix code style issues in this file.

🧹 Nitpick comments (1)
airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py (1)

285-288: Implementation looks more targeted and handles edge cases better

This change improves the handling of the state migration for parent streams. By checking for empty parent state and incremental dependency together in the loop, you're ensuring that child state migration happens only when needed for specific parent streams.

One potential concern: Since _migrate_child_state_to_parent_state returns a complete parent state dictionary for all streams, assigning this to parent_state repeatedly in the loop could potentially cause overwriting if multiple parent streams qualify for migration. But looking at line 342-346, the method builds parent state for all configurations at once, so this might not be an issue. Would you consider storing the migrated state once outside the loop for clarity, or is the current approach intentional? wdyt?

 # Set state for each parent stream with an incremental dependency
 for parent_config in self.parent_stream_configs:
-    if not parent_state.get(parent_config.stream.name, {}) and parent_config.incremental_dependency:
-        # Migrate child state to parent state format
-        parent_state = self._migrate_child_state_to_parent_state(stream_state)
+    # Check if any parent stream needs migration
+    needs_migration = any(
+        not parent_state.get(config.stream.name, {}) and config.incremental_dependency
+        for config in self.parent_stream_configs
+    )
+    if needs_migration:
+        # Migrate child state to parent state format - only once
+        parent_state = self._migrate_child_state_to_parent_state(stream_state)
+        break
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 741a2a0 and c4ca314.

📒 Files selected for processing (1)
  • airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Linters
airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py

[error] 1-1: Ruff formatting check failed. 1 file would be reformatted. Please run 'poetry run ruff format' to fix code style issues in this file.

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • 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 (Fast)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: SDM Docker Image Build
  • GitHub Check: Analyze (python)

@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.

@lazebnyi lazebnyi requested review from maxi297 and bazarnov March 13, 2025 19:59
@lazebnyi lazebnyi merged commit 591a079 into main Mar 13, 2025
26 checks passed
@lazebnyi lazebnyi deleted the lazebnyi/fix-initial-state-for-partition-router branch March 13, 2025 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants