Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 362063 - Nested subqueries produce missing conditions between tables.
Summary: Nested subqueries produce missing conditions between tables.
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P2 critical with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-26 09:27 EDT by Roger Wegmann CLA
Modified: 2022-06-09 10:21 EDT (History)
3 users (show)

See Also:


Attachments
Maven project that shows the bug. (5.06 KB, application/zip)
2011-10-26 09:29 EDT, Roger Wegmann CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Roger Wegmann CLA 2011-10-26 09:27:07 EDT
Build Identifier: 2.2.0.v20110202-r8913

As soon as I nest a subquery inside a subquery, a table from the outer query referenced inside the subquery is no longer linked together by a condition. 

I added a small maven project to demonstrate it. It also includes in Test.java the generated SQL which is wrong in my option.

Reproducible: Always

Steps to Reproduce:
1. Extract the attached test.zip.
2. Go into the directory test.
3. Call mvn test.
Comment 1 Roger Wegmann CLA 2011-10-26 09:29:51 EDT
Created attachment 205979 [details]
Maven project that shows the bug.
Comment 2 Roger Wegmann CLA 2011-11-08 10:44:17 EST
Does somebody care about reported bugs?
Comment 3 Tom Ware CLA 2011-11-09 10:41:41 EST
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 4 James Sutherland CLA 2011-11-22 11:44:09 EST
Query is,

/* get from all updates with the same name the newest, restricted to all tags (this is only to show the bug) */
				updateQuery = entityManager.createQuery( 
					"SELECT" 
						+ " u" 
					+ " FROM" 
						+ " Update u" 
					+ " WHERE" 
						+ " u.timestamp = ("
							+ " SELECT"
								+ " MAX( uu.timestamp )"  
							+ " FROM"
								+ " Update uu"  
							+ " JOIN"
								+ " uu.tags t"  
							+ " WHERE"
								+ " uu.name = u.name"
								+ " AND t.id IN ( SELECT tt.id FROM Tag tt )"
						+ " )", 
					Update.class
				);
				updates = updateQuery.getResultList();
				assertEquals( 2, updates.size() );				
				
				/* 

				Bug: Solution 1) There is a condition like "t0.id = t2.id" missing. 
					  Solution 2) t2 should not be there and t0 should be used instead.

				Query generated by Eclipse Link (some brackets are removed):

				SELECT
					t0.id,
					t0.NAME,
					t0.TIMESTAMP 
				FROM
					UPDATE t0 
				WHERE
					t0.TIMESTAMP = (
						SELECT
							MAX(t1.TIMESTAMP) 
						FROM
							TAGS t4,
							TAG t3,
							UPDATE t2,
							UPDATE t1 
						WHERE
							t1.NAME = t2.NAME
							AND t3.id IN (
								SELECT
									t5.id 
								FROM
									TAG t5
							)
							AND t4.update = t1.id
							AND t3.id = t4.tag
					)
				 
				 */
Comment 5 James Sutherland CLA 2011-11-23 14:08:45 EST
I could not recreate any issue with this.

I assume it was fixed.  If you still have issues on the latest build, please re-open the bug.
Comment 6 James Sutherland CLA 2011-11-23 14:10:13 EST
It is also odd, because "= (sub-select)" did not used to be supported (it is now in 2.4).  Previous you would need "= ANY(sub-select)", so is odd.
Comment 7 James Sutherland CLA 2011-11-23 14:10:52 EST
Test added, testNestedSubqueries to complex JPQL suite.
Comment 8 Roger Wegmann CLA 2011-11-23 17:06:14 EST
Also if you use " = ANY ( ... )" it doesn't work. If you try my maven project, you can see the error. And I think there is a difference between returning a wrong result and throwing an exception like "not supported" or something like that. If a query returns a result, I expect either a syntax or not supported exception or a correct result. I still think that this is an important bug. And I can't test 2.4 because it is not yet in the maven repository. Or what is the correct link to the maven repository where I can find the 2.4 version?

updateQuery = entityManager.createQuery( 
  "SELECT" 
    + " u" 
  + " FROM" 
    + " Update u" 
  + " WHERE" 
    + " u.timestamp = ANY ("
      + " SELECT"
        + " MAX( uu.timestamp )"  
      + " FROM"
        + " Update uu"  
      + " JOIN"
        + " uu.tags t"  
      + " WHERE"
        + " uu.name = u.name"
	+ " AND t.id IN ( SELECT tt.id FROM Tag tt )"
    + " )", 
  Update.class
);
updates = updateQuery.getResultList();
assertEquals( 2, updates.size() );				

Still doesn't work!
Comment 9 James Sutherland CLA 2011-12-07 11:19:12 EST
What error do you get?  Did you try the latest 2.4 build from trunk?

This should work with the latest 2.4/trunk build.
Comment 10 James Sutherland CLA 2011-12-12 10:33:01 EST
This was fixed in trunk/2.4, please test on trunk before reopening.

The trunk jar can be downloaded from,

http://www.eclipse.org/eclipselink/downloads/nightly.php
Comment 11 Roger Wegmann CLA 2011-12-12 12:43:17 EST
I tested with the trunk (2.4) and I can confirm that it works there. But as long as the version 2.4 is not released, I think it should be fixed also in at least one released version. This is in my opinion a very dangerous bug, because it doesn't throw an exception, it simply returns wrong results.
Comment 12 Roger Wegmann CLA 2011-12-12 12:57:28 EST
I am sorry, I can also confirm that it works in version 2.3.1 and higher.
Comment 13 Eclipse Webmaster CLA 2022-06-09 10:21:13 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink