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

fix(low-code cdk pagination): Fix the offset strategy so that it resets back to 0 when a stream is an incremental data feed #202

Merged
merged 3 commits into from
Jan 6, 2025

Conversation

brianjlai
Copy link
Contributor

@brianjlai brianjlai commented Jan 6, 2025

Fixes https://github.com/airbytehq/oncall/issues/7210

What

We were observing this error for low-code connectors using the offset pagination strategy:

  File "/home/airbyte/.pyenv/versions/3.10.15/lib/python3.10/site-packages/airbyte_cdk/sources/declarative/requesters/paginators/strategies/offset_increment.py", line 87, in reset
    raise ValueError(
ValueError: Reset value None for OffsetIncrement pagination strategy was not an integer

How

After investigating this a bit, I realized that this was only happening when we used an incremental stream where is_data_feed was enabled.

And that is because the StopConditionPaginationStrategyDecorator that gets instantiated for data feeds is not implemented correctly for reseting back to 0. We accept optional values, but the problem is that the decorator can still accidentally pass in None when the surrounding reset() passes None. Instead we need to call reset() with no incoming parameter. I also fixed this in the OffsetPaginationStrategy as well just to cover all our bases.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced pagination strategies with more flexible reset behaviors
    • Added support for stop page conditions in data feed pagination
  • Tests

    • Introduced new test for data feed paginator with stop page condition
    • Simplified offset increment reset test logic
  • Bug Fixes

    • Improved handling of reset values in pagination strategies
    • Added support for None reset values in offset increment and stop condition strategies

@brianjlai brianjlai changed the title fix(low-code cdk pagination) Fix the offset strategy so that it resets back to 0 when a stream is an incremental data feed fix(low-code cdk pagination): Fix the offset strategy so that it resets back to 0 when a stream is an incremental data feed Jan 6, 2025
Copy link
Contributor

coderabbitai bot commented Jan 6, 2025

Warning

Rate limit exceeded

@brianjlai has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 32 minutes and 35 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 356e321 and 62c707f.

📒 Files selected for processing (1)
  • unit_tests/sources/declarative/requesters/paginators/test_stop_condition.py (1 hunks)
📝 Walkthrough

Walkthrough

The pull request introduces modifications to pagination strategies in the Airbyte CDK, focusing on enhancing the flexibility of reset methods in the OffsetIncrement and StopConditionPaginationStrategyDecorator classes. The changes allow for more nuanced handling of reset values, particularly when dealing with None values. A new test case is also added to validate the behavior of a paginator with a stop page condition, expanding the test coverage for these pagination mechanisms.

Changes

File Change Summary
airbyte_cdk/sources/declarative/requesters/paginators/strategies/offset_increment.py Modified reset method to handle None reset values by setting offset to 0
airbyte_cdk/sources/declarative/requesters/paginators/strategies/stop_condition.py Updated reset method to conditionally call delegate reset based on reset value
unit_tests/sources/declarative/requesters/paginators/test_default_paginator.py Added new test function test_data_feed_paginator_with_stop_page_condition
unit_tests/sources/declarative/requesters/paginators/test_offset_increment.py Simplified test_offset_increment_reset function logic

Sequence Diagram

sequenceDiagram
    participant Paginator
    participant OffsetIncrement
    participant StopCondition

    Paginator->>OffsetIncrement: reset(reset_value)
    alt reset_value is None
        OffsetIncrement-->>Paginator: Set offset to 0
    else reset_value is not an integer
        OffsetIncrement--xPaginator: Raise ValueError
    end

    Paginator->>StopCondition: reset(reset_value)
    alt reset_value is provided
        StopCondition->>StopCondition: Call delegate reset with value
    else reset_value is None
        StopCondition->>StopCondition: Call delegate reset without value
    end
Loading

What do you think about these changes? They seem to add more flexibility to how reset values are handled! Wdyt? The sequence diagram might help visualize the new flow. Cheers! 🚀


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/requesters/paginators/strategies/stop_condition.py (1)

56-59: Consider using a more concise implementation?

The current implementation works correctly, but we could make it more concise using Python's optional parameter handling, wdyt?

-        if reset_value:
-            self._delegate.reset(reset_value)
-        else:
-            self._delegate.reset()
+        self._delegate.reset(reset_value) if reset_value is not None else self._delegate.reset()
airbyte_cdk/sources/declarative/requesters/paginators/strategies/offset_increment.py (1)

Line range hint 86-92: How about improving the error message?

The error message could be more descriptive to help users understand the valid input range, wdyt?

         if reset_value is None:
             self._offset = 0
         elif not isinstance(reset_value, int):
             raise ValueError(
-                f"Reset value {reset_value} for OffsetIncrement pagination strategy was not an integer"
+                f"Reset value {reset_value} for OffsetIncrement pagination strategy must be either None or an integer"
             )
         else:
📜 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 366f98a.

📒 Files selected for processing (4)
  • airbyte_cdk/sources/declarative/requesters/paginators/strategies/offset_increment.py (1 hunks)
  • airbyte_cdk/sources/declarative/requesters/paginators/strategies/stop_condition.py (1 hunks)
  • unit_tests/sources/declarative/requesters/paginators/test_default_paginator.py (3 hunks)
  • unit_tests/sources/declarative/requesters/paginators/test_offset_increment.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-the-guardian-api' (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)
🔇 Additional comments (2)
unit_tests/sources/declarative/requesters/paginators/test_offset_increment.py (1)

107-108: LGTM!

The test implementation is clean and covers all edge cases including the new None value handling.

unit_tests/sources/declarative/requesters/paginators/test_default_paginator.py (1)

364-396: Add assertions to validate the test case?

The test sets up the paginator correctly but doesn't validate its behavior. Should we add some assertions to verify that:

  1. The paginator correctly stops when the cursor condition is met
  2. The offset resets to 0 when reset is called
  3. The page size is correctly applied

Here's a suggested implementation, wdyt?

    response = requests.Response()
    response._content = json.dumps({"updated_at": "2024-01-02"}).encode("utf-8")
    
    # Verify initial state
    assert paginator.get_request_params() == {"limit": 2, "offset": 0}
    
    # Verify pagination stops when cursor condition is met
    token = paginator.next_page_token(response, 2, {"updated_at": "2023-12-31"})
    assert token is None
    
    # Verify offset resets to 0
    paginator.reset()
    assert paginator.get_request_params() == {"limit": 2, "offset": 0}
✅ Verification successful

The suggested assertions look good and align with existing test patterns! 👍

The proposed assertions cover the key behaviors we typically test for paginators in the codebase:

  • Initial state validation
  • Cursor-based stop condition
  • Reset functionality
  • Page size verification

I noticed we could also add one more assertion to make it even more robust, wdyt?

# Verify pagination continues when cursor condition is not met
token = paginator.next_page_token(response, 2, {"updated_at": "2024-01-03"})
assert token == 2  # Next offset should be 2
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find test files containing DefaultPaginator tests
rg -l "DefaultPaginator" --type py "unit_tests/"

# Look for similar test patterns
rg "test.*paginator.*cursor" --type py "unit_tests/" -B 2 -A 2

# Search for assertion patterns in paginator tests
rg "assert.*paginator" --type py "unit_tests/" -B 1 -A 1

Length of output: 11753

@github-actions github-actions bot added the bug Something isn't working label Jan 6, 2025
Copy link
Contributor

@natikgadzhi natikgadzhi left a comment

Choose a reason for hiding this comment

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

Clean work. Thank you for the fix @brianjlai

@brianjlai brianjlai merged commit 884897e into main Jan 6, 2025
19 checks passed
@brianjlai brianjlai deleted the brian/fix_offset_pagination_with_data_feed branch January 6, 2025 22:44
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