Skip to content

Fix syntax error editing SQL functions/procedures with a 'return' substring in the body#10061

Open
dpage wants to merge 2 commits into
pgadmin-org:masterfrom
dpage:worktree-fix+issue-10059-proc-pure-sql-detection
Open

Fix syntax error editing SQL functions/procedures with a 'return' substring in the body#10061
dpage wants to merge 2 commits into
pgadmin-org:masterfrom
dpage:worktree-fix+issue-10059-proc-pure-sql-detection

Conversation

@dpage

@dpage dpage commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Editing a SQL-language function/procedure whose body merely contains the substring return (e.g. an INSERT ... RETURNING clause, or an identifier like returned_value) generated invalid SQL on save, failing with a syntax error.
  • Root cause: FunctionView._is_function_def_sql_standard used the regex (?=return), which matches return anywhere in the body, so a plain SQL body was wrongly classified as a SQL-standard (BEGIN ATOMIC / RETURN) body. The update.sql (and related) templates then emitted the body without the AS $BODY$ ... $BODY$ wrapper, producing invalid SQL.
  • Fix: anchor the RETURN form to the start of the body (^\s*return\b) so only genuine SQL-standard bodies are detected. The change is in the shared detection helper, so it covers both functions and procedures.

Test plan

  • New unit test test_is_function_def_sql_standard.py covering: BEGIN ATOMIC body, RETURN form (with/without leading whitespace), RETURNING clause, returned_value identifier, plain SELECT, non-sql language, and empty body — all pass.
  • pycodestyle clean on changed files.

Closes #10059

Summary by CodeRabbit

  • Bug Fixes

    • Fixed incorrect SQL wrapper generation when editing SQL-language functions and procedures containing the word "return", ensuring bodies are classified correctly.
  • Tests

    • Added unit tests to validate detection of SQL-standard function/procedure bodies, including cases with leading whitespace and similar substrings.
  • Documentation

    • Added release note documenting the bug fix.

…string in the body.

The SQL-standard body detection used a regex that matched 'return'
anywhere in the body, so a plain SQL body containing a RETURNING clause
(or an identifier like 'returned_value') was wrongly treated as a
SQL-standard (BEGIN ATOMIC / RETURN) body. The generated CREATE OR
REPLACE statement then dropped the AS $BODY$ ... $BODY$ wrapper,
producing invalid SQL and a syntax error on save.

Anchor the RETURN form to the start of the body so only genuine
SQL-standard bodies are detected. Add unit tests for the detection.

Closes pgadmin-org#10059
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2a46cb52-6b8e-431c-a09d-02deb9ee65c4

📥 Commits

Reviewing files that changed from the base of the PR and between 371fc76 and 693101d.

📒 Files selected for processing (2)
  • web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
  • web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • web/pgadmin/browser/server_groups/servers/databases/schemas/functions/init.py

Walkthrough

Refines _is_function_def_sql_standard to require RETURN at the start of the body, adds unit tests for edge cases, and updates release notes referencing Issue #10059.

Changes

SQL-standard function body detection fix

Layer / File(s) Summary
Fix SQL-standard body detection regex
web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
Updated _is_function_def_sql_standard to anchor return at the start of the body (allowing leading whitespace) and clarified inline comments distinguishing true SQL-standard RETURN bodies from incidental substring matches.
Test coverage for SQL-standard detection
web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py
Added TestIsFunctionDefSqlStandard with a scenarios matrix covering positives (BEGIN ATOMIC, RETURN with leading whitespace) and negatives (RETURNING clause, identifier containing "returned", non-sql language, empty body, RETURN not at start).
Release notes documentation
docs/en_US/release_notes_9_16.rst
Added a bug-fix entry referencing Issue #10059 describing the corrected SQL generation behavior when editing SQL-language functions/procedures containing "return".

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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 and specifically describes the primary fix: addressing a syntax error when editing SQL functions/procedures that contain 'return' in their body.
Linked Issues check ✅ Passed The PR fully addresses issue #10059 by fixing the FunctionView._is_function_def_sql_standard detection logic to properly distinguish SQL-standard bodies from plain SQL containing 'return' substrings, resolving the missing AS $BODY$ wrapper issue.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the reported bug: updating the regex pattern, improving comments, adding comprehensive unit tests, and documenting the fix in release notes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 (1)
web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py (1)

