Skip to content

fix(athena): strip trailing semicolon on unlimited query path - #2535

Merged
goldmedal merged 4 commits into
Canner:mainfrom
Bartok9:fix/athena-strip-unlimited-semicolon
Jul 27, 2026
Merged

goldmedal merged 4 commits into
Canner:mainfrom
Bartok9:fix/athena-strip-unlimited-semicolon

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Athena only stripped ; when wrapping with LIMIT.
  • Unlimited path forwarded terminators to cursor.execute.
  • Strip once; reuse stripped SQL for both branches.

License

core/wren/** Apache-2.0.

Verification

python3 -m pytest core/wren/tests/unit/test_athena_strip_unlimited_query.py -q

Summary by CodeRabbit

  • Bug Fixes

    • Improved Athena SQL execution by stripping any trailing semicolon before running queries.
    • Ensured the limited-query SQL wrapper isn’t affected by trailing line comments.
  • Tests

    • Added a unit test to confirm the unlimited Athena query path removes trailing semicolons before execution.

@github-actions github-actions Bot added python Pull requests that update Python code core labels Jul 18, 2026
@Bartok9
Bartok9 force-pushed the fix/athena-strip-unlimited-semicolon branch from 7e7f1a6 to e88b4f2 Compare July 18, 2026 06:06
Unlimited Athena executes left client-pasted `;` intact; strip first so
limited and unlimited paths are consistent.
@Bartok9
Bartok9 force-pushed the fix/athena-strip-unlimited-semicolon branch from e88b4f2 to c96b577 Compare July 18, 2026 06:06
@coderabbitai

coderabbitai Bot commented Jul 18, 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: fd99e9fc-3ad7-4715-839e-43c379348d85

📥 Commits

Reviewing files that changed from the base of the PR and between 7aee4ff and a240f9b.

📒 Files selected for processing (1)
  • core/wren/src/wren/connector/athena.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/wren/src/wren/connector/athena.py

Walkthrough

Athena query execution now strips trailing semicolons before execution and reuses the stripped SQL for limit wrapping. A focused unit test verifies semicolon removal on the unlimited query path.

Changes

Athena query execution

Layer / File(s) Summary
Unlimited query execution and regression coverage
core/wren/src/wren/connector/athena.py, core/wren/tests/unit/test_athena_strip_unlimited_query.py
AthenaConnector.query uses semicolon-free SQL for direct execution and limit-wrapped queries. Tests verify that unlimited queries pass stripped SQL to the cursor.

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

Possibly related issues

Possibly related PRs

  • Canner/WrenAI#2457: Modifies Athena query semicolon handling, including subquery wrapping.
  • Canner/WrenAI#2534: Updates the same unlimited-query behavior with matching regression coverage.
  • Canner/WrenAI#2421: Applies related trailing-semicolon preprocessing to Athena EXPLAIN SQL.

Suggested reviewers: goldmedal

Poem

A rabbit trims SQL with care,
No trailing semicolon hiding there.
Queries hop, wrappers align,
Athena runs them fine.
Clean little statements everywhere!

🚥 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 clearly matches the main change: stripping a trailing semicolon for Athena unlimited queries.
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.

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/tests/unit/test_athena_strip_unlimited_query.py`:
- Around line 9-31: Replace both source-text and dummy-function tests with one
runtime behavior test using a mocked AthenaConnector, preferably via the
existing _make_mock_connector() helper or the setup pattern from
test_athena_limit_pushdown.py. Call connector.query("SELECT 1;") without a limit
and assert the mocked cursor.execute receives "SELECT 1", verifying the actual
connector interaction.
🪄 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: 545cfa47-6b07-4a00-b940-598cdcb8696f

📥 Commits

Reviewing files that changed from the base of the PR and between 3dac00a and c96b577.

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

Bartok9 added 2 commits July 18, 2026 04:05
Unit collection failed: f-string expression cannot include backslash.
Move re.sub out of the f-string. Apply ruff format on query wrap.
@Bartok9

Bartok9 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @coderabbitai — good call. Replaced the source-text/dummy-function tests with a single runtime behavior test using a mocked AthenaConnector (same pattern as test_athena_limit_pushdown.py): calls connector.query("SELECT 1;") with no limit and asserts the mocked cursor receives SELECT 1. Pushed.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@Bartok9 Thanks—this runtime-level assertion directly validates the unlimited execution path and is a much more robust regression test.

🐇 ✅


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@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_athena_strip_unlimited_query.py (1)

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

Remove unnecessary _CM context manager and contextlib.closing patch.

MagicMock handles method calls out of the box. Since cursor is already a mock (and its close method is explicitly mocked on line 29), contextlib.closing will natively delegate the exit call to cursor.close() without raising any errors.

Removing this custom context manager and the standard library patch eliminates redundant boilerplate and keeps the test purely focused on connector behavior.

♻️ Proposed refactor
-class _CM:
-    def __init__(self, obj):
-        self.obj = obj
-
-    def __enter__(self):
-        return self.obj
-
-    def __exit__(self, *a):
-        return False
-
-
 def _make_mock_connector():
     connector = AthenaConnector.__new__(AthenaConnector)
     connector.connection = MagicMock()
     cursor = MagicMock()
     cursor.close = MagicMock()
     connector.connection.cursor.return_value = cursor
     return connector, cursor
 
 
 def test_query_unlimited_path_strips_trailing_semicolon():
     connector, cursor = _make_mock_connector()
-    with (
-        patch(
-            "wren.connector.athena._build_athena_arrow_table", return_value=MagicMock()
-        ),
-        patch("wren.connector.athena.contextlib.closing", side_effect=lambda c: _CM(c)),
-    ):
+    with patch(
+        "wren.connector.athena._build_athena_arrow_table", return_value=MagicMock()
+    ):
         connector.query("SELECT 1;")
🤖 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_athena_strip_unlimited_query.py` around lines 14 -
41, Remove the `_CM` helper and the `contextlib.closing` patch from
`test_query_unlimited_path_strips_trailing_semicolon`. Keep the `cursor` mock
and its explicit `close` setup in `_make_mock_connector`, allowing the test to
exercise the connector with the native `contextlib.closing` behavior.
🤖 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_athena_strip_unlimited_query.py`:
- Around line 14-41: Remove the `_CM` helper and the `contextlib.closing` patch
from `test_query_unlimited_path_strips_trailing_semicolon`. Keep the `cursor`
mock and its explicit `close` setup in `_make_mock_connector`, allowing the test
to exercise the connector with the native `contextlib.closing` behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 65a30c31-515d-4b86-b016-df4f5f30dbe5

📥 Commits

Reviewing files that changed from the base of the PR and between fce7ac7 and 7aee4ff.

📒 Files selected for processing (1)
  • core/wren/tests/unit/test_athena_strip_unlimited_query.py

Comment thread core/wren/src/wren/connector/athena.py Resolved
@Bartok9

Bartok9 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @goldmedal — done. Folded the intermediate stripped variable into executed directly (executed = strip_trailing_semicolon(sql), then reused in the limit-wrap branch). Pushed in a240f9b.

@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

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