Skip to content

fix(canner): strip trailing semicolon on unlimited query path - #2488

Merged
goldmedal merged 3 commits into
Canner:mainfrom
Bartok9:fix/canner-strip-unlimited-query
Jul 20, 2026
Merged

fix(canner): strip trailing semicolon on unlimited query path#2488
goldmedal merged 3 commits into
Canner:mainfrom
Bartok9:fix/canner-strip-unlimited-query

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Always strip trailing ; in Canner query() (limited and unlimited).
  • Keeps pasted client SQL twice-terminated from failing inconsistently vs dry_run/limit wrap.

Motivation

Limited path already stripped before subquery wrap; unlimited pasted SELECT …; with extra terminator whitespace differed. Align execute path.

Verification

  • Without fix: unlimited execute keeps trailing ; (test fail)
  • With fix: pytest tests/unit/test_canner_semicolon.py → 3 passed
  • Apache-2.0 core/**

Files

  • core/wren/src/wren/connector/canner.py
  • core/wren/tests/unit/test_canner_semicolon.py

Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL execution by consistently stripping trailing semicolons (and trailing whitespace/newlines) before running queries.
    • Ensured identical semicolon handling for both unlimited queries and queries using a row limit wrapper.
    • Kept semicolons inside quoted SQL strings unchanged.
  • Tests
    • Added unit coverage for semicolon handling in both unlimited and limited query paths, including trailing whitespace/newline scenarios.

@github-actions github-actions Bot added python Pull requests that update Python code core labels Jul 13, 2026
@coderabbitai

coderabbitai Bot commented Jul 13, 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

Run ID: 195908d3-621a-44f6-ae20-caab0460b334

📥 Commits

Reviewing files that changed from the base of the PR and between 7d21a2f and 5ec5ec2.

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

Walkthrough

Canner query processing now removes trailing semicolons before SQL execution or LIMIT wrapping. New unit tests cover helper behavior and both query paths.

Changes

Canner SQL handling

Layer / File(s) Summary
Normalize SQL before query composition
core/wren/src/wren/connector/canner.py
CannerConnector.query() strips trailing semicolons before unlimited execution or LIMIT wrapping.
Validate semicolon stripping paths
core/wren/tests/unit/test_canner_semicolon.py
Tests cover quoted semicolons, mocked query setup, unlimited queries, and LIMIT-wrapped queries.

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

Possibly related issues

Possibly related PRs

  • Canner/WrenAI#2480 — Also changes Canner query semicolon handling and shared helper usage.
  • Canner/WrenAI#2489 — Applies unconditional semicolon stripping to connector query execution.
  • Canner/WrenAI#2490 — Covers the same unlimited-query normalization pattern with targeted tests.

Suggested reviewers: goldmedal

Poem

A bunny brushed semicolons away,
So LIMIT-wrapped queries could play.
Quoted marks stayed snug in their string,
While tests gave a cheerful spring.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the semicolon-stripping fix, though it mentions only the unlimited path while the change applies to both paths.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

🧹 Nitpick comments (1)
core/wren/tests/unit/test_canner_semicolon.py (1)

36-42: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Patch _build_arrow_table via monkeypatch or patch to avoid leaking global state.

Both tests directly assign canner_mod._build_arrow_table = _fake_table (lines 37, 61) without restoring the original afterward. This mutates the module globally and persists for the rest of the test session, potentially breaking other tests that rely on the real function. Use monkeypatch.setattr or unittest.mock.patch as a context manager/decorator to ensure automatic cleanup.

Additionally, the fallback to _build_pg_arrow_table (lines 38-39, 62-63) is likely dead code — query() calls _build_arrow_table (canner.py line 276).

♻️ Proposed refactor using monkeypatch
 def test_query_unlimited_strips_trailing_semicolon(monkeypatch):
     connector = CannerConnector.__new__(CannerConnector)
     connector.connection = MagicMock()
     cursor = MagicMock()
     connector.connection.cursor.return_value.__enter__.return_value = cursor
     import pyarrow as pa

-    import wren.connector.canner as canner_mod
-
-    def _fake_table(cur):
-        import pyarrow as pa
-        return pa.table({"x": [1]})
-
-    if hasattr(canner_mod, "_build_arrow_table"):
-        canner_mod._build_arrow_table = _fake_table  # type: ignore
-    elif hasattr(canner_mod, "_build_pg_arrow_table"):
-        canner_mod._build_pg_arrow_table = _fake_table  # type: ignore
-    else:
-        raise AssertionError(dir(canner_mod)[:50])
+    monkeypatch.setattr(
+        "wren.connector.canner._build_arrow_table",
+        lambda cur: pa.table({"x": [1]}),
+    )

     connector.query("SELECT 1 AS x;")
     cursor.execute.assert_called_once_with("SELECT 1 AS x")

Also applies to: 60-63

🤖 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_canner_semicolon.py` around lines 36 - 42, Update
both test setup blocks around the fake table injection to use pytest
monkeypatch.setattr or unittest.mock.patch, ensuring the original builder is
automatically restored after each test. Target _build_arrow_table directly,
remove the fallback assignment to _build_pg_arrow_table and its related hasattr
branching, and retain a clear failure if the expected symbol is unavailable.
🤖 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.

Nitpick comments:
In `@core/wren/tests/unit/test_canner_semicolon.py`:
- Around line 36-42: Update both test setup blocks around the fake table
injection to use pytest monkeypatch.setattr or unittest.mock.patch, ensuring the
original builder is automatically restored after each test. Target
_build_arrow_table directly, remove the fallback assignment to
_build_pg_arrow_table and its related hasattr branching, and retain a clear
failure if the expected symbol is unavailable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 28ae6a08-1e96-4c06-b560-c920d01d6565

📥 Commits

Reviewing files that changed from the base of the PR and between f0270c1 and 09cf2aa.

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

Bartok9 added 2 commits July 17, 2026 08:13
Unlimited query() previously executed raw SQL including a trailing
terminator; keep the same strip used before LIMIT subquery wrapping.
Unit-test image lacks psycopg; query() imports it at call time so the
two query tests failed with ModuleNotFoundError. Provide a psycopg stub
fixture (with errors.QueryCanceled) and switch the _build_arrow_table
override to monkeypatch.setattr so module state is restored and the dead
_build_pg_arrow_table fallback is dropped (CodeRabbit nit).
@Bartok9
Bartok9 force-pushed the fix/canner-strip-unlimited-query branch from 7d21a2f to 2290ddf Compare July 17, 2026 12:13
CI unit collection failed: test imported private _strip_trailing_semicolon
from canner, but canner only reuses the public helper from base.
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