25-65: ⚡ Quick win

Add a regression case for “RETURN not at body start”.

Please add a negative scenario like prosrc='SELECT 1;\nRETURN 2;' (expected False) to lock the “start of body only” contract and prevent future multiline-anchor regressions.

🤖 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
`@web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py`
around lines 25 - 65, Add a negative scenario to the existing scenarios list to
assert that a RETURN not at the start of the body is not considered
SQL-standard: insert an entry into the scenarios list (near the other
SQL-standard cases) with data=dict(lanname='sql', prosrc='SELECT 1;\nRETURN 2;')
and expected=False; ensure the tuple follows the same format as other cases so
the test covering the function that consumes scenarios will catch
multiline-anchor regressions.
🤖 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
`@web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py`:
- Line 1768: The regex r"^\s*return\b" is anchored to line-start under
re.MULTILINE and should instead match only at the start of the entire prosrc
body; replace the caret anchor with a true start-of-string anchor (e.g. \A) so
the check on prosrc (where the pattern is compiled with re.MULTILINE) detects
only bodies that begin with 'return'; apply the same change to the other
occurrences noted around the re.MULTILINE usage (lines referenced in the
comment).

---

Nitpick comments:
In
`@web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py`:
- Around line 25-65: Add a negative scenario to the existing scenarios list to
assert that a RETURN not at the start of the body is not considered
SQL-standard: insert an entry into the scenarios list (near the other
SQL-standard cases) with data=dict(lanname='sql', prosrc='SELECT 1;\nRETURN 2;')
and expected=False; ensure the tuple follows the same format as other cases so
the test covering the function that consumes scenarios will catch
multiline-anchor regressions.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 11218534-b3b6-4a8a-ad33-42e91454abcf

📥 Commits

Reviewing files that changed from the base of the PR and between 04fa05c and 371fc76.

📒 Files selected for processing (3)
  • docs/en_US/release_notes_9_16.rst
  • web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py
  • web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py

Comment thread web/pgadmin/browser/server_groups/servers/databases/schemas/functions/__init__.py Outdated
Address review feedback: \A anchors the RETURN form to the start of the
body so a multi-line plain SQL body whose later line begins with
'return' is not matched under re.MULTILINE.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes an SQL generation bug when saving SQL-language functions/procedures whose body merely contains the substring return, preventing them from being misclassified as SQL-standard bodies and producing invalid DDL.

Changes:

  • Tighten SQL-standard body detection by requiring RETURN to appear at the start of the function/procedure body.
  • Add regression-focused unit tests for _is_function_def_sql_standard covering RETURN, BEGIN ATOMIC, and false-positive cases like RETURNING/identifiers.
  • Document the fix in the v9.16 release notes (Issue #10059).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
web/pgadmin/browser/server_groups/servers/databases/schemas/functions/init.py Refines regex logic for detecting SQL-standard function/procedure bodies to avoid false positives.
web/pgadmin/browser/server_groups/servers/databases/schemas/functions/tests/test_is_function_def_sql_standard.py Adds regression unit tests validating correct classification of SQL-standard vs plain SQL bodies.
docs/en_US/release_notes_9_16.rst Adds a bug-fix release note entry for Issue #10059.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1761 to +1765
# valid regex, these combination a must in definition to detect a
# standard sql or pure sql
# standard sql or pure sql. The RETURN form must start the body
# (as a keyword) so that a plain SQL body which merely contains the
# substring "return" (e.g. a "RETURNING" clause or an identifier
# like "returned_value") is not mistaken for a SQL-standard body.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Updating stored procedures fails with a syntax error, even with valid SQL code.

2 participants