Issue Details (XML | Word | Printable)

Key: DERBY-2370
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: A B
Reporter: Dyre Tjeldvoll
Votes: 1
Watchers: 0
Operations

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

EXISTS may return the wrong value for sub-queries involving set operations

Created: 23/Feb/07 09:14 AM   Updated: 30/Jun/09 04:12 PM
Return to search
Component/s: SQL
Affects Version/s: 10.2.2.0
Fix Version/s: 10.3.1.4

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works d2370_engine_v1.patch 2007-04-20 10:52 PM A B 13 kB
Text File Licensed for inclusion in ASF works d2370_tests_v1.patch 2007-04-20 10:52 PM A B 24 kB
File Licensed for inclusion in ASF works d2370_v1.stat 2007-04-20 10:52 PM A B 0.8 kB
HTML File Licensed for inclusion in ASF works d2370_writeup_v1.html 2007-04-20 10:52 PM A B 24 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2007-06-16 04:35 PM Rick Hillegas 5 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2007-05-22 09:23 PM A B 5 kB
File Licensed for inclusion in ASF works repro.sql 2007-02-23 09:15 AM Dyre Tjeldvoll 1 kB
Issue Links:
Duplicate
 
Incorporates
 

Issue & fix info: Release Note Needed
Resolution Date: 22/May/07 09:24 PM


 Description  « Hide
It seems like EXISTS on a SELECT returning zero rows returns false (as
expected), but EXISTS on INTERSECT of two disjunct sets returns true,
e.g EXISTS (values 1 intersect values 2).

Yip Ng wrote on derby-dev:
I believe its probably got to do with the EXISTS subquery transforming
the original RCL to
a TRUE boolean value for the INTERSECT. So during row comparison at
execution time
for INTERSECT processing since true == true(thus intersects), so it
will always return 'BAD'. Likewise,

select * from ( values 'OK' ) as T where exists (values 1 except values 2);

This supposedly should return 'OK' but because of the boolean
transformation mentioned
above for EXISTS subquery, it will return no rows for EXCEPT
processing.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Dyre Tjeldvoll added a comment - 23/Feb/07 09:15 AM
ij script to reproduce the bug.

Dyre Tjeldvoll made changes - 23/Feb/07 09:15 AM
Field Original Value New Value
Attachment repro.sql [ 12351872 ]
A B made changes - 22/Mar/07 06:53 PM
Link This issue is part of DERBY-2034 [ DERBY-2034 ]
A B made changes - 17/Apr/07 05:22 PM
Assignee A B [ army ]
A B added a comment - 20/Apr/07 10:52 PM
Yip was exactly right in his diagnosis of the problem. I'm attaching a short writeup of the problem and a proposed solution as d2370_writeup_v1.patch.

I'm also attaching a patch, d2370_engine_v1.patch, that implements the change by doing the following:

  1. Adds the ability to mark a FromList as "transparent", and updates FromList.bindExpressions() to
     pass the outer FROM list down (instead of "this") if the FromList is transparent.
  2. Updates FromList.expandAll(...) to account for the fact that outer FROM tables might now appear
     in a nested FromList (as a result of "transparent" FromLists; see code comments for details).
  3. Modifies the "setResultToBooleanTrue()" signature to return a ResultSetNode (it was "void" previously).
  4. Modifies ResultSetNode.setResultToBooleanTrue() to always return "this".
  5. Modifies SetOperatorNode.setResultToBooleanTrue() so that it now creates an internal "SELECT *" query
     whose FROM list contains just the SetOperatorNode. Then we transform the "*" for the new SELECT into
     "TRUE" and leave the SetOperatorNode's result columns UN-transformed. Finally, we mark the new
     SelectNode's FromList as "transparent" and return the new SelectNode.

I've included a corresponding patch, d2370_tests_v1.patch, that contains slight modifications to two tests: lang/union.sql and lang/ResultSetsFromPreparedStatementTest. The latter changes are expected based on comments in the test; the former (lang/union.sql) has a couple of queries that now fail when they used to succeed. However, I think the failures are correct--i.e. that Derby should have been failing prior to these changes and was not--so I've updated lang/union.sql accordingly. I will send an email about this to derby-dev to see if I can get any feedback/suggestions around this. And finally, d2370_tests_v1.patch creates a new JUnit test, lang/ExistsWithSetOpsTest, which captures and builds on the repro.sql script attached to this issue.

I ran derbyall and suites.All with a single failure:

   jdbcapi/parameterMetaDataJdbc30.java

This failure only occurred for the client framework and the diff showed a failure to connect--which doesn't seem related to my changes. When I ran the test independently it passed as expected. So I think it was just a fluke.

Reviews or other feedback would be greatly appreciated, as always.

A B made changes - 20/Apr/07 10:52 PM
Attachment d2370_writeup_v1.html [ 12355962 ]
Attachment d2370_engine_v1.patch [ 12355963 ]
Attachment d2370_tests_v1.patch [ 12355964 ]
A B made changes - 20/Apr/07 10:52 PM
Attachment d2370_v1.stat [ 12355965 ]
A B made changes - 20/Apr/07 10:57 PM
Derby Info [Patch Available, Existing Application Impact]
Dyre Tjeldvoll added a comment - 25/Apr/07 11:13 AM
I have looked at the writeup and and also looked at the
patch. While I am certainly no expert in this area I think the
explanation and solution in the writeup looks very sound. The
patch seem to implement what is suggested, it is clear, well
documented and tested. +1 from me.

Repository Revision Date User Message
ASF #532509 Wed Apr 25 22:26:40 UTC 2007 abrown DERBY-2370: Fix incorrect results for EXISTS queries with SET operator
expressions. In particular:

  1. Adds the ability to mark a FromList as "transparent", and updates
     FromList.bindExpressions() to pass the outer FROM list down (instead
     of "this") if the FromList is transparent.
  2. Updates FromList.expandAll(...) to account for the fact that outer
     FROM tables might now appear in a nested FromList (as a result of
     "transparent" FromLists; see code comments for details).
  3. Modifies the "setResultToBooleanTrue()" signature to return a
     ResultSetNode (it was "void" previously).
  4. Modifies ResultSetNode.setResultToBooleanTrue() to always return "this".
  5. Modifies SetOperatorNode.setResultToBooleanTrue() so that it now creates
     an internal "SELECT *" query whose FROM list contains just the
     SetOperatorNode. Then we transform the "*" for the new SELECT into "TRUE"
     and leave the SetOperatorNode's result columns UN-transformed. Finally,
     mark the new SelectNode's FromList as "transparent" and return the new
     SelectNode.
  6. Updates tests where necessary (including changed behavior in union.sql)
     and adds a new JUnit test, lang/ExistsWithSetOpsTest, to verify new
     behavior.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/union.out
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SubqueryNode.java
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/FromList.java
ADD /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ExistsWithSetOpsTest.java
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ResultSetsFromPreparedStatementTest.java
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/union.sql
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SetOperatorNode.java
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java

A B added a comment - 25/Apr/07 10:30 PM
Thank you for the review, Dyre. I synced with the latest codeline and re-ran derbyall and suites.All as a sanity check, then committed the patch with svn #532509:

  URL: http://svn.apache.org/viewvc?view=rev&rev=532509

As I haven't heard any comments regarding the new behavior for certain queries (namely, throwing an error where Derby used to succeed in some cases), I'm working under the assumption that this is okay and am marking the issue RESOLVED.

Many thanks for your time!

A B made changes - 25/Apr/07 10:30 PM
Derby Info [Existing Application Impact, Patch Available] [Existing Application Impact]
Resolution Fixed [ 1 ]
Fix Version/s 10.3.0.0 [ 12310800 ]
Status Open [ 1 ] Resolved [ 5 ]
Dyre Tjeldvoll made changes - 26/Apr/07 06:23 AM
Status Resolved [ 5 ] Closed [ 6 ]
A B added a comment - 22/May/07 09:22 PM
Reopening to attach release note.

A B made changes - 22/May/07 09:22 PM
Status Closed [ 6 ] Reopened [ 4 ]
Resolution Fixed [ 1 ]
A B added a comment - 22/May/07 09:23 PM
Attaching first draft of the release note.

A B made changes - 22/May/07 09:23 PM
Attachment releaseNote.html [ 12357926 ]
A B added a comment - 22/May/07 09:24 PM
Marking issue resolved (again). I'll wait a few days before closing it in case there are comments on the release note...

A B made changes - 22/May/07 09:24 PM
Status Reopened [ 4 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Derby Info [Existing Application Impact] [Existing Application Impact, Release Note Needed]
Rick Hillegas added a comment - 16/Jun/07 04:35 PM
Scrub the html in the release note so that it can be digested by the SAX parser in the release note generator.

Rick Hillegas made changes - 16/Jun/07 04:35 PM
Attachment releaseNote.html [ 12359931 ]
Dyre Tjeldvoll made changes - 18/Mar/08 10:22 PM
Status Resolved [ 5 ] Closed [ 6 ]
Knut Anders Hatlen made changes - 17/Nov/08 02:00 PM
Link This issue is duplicated by DERBY-3951 [ DERBY-3951 ]
Dag H. Wanvik made changes - 30/Jun/09 04:12 PM
Issue & fix info [Existing Application Impact, Release Note Needed] [Release Note Needed]