Skip to content

fix(spark): strip trailing semicolon before sql/dry_run#2464

Merged
goldmedal merged 3 commits into
Canner:mainfrom
Bartok9:fix/spark-strip-semicolon-limit
Jul 22, 2026
Merged

fix(spark): strip trailing semicolon before sql/dry_run#2464
goldmedal merged 3 commits into
Canner:mainfrom
Bartok9:fix/spark-strip-semicolon-limit

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Strip trailing ; / whitespace before Spark Connect sql() in both query and dry_run.
  • Client SQL ending in a semicolon no longer fails or mis-parses on Spark Connect.

Motivation

Postgres/redshift/duckdb/databricks/datafusion already sanitize trailing semicolons before plan/execute paths. Spark still passed raw SQL into SparkSession.sql(...), so statements like SELECT 1; hit Connect/parser multi-statement roughness and pouch provenance protection. Reuse the terminating-run strip helper so literals such as SELECT ';' stay intact while terminal ; is removed.

Verification

  • core/wren/.venv/bin/python -m pytest tests/unit/test_spark_semicolon.py -q — 4 passed
  • Mocked session: query("SELECT 1;", limit=2) calls sql("SELECT 1"); dry_run strips trailing whitespace+semicolon
  • Did NOT change: client-side Arrow slice limit behavior

License

core/wren/** only (Apache-2.0).

Summary by CodeRabbit

  • Bug Fixes
    • Improved Spark SQL execution by automatically removing trailing semicolons (and surrounding whitespace/newlines) before running queries.
    • Ensured semicolons inside string literals remain unchanged.
    • Preserved dry-run behavior, including keeping the limit at zero.
  • Tests
    • Added unit coverage for trailing-semicolon normalization for both query execution and dry-run.

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

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

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: e98483b1-7305-4bf7-bb55-d42fc10937b5

📥 Commits

Reviewing files that changed from the base of the PR and between 6a9b5f2 and fda0362.

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

Walkthrough

Spark query and dry-run execution now strip trailing semicolons and whitespace before calling the Spark session. Unit tests cover normalization, string literals, unchanged SQL, and the dry-run zero-row limit.

Changes

Spark SQL normalization

Layer / File(s) Summary
Normalize trailing semicolons
core/wren/src/wren/connector/spark.py
Uses strip_trailing_semicolon() for query() and dry_run() before Spark SQL execution.
Validate semicolon behavior
core/wren/tests/unit/test_spark_semicolon.py
Tests query and dry-run normalization, string-literal preservation, unchanged SQL, and the dry-run zero-row limit.

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

Possibly related issues

Possibly related PRs

  • Canner/WrenAI#2422 — Applies related SQL preprocessing to the Databricks dry-run path.
  • Canner/WrenAI#2423 — Applies equivalent normalization to Oracle query and dry-run paths with tests.
  • Canner/WrenAI#2430 — Applies equivalent normalization to DataFusion query handling.

Suggested reviewers: goldmedal

Poem

A bunny trims the SQL tail,
While quoted marks remain without fail.
Queries hop, dry-runs count,
Zero rows are the amount. 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.09% 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 matches the main change: Spark now strips trailing semicolons before sql and dry_run.
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_spark_semicolon.py (1)

16-26: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove unused mock variables df and dfto.

Lines 18-20 create df and dfto as MagicMock objects that are never used. The actual mock chain is set up on line 24 via session.sql.return_value.toPandas.return_value, making these variables dead code that adds confusion about the test's mock setup.

🧹 Proposed cleanup
 def test_query_strips_trailing_semicolon_before_sql() -> None:
     connector, session = _make_mock_connector()
-    df = MagicMock()
-    df.attrs = {}
-    dfto = MagicMock()
     # pandas DF mock for pa.Table.from_pandas
     import pandas as pd
🤖 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_spark_semicolon.py` around lines 16 - 26, Remove
the unused MagicMock variables df and dfto from
test_query_strips_trailing_semicolon_before_sql; retain only the
session.sql.return_value.toPandas.return_value setup needed by the test.
🤖 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_spark_semicolon.py`:
- Around line 16-26: Remove the unused MagicMock variables df and dfto from
test_query_strips_trailing_semicolon_before_sql; retain only the
session.sql.return_value.toPandas.return_value setup needed by the test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9e41eb13-e1b9-465d-8584-7d69ce3d0a23

📥 Commits

Reviewing files that changed from the base of the PR and between 0ba4fae and dcd1240.

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

Bartok9 added a commit to Bartok9/WrenAI that referenced this pull request Jul 10, 2026
@Bartok9

Bartok9 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed CodeRabbit trivial: removed unused df / dfto MagicMocks in the spark semicolon unit test.

Bartok9 added a commit to Bartok9/WrenAI that referenced this pull request Jul 10, 2026
@Bartok9
Bartok9 force-pushed the fix/spark-strip-semicolon-limit branch from e9a26bd to e331b6f Compare July 10, 2026 14:51
@goldmedal

Copy link
Copy Markdown
Collaborator

#2478 fixed the building failure of wren-core-py. Could you rebase on main to fix it?

Bartok9 added a commit to Bartok9/WrenAI that referenced this pull request Jul 13, 2026
@Bartok9
Bartok9 force-pushed the fix/spark-strip-semicolon-limit branch from e331b6f to 6a9b5f2 Compare July 13, 2026 02:22
@Bartok9

Bartok9 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@goldmedal Done — rebased onto current main (includes #2478's wren-core-py build fix). Force-pushed; CI should pick it up. Thanks!

@goldmedal

Copy link
Copy Markdown
Collaborator

This PR should also change to the shared strip_trailing_semicolon function.

@goldmedal

Copy link
Copy Markdown
Collaborator

@Bartok9, could you modify this PR?

Bartok9 added 3 commits July 21, 2026 21:57
Spark Connect sql() mishandles trailing semicolons; strip them with
the same terminating-run helper pattern used by other connectors.
Route query/dry_run through connector.base.strip_trailing_semicolon
instead of a per-connector copy, per Canner#2441 consolidation (Canner#2480).
@Bartok9
Bartok9 force-pushed the fix/spark-strip-semicolon-limit branch from 7f1d274 to fda0362 Compare July 22, 2026 01:58
@Bartok9

Bartok9 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@goldmedal Done — refactored to route query and dry_run through the shared strip_trailing_semicolon helper in connector/base.py (dropped the per-connector copy), and rebased onto current main which now includes it via #2480. Tests updated to import the shared helper. Thanks! 🙏

@goldmedal
goldmedal merged commit f4b45ed into Canner:main Jul 22, 2026
11 checks passed
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