Skip to content

feat(ibis): query cache with connection url #1162

Merged
goldmedal merged 2 commits intoCanner:mainfrom
douenergy:query-cache-with-connection-url
Apr 25, 2025
Merged

feat(ibis): query cache with connection url #1162
goldmedal merged 2 commits intoCanner:mainfrom
douenergy:query-cache-with-connection-url

Conversation

@douenergy
Copy link
Copy Markdown
Contributor

@douenergy douenergy commented Apr 25, 2025

We can support query cache from connection info using connection URL.

Summary by CodeRabbit

  • Bug Fixes

    • Improved cache key generation to support both connection URLs and traditional connection info, ensuring correct caching behavior for different connection types.
  • Tests

    • Added tests to verify query caching and cache override functionality when using connection URLs for PostgreSQL connections. These tests confirm correct cache hits, overrides, and consistent query results.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 25, 2025

Walkthrough

This change updates the internal cache key generation logic to support both traditional connection info objects and the newer ConnectionUrl type, ensuring that cache keys are generated appropriately for each case. Additionally, debug logging is added for cache key components. New asynchronous tests are introduced for both v2 and v3 PostgreSQL query routers to validate caching and cache override behavior when using connection URLs, confirming that cache hits, misses, and overrides function as expected with this connection format.

Changes

File(s) Change Summary
ibis-server/app/query_cache/init.py Modified _generate_cache_key to handle ConnectionUrl objects and added debug logging of key components.
ibis-server/tests/routers/v2/connector/test_postgres.py Added two async tests for cache enable and cache override using connection URLs in v2 PostgreSQL router.
ibis-server/tests/routers/v3/connector/postgres/test_query.py Added two async tests for cache enable and cache override using connection URLs in v3 PostgreSQL router.
ibis-server/tests/routers/v3/connector/postgres/test_fallback_v2.py Added three async tests for basic query, cache enable, and cache override using connection URLs in v3 PostgreSQL fallback router.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Router
    participant QueryCacheManager
    participant Database

    Client->>Router: POST query with connection URL (cacheEnable=true)
    Router->>QueryCacheManager: Generate cache key (detects ConnectionUrl)
    QueryCacheManager->>QueryCacheManager: Check cache for key
    alt Cache miss
        QueryCacheManager->>Database: Execute query
        Database-->>QueryCacheManager: Query result
        QueryCacheManager->>QueryCacheManager: Store result in cache
        QueryCacheManager-->>Router: Return result (X-Cache-Hit: false)
    else Cache hit
        QueryCacheManager-->>Router: Return cached result (X-Cache-Hit: true)
    end
    Router-->>Client: Respond with results and cache headers
Loading
sequenceDiagram
    participant Client
    participant Router
    participant QueryCacheManager
    participant Database

    Client->>Router: POST query with connection URL (cacheEnable=true, overrideCache=true)
    Router->>QueryCacheManager: Generate cache key (detects ConnectionUrl)
    QueryCacheManager->>Database: Execute query (force bypass cache)
    Database-->>QueryCacheManager: Query result
    QueryCacheManager->>QueryCacheManager: Update cache with new result
    QueryCacheManager-->>Router: Return result (X-Cache-Override: true)
    Router-->>Client: Respond with results and override headers
Loading

Possibly related PRs

Suggested reviewers

  • goldmedal

Poem

In the cache where queries dwell,
A new key’s forged, a secret spell—
URLs or info, both now fit,
The cache decides where data’ll sit.
Tests hop along to check each case,
Ensuring cache is in its place!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@github-actions github-actions bot added ibis python Pull requests that update Python code labels Apr 25, 2025
@douenergy douenergy requested a review from goldmedal April 25, 2025 03:42
Copy link
Copy Markdown
Contributor

@goldmedal goldmedal left a comment

Choose a reason for hiding this comment

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

Could you add the case for the fallback test? Others make sense to me.

Copy link
Copy Markdown
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 (2)
ibis-server/tests/routers/v3/connector/postgres/test_fallback_v2.py (2)

