Issue Details (XML | Word | Printable)

Key: DERBY-3301
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Thomas Nielsen
Reporter: Craig Russell
Votes: 0
Watchers: 2
Operations

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

Incorrect result from query with nested EXIST

Created: 03/Jan/08 06:51 PM   Updated: 30/Jun/09 04:12 PM
Return to search
Component/s: SQL
Affects Version/s: 10.1.3.1, 10.2.1.6, 10.3.2.1
Fix Version/s: 10.3.3.0, 10.4.1.3

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works d3301-queryplan.log 2008-01-18 10:14 AM Thomas Nielsen 8 kB
File Licensed for inclusion in ASF works derby-3301-1.diff 2008-01-22 08:48 AM Thomas Nielsen 4 kB
File Licensed for inclusion in ASF works derby-3301-1.stat 2008-01-22 08:48 AM Thomas Nielsen 0.2 kB
File Licensed for inclusion in ASF works derby-3301-2.diff 2008-01-23 09:13 AM Thomas Nielsen 5 kB
File Licensed for inclusion in ASF works derby-3301-3.diff 2008-01-23 08:34 PM Thomas Nielsen 6 kB
File Licensed for inclusion in ASF works derby-3301-3b.diff 2008-01-23 09:32 PM Thomas Nielsen 6 kB
File Licensed for inclusion in ASF works derby-3301-4.diff 2008-01-25 01:35 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-4b.diff 2008-01-26 07:20 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-4b.stat 2008-01-26 07:20 PM Thomas Nielsen 0.2 kB
File Licensed for inclusion in ASF works derby-3301-4c.diff 2008-01-28 06:19 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-5.diff 2008-01-30 07:57 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-6.diff 2008-01-31 08:32 AM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-7.diff 2008-01-31 07:22 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-8.diff 2008-01-31 09:32 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-extra.sql 2008-01-31 08:32 AM Thomas Nielsen 2 kB
File Licensed for inclusion in ASF works derby-3301-test-1.diff 2008-01-24 05:14 PM Thomas Nielsen 8 kB
File Licensed for inclusion in ASF works derby-3301-test-1.stat 2008-01-24 05:14 PM Thomas Nielsen 0.2 kB
File Licensed for inclusion in ASF works derby-3301-test-2.diff 2008-01-26 07:20 PM Thomas Nielsen 9 kB
File Licensed for inclusion in ASF works derby-3301-test-3.diff 2008-01-28 08:37 PM Thomas Nielsen 9 kB
File Licensed for inclusion in ASF works derby-3301-test-3.stat 2008-01-28 07:22 PM Thomas Nielsen 0.2 kB
File Licensed for inclusion in ASF works derby-3301-test-master-2.diff 2008-01-31 09:32 PM Thomas Nielsen 14 kB
File Licensed for inclusion in ASF works derby-3301-test-master-3.diff 2008-02-07 06:54 PM Thomas Nielsen 3 kB
File Licensed for inclusion in ASF works derby-3301-test-master-3.stat 2008-02-07 06:54 PM Thomas Nielsen 0.2 kB
File Licensed for inclusion in ASF works derby-3301-test-master.diff 2008-01-30 09:31 PM Thomas Nielsen 7 kB
File Licensed for inclusion in ASF works derby-3301-test-master.stat 2008-01-30 09:31 PM Thomas Nielsen 0.1 kB
HTML File Licensed for inclusion in ASF works Derby-3301.html 2008-03-19 09:36 PM Craig Russell 4 kB
File Licensed for inclusion in ASF works derby-3301.sql 2008-01-07 05:51 PM Craig Russell 2 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2008-03-25 09:45 AM Thomas Nielsen 4 kB
HTML File Licensed for inclusion in ASF works releaseNote.html 2008-03-19 09:58 PM Craig Russell 4 kB
Issue Links:
Dependants
 
Incorporates
 
Reference

Urgency: Urgent
Issue & fix info: Release Note Needed
Resolution Date: 15/Feb/08 08:23 AM


 Description  « Hide
Derby returns unexpected results from a query with embedded EXIST clauses. The query SQL is generated by the JPOX jdo implementation and is supposed to return all of the PERSONS and PROJECTS where there is an entry in the join table. This query works as expected when using MySQL.

Here's the query:

SELECT UNBOUND_E.PERSONID, UNBOUND_P.PROJID
FROM applicationidentity0.DEPARTMENTS THIS,
     applicationidentity0.PERSONS UNBOUND_E,
     applicationidentity0.PROJECTS UNBOUND_P
WHERE EXISTS (
    SELECT 1 FROM applicationidentity0.PERSONS THIS_EMPLOYEES_E
    WHERE EXISTS (
        SELECT 1 FROM applicationidentity0.PROJECT_MEMBER THIS_EMPLOYEES_E_PROJECTS_P
        WHERE THIS_EMPLOYEES_E_PROJECTS_P."MEMBER" = THIS_EMPLOYEES_E.PERSONID
        AND THIS_EMPLOYEES_E_PROJECTS_P."MEMBER" = THIS_EMPLOYEES_E.PERSONID
        AND THIS_EMPLOYEES_E.DEPARTMENT = THIS.ID
        AND THIS_EMPLOYEES_E.DEPARTMENT = THIS.ID
        AND UNBOUND_P.PROJID = THIS_EMPLOYEES_E_PROJECTS_P.PROJID
        AND UNBOUND_E.PERSONID = THIS_EMPLOYEES_E.PERSONID)
    );
PERSONID |PROJID
-----------------------
3 |1
5 |3
4 |3
2 |1
1 |1
5 rows selected

I'm expecting 7 rows to be returned here, one row for each row in the join table.

Here's the schema:
CREATE TABLE departments (
    ID INTEGER NOT NULL,
    NAME VARCHAR(32) NOT NULL,
    EMP_OF_THE_MONTH INTEGER,
    COMPANYID INTEGER,
    DISCRIMINATOR VARCHAR(255),
    CONSTRAINT DEPTS_COMP_FK FOREIGN KEY (COMPANYID) REFERENCES companies,
    CONSTRAINT DEPTS_PK PRIMARY KEY (ID)
);

CREATE TABLE persons (
    PERSONID INTEGER NOT NULL,
    FIRSTNAME VARCHAR(32) NOT NULL,
    LASTNAME VARCHAR(32) NOT NULL,
    MIDDLENAME VARCHAR(32),
    BIRTHDATE TIMESTAMP NOT NULL,
    ADDRID INTEGER,
    STREET VARCHAR(64),
    CITY VARCHAR(64),
    STATE CHAR(2),
    ZIPCODE CHAR(5),
    COUNTRY VARCHAR(64),
    HIREDATE TIMESTAMP,
    WEEKLYHOURS REAL,
    DEPARTMENT INTEGER,
    FUNDINGDEPT INTEGER,
    MANAGER INTEGER,
    MENTOR INTEGER,
    HRADVISOR INTEGER,
    SALARY REAL,
    WAGE REAL,
    DISCRIMINATOR varchar(255) NOT NULL,
    CONSTRAINT PERS_DEPT_FK FOREIGN KEY (DEPARTMENT) REFERENCES departments,
    CONSTRAINT PERS_FUNDDEPT_FK FOREIGN KEY (FUNDINGDEPT) REFERENCES departments,
    CONSTRAINT PERS_MANAGER_FK FOREIGN KEY (MANAGER) REFERENCES persons,
    CONSTRAINT PERS_MENTOR_FK FOREIGN KEY (MENTOR) REFERENCES persons,
    CONSTRAINT PERS_HRADVISOR_FK FOREIGN KEY (HRADVISOR) REFERENCES persons,
    CONSTRAINT EMPS_PK PRIMARY KEY (PERSONID)
);

CREATE TABLE projects (
    PROJID INTEGER NOT NULL,
    NAME VARCHAR(32) NOT NULL,
    BUDGET DECIMAL(11,2) NOT NULL,
    DISCRIMINATOR VARCHAR(255),
    CONSTRAINT PROJS_PK PRIMARY KEY (PROJID)
);
CREATE TABLE project_member (
    PROJID INTEGER REFERENCES projects NOT NULL,
    MEMBER INTEGER REFERENCES persons NOT NULL
);

