IBM Support

LI72808: EARLY EVALUATION OF EXPRESSION IN SQL UDF RESULTS IN RUNTIME ERROR.

Subscribe

You can track all active APARs for this component.

 

APAR status

  • Closed as program error.

Error description

  • An expression within a SQL UDF that should not be executed due
    to the procedural logic (e.g. within an IF statement) is
    executed anyway and may result in a runtime error.
    
    To illustrate the problem, consider the following UDF:
    
    --------------------
    CREATE FUNCTION GETDATE( iYear VARCHAR(4), iMonth VARCHAR(2) )
        RETURNS DATE
        LANGUAGE SQL
    BEGIN atomic
        IF iYear BETWEEN '0000' AND '9999' AND
           iMonth BETWEEN '01' AND '12'
        THEN
          RETURN DATE( iYear || iMonth || '01' ); --
        END IF; --
    
        RETURN NULL; --
    END;
    --------------------
    
    When executed with a month of '13', the expression "DATE( iYear
    || iMonth || '01' )" is evaluated despite the fact that the IF
    predicate is false and results in a runtime error:
    
    
    VALUES GETDATE( '2007', '13' );
    SQL0181N  The string representation of a datetime value is out
    of range.
    SQLSTATE=22007
    
    The expression is executed due to early evaluation optimization.
    

Local fix

  • Replace the input argument variables in the failing expression
    with local variables, for example:
    
    --------------------
    CREATE FUNCTION GETDATE( iYear VARCHAR(4), iMonth VARCHAR(2) )
        RETURNS DATE
        LANGUAGE SQL
    BEGIN atomic
        DECLARE year  VARCHAR(4); --
        DECLARE month VARCHAR(2); --
    
        IF iYear BETWEEN '0000' AND '9999' AND
           iMonth BETWEEN '01' AND '12'
        THEN
          set year = iYear; --
          set month = iMonth; --
          RETURN DATE( year || month || '01' ); --
        END IF; --
    
        RETURN NULL; --
    END;
    --------------------
    

Problem summary

  • This defect affects all V82, V91 and V95 users.
    
    ERROR DESCRIPTION:
    An expression within a SQL UDF that should not be executed due
    to the procedural logic (e.g. within an IF statement) is
    executed anyway and may result in a runtime error.
    
    To illustrate the problem, consider the following UDF:
    
    --------------------
    CREATE FUNCTION GETDATE( iYear VARCHAR(4), iMonth VARCHAR(2) )
        RETURNS DATE
        LANGUAGE SQL
    BEGIN atomic
        IF iYear BETWEEN '0000' AND '9999' AND
           iMonth BETWEEN '01' AND '12'
        THEN
          RETURN DATE( iYear || iMonth || '01' );
        END IF;
    
        RETURN NULL;
    END;
    --------------------
    
    When executed with a month of '13', the expression "DATE( iYear
    || iMonth || '01' )" is evaluated despite the fact that the IF
    predicate is false and results in a runtime error:
    
    VALUES GETDATE( '2007', '13' );
    SQL0181N  The string representation of a datetime value is out
    of range.
    SQLSTATE=22007
    
    The expression is executed due to early evaluation optimization.
    
    LOCAL FIX:
    Replace the input argument variables in the failing expression
    with local variables, for example:
    
    --------------------
    CREATE FUNCTION GETDATE( iYear VARCHAR(4), iMonth VARCHAR(2) )
        RETURNS DATE
        LANGUAGE SQL
    BEGIN atomic
        DECLARE year  VARCHAR(4);
        DECLARE month VARCHAR(2);
    
        IF iYear BETWEEN '0000' AND '9999' AND
           iMonth BETWEEN '01' AND '12'
        THEN
          set year = iYear;
          set month = iMonth;
          RETURN DATE( year || month || '01' );
        END IF;
    
        RETURN NULL;
    END;
    --------------------
    

Problem conclusion

  • First fixed in DB2 UDB Version 9.1, FixPak 5.
    

Temporary fix

Comments

APAR Information

  • APAR number

    LI72808

  • Reported component name

    DB2 UDE ESE LIN

  • Reported component ID

    5765F4104

  • Reported release

    910

  • Status

    CLOSED PER

  • PE

    NoPE

  • HIPER

    NoHIPER

  • Special Attention

    NoSpecatt

  • Submitted date

    2007-12-03

  • Closed date

    2008-06-24

  • Last modified date

    2008-06-24

  • APAR is sysrouted FROM one or more of the following:

  • APAR is sysrouted TO one or more of the following:

    LI72809 LI72810

Fix information

  • Fixed component name

    DB2 UDE ESE LIN

  • Fixed component ID

    5765F4104

Applicable component levels

  • R820 PSY

       UP

  • R910 PSY

       UP

  • R950 PSY

       UP

[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SSEPGG","label":"DB2 for Linux- UNIX and Windows"},"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"910","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

Modified date:
16 October 2021