fix(clickhouse): coerce LIMIT before SQL interpolation - #2627
Conversation
Reject negative and non-numeric limits before ClickHouse LIMIT wrapping.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThe ClickHouse connector now rejects negative numeric limits before integer coercion and validates limits before SQL interpolation. Unit tests cover fractional negatives, invalid strings, and numeric-string SQL generation. ChangesClickHouse limit validation
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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: 1
🤖 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/clickhouse.py`:
- Around line 386-397: Update _coerce_limit to reject negative numeric inputs
before int() truncates fractional values, while preserving safe coercion for
valid limits and existing None handling. Add a regression test covering
limit=-0.5 and assert that it raises ValueError rather than producing LIMIT 0.
🪄 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 Plus
Run ID: c1115fa7-3bde-4d77-8236-7ad7f3ace67c
📒 Files selected for processing (2)
core/wren/src/wren/connector/clickhouse.pycore/wren/tests/unit/test_clickhouse_coerce_limit.py
|
Good catch, @coderabbitai — |
|
Tip For best results, initiate chat on the files or code changes.
🐇 ✅ If you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! You are interacting with an AI system. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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/clickhouse.py`:
- Around line 394-400: Update the limit validation before int() coercion to
reject negative values for every accepted numeric type, including Decimal and
Fraction, so values such as -0.5 cannot become zero; alternatively, explicitly
reject unsupported numeric types. Preserve the existing non-negative coercion
and error behavior in the surrounding limit handling.
🪄 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 Plus
Run ID: 088d79df-f424-4300-b0dc-eafd0e62395d
📒 Files selected for processing (2)
core/wren/src/wren/connector/clickhouse.pycore/wren/tests/unit/test_clickhouse_coerce_limit.py
Broaden the pre-int() negative guard from (int, float) to numbers.Number
so Decimal('-0.5') / Fraction(-1, 2) cannot slip through as LIMIT 0.
Adds regression tests for both.
|
Thanks @coderabbitai — good follow-up. Broadened the pre- |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
git show --format=fuller --stat 6c3053a --
git diff 6c3053a^ 6c3053a -- \
core/wren/src/wren/connector/clickhouse.py \
core/wren/tests/unit/test_clickhouse_coerce_limit.pyLength of output: 3210
🐇 ✅ ✏️ Learnings added
You are interacting with an AI system. |
|
Fixed the |
Summary
Coerce
limitto a non-negative int before ClickHouse LIMIT subquery wrapping.Failure / reproduction
Before this change,
ClickHouseConnector.queryinterpolated the raw{limit}directly into the generated SQL:So a client-supplied bad value flowed straight into the query text. E.g. calling
query(sql, limit=-1)produced:and a non-numeric value like
limit="1; DROP TABLE t"interpolated verbatim — an injection-shaped input that should never reach the driver.Fix
Add
_coerce_limitand apply it at the start ofquery: reject negatives and non-integer strings, accept numeric strings, so only a validated non-negative int is ever interpolated.Verification
Covers: negative rejection, injection-like/non-numeric rejection, numeric-string acceptance.
Duplicate check
Searched open PRs/issues for existing ClickHouse limit coercion. Related sibling connector PRs (#2624/#2625/#2626) apply the same pattern to other connectors; this PR is the ClickHouse-specific counterpart and does not overlap their files.
Summary by CodeRabbit
LIMITinputs by rejecting negative numeric values, including fractional inputs (decimals and fractions), before execution.LIMITvalues.LIMITcoercion scenarios.