Fix SnowflakeConnection.query() cache key to include params#13652
Fix SnowflakeConnection.query() cache key to include params#13652sfc-gh-kmcgrady merged 2 commits intodevelopfrom
Conversation
The params argument was captured as a closure variable instead of being passed as an explicit parameter to the cached _query function. This caused different parameter values to incorrectly share the same cache entry. Now params is an explicit parameter so st.cache_data includes it in the cache key. Fixes #13644 Co-Authored-By: Claude <[email protected]>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical caching bug in SnowflakeConnection.query() where the params argument was captured as a closure variable instead of being an explicit parameter to the cached _query function. This caused queries with different parameter values to incorrectly share cache entries, returning stale results.
Changes:
- Modified
_queryfunction signature to includeparamsas an explicit parameter, ensuring it's included in the cache key - Updated the
_querycall to passparamsexplicitly - Added test coverage to verify different parameter values create separate cache entries
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/streamlit/connections/snowflake_connection.py | Fixed the _query function to accept params as an explicit parameter and pass it through correctly, aligning with the SQLConnection pattern |
| lib/tests/streamlit/connections/snowflake_connection_test.py | Added test to verify cache behavior with different params values, ensuring separate cache entries are created |
| # Should have been called twice (once for each unique params value) | ||
| assert conn._instance.cursor.call_count == 2 | ||
| assert mock_cursor.execute.call_count == 2 |
There was a problem hiding this comment.
The test validates cache behavior (call counts) but doesn't verify that the execute method was called with the correct params values. Consider adding assertions to verify the actual parameters passed, such as checking that execute was called with params=["active"] and params=["inactive"]. This would strengthen the test by ensuring not just that caching works, but that the correct data flows through to the underlying Snowflake connector.
SummaryThis PR fixes a caching bug in The fix makes Code QualityThe code change is minimal, focused, and correct: Changes in
This pattern is now consistent with The fix correctly addresses the closure variable capture issue - previously, Test CoverageThe new test Strengths:
The test logic:
This implicitly includes a negative assertion: the test verifies that same params DO hit the cache (2 calls, not 4). Backwards CompatibilityThis change is fully backwards compatible:
Security & RiskNo security concerns identified. Low regression risk:
RecommendationsNo blocking issues. The PR is well-implemented with appropriate test coverage. Minor suggestions (non-blocking):
VerdictAPPROVED: This is a correct and well-tested bug fix that addresses a real caching issue where different query parameters incorrectly shared cache entries. The implementation is consistent with existing patterns in This is an automated AI review using |
Add inline comment clarifying why params must be an explicit parameter (to include it in the cache key). Also add assertions to verify execute was called with the correct params values. Co-Authored-By: Claude <[email protected]>
Describe your changes
Fixed a bug in
SnowflakeConnection.query()where theparamsargument was captured as a closure variable instead of being passed as an explicit parameter to the cached_queryfunction. This caused different parameter values to incorrectly share the same cache entry, returning stale results when queries were executed with different parameters.The fix makes
paramsan explicit parameter of the_queryfunction, ensuringst.cache_dataincludes it in the cache key, consistent with howSQLConnection.query()handles this pattern.Closes: #13644
Testing Plan
Added a new test
test_query_caches_separately_for_different_paramsthat verifies different parameter values create separate cache entries while identical parameters correctly hit the cache.Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.