jsr338-experts@jpa-spec.java.net

[jsr338-experts] Re: query improvements: downcasting

From: Linda DeMichiel <linda.demichiel_at_oracle.com>
Date: Thu, 10 Mar 2011 10:45:21 -0800

On 3/10/2011 8:11 AM, Matthew Adams wrote:
> Wait a second, here. It just occurred to me that downcasting could be
> used as part of the filter expression and/or as part of the projection
> expression.
>
> Quick terminological review:
> Projection == the shape of the results (the "select" part)
> Filter == the conditions the data must satisfy (the "where" part)
> Fetch plan == the breadth & depth of the objects that are returned in
> the project (not defined in JPA yet)
>
> Now, take Linda's first example:
>
> SELECT b.name, b.ISBN
> FROM Order o JOIN TREAT(o.product AS Book) b
>
> This query would obviously fail all of the time, unless all orders had
> a product that was of type Book. It's clear to me in this example
> that the downcast is being used to filter results. In this case,
> there should be no ClassCastException thrown -- the expression should
> be considered as returning a false in the condition. It's like a
> WHERE without the WHERE.
>
> Now, let's modify it a bit and use it only in the projection of the query:
>
> SELECT TREAT(o.product AS Book).ISBN
> FROM Order o JOIN o.product
> WHERE ...
>
> The ellipsis ("...") in the filter would be some set of conditions
> that should only cause the return of orders with products of type
> Book. In this case, if some orders with products not of type Book
> were returned, the user's expectation was certainly not met and a
> ClassCastException should be thrown.
>

I think that to be consistent with the use of treat to provide filtering
in the FROM and WHERE clauses that this would need to return null.

To exclude products that were not books, the query could, for example,
use the TYPE operator in the WHERE clause.

You might find the following of interest on the null vs exception issue

 From the Oracle Database SQL Language Reference:
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions218.htm#SQLRF06148

 From DB2:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r8/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0023463.html

-Linda


> I think that there are two uses/contexts for the use of downcasting
> here that we were not separating thus far in the discussion.
> Thoughts?
>
> -matthew
>
>
> On Thu, Mar 10, 2011 at 9:37 AM, Emmanuel Bernard
> <emmanuel.bernard_at_jboss.com> wrote:
>> no that was a copy/paste error.
>>
>> On 8 mars 2011, at 21:34, Werner Keil wrote:
>>
>>> @Emanuel, was there a reason why you changed your Java example to: "p.projects > 1000" not "p.budget > 1000" ?
>>
>
>
>