Skip to content

fix(duckdb): coerce LIMIT before SQL interpolation - #2637

Closed
Bartok9 wants to merge 4 commits into
Canner:mainfrom
Bartok9:fix/duckdb-coerce-limit
Closed

fix(duckdb): coerce LIMIT before SQL interpolation#2637
Bartok9 wants to merge 4 commits into
Canner:mainfrom
Bartok9:fix/duckdb-coerce-limit

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

DuckDB wraps user SQL with LIMIT {int(limit)} without rejecting negatives. Add _coerce_limit + unit tests.

Test plan

  • cd core/wren && .venv/bin/python -m pytest tests/unit/test_duckdb_coerce_limit.py -q (3 passed)

Summary by CodeRabbit

  • Bug Fixes

    • Improved query limit validation to reject negative, fractional, boolean, and unsafe values.
    • Added support for valid numeric limits provided as strings.
    • Ensured queries without a limit continue to execute correctly.
  • Tests

    • Added coverage for invalid, potentially malicious, negative, fractional, and string-based limit values.
    • Verified that valid limits are safely applied during query execution.

Validate limit before embedding it in the subquery LIMIT wrap; reject
negatives and non-numeric strings.
@github-actions github-actions Bot added python Pull requests that update Python code core labels Jul 31, 2026
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 671ecc35-f131-4d38-976f-9bc78c8be36a

📥 Commits

Reviewing files that changed from the base of the PR and between 72c65f3 and 6c9b541.

📒 Files selected for processing (3)
  • core/wren/src/wren/connector/base.py
  • core/wren/src/wren/connector/duckdb.py
  • core/wren/tests/unit/test_duckdb_coerce_limit.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • core/wren/src/wren/connector/duckdb.py
  • core/wren/tests/unit/test_duckdb_coerce_limit.py

Walkthrough

DuckDB limit handling now accepts numeric strings and validates all limits before SQL interpolation. Invalid, fractional, negative, and boolean values raise ValueError. Tests cover limited and unlimited query construction.

Changes

DuckDB limit validation

Layer / File(s) Summary
Limit coercion contract
core/wren/src/wren/connector/base.py, core/wren/src/wren/connector/duckdb.py, core/wren/tests/unit/test_duckdb_coerce_limit.py
The query interface accepts string limits. _coerce_limit preserves None, converts integer-compatible values, and rejects invalid values.
Query limit integration
core/wren/src/wren/connector/duckdb.py, core/wren/tests/unit/test_duckdb_coerce_limit.py
DuckDBConnector.query validates limits before SQL interpolation. Tests verify numeric LIMIT output and semicolon removal for unlimited queries.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: goldmedal

Poem

A rabbit checks each limit line,
Safe numbers enter SQL in time.
Bad values stop at the gate,
None keeps queries semicolon-free and straight.
DuckDB tests confirm the state.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes a summary and test command, but omits the required failure reproduction with actual error output and the Duplicate check section. Add the reproduction and actual error output, use the required testing section, and document which open PRs were checked.
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: coercing DuckDB LIMIT values before SQL interpolation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
core/wren/tests/unit/test_duckdb_coerce_limit.py (1)

14-33: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the unlimited query path.

The helper preserves None, and query has a separate unlimited branch. Add tests for _coerce_limit(None) and query(..., limit=None) to confirm that the trailing semicolon is stripped and no LIMIT clause is added.

🤖 Prompt for 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.

In `@core/wren/tests/unit/test_duckdb_coerce_limit.py` around lines 14 - 33, Add
coverage for the unlimited path using _coerce_limit and DuckDBConnector.query:
assert _coerce_limit(None) preserves None, then invoke query with limit=None and
verify the executed SQL has its trailing semicolon removed and contains no LIMIT
clause.
core/wren/src/wren/connector/duckdb.py (1)

16-20: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Align the shared query annotation with the accepted limit contract.

DuckDBConnector.query() and the test call _coerce_limit(...) with strings, but the abstract ConnectorABC.query() contract still declares limit: int | None. Make the DuckDB helper, DuckDBConnector.query(), and the shared abstract contract use int | str | None so callers, tests, and implementations agree.

🤖 Prompt for 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.

