Skip to content

Fix package datum misresolution in plisql_param_compile - #1381

Merged
gaoxueyu merged 1 commit into
IvorySQL:masterfrom
db-nikhil:db-nikhil/pkg_var_bugfix
Jul 17, 2026
Merged

gaoxueyu merged 1 commit into
IvorySQL:masterfrom
db-nikhil:db-nikhil/pkg_var_bugfix

Conversation

@db-nikhil

@db-nikhil db-nikhil commented Jul 7, 2026 •

Copy link
Copy Markdown

Fixes #1382

plisql_param_compile rebinds dno to the resolved datum's own dno, which for a package-qualified reference lives in the package's datum namespace rather than the calling function's. That dno was then compared against expr->target_param (a dno in the calling function's namespace to decide whether to install the plisql_param_eval_var_check fast path. Unlike its siblings plisql_param_eval_var/_ro, that path indexes estate->datums[dno] directly with no pkgoid awareness, so a coincidental numeric match between the two namespaces (common for small packages/blocks, e.g. both being dno 0) made it read and return an unrelated local variable's value instead of the package datum.

Symptom: referencing a package CONSTANT/variable more than once while evaluating a simple expression could silently return the wrong value

  • either empty (uninitialized target) or the stale value of whatever variable happened to share that dno in the calling block.

Skip that fast path for package-qualified datums so they fall through to plisql_param_eval_var_ro, which already resolves package datums correctly via get_package_datum_bydno(). Adds a regression case to ora_package.sql reproducing the exact failure pattern.

Reported-by: Yasir Hussain Shah [email protected]

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Resolved inconsistent behavior when repeatedly evaluating package constants in expressions, especially for CRLF/newline-containing strings.
    • Improved evaluation for simple expressions involving package-qualified references to ensure correct variable handling.
  • Tests

    • Added a new regression test that exercises repeated expression building using both a package constant and a mutable package variable, including validation of CRLF/newline results and expected pass/fail checks.

@coderabbitai

coderabbitai Bot commented Jul 7, 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 983baa5d-10b9-4d9b-8327-7f300f1f035b

📥 Commits

Reviewing files that changed from the base of the PR and between 6fb45e7 and 3363aae.

📒 Files selected for processing (3)
  • src/oracle_test/regress/expected/ora_package.out
  • src/oracle_test/regress/sql/ora_package.sql
  • src/pl/plisql/src/pl_exec.c
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/oracle_test/regress/sql/ora_package.sql
  • src/pl/plisql/src/pl_exec.c
  • src/oracle_test/regress/expected/ora_package.out

📝 Walkthrough

Walkthrough

Adjusts the varlena optimization check in plisql_param_compile() to require an invalid pkgoid, and adds regression tests that repeat CRLF constant and variable concatenation checks in a package block.

Changes

Package Constant Fast-Path Fix and Regression Test

Layer / File(s) Summary
Fix param compile optimization guard
src/pl/plisql/src/pl_exec.c
Adds a pkgoid invalidity requirement to the dno-matching condition used to select plisql_param_eval_var_check, with an explanatory comment.
Regression test for repeated constant usage
src/oracle_test/regress/sql/ora_package.sql, src/oracle_test/regress/expected/ora_package.out
Adds pkg_const_repeat_test, a DO block that validates repeated concatenation behavior for a CRLF constant and a mutable package variable, and the matching expected output.

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

Suggested reviewers: OreoYang, jiaoshuntian

🚥 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 accurately summarizes the core fix in plisql_param_compile and matches the regression test added for the bug.
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.

🧹 Nitpick comments (2)
src/pl/plisql/src/pl_exec.c (1)

7101-7118: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Fix looks correct.

The added !OidIsValid(((PLiSQL_var *) datum)->pkgoid) guard correctly prevents the fast path from misapplying to package-qualified datums, matching how get_package_datum_bydno dispatches on pkgoid. The explanatory comment is accurate and helpful.

Minor nit: datum->pkgoid is already accessible directly on the base PLiSQL_datum struct (as used at Line 7145), so the cast to (PLiSQL_var *) datum here is unnecessary.

🧹 Optional cleanup
-		if (isvarlena && dno == expr->target_param && expr->expr_simple_expr &&
-			!OidIsValid(((PLiSQL_var *) datum)->pkgoid))
+		if (isvarlena && dno == expr->target_param && expr->expr_simple_expr &&
+			!OidIsValid(datum->pkgoid))
🤖 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 `@src/pl/plisql/src/pl_exec.c` around lines 7101 - 7118, The fast-path guard in
the PL/pgSQL parameter setup is correct, but the new `pkgoid` check is using an
unnecessary cast to `PLiSQL_var` even though `pkgoid` is available on
`PLiSQL_datum`. Update the condition in this `plisql_param_eval_var_check`
selection logic to read `pkgoid` directly from `datum`, keeping the existing
`dno`, `expr->target_param`, and `expr->expr_simple_expr` checks unchanged.
src/oracle_test/regress/sql/ora_package.sql (1)

6133-6171: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Consider covering the non-constant variable case too.

The PR objectives mention the bug affects "repeated references to a package CONSTANT or variable," but this test only exercises the CONSTANT scenario. Since the underlying misresolution is about dno/pkgoid handling regardless of constant-ness, adding a parallel case for a mutable package variable would close the coverage gap.

