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.