In `@core/wren/src/wren/connector/duckdb.py` around lines 16 - 20, Update the
limit type annotation consistently across _coerce_limit,
DuckDBConnector.query(), and the shared ConnectorABC.query() contract from int |
None to int | str | None, preserving the existing coercion behavior and
implementation signatures.
🤖 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/duckdb.py`:
- Around line 20-22: Update _coerce_limit to reject non-integral numeric inputs
before calling int(), ensuring values such as -0.5 cannot become zero; retain
the non-negative validation for integer-like values. Update the function’s
type/contract comment to document the accepted inputs and add a regression test
covering fractional negative limits.

---

Nitpick comments:
In `@core/wren/src/wren/connector/duckdb.py`:
- Around line 16-20: Update the limit type annotation consistently across
_coerce_limit, DuckDBConnector.query(), and the shared ConnectorABC.query()
contract from int | None to int | str | None, preserving the existing coercion
behavior and implementation signatures.

In `@core/wren/tests/unit/test_duckdb_coerce_limit.py`:
- Around line 14-33: Add coverage for the unlimited path using _coerce_limit and
DuckDBConnector.query: assert _coerce_limit(None) preserves None, then invoke
query with limit=None and verify the executed SQL has its trailing semicolon
removed and contains no LIMIT clause.
🪄 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: 8b14bebe-a2c7-448c-85d3-0156b2e73854

📥 Commits

Reviewing files that changed from the base of the PR and between 74bf59e and 81dc499.

📒 Files selected for processing (2)
  • core/wren/src/wren/connector/duckdb.py
  • core/wren/tests/unit/test_duckdb_coerce_limit.py

Comment thread core/wren/src/wren/connector/duckdb.py Outdated
Bartok9 added 3 commits July 31, 2026 02:14
…ted path

- _coerce_limit rejects non-integral floats (e.g. -0.5) and bools
- annotate limit as int|str|None across helper, query, and ConnectorABC
- add tests for None passthrough and unlimited query semicolon strip
@goldmedal

Copy link
Copy Markdown
Collaborator

Closing in favour of a single consolidated change — thanks for the work, the underlying tidy-up is worth doing, just not as one PR per connector.

Why this is being closed rather than reviewed:

  1. This is one mechanical change spread across eight PRs. The contribution bar added in docs: set an explicit contribution bar for agent-authored PRs #2602 asks for exactly this to be a single diff: "A mechanical change repeated across several files or connectors belongs in one PR, not one PR per file. Reviewers need to see the resulting convention in a single diff."

  2. A shared helper already exists in this same batch, and none of these PRs use it. refactor(connector): centralize LIMIT coercion #2624 adds coerce_limit() to connector/base.py. Every other PR in the series re-declares a private _coerce_limit in its own module instead of importing it. Merged as-is the repo would carry nine copies of the same function (the eight here plus the existing one at connector/mysql.py:44).

  3. The copies have already diverged before merge. base.py (refactor(connector): centralize LIMIT coercion #2624), postgres, trino, oracle, redshift and bigquery use int(limit) then a negativity check; fix(clickhouse): coerce LIMIT before SQL interpolation #2627 (clickhouse) additionally rejects fractional negatives such as -0.5 via numbers.Number; fix(duckdb): coerce LIMIT before SQL interpolation #2637 (duckdb) additionally rejects bool and non-integral float. Three different semantics for one contract is the specific outcome a shared helper prevents.

  4. Coverage is inconsistent. connector/canner.py:255 interpolates a bare {limit} and is not covered by any PR in the series, while bigquery, duckdb and redshift already interpolate {int(limit)} today — for those three the only behavioural delta is the negativity check.

On the fix: label and the stated failure. The reproduction in the description calls connector.query(sql, limit="1; DROP TABLE users") directly. Tracing the call paths:

  • run_sql in mcp_server.py declares limit: int | None, so a non-numeric string is rejected by tool-argument validation, and mcp_server.py:84 already rejects negatives and clamps to MAX_ROW_LIMIT.
  • The CLI declares --limit/-l as Optional[int], so a non-integer is rejected at parse time.
  • That leaves Engine.query(sql, limit) as a Python API. At that boundary the caller already supplies sql verbatim — anyone able to pass limit="1; DROP TABLE t" can pass that as sql instead. limit is not a lower-trust channel than sql there, so this is not an injection path.

What remains is genuine but smaller: a negative limit currently surfaces as a driver-level error instead of a clear ValueError, and the coercion contract is inconsistent across connectors. That is refactor:, per "fix: requires a reproducible failure that the change repairs."

What we would take instead — a single PR, refactor(connector): centralize LIMIT coercion, that:

  • keeps one coerce_limit() in connector/base.py, with the strictest semantics of the three variants above (reject bool, non-integral values, and negatives);
  • routes every interpolating connector through it — including canner.py, and replacing the private copy in mysql.py;
  • leaves ConnectorABC.query's signature as int | None (see the per-PR note on fix(duckdb): coerce LIMIT before SQL interpolation #2637 below);
  • tests the helper once in tests/unit/test_coerce_limit.py, with at most a smoke test per connector proving it is wired in, rather than repeating the same six cases eight times.

#2624 is the natural home for that; it is being kept open with a note to that effect.


On this PR specifically — two extra reasons beyond the shared ones. It edits connector/base.py, which conflicts with #2624 in the same batch; and it widens ConnectorABC.query to limit: int | str | None. That widening formalises str as an accepted limit type on the public connector interface, which runs opposite to the stated goal of rejecting string limits. Your bool / non-integral float rejection is worth keeping in the shared helper.

@goldmedal goldmedal closed this Aug 3, 2026
@Bartok9

Bartok9 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @goldmedal — agreed on consolidating. Closing as one PR per connector was the wrong shape given #2602 and the diverging _coerce_limit copies.

I'll pivot the strict bits from this PR (reject bool / non-integral floats, keep ConnectorABC.query as int | None) into #2624 as a single refactor(connector): centralize LIMIT coercion, including canner.py and replacing the private helper in mysql.py, with one shared test module plus light wiring smokes.

Appreciate the detailed write-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants