Issue Details (XML | Word | Printable)

Key: OPENJPA-312
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Daniel Lee
Reporter: Daniel Lee
Votes: 0
Watchers: 1
Operations

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

derby fails with duplicate primary key(s) in group by list

Created: 10/Aug/07 01:43 AM   Updated: 16/Aug/07 01:59 PM
Return to search
Component/s: sql
Affects Version/s: 1.0.0
Fix Version/s: 1.0.0

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works OPENJPA-312.patch 2007-08-15 07:15 PM Daniel Lee 3 kB
Text File Licensed for inclusion in ASF works OPENJPA-312.patch 2007-08-11 12:00 AM Daniel Lee 3 kB
Text File Licensed for inclusion in ASF works OPENJPA-312.patch 2007-08-10 01:45 AM Daniel Lee 2 kB

Resolution Date: 16/Aug/07 01:59 PM


 Description  « Hide
derby fails with duplicate primary key(s) in group by list

With query "select o.customer, avg(o.amount) from Order o group by o.customer" the push-down query contains duplicate columns in the group by clause. This is okay when DB2 and other DB that tolerate the duplicates but Derby returns error.

Of course, we can ask fix on Derby but we can also easy fix in OpenJPA to avoid duplicates in the group by list. Please refer to the following for the error result and the attach patch for the fix.

Output from running the query that generate duplicate in the group by list:
6429 demo TRACE [main] openjpa.Query - Executing query: select o.customer, avg(o.amount) from Order o group by o.customer
6639 demo TRACE [main] openjpa.jdbc.SQL - <t 1094861122, conn 1639735740> executing prepstmnt 1405375428 SELECT t1.countryCode, t1.id, t1.version, t1.city, t1.state, t1.street, t1.zip, t1.creditRating, t1.name, AVG(t0.amount) FROM Order t0 INNER JOIN Customer t1 ON t0.customer_countryCode = t1.countryCode AND t0.customer_id = t1.id GROUP BY t1.countryCode, t1.id, t1.version, t1.countryCode, t1.id, t1.city, t1.state, t1.street, t1.zip, t1.countryCode, t1.id, t1.creditRating, t1.name



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Daniel Lee added a comment - 10/Aug/07 01:45 AM
The fix for OpenJPA-312.

Daniel Lee made changes - 10/Aug/07 01:45 AM
Field Original Value New Value
Attachment OPENJPA-312.patch [ 12363548 ]
Kevin Sutter made changes - 10/Aug/07 01:38 PM
Assignee Daniel Lee [ dtlee ]
Kevin Sutter added a comment - 10/Aug/07 04:57 PM
Daniel,
The basic idea of the patch looks okay, but the code changes do not look consistent. In some cases, you use "sql" and in others, you are using "sql.getSQL()". I would prefer just using "sql".

Also, you check for an empty _grouping after you check if the desired group sub-string is already present in the _grouping. This seems backwards. I know it's only being used to determine whether to use a comma or not. So, instead of doing these extra empty checks, could we do something along the following. I think this accomplishes the same goal.

         getJoins(joins, true);
         if (_grouping == null) {
             _grouping = new SQLBuffer(_dict);
             _grouping.append(sql);
        }
        else if (_grouping.getSQL().indexOf(sql) < 0)
             _grouping.append(", " + sql);

Thanks,
Kevin


Daniel Lee added a comment - 11/Aug/07 12:00 AM
new fix

Daniel Lee made changes - 11/Aug/07 12:00 AM
Attachment OPENJPA-312.patch [ 12363642 ]
Kevin Sutter added a comment - 13/Aug/07 04:20 PM
Daniel,
The code is much cleaner now. Thanks. But, I don't think the performance of StringTokenizer is worth any advantages it might provide. Is there some reason why the previous mechanism of just using indexOf() wasn't sufficient? I like the idea of isolating this check to a single boolean method like you have, but I would like to see a better performing implementation.

Thanks,
Kevin

Daniel Lee added a comment - 15/Aug/07 07:14 PM
Per discussion conclusion, attached here is the patch of fix using a ListArray to keep track of the columns in _grouping. StringTokenizer has been removed.

Thanks,
Daniel

Daniel Lee made changes - 15/Aug/07 07:15 PM
Attachment OPENJPA-312.patch [ 12363879 ]
Kevin Sutter added a comment - 15/Aug/07 10:49 PM
Daniel, Thanks for the updated patch. Per our side discussion, I am making a couple of slight changes to your patch, but the intent is still there. Thanks for resolving this issue. I will be committing the changes shortly.

Kevin

Repository Revision Date User Message
ASF #566381 Wed Aug 15 22:56:44 UTC 2007 kwsutter OPENJPA-312. Committing these changes for Daniel. I decided to make a common private utility method out of the common code across the proposed patch, but the intent of the original patch is still there. Thanks, Daniel, for posting the fix.
Files Changed
MODIFY /openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java

Kevin Sutter added a comment - 16/Aug/07 01:59 PM
Resolved via r566381.

Kevin Sutter made changes - 16/Aug/07 01:59 PM
Resolution Fixed [ 1 ]
Status Open [ 1 ] Resolved [ 5 ]