111-121: Basic connection URL query test looks good, but should validate result data

The test successfully verifies that queries can be executed using the connection URL format. However, unlike the parallel test_query function, this test doesn't validate the structure and content of the returned data.

Consider extending this test to validate the response data similar to the original test:

async def test_query_with_connection_url(client, manifest_str, connection_url):
    response = await client.post(
        url=f"{base_url}/query",
        json={
            "connectionInfo": {"connectionUrl": connection_url},
            "manifestStr": manifest_str,
            "sql": "SELECT orderkey FROM orders LIMIT 1",
        },
    )
    assert response.status_code == 200
+    result = response.json()
+    assert len(result["columns"]) == 1
+    assert len(result["data"]) == 1

123-155: Cache hit/miss test with connection URL looks good

This test correctly validates the cache functionality with connection URLs, properly checking for cache misses on first requests, hits on subsequent requests, and verifying result consistency.

However, one minor inconsistency compared to the original test_query_with_cache function:

Consider adding the timestamp validation that exists in the original test:

    assert response2.status_code == 200
    assert response2.headers["X-Cache-Hit"] == "true"
+    assert int(response2.headers["X-Cache-Create-At"]) > 1743984000  # 2025.04.07
    result2 = response2.json()
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4a4282d and a7ab84e.

📒 Files selected for processing (2)
  • ibis-server/app/query_cache/__init__.py (2 hunks)
  • ibis-server/tests/routers/v3/connector/postgres/test_fallback_v2.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ibis-server/app/query_cache/init.py
🧰 Additional context used
🧬 Code Graph Analysis (1)
ibis-server/tests/routers/v3/connector/postgres/test_fallback_v2.py (4)
ibis-server/tests/routers/v3/connector/postgres/test_query.py (4)
  • test_query_with_connection_url (202-216)
  • manifest_str (96-97)
  • test_query_with_connection_url_and_cache_enable (219-249)
  • test_query_with_connection_url_and_cache_override (252-279)
ibis-server/tests/conftest.py (1)
  • client (18-23)
ibis-server/tests/routers/v3/connector/postgres/test_model_substitute.py (1)
  • manifest_str (46-47)
ibis-server/tests/routers/v3/connector/postgres/conftest.py (1)
  • connection_url (48-50)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: ci
🔇 Additional comments (2)
ibis-server/tests/routers/v3/connector/postgres/test_fallback_v2.py (2)

157-185: Cache override test with connection URL is well implemented

The test correctly validates that cache override functionality works with connection URLs. It properly verifies the override flag is set and that timestamps are logically consistent.


111-185:

Details

❓ Verification inconclusive

Verify connection URL and connection info caching are equivalent

The tests you've added ensure that connection URL-based queries work with the caching system, but it's worth verifying that both connection methods produce the same cache key for equivalent connections.


🏁 Script executed:

#!/bin/bash
# Find where cache keys are generated from connection info
echo "Searching for cache key generation code..."
rg -A 10 "def get_cache_key" --type py

# Look for connection URL handling
echo "Searching for connection URL handling in cache code..."
rg -A 5 "connectionUrl" --type py

Length of output: 13901


Ensure cache key parity for connectionUrl and structured connectionInfo
We still need to confirm that both ways of specifying a connection produce the exact same cache key. Since no get_cache_key definition surfaced in the initial search, please:

  • Locate the function responsible for computing cache keys (e.g. in your cache or router modules—often under ibis-server/app/cache or alongside your query handlers).
  • Add a small unit test that:
    • Constructs a PostgresConnectionInfo model (with host, port, user, password, database).
    • Builds the equivalent connectionUrl string.
    • Invokes the cache‐key function on both inputs and asserts the returned keys are identical.

This will guarantee that caching is consistent regardless of how the client supplies connection details.

Copy link
Copy Markdown
Contributor

@goldmedal goldmedal left a comment

Choose a reason for hiding this comment

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

Thanks @douenergy 👍

@goldmedal goldmedal merged commit 1693c3b into Canner:main Apr 25, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ibis python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants