Derby
  1. Derby
  2. DERBY-680

In ij, executing a prepared statement with numeric/decimal parameter fails with NullPointerException in J2ME/CDC/FP

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 10.2.1.6
    • Fix Version/s: 10.3.1.4
    • Component/s: Tools
    • Labels:
      None
    • Environment:
      j9_foundation VM in IBM WCTME 5.7

      Description

      NPE is thrown in ij when executing prepared statement which

      • has numeric/decimal parameters
      • does not return any result set

      Repro for this problem is the test lang/cast.sql. This test currently fails in CDC/FP.

      The following lines in the test throw NPE:
      execute q10 using 'values 123456.78';
      execute q11 using 'values 123456.78';

      where q10 is "prepare q10 as 'insert into t1 (num) values cast(? as numeric(18))';"
      and q11 is "prepare q11 as 'insert into t1 (dc) values cast(? as decimal(18))';"

      The stack trace for failure is:
      java.lang.NullPointerException
      at org.apache.derby.impl.tools.ij.util.DisplayMulti(util.java:666)
      at org.apache.derby.impl.tools.ij.utilMain.displayResult(utilMain.java:398)
      at org.apache.derby.impl.tools.ij.utilMain.go(utilMain.java:290)
      at org.apache.derby.impl.tools.ij.Main.go(Main.java:203)
      at org.apache.derby.impl.tools.ij.Main.mainCore(Main.java:169)
      at org.apache.derby.impl.tools.ij.Main.main(Main.java:75)
      at org.apache.derby.tools.ij.main(ij.java:56)

      This happens in the following code. Since the above prepared statements do not return result sets, call to getMetaData() will return null. But in the code, no check is done to see if getMetaData() returns null before calling getColumnType.
      -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      // In J2ME there is no object that represents
      // a DECIMAL value. By default use String to
      // pass values around, but for integral types
      // first convert to a integral type from the DECIMAL
      // because strings like 3.4 are not convertible to
      // an integral type.
      switch (ps.getMetaData().getColumnType(c))

      { case Types.BIGINT: ps.setLong(c, rs.getLong(c)); break; case Types.INTEGER: case Types.SMALLINT: case Types.TINYINT: ps.setInt(c, rs.getInt(c)); break; default: ps.setString(c,rs.getString(c)); break; }

      -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

      1. derby-680_v2.diff
        6 kB
        Deepa Remesh
      2. derby-680_v2.status
        0.3 kB
        Deepa Remesh

        Activity

        Deepa Remesh created issue -
        Hide
        Deepa Remesh added a comment -

        By changing the above code to check for the value returned by ps.getMetaData() , the prepared statements q10 and q11 listed in the description above work. The test lang/cast.sql also passes with JSR169.
        -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        // In J2ME there is no object that represents
        // a DECIMAL value. By default use String to
        // pass values around, but for integral types
        // first convert to a integral type from the DECIMAL
        // because strings like 3.4 are not convertible to
        // an integral type.
        ResultSetMetaData psmd = ps.getMetaData();
        if(psmd != null) {
        switch (psmd.getColumnType(c))

        { case Types.BIGINT: ps.setLong(c, rs.getLong(c)); break; case Types.INTEGER: case Types.SMALLINT: case Types.TINYINT: ps.setInt(c, rs.getInt(c)); break; default: ps.setString(c,rs.getString(c)); break; }

        }
        else

        { ps.setString(c,rs.getString(c)); }

        -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        But the following case does not work because there is no way to get parameter metadata in JSR169 since ParameterMetaData is not available. So, there is no equivalent to "switch (ps.getMetaData().getColumnType(c))" to determine the the column/parameter type and call the corresponding setXXX method.

        -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        ij> create table int_tab (i int);
        ij> prepare i1 as 'insert into int_tab values ';
        ij> execute i1 using 'values 3.4';
        IJ WARNING: Autocommit may close using result set
        ERROR 22018: Invalid character string format for type INTEGER.
        ERROR 22018: Invalid character string format for type INTEGER.
        at org.apache.derby.iapi.error.StandardException.newException(StandardEx
        ception.java:311)
        at org.apache.derby.iapi.types.DataType.invalidFormat(DataType.java:1049
        )
        at org.apache.derby.iapi.types.SQLInteger.setValue(SQLInteger.java:340)
        at org.apache.derby.impl.jdbc.EmbedPreparedStatement.setString(EmbedPrep
        aredStatement.java:452)
        at org.apache.derby.impl.tools.ij.util.DisplayMulti(util.java:684)
        at org.apache.derby.impl.tools.ij.utilMain.displayResult(utilMain.java:3
        98)
        at org.apache.derby.impl.tools.ij.utilMain.go(utilMain.java:290)
        at org.apache.derby.impl.tools.ij.Main.go(Main.java:203)
        at org.apache.derby.impl.tools.ij.Main.mainCore(Main.java:169)
        at org.apache.derby.impl.tools.ij.Main.main(Main.java:75)
        at org.apache.derby.tools.ij.main(ij.java:56)
        -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        I would like to know if anyone has suggestions to handle this. Thanks.

        Show
        Deepa Remesh added a comment - By changing the above code to check for the value returned by ps.getMetaData() , the prepared statements q10 and q11 listed in the description above work. The test lang/cast.sql also passes with JSR169. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // In J2ME there is no object that represents // a DECIMAL value. By default use String to // pass values around, but for integral types // first convert to a integral type from the DECIMAL // because strings like 3.4 are not convertible to // an integral type. ResultSetMetaData psmd = ps.getMetaData(); if(psmd != null) { switch (psmd.getColumnType(c)) { case Types.BIGINT: ps.setLong(c, rs.getLong(c)); break; case Types.INTEGER: case Types.SMALLINT: case Types.TINYINT: ps.setInt(c, rs.getInt(c)); break; default: ps.setString(c,rs.getString(c)); break; } } else { ps.setString(c,rs.getString(c)); } ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- But the following case does not work because there is no way to get parameter metadata in JSR169 since ParameterMetaData is not available. So, there is no equivalent to "switch (ps.getMetaData().getColumnType(c))" to determine the the column/parameter type and call the corresponding setXXX method. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ij> create table int_tab (i int); ij> prepare i1 as 'insert into int_tab values '; ij> execute i1 using 'values 3.4'; IJ WARNING: Autocommit may close using result set ERROR 22018: Invalid character string format for type INTEGER. ERROR 22018: Invalid character string format for type INTEGER. at org.apache.derby.iapi.error.StandardException.newException(StandardEx ception.java:311) at org.apache.derby.iapi.types.DataType.invalidFormat(DataType.java:1049 ) at org.apache.derby.iapi.types.SQLInteger.setValue(SQLInteger.java:340) at org.apache.derby.impl.jdbc.EmbedPreparedStatement.setString(EmbedPrep aredStatement.java:452) at org.apache.derby.impl.tools.ij.util.DisplayMulti(util.java:684) at org.apache.derby.impl.tools.ij.utilMain.displayResult(utilMain.java:3 98) at org.apache.derby.impl.tools.ij.utilMain.go(utilMain.java:290) at org.apache.derby.impl.tools.ij.Main.go(Main.java:203) at org.apache.derby.impl.tools.ij.Main.mainCore(Main.java:169) at org.apache.derby.impl.tools.ij.Main.main(Main.java:75) at org.apache.derby.tools.ij.main(ij.java:56) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- I would like to know if anyone has suggestions to handle this. Thanks.
        Hide
        Deepa Remesh added a comment -

        Attaching a patch 'derby-680.diff'. This patch removes the check "ps.getMetaData().getColumnType(c)" from org.apache.derby.impl.tools.ij.util.java. This was checking ResultSetMetaData, whereas the actual check should be for ParameterMetaData. But ParameterMetaData is not available in JSR169. So currently, get/set String has to be used for all types. With this change, lang/cast.sql does not give NPE. I have added a new master for ejbql.sql. This test was passing because coincidentally ResultSetMetaData and ParameterMetaData were of same type in the statements used.

        With this patch, I ran derbyall with Sun JDK1.4.2 and j9_foundation vm in IBM WCTME5.7. No new failures. Please review/commit this patch.

        Show
        Deepa Remesh added a comment - Attaching a patch 'derby-680.diff'. This patch removes the check "ps.getMetaData().getColumnType(c)" from org.apache.derby.impl.tools.ij.util.java. This was checking ResultSetMetaData, whereas the actual check should be for ParameterMetaData. But ParameterMetaData is not available in JSR169. So currently, get/set String has to be used for all types. With this change, lang/cast.sql does not give NPE. I have added a new master for ejbql.sql. This test was passing because coincidentally ResultSetMetaData and ParameterMetaData were of same type in the statements used. With this patch, I ran derbyall with Sun JDK1.4.2 and j9_foundation vm in IBM WCTME5.7. No new failures. Please review/commit this patch.
        Deepa Remesh made changes -
        Field Original Value New Value
        Attachment derby-680.diff [ 12321332 ]
        Attachment derby-680.status [ 12321333 ]
        Hide
        Daniel John Debrunner added a comment -

        I'm looking at reviewing & committing this patch.

        Show
        Daniel John Debrunner added a comment - I'm looking at reviewing & committing this patch.
        Hide
        Daniel John Debrunner added a comment -

        I think the code is correct but I wonder if the ejbql test should be modified so that a CDC/Foundation specific master file is not required?
        Would the purpose of the test be changed by adding a cast to the expression that causes the failure in J2ME?

        Show
        Daniel John Debrunner added a comment - I think the code is correct but I wonder if the ejbql test should be modified so that a CDC/Foundation specific master file is not required? Would the purpose of the test be changed by adding a cast to the expression that causes the failure in J2ME?
        Hide
        Deepa Remesh added a comment -

        Thanks Dan for looking at this patch. lang/ejbql.sql tests abs and absval functions. I think using cast may not be okay. These are the execute statements which fail in J2ME (leaving out statements which pass):

        create table myint( a int );
        insert into myint values (null);
        autocommit off;

        – Prepare Statements, should pass and return 1
        prepare p1 as 'select abs from myint';
        prepare p1 as 'select 1 from myint where ? <= 4';
        execute p1 using 'values absval( 4.4 )';
        execute p1 using 'values absval( -4.4 )';

        – Prepare Statements, should pass and return 1
        prepare p2 as 'select

        {fn abs(?)}

        from myint';
        prepare p2 as 'select 1 from myint where ? <= 4';
        execute p2 using 'values

        {fn abs( 4.4 )}

        ';
        execute p2 using 'values

        {fn abs( -4.4 )}

        ';
        execute p2 using 'values

        {fn abs( -4.44444444444444444444444 )}

        ';

        Please look at these statements and let me know if you think it is okay to change them .

        Show
        Deepa Remesh added a comment - Thanks Dan for looking at this patch. lang/ejbql.sql tests abs and absval functions. I think using cast may not be okay. These are the execute statements which fail in J2ME (leaving out statements which pass): create table myint( a int ); insert into myint values (null); autocommit off; – Prepare Statements, should pass and return 1 prepare p1 as 'select abs from myint'; prepare p1 as 'select 1 from myint where ? <= 4'; execute p1 using 'values absval( 4.4 )'; execute p1 using 'values absval( -4.4 )'; – Prepare Statements, should pass and return 1 prepare p2 as 'select {fn abs(?)} from myint'; prepare p2 as 'select 1 from myint where ? <= 4'; execute p2 using 'values {fn abs( 4.4 )} '; execute p2 using 'values {fn abs( -4.4 )} '; execute p2 using 'values {fn abs( -4.44444444444444444444444 )} '; Please look at these statements and let me know if you think it is okay to change them .
        Hide
        Daniel John Debrunner added a comment -

        I think it's ok to change these SQL statements to not use prepared statements and ij's using clause.
        The return value of the fn abs is what is being tested here, not the fact that if you pass 4.4 into a prepared statement and compare it to 4 you get false.

        Show
        Daniel John Debrunner added a comment - I think it's ok to change these SQL statements to not use prepared statements and ij's using clause. The return value of the fn abs is what is being tested here, not the fact that if you pass 4.4 into a prepared statement and compare it to 4 you get false.
        Deepa Remesh made changes -
        Attachment derby-680.diff [ 12321332 ]
        Deepa Remesh made changes -
        Attachment derby-680.status [ 12321333 ]
        Hide
        Deepa Remesh added a comment -

        Attaching 'derby-680_v2.diff'. This patch changes the test lang/ejbql.sql as suggested by Dan. Avoids a new master for j9_foundation.

        Show
        Deepa Remesh added a comment - Attaching 'derby-680_v2.diff'. This patch changes the test lang/ejbql.sql as suggested by Dan. Avoids a new master for j9_foundation.
        Deepa Remesh made changes -
        Attachment derby-680_v2.status [ 12321829 ]
        Attachment derby-680_v2.diff [ 12321828 ]
        Deepa Remesh made changes -
        Attachment derby-680_v2.diff [ 12321828 ]
        Deepa Remesh made changes -
        Attachment derby-680_v2.status [ 12321829 ]
        Hide
        Deepa Remesh added a comment -

        lang/cast.sql is failing in J2ME because of some recent additions to the test which require BigDecimal class. Resubmitting the patch without enabling lang/cast.sql.

        Show
        Deepa Remesh added a comment - lang/cast.sql is failing in J2ME because of some recent additions to the test which require BigDecimal class. Resubmitting the patch without enabling lang/cast.sql.
        Deepa Remesh made changes -
        Attachment derby-680_v2.diff [ 12321832 ]
        Attachment derby-680_v2.status [ 12321833 ]
        Hide
        Kathey Marsden added a comment -

        Since the prepared statements have been taken out of lang/ejbql.sql, I wonder if we are still testing the case that was giving the NPE ( ij, executing a prepared statement with numeric/decimal parameters). Is that covered in another test?

        Show
        Kathey Marsden added a comment - Since the prepared statements have been taken out of lang/ejbql.sql, I wonder if we are still testing the case that was giving the NPE ( ij, executing a prepared statement with numeric/decimal parameters). Is that covered in another test?
        Hide
        Deepa Remesh added a comment -

        lang/ejbql.sql tests 'abs' and 'absval' functions. So removing the prepared statements from lang/ejbql.sql does not remove any testing. Dan mentioned this in an earlier comment.

        The test that was giving NPE was lang/cast.sql. With the patch, I have run the test in J2ME and verified that the NPE does not occur. However, this test is still disabled in J2ME because some recent additions to the test require BigDecimal class. Because of these additional tests, it fails towards the end but the failure is not related to this issue.

        Show
        Deepa Remesh added a comment - lang/ejbql.sql tests 'abs' and 'absval' functions. So removing the prepared statements from lang/ejbql.sql does not remove any testing. Dan mentioned this in an earlier comment. The test that was giving NPE was lang/cast.sql. With the patch, I have run the test in J2ME and verified that the NPE does not occur. However, this test is still disabled in J2ME because some recent additions to the test require BigDecimal class. Because of these additional tests, it fails towards the end but the failure is not related to this issue.
        Hide
        Kathey Marsden added a comment -

        I think we should have a regression test that verifies the fix submitted with the fix, , either by enabling cast.sql with J2ME or adding the case to another test that already runs with J2ME

        Show
        Kathey Marsden added a comment - I think we should have a regression test that verifies the fix submitted with the fix, , either by enabling cast.sql with J2ME or adding the case to another test that already runs with J2ME
        Hide
        Daniel John Debrunner added a comment -

        Submitted changes to ejbql test from patch derby-680_v2.diff , as they are separate from the code change to ij.

        Show
        Daniel John Debrunner added a comment - Submitted changes to ejbql test from patch derby-680_v2.diff , as they are separate from the code change to ij.
        Hide
        Daniel John Debrunner added a comment -

        I believe this was fixed by the changes to ij.util for DERBY-2228. The NPE resulted due to fetching the ResultSetMetaData for an INSERT statement which was of course null. The fixed code no longer uses the ResultSetMetaData which was incorrect anyway.

        Show
        Daniel John Debrunner added a comment - I believe this was fixed by the changes to ij.util for DERBY-2228 . The NPE resulted due to fetching the ResultSetMetaData for an INSERT statement which was of course null. The fixed code no longer uses the ResultSetMetaData which was incorrect anyway.
        Daniel John Debrunner made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Fix Version/s 10.3.0.0 [ 12310800 ]
        Hide
        Dyre Tjeldvoll added a comment -

        This issue is resolved and has not been updated in the last 12 months (since 24/Jan/07).

        Show
        Dyre Tjeldvoll added a comment - This issue is resolved and has not been updated in the last 12 months (since 24/Jan/07).
        Dyre Tjeldvoll made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Gavin made changes -
        Workflow jira [ 12343390 ] Default workflow, editable Closed status [ 12797570 ]

          People

          • Assignee:
            Deepa Remesh
            Reporter:
            Deepa Remesh
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development