Issue Details (XML | Word | Printable)

Key: DERBY-3538
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: A B
Reporter: A B
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Derby

NullPointerException during execution for query with LEFT OUTER JOIN whose inner table selects all constants.

Created: 14/Mar/08 05:26 AM   Updated: 04/May/09 06:22 PM
Return to search
Component/s: SQL
Affects Version/s: 10.0.2.0, 10.0.2.1, 10.1.1.0, 10.1.2.1, 10.1.3.1, 10.2.1.6, 10.2.2.0, 10.3.1.4, 10.3.2.1
Fix Version/s: 10.1.3.2, 10.2.2.1, 10.3.3.0, 10.4.1.3, 10.5.1.1

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works d3538_notTested.diff 2008-03-14 05:37 AM A B 0.9 kB
Text File Licensed for inclusion in ASF works derby-3538_diff.txt 2008-03-17 05:21 PM Kathey Marsden 3 kB

Resolution Date: 20/Mar/08 03:31 PM


 Description  « Hide
For a query having a LEFT OUTER JOIN such that the right, or "inner", table is a SELECT subquery whose result column list consists entirely of constants, Derby may throw an execution-time NPE while trying to apply the join predicate. I say "may" because it depends on which join strategy the optimizer chooses.

Using optimizer overrides I was able to reproduce this problem against trunk with the following (admittedly nonsense) query:

  create table t1 (i int, j int);
  insert into t1 values (-1, -2), (-2, -4), (-3, -9);

  select * from
    t1 left outer join
    (select -1 a, 1 b from t1) x0 --DERBY-PROPERTIES joinStrategy=NESTEDLOOP
   on x0.a = t1.i;

I |J |A |B
-----------------------------------------------
-1 |-2 |-1 |1
-1 |-2 |-1 |1
-1 |-2 |-1 |1
ERROR 38000: The exception 'java.lang.NullPointerException' was thrown while evaluating an expression.
ERROR XJ001: Java exception: ': java.lang.NullPointerException'.

Running the same query also failed with the same NPE on 10.0.2.1, even though optimizer overrides don't exist there. So I'm marking all known releases to be affected by this issue.

Note: while this particular query may not make much sense, I have seen a user with a very large, auto-generated query that, when executed, fails due to this problem. So it is worth investigating...

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
A B added a comment - 14/Mar/08 05:37 AM
I did some tracing and it *appears* that the problem is in the "doProjection()" method of ProjectRestrictResultSet. During code generation we recognize that the SELECT has all constants and thus that its result set is "reusable"; see ProjectRestrictNode.generateMinion(), esp. the call to:

  mb.push(resultColumns.reusableResult());

At execution, ProjectResrictResultSet sees that it can reuse the result set so it "caches" the execution row in doProjection() and then just returns that on subsequent calls. However, when returning the cached row, the method does *not* call "setCurrentRow()" with the cached row. In some cases (esp. left outer join) that can mean that the "current execution row" corresponding to the ProjectRestrictResultSet remains null when it should be set to the cached row. Thus when it comes time to evaluate the ON predicate, which references the ProjectRestrictResultSet's execution row, the predicate fails with an NPE because the "current execution row" is not set for that PRRS.

I'm attaching a quick change that resolves the reported NPE. I have *not* run any tests on this, so it's not for commit (yet). I don't think I'll have much time to follow-up with this anytime soon, so if anyone out there can pick it up and take it to completion, that'd be great...

A B made changes - 14/Mar/08 05:37 AM
Field Original Value New Value
Attachment d3538_notTested.diff [ 12377873 ]
Thomas Nielsen added a comment - 14/Mar/08 07:57 AM
Patch, comments and explaination is very good.

Running some tests on it now.

Kathey Marsden added a comment - 17/Mar/08 05:21 PM
Here is the patch with a test added. I ran suites.All and derbyall with Army's patch and all passed. Thomas if you have not seen any issues with the patch I would like to commit and start backporting back to 10.4,3,2 &1.

