Skip to content

refactor(connector): share strip_trailing_semicolon in base - #2480

Merged
goldmedal merged 8 commits into
Canner:mainfrom
Bartok9:fix/2441-shared-strip-trailing-semicolon
Jul 14, 2026
Merged

goldmedal merged 8 commits into
Canner:mainfrom
Bartok9:fix/2441-shared-strip-trailing-semicolon

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Move strip_trailing_semicolon (regex terminating-run) into wren.connector.base and migrate connectors onto it.
  • Unifies multi-semicolon behavior (including trino/mysql) and removes 10 local helper copies.

Motivation

Closes #2441.

Per-connector copies of _strip_trailing_semicolon drifted (regex run vs single removesuffix / rstrip). Maintainers agreed in the #2430 review thread to consolidate on one helper in connector/base.py. Scope is core/wren only (Apache-2.0).

Verification

  • core/wren/.venv/bin/python -m pytest tests/unit/test_strip_trailing_semicolon.py tests/unit/test_athena_semicolon.py tests/unit/test_databricks_semicolon.py tests/unit/test_redshift_semicolon.py tests/unit/test_oracle_semicolon.py tests/unit/test_datafusion_semicolon.py tests/unit/test_trino_parser.py::test_strip_trailing_semicolon tests/connectors/test_postgres.py -k "strip or semicolon" -q — 38 passed
  • tests/connectors/test_canner.py -k strip — 7 passed
  • Did NOT change: connector query/dry_run control flow beyond the shared helper

Real behavior proof (shared helper):
strip_trailing_semicolon("SELECT 1 ; ;") == "SELECT 1"
strip_trailing_semicolon("SELECT 'a;b' AS x;") == "SELECT 'a;b' AS x"

Summary by CodeRabbit

  • Bug Fixes

    • Improved SQL handling across supported connectors by consistently removing only trailing semicolons (and surrounding whitespace) before wrapping queries and generating dry-run SQL.
    • Preserves semicolons inside quoted string literals.
    • Reduces failures in limited-result queries, including EXPLAIN-style dry runs.
  • Refactor

    • Consolidated trailing-semicolon stripping into a shared helper used by multiple connectors.
  • Tests

    • Added unit test coverage for the shared trailing-semicolon behavior.
    • Updated connector tests to use the centralized semicolon-handling logic.

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

coderabbitai Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Centralizes trailing-semicolon stripping in wren.connector.base, updates eleven connectors to use it for SQL wrapping and explain queries, and aligns existing tests with the shared helper while adding parametrized coverage.

Changes

SQL preprocessing refactor

Layer / File(s) Summary
Shared stripping helper
core/wren/src/wren/connector/base.py, core/wren/tests/unit/test_strip_trailing_semicolon.py
Adds and tests strip_trailing_semicolon, removing trailing semicolons and whitespace while preserving semicolons inside quoted strings.
Connector adoption
core/wren/src/wren/connector/{athena,canner,clickhouse,databricks,datafusion,duckdb,mysql,oracle,postgres,redshift,trino,snowflake}.py
Removes local implementations and uses the shared helper for query wrapping, limits, and EXPLAIN statements.
Test alignment
core/wren/tests/connectors/*, core/wren/tests/unit/{test_athena_semicolon,test_databricks_semicolon,test_datafusion_semicolon,test_oracle_semicolon,test_redshift_semicolon,test_trino_parser}.py
Updates helper imports to reference the shared base implementation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

  • Canner/WrenAI issue 2441 — Proposes consolidating connector-specific semicolon stripping into the shared base helper.

Possibly related PRs

Suggested reviewers: goldmedal

Poem

A rabbit trims semicolons neat,
Wrapped queries now taste complete.
One shared helper leads the way,
Tests hop after, bright and gray.
Quoted semicolons safely stay! 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: moving the semicolon-stripping helper into base and sharing it across connectors.
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

🤖 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/mysql.py`:
- Line 21: Remove the duplicate strip_trailing_semicolon entry from the import
statement in the MySQL connector, keeping a single import alongside
ConnectorABC.
🪄 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: 8faa526f-b212-4a68-a580-3c369cf80785

📥 Commits

Reviewing files that changed from the base of the PR and between bdaefa9 and 9aa20eb.

📒 Files selected for processing (21)
  • core/wren/src/wren/connector/athena.py
  • core/wren/src/wren/connector/base.py
  • core/wren/src/wren/connector/canner.py
  • core/wren/src/wren/connector/clickhouse.py
  • core/wren/src/wren/connector/databricks.py
  • core/wren/src/wren/connector/datafusion.py
  • core/wren/src/wren/connector/duckdb.py
  • core/wren/src/wren/connector/mysql.py
  • core/wren/src/wren/connector/oracle.py
  • core/wren/src/wren/connector/postgres.py
  • core/wren/src/wren/connector/redshift.py
  • core/wren/src/wren/connector/trino.py
  • core/wren/tests/connectors/test_canner.py
  • core/wren/tests/connectors/test_postgres.py
  • core/wren/tests/unit/test_athena_semicolon.py
  • core/wren/tests/unit/test_databricks_semicolon.py
  • core/wren/tests/unit/test_datafusion_semicolon.py
  • core/wren/tests/unit/test_oracle_semicolon.py
  • core/wren/tests/unit/test_redshift_semicolon.py
  • core/wren/tests/unit/test_strip_trailing_semicolon.py
  • core/wren/tests/unit/test_trino_parser.py

Comment thread core/wren/src/wren/connector/mysql.py Resolved
@goldmedal

Copy link
Copy Markdown
Collaborator

The shared CI failure here (maturin failed → cannot update the lock file core/wren-core-py/Cargo.lock because --locked was passed) came from the Rust core 0.2.0 bump in #2467 leaving core/wren-core-py/Cargo.lock out of sync. That's now fixed on main by #2478 — please rebase onto latest main to pick up the lockfile fix and turn CI green. 🙏

@Bartok9
Bartok9 force-pushed the fix/2441-shared-strip-trailing-semicolon branch from a87586d to cb8f97e Compare July 13, 2026 12:10

@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/src/wren/connector/oracle.py (1)

17-21: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Drop the unused alias and keep imports contiguous

  • _strip_trailing_semicolon is no longer used; remove it.
  • Move from wren.model.error import ... up with the other imports so the module has a single import block.
🤖 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/oracle.py` around lines 17 - 21, Remove the
unused _strip_trailing_semicolon alias from the module, and move the
wren.model.error import into the contiguous top-level import block alongside the
other imports.
🤖 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/src/wren/connector/oracle.py`:
- Around line 17-21: Remove the unused _strip_trailing_semicolon alias from the
module, and move the wren.model.error import into the contiguous top-level
import block alongside the other imports.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f4279de5-3cb2-42d1-aed9-7bca38208b1d

📥 Commits

Reviewing files that changed from the base of the PR and between a87586d and cb8f97e.

📒 Files selected for processing (21)
  • core/wren/src/wren/connector/athena.py
  • core/wren/src/wren/connector/base.py
  • core/wren/src/wren/connector/canner.py
  • core/wren/src/wren/connector/clickhouse.py
  • core/wren/src/wren/connector/databricks.py
  • core/wren/src/wren/connector/datafusion.py
  • core/wren/src/wren/connector/duckdb.py
  • core/wren/src/wren/connector/mysql.py
  • core/wren/src/wren/connector/oracle.py
  • core/wren/src/wren/connector/postgres.py
  • core/wren/src/wren/connector/redshift.py
  • core/wren/src/wren/connector/trino.py
  • core/wren/tests/connectors/test_canner.py
  • core/wren/tests/connectors/test_postgres.py
  • core/wren/tests/unit/test_athena_semicolon.py
  • core/wren/tests/unit/test_databricks_semicolon.py
  • core/wren/tests/unit/test_datafusion_semicolon.py
  • core/wren/tests/unit/test_oracle_semicolon.py
  • core/wren/tests/unit/test_redshift_semicolon.py
  • core/wren/tests/unit/test_strip_trailing_semicolon.py
  • core/wren/tests/unit/test_trino_parser.py
🚧 Files skipped from review as they are similar to previous changes (18)
  • core/wren/src/wren/connector/redshift.py
  • core/wren/tests/connectors/test_postgres.py
  • core/wren/tests/unit/test_strip_trailing_semicolon.py
  • core/wren/tests/unit/test_athena_semicolon.py
  • core/wren/src/wren/connector/postgres.py
  • core/wren/tests/unit/test_redshift_semicolon.py
  • core/wren/tests/unit/test_trino_parser.py
  • core/wren/src/wren/connector/databricks.py
  • core/wren/src/wren/connector/mysql.py
  • core/wren/src/wren/connector/datafusion.py
  • core/wren/tests/unit/test_databricks_semicolon.py
  • core/wren/src/wren/connector/base.py
  • core/wren/tests/unit/test_datafusion_semicolon.py
  • core/wren/src/wren/connector/trino.py
  • core/wren/tests/connectors/test_canner.py
  • core/wren/tests/unit/test_oracle_semicolon.py
  • core/wren/src/wren/connector/duckdb.py
  • core/wren/src/wren/connector/canner.py

@Bartok9

Bartok9 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @goldmedal 🙏 This branch already includes the core/wren-core-py/Cargo.lock sync from #2478 (and the follow-up #2484 release CI fix), so the maturin --locked failure should be resolved. Also addressed CodeRabbit's note: dropped the now-unused _strip_trailing_semicolon alias in oracle.py and kept the wren.model.error import contiguous. mysql.py had no duplicate import. CI should be green now.

@goldmedal

Copy link
Copy Markdown
Collaborator

One gap before this lands: snowflake isn't migrated. core/wren/src/wren/connector/snowflake.py still carries its own identical copy on main:

_TRAILING_SEMICOLONS_RE = re.compile(r"[;\s]+\Z")

def _strip_trailing_semicolon(sql: str) -> str:
    ...
    return _TRAILING_SEMICOLONS_RE.sub("", sql)

This PR migrates 11 connectors (athena, canner, clickhouse, databricks, datafusion, duckdb, oracle, postgres, redshift, trino, mysql) but leaves snowflake with a 12th, byte-identical copy — so the "single shared helper" goal isn't fully met, and the description ("migrate every connector onto it" / "removes 10 local helper copies") slightly overstates it. It's almost certainly just because snowflake's copy landed after #2441 was scoped (it's not in that issue's enumeration).

Since it uses the exact same regex, folding it in is a one-file mechanical change:

- _TRAILING_SEMICOLONS_RE = re.compile(r"[;\s]+\Z")
- def _strip_trailing_semicolon(sql: str) -> str:
-     ...
-     return _TRAILING_SEMICOLONS_RE.sub("", sql)
+ from wren.connector.base import strip_trailing_semicolon

(plus the two call sites, and drop the now-unused import re if nothing else uses it).

Suggest including snowflake here so the shared helper is genuinely the single source of truth before other PRs rebase onto it — the open snowflake fix adds another strip call site, and it'd be better for that to land on the shared helper than to grow the local copy.

Otherwise the migration itself looks clean: no leftover local helpers in the 11, all wired to base, and the new test_strip_trailing_semicolon.py nicely locks in the intended unification ("SELECT 1 ; ;" → "SELECT 1", string-literal preservation) that trino/mysql previously diverged on.

@Bartok9

Bartok9 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Good catch @goldmedal 🙏 — snowflake's copy landed after #2441 was scoped, so it wasn't in the enumeration. Just folded it in (b3ae60b): dropped snowflake's local _strip_trailing_semicolon + _TRAILING_SEMICOLONS_RE and the now-unused import re, wired the query() call site to base.strip_trailing_semicolon. All 12 connectors now share the single helper — genuine single source of truth before other PRs rebase onto it. Same regex/behavior, so no functional change.

@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

🤖 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 7-9: Update test_snowflake_limit_pushdown.py to import
strip_trailing_semicolon from wren.connector.base instead of the removed
_strip_trailing_semicolon from wren.connector.snowflake, and replace all test
calls accordingly.
🪄 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: 7398ffc7-ff05-44ae-a8cb-1fb696ed3083

📥 Commits

Reviewing files that changed from the base of the PR and between cb8f97e and b3ae60b.

📒 Files selected for processing (2)
  • core/wren/src/wren/connector/oracle.py
  • core/wren/src/wren/connector/snowflake.py

@Bartok9
Bartok9 force-pushed the fix/2441-shared-strip-trailing-semicolon branch from b3ae60b to 9d9cd4e Compare July 14, 2026 03:35
@Bartok9

Bartok9 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main and pushed (9d9cd4e). Heads up: main had reintroduced a fresh local _strip_trailing_semicolon in athena.py after this branch was cut, so the CI merge left a dangling call site — I've now folded athena onto base.strip_trailing_semicolon too and confirmed no other connector carries a local copy. ruff check + ruff format --check pass clean locally. All connectors now genuinely share the single helper. 🙏

@goldmedal

Copy link
Copy Markdown
Collaborator

Filed the follow-up plan so the remaining trailing-semicolon work stays coherent after this consolidation lands, instead of a stream of per-connector PRs:

Both #2497 and #2498 are blocked on this PR (they should import the shared helper rather than re-add local copies). Redundant PRs will be closed in favor of these: #2463 (⊂ #2483), and #2474 / #2475 (their trino/mysql multi-semicolon fix is already delivered by this PR's migration).

@Bartok9

Bartok9 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Perfect — thanks for sequencing this @goldmedal 🙏. Consolidating to a single base.strip_trailing_semicolon here first is exactly the right base for #2497 (unlimited query() path) and #2498 (dry_run/EXPLAIN/describe) to import rather than re-add local copies. Helper is stable, all 12 connectors share it, ruff clean. Happy to rebase promptly if #2483 or anything else lands first so those two stay unblocked.

…down tests

The athena and snowflake limit-pushdown tests still imported the removed
private _strip_trailing_semicolon; point them at the shared
wren.connector.base helper (aliased) to match the other migrated tests.
@Bartok9

Bartok9 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Pushed aa09bfd — fixed the failing unit tests job. The athena and snowflake limit-pushdown test modules (test_athena_limit_pushdown.py, test_snowflake_limit_pushdown.py) were still importing the now-removed private _strip_trailing_semicolon from their connector modules, causing an ImportError at collection time. Repointed both at the shared wren.connector.base.strip_trailing_semicolon (aliased), matching the pattern already used by the other migrated tests. ruff check + ruff format clean. CI should go green now. 🙏

@goldmedal

Copy link
Copy Markdown
Collaborator

Thanks — and nice catch folding athena + snowflake back in; good to have all 12 connectors on the single helper.

Let's keep #2480 first, so no need to pre-rebase for a #2483-first ordering. Once this merges, #2497 and #2498 build straight on top of base.strip_trailing_semicolon.

#2483 will be absorbed, not merged independently: its unlimited-query() half folds into #2497 and its dry_run half into #2498 (both importing the shared helper rather than the datafusion-local copy), then we'll close it along with #2463. That keeps datafusion consistent with every other connector instead of fixing it via its own PR.

So the path is: merge #2480 → #2497 / #2498 → close #2483 / #2463 / #2474 / #2475. Appreciate the quick turnaround on the rebase 🙏

@goldmedal goldmedal left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Bartok9, nice refactoring 👍

@goldmedal
goldmedal merged commit e4f0139 into Canner:main Jul 14, 2026
8 checks passed
@Bartok9

Bartok9 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

🎉 Merged — thanks @goldmedal. Confirmed path: #2480 (done) → #2497 (unlimited query()) / #2498 (dry_run/EXPLAIN/describe), both importing base.strip_trailing_semicolon rather than any datafusion-local copy → then close #2483 / #2463 / #2474 / #2475. I'll get #2497 and #2498 rebased on the new base tip and green so datafusion lands consistent with the other 11 connectors. 🙏

Bartok9 added a commit to Bartok9/WrenAI that referenced this pull request Jul 22, 2026
Route query/dry_run through connector.base.strip_trailing_semicolon
instead of a per-connector copy, per Canner#2441 consolidation (Canner#2480).
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.

refactor(wren): consolidate per-connector strip_trailing_semicolon into a shared helper in connector/base.py

2 participants