ij> connect 'jdbc:derby:/Users/clr/apache/jdo/trunk/tck2/target/database/derby/jdotckdb';
ij> set schema applicationidentity0;
0 rows inserted/updated/deleted
ij> select * from persons;
PERSONID |FIRSTNAME |LASTNAME |MIDDLENAME |BIRTHDATE |ADDRID |STREET |CITY |STA&|ZIPC&|COUNTRY |HIREDATE |WEEKLYHOURS |DEPARTMENT |FUNDINGDEPT|MANAGER |MENTOR |HRADVISOR |SALARY |WAGE |DISCRIMINATOR
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 |emp1First |emp1Last |emp1Middle |1970-06-09 21:00:00.0 |NULL |NULL |NULL |NULL|NULL |NULL |1998-12-31 21:00:00.0 |40.0 |NULL |NULL |NULL |NULL |NULL |20000.0 |NULL |org.apache.jdo.tck.pc.company.FullTimeEmployee
2 |emp2First |emp2Last |emp2Middle |1975-12-21 21:00:00.0 |NULL |NULL |NULL |NULL|NULL |NULL |2003-06-30 21:00:00.0 |40.0 |NULL |NULL |NULL |NULL |NULL |10000.0 |NULL |org.apache.jdo.tck.pc.company.FullTimeEmployee
3 |emp3First |emp3Last |emp3Middle |1972-09-04 21:00:00.0 |NULL |NULL |NULL |NULL|NULL |NULL |2002-08-14 21:00:00.0 |19.0 |NULL |NULL |NULL |NULL |NULL |NULL |15.0 |org.apache.jdo.tck.pc.company.PartTimeEmployee
4 |emp4First |emp4Last |emp4Middle |1973-09-05 21:00:00.0 |NULL |NULL |NULL |NULL|NULL |NULL |2001-04-14 21:00:00.0 |0.0 |NULL |NULL |NULL |NULL |NULL |NULL |13.0 |org.apache.jdo.tck.pc.company.PartTimeEmployee
5 |emp5First |emp5Last |emp5Middle |1962-07-04 21:00:00.0 |NULL |NULL |NULL |NULL|NULL |NULL |1998-08-14 21:00:00.0 |0.0 |NULL |NULL |NULL |NULL |NULL |45000.0 |NULL |org.apache.jdo.tck.pc.company.FullTimeEmployee

5 rows selected
ij> select * from projects;
PROJID |NAME |BUDGET |DISCRIMINATOR
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 |orange |2500000.99 |org.apache.jdo.tck.pc.company.Project
2 |blue |50000.00 |org.apache.jdo.tck.pc.company.Project
3 |green |2000.99 |org.apache.jdo.tck.pc.company.Project

3 rows selected
ij> select * from project_member;
PROJID |MEMBER
-----------------------
2 |3
1 |3
2 |2
3 |5
3 |4
1 |2
1 |1

7 rows selected



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #617689 Fri Feb 01 22:48:12 UTC 2008 clr JDO-435 Query with projected variables returns duplicates and extra results that do not satisfy filter
The failing test methods are removed from VariableInResult which now passes.
The failing test methods are added to VariableInResultNavigation which includes several more test methods.
The failures in VariableInResultNavigation are due to one of the following bugs:
DERBY-3301
http://www.jpox.org/servlet/jira/browse/RDBMS-45
http://www.jpox.org/servlet/jira/browse/RDBMS-59
Files Changed
ADD /db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/VariableInResultNavigation.java
MODIFY /db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/VariableInResult.java
ADD /db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/VariableInResultNavigation.java
MODIFY /db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/VariableInResult.java

Repository Revision Date User Message
ASF #618586 Tue Feb 05 09:29:32 UTC 2008 dyre DERBY-3301: Incorrect result from query with nested EXIST
Prevent the optimizer from flattening subqueries that
need to be evaluated to get correct results.

Patch contributed by Thomas Nielsen
Patch files: DERBY-3301-8.diff, DERBY-3301-test-MASTER-2.diff,
DERBY-3301-TEST-3.diff
Files Changed
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/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SubqueryList.java
ADD /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NestedWhereSubqueryTest.java
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java

Repository Revision Date User Message
ASF #619591 Thu Feb 07 19:34:39 UTC 2008 abrown DERBY-3301: Follow-up patch to fix comments in subqueryFlattening.sql so
that they match/explain the printed query plans, which changed slightly
as a result of DERBY-3301.

Contributed by Thomas Nielsen.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/subqueryFlattening.sql

Repository Revision Date User Message
ASF #628661 Mon Feb 18 09:25:07 UTC 2008 dyre DERBY-3301: Incorrect result from query with nested EXIST

Prevent the optimizer from flattening subqueries that
need to be evaluated to get correct results.

Merged with svn merge -r 618585:618586 ../derby-repro

Simple merge with no conflicts; no additional changes were necessary.
Files Changed
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/compile/SubqueryNode.java
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/compile/SubqueryList.java
ADD /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NestedWhereSubqueryTest.java (from /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NestedWhereSubqueryTest.java)
MODIFY /db/derby/code/branches/10.3/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java

Repository Revision Date User Message
ASF #628669 Mon Feb 18 09:42:42 UTC 2008 dyre DERBY-3301, merging to 10.3: Follow-up patch to fix comments in
subqueryFlattening.sql so that they match/explain the printed query plans,
which changed slightly as a result of DERBY-3301.

Merged with svn merge -r 619590:619591 ../derby-repro

Simple merge with no conflicts; no additional changes were necessary.
Files Changed
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/master/subqueryFlattening.out
MODIFY /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/functionTests/tests/lang/subqueryFlattening.sql