Kathey Marsden made changes - 17/Mar/08 05:21 PM
Attachment derby-3538_diff.txt [ 12378047 ]
Repository Revision Date User Message
ASF #638425 Tue Mar 18 15:58:54 UTC 2008 kmarsden DERBY-3538 NullPointerException during execution for query with LEFT OUTER JOIN whose inner table selects all constants.

Contributed by Army Brown

Here are some of Army's comments ...
it *appears* that the problem is in the "doProjection()" method of ProjectRestrictResultSet. During code generation we recognize that the SELECT has all constants and thus that its result set is "reusable"; see ProjectRestrictNode.generateMinion(), esp. the call to:

  mb.push(resultColumns.reusableResult());

At execution, ProjectResrictResultSet sees that it can reuse the result set so it "caches" the execution row in doProjection() and then just returns that on subsequent calls. However, when returning the cached row, the method does *not* call "setCurrentRow()" with the cached row. In some cases (esp. left outer join) that can mean that the "current execution row" corresponding to the ProjectRestrictResultSet remains null when it should be set to the cached row. Thus when it comes time to evaluate the ON predicate, which references the ProjectRestrictResultSet's execution row, the predicate fails with an NPE because the "current execution row" is not set for that PRRS.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/joins.sql
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/joins.out
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ProjectRestrictResultSet.java

Repository Revision Date User Message
ASF #639040 Wed Mar 19 22:17:13 UTC 2008 kmarsden DERBY-3538 NullPointerException during execution for query with LEFT OUTER JOIN whose inner table selects all constants.

port revision 638425 from trunk
Files Changed
MODIFY /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/lang/joins.sql
MODIFY /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/master/joins.out
MODIFY /db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/execute/ProjectRestrictResultSet.java

Repository Revision Date User Message
ASF #639157 Thu Mar 20 04:44:09 UTC 2008 kmarsden DERBY-3538 NullPointerException during execution for query with LEFT OUTER JOIN whose inner table selects all constants.

Contributed by Army Brown
port from trunk revison 638425
Files Changed
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/lang/joins.sql
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/master/joins.out
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/execute/ProjectRestrictResultSet.java

Repository Revision Date User Message
ASF #639294 Thu Mar 20 14:11:03 UTC 2008 kmarsden DERBY-3538 NullPointerException during execution for query with LEFT OUTER JOIN whose inner table selects all constants.

Contributeed by Army Brown.
port from trunk revision 638425
Files Changed
MODIFY /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/lang/joins.sql
MODIFY /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/master/joins.out
MODIFY /db/derby/code/branches/10.2/java/engine/org/apache/derby/impl/sql/execute/ProjectRestrictResultSet.java

Kathey Marsden added a comment - 20/Mar/08 03:31 PM
Committed fix and ported to 10.4,10.3,10.2, and 10.1

Kathey Marsden made changes - 20/Mar/08 03:31 PM
Fix Version/s 10.1.3.2 [ 12311972 ]
Resolution Fixed [ 1 ]
Fix Version/s 10.4.1.0 [ 12313072 ]
Fix Version/s 10.5.0.0 [ 12313010 ]
Fix Version/s 10.3.2.2 [ 12312885 ]
Fix Version/s 10.2.2.1 [ 12312251 ]
Assignee A B [ army ]
Status Open [ 1 ] Resolved [ 5 ]
Repository Revision Date User Message
ASF #639316 Thu Mar 20 15:31:50 UTC 2008 kmarsden DERBY-3538 NullPointerException during execution for query with LEFT OUTER JOIN whose inner table selects all constants.

Contributeed by Army Brown.
port from trunk revision 638425
Files Changed
MODIFY /db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/lang/joins.sql
MODIFY /db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/joins.out
MODIFY /db/derby/code/branches/10.1/java/engine/org/apache/derby/impl/sql/execute/ProjectRestrictResultSet.java

Thomas Nielsen added a comment - 25/Mar/08 09:17 AM
Sorry for the very late reply. Just for the record I saw no problems with the patch applied.

Myrna van Lunteren made changes - 04/May/09 06:22 PM
Fix Version/s 10.5.0.0 [ 12313010 ]
Fix Version/s 10.5.1.1 [ 12313771 ]