Skip to content

fix: allow parenthesized expressions in SELECT INTO statements - #989

Merged
gaoxueyu merged 1 commit into
IvorySQL:masterfrom
rophy:fix/981
Dec 1, 2025
Merged

gaoxueyu merged 1 commit into
IvorySQL:masterfrom
rophy:fix/981

Conversation

@rophy

@rophy rophy commented Nov 30, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #981

The PL/iSQL parser rejected SELECT INTO statements when the select list started with a parenthesized expression, incorrectly treating them as procedure calls.

Changed the parser to recognize SELECT as a SQL statement even when followed by '('.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected parsing of SELECT statements with parenthesized expressions in INTO clauses, enabling complex expressions like SELECT (arithmetic_expr) INTO variable.
  • Tests

    • Added test cases validating SELECT INTO behavior with various parenthesized expression patterns, including nested parentheses, arithmetic operations, CASE, CAST, and complex nested constructs.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Nov 30, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The PL/iSQL parser grammar was modified to correctly handle SELECT INTO statements with parenthesized expressions. Previously, patterns like SELECT (100 - x) * 0.01 were incorrectly routed as function calls; now they are properly parsed as EXECSQL statements. Comprehensive tests were added to validate the fix.

Changes

Cohort / File(s) Summary
Grammar fix for SELECT with parentheses
src/pl/plisql/src/pl_gram.y
Modified stmt_execsql rule to exclude SELECT keyword from call-path routing when followed by ( or ;, allowing parenthesized expressions in SELECT INTO statements
Validation tests
src/pl/plisql/src/sql/plisql_simple.sql
Added test cases covering single/nested parentheses, arithmetic expressions, function calls, CASE/CAST expressions, and edge cases within SELECT INTO statements

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Grammar logic: Verify the SELECT keyword detection and routing logic avoids unintended side effects on other statement types
  • Test coverage: Confirm test cases comprehensively cover nested parentheses, complex expressions, and edge cases documented in the linked issue

Poem

🐰 A SELECT with parentheses, once so feared,
Now parses with grace—the bug disappeared!
From call-path confusion to execsql's delight,
Our parser's arithmetic now shines so bright! ✨

Pre-merge checks and finishing touches

✅ 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 'fix: allow parenthesized expressions in SELECT INTO statements' accurately describes the main change: enabling SELECT INTO to accept parenthesized expressions by modifying the parser.
Linked Issues check ✅ Passed The PR successfully addresses issue #981 by modifying pl_gram.y to treat SELECT followed by '(' as an EXECSQL statement, and adds comprehensive test cases in plisql_simple.sql validating the fix.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing SELECT INTO with parenthesized expressions: grammar modifications for proper parsing and new test cases for validation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/pl/plisql/src/sql/plisql_simple.sql (1)

128-203: Comprehensive test coverage for parenthesized SELECT expressions.

The test cases thoroughly validate the parser fix across a wide range of scenarios including nested parentheses, nested SELECTs, function calls, CASE expressions, CAST operations, and complex arithmetic. The progressive complexity from simple to edge cases is well-structured.

Consider adding one additional test case to verify that regular function calls (non-SELECT) followed by parentheses still work correctly, ensuring the fix doesn't break existing functionality:

-- Verify function calls still work
create function test_call_func() returns int language plisql
as $$begin return 42; end$$;
/

do $$
declare
 result int;
begin
 result := test_call_func();
 raise notice 'Function call: %', result;
end; $$;

This would provide defense against regressions in the call path routing.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c51d958 and 701929d.

⛔ Files ignored due to path filters (1)
  • src/pl/plisql/src/expected/plisql_simple.out is excluded by !**/*.out
📒 Files selected for processing (2)
  • src/pl/plisql/src/pl_gram.y (1 hunks)
  • src/pl/plisql/src/sql/plisql_simple.sql (1 hunks)
🔇 Additional comments (1)
src/pl/plisql/src/pl_gram.y (1)

2671-2678: LGTM! Parser fix correctly handles SELECT followed by parentheses.

The logic correctly distinguishes between SELECT statements (SQL) and function calls by adding a case-insensitive check for the "select" keyword. When the word is SELECT, it bypasses the call path and routes to EXECSQL, allowing parenthesized expressions like SELECT (100 - x) * 0.01 INTO ... to parse correctly.

Logic verification:

  • pg_strcasecmp($1.ident, "select") != 0 → true when word is NOT "select", routes to call path
  • When word IS "select" (any case) → condition false, falls through to make_execsql_stmt ✓
  • Handles both SELECT(...) and SELECT; cases ✓

Note: This gives SQL keywords precedence over potential user-defined functions with the same name (e.g., a function named "select"), which is standard PL/pgSQL behavior.

@rophy

rophy commented Nov 30, 2025

Copy link
Copy Markdown
Contributor Author

Closing for testing purposes

@rophy rophy closed this Nov 30, 2025
@rophy rophy reopened this Nov 30, 2025
* like SELECT (100 - x) * 0.01 INTO ...
*/
if ((tok == '(' || tok == ';') &&
pg_strcasecmp($1.ident, "select") != 0)

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.

Good solution, in procedural languages body, the statement SELECT (100 - x) * 0.01 INTO result;is wrongly treated as a function call.

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.

PL/iSQL Parser Rejects Parentheses in SELECT INTO Expression

3 participants