This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 347562 - Query parse error when using GROUP BY/HAVING
Summary: Query parse error when using GROUP BY/HAVING
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P2 major with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-28 17:29 EDT by Edward Rayl CLA
Modified: 2022-06-09 10:21 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Edward Rayl CLA 2011-05-28 17:29:15 EDT
Build Identifier: 2.2.0

The query below fails to parse correctly.  This query is on the Oracle sample schema called HR, with the appropriate entities mapped to it.

    select d from Department d group by d having COUNT(d.employees1) = 3

with the following SQL output:

    SQL: SELECT DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID 
    FROM DEPARTMENTS 

This next query works however:

    select d, COUNT(d.employees1) from Department d group by d having COUNT(d.employees1) = 3

with the following SQL output:

    SQL: SELECT t0.DEPARTMENT_ID, t0.DEPARTMENT_NAME, t0.MANAGER_ID, 
        t0.LOCATION_ID, COUNT(t1.EMPLOYEE_ID) 
    FROM DEPARTMENTS t0, EMPLOYEES t1 
    WHERE (t1.DEPARTMENT_ID = t0.DEPARTMENT_ID) 
    GROUP BY t0.DEPARTMENT_ID, t0.DEPARTMENT_NAME, t0.MANAGER_ID, t0.LOCATION_ID 
    HAVING (COUNT(t1.EMPLOYEE_ID) = ?) 

The specification should allow the first query. A portion of Section 4.7 of JSR 317: Java Persistence API, Version 2.0 says;

The HAVING clause is used to filter over the groups, and can contain aggregate functions over
attributes included in the groups and/or functions or other query language operators over the attributes that are used for grouping. It is not required that an aggregate function used in the HAVING clause also be used in the SELECT clause.


Reproducible: Always

Steps to Reproduce:
1. Create JPA entities for the Oracle HR schema
2. Create a JPQL query as shown above.
3. The result of the query should be one and only one department, not 27
Comment 1 Edward Rayl CLA 2011-05-29 10:38:22 EDT
This bug can be reproduced in 2.3.0 Milestone Build RC2.
Comment 2 Edward Rayl CLA 2011-06-03 16:05:32 EDT
This bug can be reproduced in 2.3.0 Milestone Build RC3.

I can email an Eclipse project containing HR entities and a command line JPQL query tool to the assignee if that would be useful.
Comment 3 Tom Ware CLA 2011-06-09 10:03:53 EDT
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 2012-02-01 13:39:43 EST
The issue seems to be a ReadAllQuery is used for the query, but a ReportQuery is required.  JPQL should check the group by and create a ReportQuery.

Will fix with Hermes.
Comment 5 James Sutherland CLA 2012-04-11 10:40:36 EDT
Fixed in 2.4
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:21:27 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink