Repository navigation
fix(snowflake): push LIMIT into SQL and strip trailing semicolon on wrap - #2456
Conversation
query(limit=N) previously executed the full statement and sliced the Arrow table in Python, so large results were fully downloaded even when the caller only needed N rows. Wrap user SQL as a subquery with LIMIT and strip a trailing semicolon so composition stays valid. Lazy-import the snowflake driver so unit tests can run without the optional extra.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughModifies the Snowflake connector to defer the Snowflake import, strip only trailing semicolons before subquery wrapping, push ChangesSnowflake LIMIT pushdown
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant SnowflakeConnector
participant Cursor
Caller->>SnowflakeConnector: query(sql, limit=5)
SnowflakeConnector->>SnowflakeConnector: _strip_trailing_semicolon(sql)
SnowflakeConnector->>Cursor: execute SELECT * FROM (cleaned sql) LIMIT 5
Cursor-->>SnowflakeConnector: Arrow table result
SnowflakeConnector-->>Caller: query result
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@core/wren/src/wren/connector/snowflake.py`:
- Around line 81-84: The Snowflake query wrapper in the code that builds
executed SQL should isolate the user-provided SQL from the trailing wrapper text
so a final `--` comment cannot swallow the closing alias and LIMIT clause.
Update the logic in the SQL construction path that uses
`_strip_trailing_semicolon(sql)` to place the inner SQL on its own lines inside
the `SELECT * FROM (...) AS _wren_sub LIMIT ...` wrapper, ensuring the suffix
remains outside any user comment and the wrapped query stays balanced.
- Around line 80-84: The LIMIT wrapper in SnowflakeQuery execution can both
change ordered preview results and be broken by trailing line comments. Update
the logic that builds the wrapped SQL in the limit-handling branch so it
preserves the original ORDER BY/LIMIT semantics (matching the current
Python-slice behavior) and ensures any trailing -- comment in
_strip_trailing_semicolon(sql) cannot swallow the closing wrapper, e.g. by
terminating the comment before appending the outer query. Use the existing
Snowflake query execution path and the _strip_trailing_semicolon helper as the
main points to adjust.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f5ccc4df-8e0d-471a-9afa-4ee8def9e83a
📒 Files selected for processing (2)
core/wren/src/wren/connector/snowflake.pycore/wren/tests/unit/test_snowflake_limit_pushdown.py
… -- comment cannot swallow suffix
|
Fixed both CodeRabbit findings in 5501e94: the user SQL now sits on its own line inside the wrapper — |
|
LGTM overall — Snowflake was the last outlier fetching a full result set to slice in Python, so pushing LIMIT into the SQL is a good fix, and the newline-delimited wrap + trailing-line-comment test are a nice touch. The lazy One nit on the
Per Snowflake's docs, an Non-blocking follow-up thought: |
|
Good catch — you're right, that comment overstates the guarantee. Snowflake doesn't guarantee a subquery's |
goldmedal review on Canner#2457 — same newline guard as Snowflake Canner#2456 so query("SELECT 1 -- pick", limit=n) does not comment out the closing wrap.
Drop the incorrect Snowflake Canner#2456 cross-reference (snowflake.py has no subquery-LIMIT wrap / semicolon guard) and the review-thread residue (PR numbers, reviewer handle). Keep only the substantive justification: multiline wrap so a trailing line comment can't swallow the closing wrap.
Summary
query(..., limit=N)into Snowflake SQL viaSELECT * FROM (<sql>) AS _wren_sub LIMIT N.;-terminated user SQL cannot break composition.snowflake.connectorso unit tests do not require the optional extra.Motivation
The previous path ran the full statement and used
arrow_table.slice(0, limit). Large queries paid full scan + transfer cost on the client even when Aster only needed a sample/page. Other connectors (postgres/redshift/clickhouse/trino) already apply LIMIT server-side; Snowflake was the outlier. Trailing-semicolon strip matches the established wrap helpers.Verification
PYTHONPATH=src pytest tests/unit/test_snowflake_limit_pushdown.py→ 3 passedSELECT 1 AS x;+limit=5isSELECT * FROM (SELECT 1 AS x) AS _wren_sub LIMIT 5limit, original SQL is executed unchanged.License
core/wren/**only (Apache-2.0).Summary by CodeRabbit
limitby pushing the LIMIT into the SQL run on Snowflake rather than slicing results in Python.dry_run).