As per path instructions, **/sql/*.sql files should "Ensure comprehensive coverage of features."

🤖 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 `@src/oracle_test/regress/sql/ora_package.sql` around lines 6133 - 6171, Add
coverage for the mutable package variable path in this regression test, since
the current `pkg_const_repeat_test` block only exercises `my_const` and does not
verify repeated references to a package variable. Extend the existing `CREATE OR
REPLACE PACKAGE`/`DO $$` test pattern to include a package variable alongside
`my_const`, then reference it multiple times in separate assignments and
assertions to confirm `plisql_param_compile`/`plisql_param_eval_var_check`
resolve it correctly with the proper package namespace handling. Keep the new
checks adjacent to the existing `pkg_const_repeat_test` scenario so the
regression covers both constant and variable cases.

Source: Path instructions

🤖 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 `@src/oracle_test/regress/sql/ora_package.sql`:
- Around line 6133-6171: Add coverage for the mutable package variable path in
this regression test, since the current `pkg_const_repeat_test` block only
exercises `my_const` and does not verify repeated references to a package
variable. Extend the existing `CREATE OR REPLACE PACKAGE`/`DO $$` test pattern
to include a package variable alongside `my_const`, then reference it multiple
times in separate assignments and assertions to confirm
`plisql_param_compile`/`plisql_param_eval_var_check` resolve it correctly with
the proper package namespace handling. Keep the new checks adjacent to the
existing `pkg_const_repeat_test` scenario so the regression covers both constant
and variable cases.

In `@src/pl/plisql/src/pl_exec.c`:
- Around line 7101-7118: The fast-path guard in the PL/pgSQL parameter setup is
correct, but the new `pkgoid` check is using an unnecessary cast to `PLiSQL_var`
even though `pkgoid` is available on `PLiSQL_datum`. Update the condition in
this `plisql_param_eval_var_check` selection logic to read `pkgoid` directly
from `datum`, keeping the existing `dno`, `expr->target_param`, and
`expr->expr_simple_expr` checks unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2da596ae-ee22-433e-a378-0139ba4eaec5

📥 Commits

Reviewing files that changed from the base of the PR and between 7fb2571 and e378236.

📒 Files selected for processing (3)
  • src/oracle_test/regress/expected/ora_package.out
  • src/oracle_test/regress/sql/ora_package.sql
  • src/pl/plisql/src/pl_exec.c

@db-nikhil
db-nikhil force-pushed the db-nikhil/pkg_var_bugfix branch from e378236 to 6fb45e7 Compare July 7, 2026 07:35
Fixes IvorySQL#1382

plisql_param_compile rebinds dno to the resolved datum's own dno,
which for a package-qualified reference lives in the package's datum
namespace rather than the calling function's. That dno was then
compared against expr->target_param (a dno in the calling function's
namespace to decide whether to install the plisql_param_eval_var_check
fast path. Unlike its siblings plisql_param_eval_var/_ro, that path
indexes estate->datums[dno] directly with no pkgoid awareness, so a
coincidental numeric match between the two namespaces (common for
small packages/blocks, e.g. both being dno 0) made it read and return
an unrelated local variable's value instead of the package datum.

Symptom: referencing a package CONSTANT/variable more than once while
evaluating a simple expression could silently return the wrong value
- either empty (uninitialized target) or the stale value of whatever
variable happened to share that dno in the calling block.

Skip that fast path for package-qualified datums so they fall through
to plisql_param_eval_var_ro, which already resolves package datums
correctly via get_package_datum_bydno(). Adds a regression case to
ora_package.sql reproducing the exact failure pattern.

Reported-by: Yasir Hussain Shah <[email protected]>
@db-nikhil
db-nikhil force-pushed the db-nikhil/pkg_var_bugfix branch from 6fb45e7 to 3363aae Compare July 7, 2026 07:56
@bigplaice

Copy link
Copy Markdown
Collaborator

Thanks for your patch! We'll review it soon.

@db-nikhil

db-nikhil commented Jul 7, 2026 •

Copy link
Copy Markdown
Author

🧹 Nitpick comments (2)

src/pl/plisql/src/pl_exec.c (1)> 7101-7118: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Fix looks correct.
🧹 Optional cleanup

-		if (isvarlena && dno == expr->target_param && expr->expr_simple_expr &&
-			!OidIsValid(((PLiSQL_var *) datum)->pkgoid))
+		if (isvarlena && dno == expr->target_param && expr->expr_simple_expr &&
+			!OidIsValid(datum->pkgoid))

In @src/pl/plisql/src/pl_exec.c around lines 7101 - 7118, The fast-path guard in
the PL/pgSQL parameter setup is correct, but the new pkgoid check is using an
unnecessary cast to PLiSQL_var even though pkgoid is available on
PLiSQL_datum. Update the condition in this plisql_param_eval_var_check
selection logic to read pkgoid directly from datum, keeping the existing
dno, expr->target_param, and expr->expr_simple_expr checks unchanged.

src/oracle_test/regress/sql/ora_package.sql (1)> 6133-6171: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Consider covering the non-constant variable case too.

🤖 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.

Nitpicks have been addressed in the latest commit

@bigplaice bigplaice 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.

This is a nice fix. Suggest to merge it.

@bigplaice bigplaice self-assigned this Jul 8, 2026
@gaoxueyu
gaoxueyu merged commit 82a83d2 into IvorySQL:master Jul 17, 2026
8 checks passed
hs-liuxh pushed a commit to hs-liuxh/IvorySQL that referenced this pull request Sep 2, 2026
Fix package datum misresolution in plisql_param_compile

(cherry picked from commit 82a83d2)
hs-liuxh pushed a commit to hs-liuxh/IvorySQL that referenced this pull request Sep 2, 2026
Fix package datum misresolution in plisql_param_compile

(cherry picked from commit 82a83d2)
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.

Package CONSTANT/variable referenced more than once in an expression returns wrong value

3 participants