Derby
  1. Derby
  2. DERBY-1030

In some situations a RETURNS NULL ON NULL function is called when one ot its parameters is NULL

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 10.6.1.0
    • Fix Version/s: 10.6.1.0
    • Component/s: SQL
    • Labels:
      None
    • Urgency:
      Normal
    • Issue & fix info:
      Repro attached
    • Bug behavior facts:
      Wrong query result

      Description

      The NULL argument to the function has to come from another function and that function's Java method has to return a Java object type corresponding to a fixed length SQL type, such as DATE, TIME and TIMESTAMP.

      Will attach repro scripts.

      1. derby479.sql
        0.9 kB
        Daniel John Debrunner
      2. derby479.java
        0.8 kB
        Daniel John Debrunner
      3. derby-1030-01-aa-disableOptimization.diff
        2 kB
        Rick Hillegas

        Issue Links

          Activity

          Hide
          Daniel John Debrunner added a comment -

          Called derby-479 as that's the issue I was looking at when I saw this related but separate issue.

          Show
          Daniel John Debrunner added a comment - Called derby-479 as that's the issue I was looking at when I saw this related but separate issue.
          Hide
          Daniel John Debrunner added a comment -

          Single patch could fix DERBY-479 and DERBY-1030

          Show
          Daniel John Debrunner added a comment - Single patch could fix DERBY-479 and DERBY-1030
          Hide
          Daniel John Debrunner added a comment -

          Single patch could fix DERBY-479 and DERBY-1030

          Show
          Daniel John Debrunner added a comment - Single patch could fix DERBY-479 and DERBY-1030
          Hide
          Mamta A. Satoor added a comment -

          Busy with grant revoke work, so unassigning myself from this one.

          Show
          Mamta A. Satoor added a comment - Busy with grant revoke work, so unassigning myself from this one.
          Hide
          Rick Hillegas added a comment -

          Moving to 10.2.2.0.

          Show
          Rick Hillegas added a comment - Moving to 10.2.2.0.
          Hide
          Daniel John Debrunner added a comment -

          Not going to get to this in the 10.2.2 timeframe

          Show
          Daniel John Debrunner added a comment - Not going to get to this in the 10.2.2 timeframe
          Hide
          Rick Hillegas added a comment - - edited

          The bug is caused by an optimization intended to eliminate needless switching between the Java domain and the SQL domain.

          Two compile-time classes manage this switching:

          JavaToSQLNode - This class is responsible for changing Java data values into SQL data values that can be used by other expressions in the query. So for instance, this class is responsible for compiling the code which changes Integer and primitive int into SQLInteger.

          SQLToJavaNode - This class is responsible for the reverse operation, that is, for turning SQL data values into Java data values before they are passed as arguments to Java methods. So for instance, this class is responsible for compiling the runtime code which changes a SQLInteger into a primitive int before invoking a Java method.

          SQLToJavaNode also compiles the NULL ON NULL INPUT check: If the SQL data value is going to be used as the argument to a NULL ON NULL INPUT routine, then the SQLToJavaNode compiles short-circuiting logic that prevents the routine from being called if the SQL data value is null.

          Some performance can be lost when switching between the Java and SQL domains. We eliminate this switching if a Java data value becomes a SQL data value only to become a Java data value immediately afterwards. This happens when the return value of a Function is passed as an argument to another Routine. E.g.:

          values ( outerFunction( innerFunction( 1 ) ) )

          In this case the AST shows a SQLToJavaNode wrapping a JavaToSQLNode returned by innerFunction. An optimization was put in to eliminate both conversion nodes in this case and to pass the return value of innerFunction directly as an argument to outerFunction.

          This optimization has a couple problems:

          1) If innerFunction is a NULL ON NULL INPUT Function and innerFunction is called with null inputs, then Derby tries to pass a null to outerFunction. This produces a verification error if outerFunction takes primitive arguments. DERBY-479 fixes this by eliminating the optimization in this case.

          2) If outerFunction is a NULL ON NULL INPUT Function, then the elimination of the SQLToJavaNode means that the NULL ON NULL INPUT check is not performed. This is the bug described by this JIRA.

          The fix is to also eliminate the optimization if the outer function is NULL ON NULL INPUT.

          Show
          Rick Hillegas added a comment - - edited The bug is caused by an optimization intended to eliminate needless switching between the Java domain and the SQL domain. Two compile-time classes manage this switching: JavaToSQLNode - This class is responsible for changing Java data values into SQL data values that can be used by other expressions in the query. So for instance, this class is responsible for compiling the code which changes Integer and primitive int into SQLInteger. SQLToJavaNode - This class is responsible for the reverse operation, that is, for turning SQL data values into Java data values before they are passed as arguments to Java methods. So for instance, this class is responsible for compiling the runtime code which changes a SQLInteger into a primitive int before invoking a Java method. SQLToJavaNode also compiles the NULL ON NULL INPUT check: If the SQL data value is going to be used as the argument to a NULL ON NULL INPUT routine, then the SQLToJavaNode compiles short-circuiting logic that prevents the routine from being called if the SQL data value is null. Some performance can be lost when switching between the Java and SQL domains. We eliminate this switching if a Java data value becomes a SQL data value only to become a Java data value immediately afterwards. This happens when the return value of a Function is passed as an argument to another Routine. E.g.: values ( outerFunction( innerFunction( 1 ) ) ) In this case the AST shows a SQLToJavaNode wrapping a JavaToSQLNode returned by innerFunction. An optimization was put in to eliminate both conversion nodes in this case and to pass the return value of innerFunction directly as an argument to outerFunction. This optimization has a couple problems: 1) If innerFunction is a NULL ON NULL INPUT Function and innerFunction is called with null inputs, then Derby tries to pass a null to outerFunction. This produces a verification error if outerFunction takes primitive arguments. DERBY-479 fixes this by eliminating the optimization in this case. 2) If outerFunction is a NULL ON NULL INPUT Function, then the elimination of the SQLToJavaNode means that the NULL ON NULL INPUT check is not performed. This is the bug described by this JIRA. The fix is to also eliminate the optimization if the outer function is NULL ON NULL INPUT.
          Hide
          Rick Hillegas added a comment -

          Attaching derby-1030-01-aa-disableOptimization.diff. This removes the over-eager optimization of switching between language domains when the outer Function is NULL ON NULL INPUT.

          Touches the following files:

          M java/engine/org/apache/derby/impl/sql/compile/StaticMethodCallNode.java

          Removes the optimization.

          M java/testing/org/apache/derbyTesting/functionTests/tests/lang/RoutineTest.java

          As part of DERBY-479, a test case was added for DERBY-1030, enshrining the wrong behavior described by this bug. The test case is changed to enshrine the correct behavior.

          The regression tests passed cleanly for me except for an error in GeneratedColumnsTest which was recently introduced into the trunk by other work:

          There was 1 failure:
          1) test_031_derby_4413(org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsTest)junit.framework.ComparisonFailure: expected:<[1]> but was:<[2]>
          at org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsHelper.assertResults(GeneratedColumnsHelper.java:346)
          at org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsHelper.assertResults(GeneratedColumnsHelper.java:310)
          at org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsTest.test_031_derby_4413(GeneratedColumnsTest.java:5442)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at org.apache.derbyTesting.junit.BaseTestCase.runBare(BaseTestCase.java:109)
          at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24)
          at junit.extensions.TestSetup$1.protect(TestSetup.java:21)
          at junit.extensions.TestSetup.run(TestSetup.java:25)
          at org.apache.derbyTesting.junit.BaseTestSetup.run(BaseTestSetup.java:57)
          at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24)
          at junit.extensions.TestSetup$1.protect(TestSetup.java:21)
          at junit.extensions.TestSetup.run(TestSetup.java:25)

          FAILURES!!!
          Tests run: 34, Failures: 1, Errors: 0

          Show
          Rick Hillegas added a comment - Attaching derby-1030-01-aa-disableOptimization.diff. This removes the over-eager optimization of switching between language domains when the outer Function is NULL ON NULL INPUT. Touches the following files: M java/engine/org/apache/derby/impl/sql/compile/StaticMethodCallNode.java Removes the optimization. M java/testing/org/apache/derbyTesting/functionTests/tests/lang/RoutineTest.java As part of DERBY-479 , a test case was added for DERBY-1030 , enshrining the wrong behavior described by this bug. The test case is changed to enshrine the correct behavior. The regression tests passed cleanly for me except for an error in GeneratedColumnsTest which was recently introduced into the trunk by other work: There was 1 failure: 1) test_031_derby_4413(org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsTest)junit.framework.ComparisonFailure: expected:< [1] > but was:< [2] > at org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsHelper.assertResults(GeneratedColumnsHelper.java:346) at org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsHelper.assertResults(GeneratedColumnsHelper.java:310) at org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsTest.test_031_derby_4413(GeneratedColumnsTest.java:5442) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.apache.derbyTesting.junit.BaseTestCase.runBare(BaseTestCase.java:109) at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24) at junit.extensions.TestSetup$1.protect(TestSetup.java:21) at junit.extensions.TestSetup.run(TestSetup.java:25) at org.apache.derbyTesting.junit.BaseTestSetup.run(BaseTestSetup.java:57) at junit.extensions.TestDecorator.basicRun(TestDecorator.java:24) at junit.extensions.TestSetup$1.protect(TestSetup.java:21) at junit.extensions.TestSetup.run(TestSetup.java:25) FAILURES!!! Tests run: 34, Failures: 1, Errors: 0
          Hide
          Bryan Pendleton added a comment -

          Thanks for the clear and detailed explanation, Rick, it was very interesting!

          > JavaToSQLNode - This class is responsible for turning SQL data values into Java data values before
          > they are passed as arguments to Java methods. So for instance, this class is responsible for compiling
          > the runtime code which changes a SQLInteger into a primitive int before invoking a Java method.
          >
          > SQLToJavaNode - This class is responsible for the reverse operation, that is, for changing Java
          > data values into SQL data values that can be used by other expressions in the query. So for instance,
          > this class is responsible for compiling the code which changes Integer and primitive int into SQLInteger.

          Did I misread this? It seems backward to me. It seems like JavaToSQLNode should
          be the one which turns a Java value into a SQL value, and SQLToJavaNode should be the one which
          turns a SQL value into a Java value.

          Show
          Bryan Pendleton added a comment - Thanks for the clear and detailed explanation, Rick, it was very interesting! > JavaToSQLNode - This class is responsible for turning SQL data values into Java data values before > they are passed as arguments to Java methods. So for instance, this class is responsible for compiling > the runtime code which changes a SQLInteger into a primitive int before invoking a Java method. > > SQLToJavaNode - This class is responsible for the reverse operation, that is, for changing Java > data values into SQL data values that can be used by other expressions in the query. So for instance, > this class is responsible for compiling the code which changes Integer and primitive int into SQLInteger. Did I misread this? It seems backward to me. It seems like JavaToSQLNode should be the one which turns a Java value into a SQL value, and SQLToJavaNode should be the one which turns a SQL value into a Java value.
          Hide
          Rick Hillegas added a comment -

          Thanks for catching that howler, Bryan. I have corrected the comment. Cheers.

          Show
          Rick Hillegas added a comment - Thanks for catching that howler, Bryan. I have corrected the comment. Cheers.
          Hide
          Rick Hillegas added a comment -

          Committed derby-1030-01-aa-disableOptimization.diff at subversion revision 886277.

          Show
          Rick Hillegas added a comment - Committed derby-1030-01-aa-disableOptimization.diff at subversion revision 886277.

            People

            • Assignee:
              Rick Hillegas
              Reporter:
              Daniel John Debrunner
            • Votes:
              1 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development