paulk-asert opened a new pull request, #315:
URL: https://github.com/apache/openjpa/pull/315

   [OPENJPA-3091](https://issues.apache.org/jira/browse/OPENJPA-3091): JPQL set 
operations (`UNION`, `INTERSECT`, `EXCEPT`) return the right rows from the 
database, but lose their projection:
   
   - a typed query, e.g. `createQuery("select a.name from Author a union select 
g.label from Gadget g", String.class)`, fails with `There is no conversion 
between query projection type "class Author" and result type "class 
java.lang.String"`;
   - untyped, each single-column row comes back as an `Object[]` of length 1;
   - executing the same set operation string a second time goes through the 
prepared query SQL cache and returns instances of the first operand's entity, 
e.g. a single `Author` in place of `[id, name]` rows. 
`TestEJBQLFunction.setOperatorRange()` worked around this by using a different 
JPQL string for every execution.
   
   `JPQLExpressionBuilder.evalSetOperatorExpressions()` built a compound 
`QueryExpressions` that only held its operands. Its projections stayed empty, 
so `QueryImpl` (result packing, single-projection unwrapping) and 
`PreparedSQLStoreQuery` (result provider on re-execution) treated it as a query 
for candidate instances. The SQL itself and 
`JDBCStoreQuery.executeSetOperatorQuery()` were already correct.
   
   Changes:
   1. Copy the projections, aliases and clauses of the first (leftmost) operand 
onto the compound.
   2. Set the compound's `distinct` to `DISTINCT_FALSE`. Its default 
`DISTINCT_AUTO` would otherwise turn on in-memory duplicate removal once it has 
projections, and `UNION ALL` would lose rows. Duplicates are the SQL set 
operator's job.
   3. `QueryExpressions.isAggregate()` returns `false` for a set operation, so 
that `select count(a) ... union all select count(b) ...` returns a row per 
operand rather than a single result.
   4. Exclude set operations from the prepared query SQL cache, as `IN` 
expressions and others are excluded. The cached path picks its result provider 
from the compound and never has its state, so supporting set operations there 
would be a larger change.
   
   Testing:
   - New tests in `TestEJBQLFunction`:
     - `setOperatorScalarResults`: `UNION`, `UNION ALL`, `INTERSECT` and 
`EXCEPT`, untyped and typed `String`, including `UNION ALL` keeping duplicates;
     - `setOperatorMultiColumnAndAggregateResults`: two-column rows typed 
`Object[]`, and `count()` operands;
     - `setOperatorRepeatedExecution`: the same scalar, multi-column and entity 
set operations executed twice, and none of them in the prepared query SQL cache 
(a plain query is, as a control).
   - All three fail without the fix. Removing any one of the four changes makes 
at least one of them, or the existing `unionAllProjection` and 
`setOperatorRange`, fail.
   - The comments in `setOperatorRange` that described the re-execution defect 
are updated.
   - The full test suites of openjpa-kernel, openjpa-jdbc and 
openjpa-persistence-jdbc pass on the default test database (4,656 tests), as do 
the RAT checks.
   
   Trade-off for review: set operations are no longer cached by the prepared 
query SQL cache, including entity-valued ones that happened to work from the 
cache before.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to