Skip to content

v_sum := v_sum + nested_func() ignores prior changes from nested procedure #1124

Closed

Description

@rophy

Bug Report

IvorySQL Version

commit 7261f1c

OS Version (uname -a)

Linux 6.8.0-87-generic #88-Ubuntu SMP PREEMPT_DYNAMIC x86_64 GNU/Linux

Configuration options (config.status --config)

'--prefix=/home/ivorysql/ivorysql' '--enable-debug' '--enable-cassert' '--with-uuid=e2fs' '--with-libxml' '--with-libxslt'

Current Behavior

When a PL/iSQL function contains both nested procedures and nested functions, using a nested function's return value in an assignment expression that also references an outer scope variable causes the outer scope variable to be reset to its initial value.

Specifically, when:

  1. A nested procedure modifies an outer scope variable (e.g., v_sum)
  2. Then a nested function is called in an expression like v_sum := v_sum + nested_func()

The value of v_sum on the right-hand side is evaluated as 0 (its initial value), not the value set by the procedure.

Example showing the bug:

CREATE OR REPLACE FUNCTION test_bug() RETURN NUMBER AS
    v_sum NUMBER := 0;

    PROCEDURE add_val(p_val NUMBER) IS
    BEGIN
        v_sum := v_sum + p_val;
    END add_val;

    FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN p_val * 3;
    END get_30;
BEGIN
    add_val(10);                  -- v_sum should be 10
    v_sum := v_sum + get_30(10);  -- v_sum should be 10 + 30 = 40
    RETURN v_sum;                 -- Returns 30 instead of 40!
END;
/

SELECT test_bug() FROM dual;  -- Returns 30 (WRONG - expected 40)

Expected behavior/code

The function should return 40:

  • After add_val(10), v_sum should be 10
  • get_30(10) returns 30
  • v_sum := v_sum + get_30(10) should compute 10 + 30 = 40
  • The function should return 40

Steps to reproduce

-- Connect to IvorySQL in Oracle compatibility mode (port 1521)

-- Test case 1: Bug reproduction
CREATE OR REPLACE FUNCTION test_bug() RETURN NUMBER AS
    v_sum NUMBER := 0;

    PROCEDURE add_val(p_val NUMBER) IS
    BEGIN
        v_sum := v_sum + p_val;
    END add_val;

    FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN p_val * 3;
    END get_30;
BEGIN
    add_val(10);
    v_sum := v_sum + get_30(10);
    RETURN v_sum;
END;
/

SELECT test_bug() FROM dual;  -- Returns 30 (BUG), expected 40

-- Test case 2: Workaround using separate variable
CREATE OR REPLACE FUNCTION test_workaround() RETURN NUMBER AS
    v_sum NUMBER := 0;
    v_temp NUMBER := 0;

    PROCEDURE add_val(p_val NUMBER) IS
    BEGIN
        v_sum := v_sum + p_val;
    END add_val;

    FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN p_val * 3;
    END get_30;
BEGIN
    add_val(10);
    v_temp := get_30(10);  -- Assign to separate variable first
    v_sum := v_sum + v_temp;
    RETURN v_sum;
END;
/

SELECT test_workaround() FROM dual;  -- Returns 40 (CORRECT)

-- Test case 3: Calling function before procedure works correctly
CREATE OR REPLACE FUNCTION test_order() RETURN NUMBER AS
    v_sum NUMBER := 0;

    FUNCTION get_30(p_val NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN p_val * 3;
    END get_30;

    PROCEDURE add_val(p_val NUMBER) IS
    BEGIN
        v_sum := v_sum + p_val;
    END add_val;
BEGIN
    v_sum := v_sum + get_30(10);  -- v_sum = 30
    add_val(10);                   -- v_sum = 40
    RETURN v_sum;
END;
/

SELECT test_order() FROM dual;  -- Returns 40 (CORRECT)

-- Cleanup
DROP FUNCTION test_bug;
DROP FUNCTION test_workaround;
DROP FUNCTION test_order;

Additional context that can be helpful for identifying the problem

Investigation findings:

  1. The bug only occurs when the nested function's return value is used in an expression that also references the outer scope variable being assigned (v_sum := v_sum + func()).

  2. If the function's return value is assigned to a separate variable first, then added to v_sum, the bug does not occur.

  3. If the nested function is called before the nested procedure, the bug does not occur.

  4. The bug occurs regardless of whether the nested procedure/function have local variables initialized from parameters.

  5. The outer scope variable appears to be reset to its initial value (not just to 0, but to whatever the variable was initialized to).

Test matrix:

Scenario Expected Actual Result
Nested function only 30 30 PASS
Nested procedures only 30 30 PASS
Procedure then function (expression) 40 30 FAIL
Function then procedure 40 40 PASS
Procedure then function (separate var) 40 40 PASS

Suspected cause:

When evaluating the right-hand side of v_sum := v_sum + get_30(10), the call to the nested function get_30() appears to trigger re-initialization of the outer scope's datum values, causing v_sum to revert to its initial value of 0 before the expression is evaluated.

This may be related to how plisql_add_initdatums() or the nested function execution context handles the parent function's local variables.

Workaround:

Use a temporary variable to capture the nested function's return value:

v_temp := nested_func();
v_sum := v_sum + v_temp;

Instead of:

v_sum := v_sum + nested_func();  -- BUG: v_sum gets reset

Activity

  1. added
    bug
    on Dec 7, 2025
  2. bigplaice commented on Dec 8, 2025

    @bigplaice
    Collaborator

    Thanks for reporting this issue! We'll look into it.

  3. OreoYang commented on Dec 16, 2025

    @OreoYang
    Contributor

    Hi Rophy,

    If you're interesting take the action you can leave /assign in the comment, and the issue will be automatically assign the issue to you.

  4. rophy commented on Dec 16, 2025

    @rophy
    ContributorAuthor

    Hi Rophy,

    If you're interesting take the action you can leave /assign in the comment, and the issue will be automatically assign the issue to you.

    Any checklist on what I should do to close the issue and/or PR? Would like to have a real postgresql experts to review the code.

  5. OreoYang commented on Dec 18, 2025

    @OreoYang
    Contributor

    Hi Rophy,
    If you're interesting take the action you can leave /assign in the comment, and the issue will be automatically assign the issue to you.

    Any checklist on what I should do to close the issue and/or PR? Would like to have a real postgresql experts to review the code.

    No action is required from you. Please give us more time review your PRs 😄

  6. OreoYang commented on Dec 19, 2025

    @OreoYang
    Contributor

    Narrow down the root cause

    CREATE OR REPLACE FUNCTION test() RETURN int AS
        v_sum int := 99;
        v_result int;
        
        FUNCTION get_30(p_val int) RETURN int IS
        BEGIN
            RETURN p_val * 3;
        END;
    BEGIN
        v_sum := v_sum + 10;  -- v_sum = 109
        v_result := v_sum + get_30(10);  -- Expected: 139, Got: 139
        RETURN v_result;
    END;
    /
    
  7. added a commit that references this issue on Jan 4, 2026
  8. moved this from Backlog to Done in IvorySQL 5.3on Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions