Return-Path: Delivered-To: apmail-db-jdo-dev-archive@www.apache.org Received: (qmail 86378 invoked from network); 9 Jun 2006 22:48:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 Jun 2006 22:48:37 -0000 Received: (qmail 47010 invoked by uid 500); 9 Jun 2006 22:48:36 -0000 Mailing-List: contact jdo-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jdo-dev@db.apache.org Delivered-To: mailing list jdo-dev@db.apache.org Received: (qmail 46999 invoked by uid 99); 9 Jun 2006 22:48:36 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Jun 2006 15:48:36 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: neutral (asf.osuosl.org: local policy) Received: from [216.174.237.13] (HELO click-mail.harbornet.com) (216.174.237.13) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Jun 2006 15:48:35 -0700 Received: from DELLZILLA (0014bf0cf8c8.click-network.com [131.191.67.190]) (authenticated bits=0) by click-mail.harbornet.com (8.12.8/8.12.8) with ESMTP id k59MvQnd029053; Fri, 9 Jun 2006 15:57:39 -0700 (PDT) From: "Matthew T. Adams" To: , Subject: JDOQL Subquery proposals (was: RE: any plans to support subqueries with similar concept as in sql) Date: Fri, 9 Jun 2006 15:47:53 -0700 Message-ID: <007101c68c16$c6c66de0$6963a8c0@DELLZILLA> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 Thread-index: AcaKQr7JtN3B5YO+S16LS3m0QnHZswBo6pOQ In-Reply-To: <1149692242.4486e952a564e@webmail.jpox.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Hi everyone, Here are 2 proposals discussed in the Fri 9 Jun JDO conference call regarding support for subqueries in JDOQL, including single-string and Query API enhancements, inspired by JPOX's proposed enhancement, documented in JPOX JIRA issue CORE-2861 (http://www.jpox.org/servlet/jira/browse/CORE-2861). The string and API enhancement proposals described here are designed to be used hand-in-hand, as the folks on the call wanted to continue to provide compatible string-based and API-based usages. In a nutshell, both proposals hinge on the use of the exising facility to declare JDOQL variables (not JDOQL parameters) to bind subqueries to superqueries. Please read thoroughly, consider, and comment. --matthew PS: Martin was on the hook to describe an alternative proposal based on a future object pattern. See separate proposal from him. Query API support ================= Proposal: Introduce new method Query.addSubquery(String variableName, Query subquery) This proposal entails utilizing the current Query API's declareVariables facility and adding a method Query.addSubquery(String,Query) to support subqueries. Essentially, a subquery is bound to a superquery via a variable declared for the superquery. The implementation handles coercing the subquery's result into the type of the variable(s) declared in the superquery. Queries can be nested to arbitrary levels. Example 1A: Find people with above average income Query superquery1a = pm.newQuery("SELECT FROM Person WHERE income > averageIncome"); superquery1a.declareVariables("BigDecimal averageIncome;"); Query subquery1a = pm.newQuery("SELECT avg(income) FROM Person"); superquery1a.addSubquery("averageIncome", subquery1a); // binds subquery to superquery Example 2A: Find average income of fathers using subquery Query superquery2a = pm.newQuery("SELECT avg(income) FROM fathers"); // in next line, Collection derived from subquery superquery2a.declareVariables("Collection fathers;"); Query subquery2a = pm.newQuery("SELECT FROM Person WHERE gender == 'M' && children.size() > 0"); superquery2a.addSubquery("fathers", subquery2a); // binds subquery to superquery Example 3A: Find average income of fathers using a single Query instance Note: this example's usage is required if the grammar specification of the variables clause remains the same (as it currently is) in the API and string forms (see JDO 2.0 spec sections 14.6.5 & 14.6.13). Query superquery3a = pm.newQuery("SELECT avg(income) FROM fathers"); // in next line, Collection derived from subquery superquery3a.declareVariables( "Collection fathers = SELECT FROM Person WHERE gender == 'M' && children.size() > 0;"); Pros: * Maintains backward compatibility. * Enhances performance by allowing for the deferral of query execution until entire query with subqueries is defined. Current Query API support requires the execution of the subquery, then execution of the superquery; current JDOQL string spec doesn't allow for subqueries at all. * Grammar of the variables clause undergoes the same enhancements in both the single-string and the API. * Compatible with single-string enhancement proposal below Cons: * Type coercion becomes more complicated than just autoboxing. * Requires that variables may always have to be explicitly defined. * Possibility that type of candidate collection of superquery must be derived (see example 2A above), or may not be known. * Possibility of using variables in place of both parameters and candidate collections. Single-string proposal ====================== Proposal: Use the existing VARIABLES JDOQL keyword in order to be compatible with the Query API proposal above. This proposal is very similar to JPOX JIRA CORE-2861 (http://www.jpox.org/servlet/jira/browse/CORE-2861), which proposes the introduction of a new JDOQL keyword "WITH" to introduce typed and named subquery results. To make this more compatible with the Query API proposal above and to avoid the need to introduce a new keyword to JDOQL, the existing JDOQL keyword "VARIABLES" would be used to introduce typed and named subqueries, except that the variable(s) would be intialized via the assignment operator, "=", or the "AS" keyword (TBD) at declaration time with a valid JDOQL expression. Variables would continue to be semicolon-delimited. Additionally, Query.toString(), for queries that employ subqueries, returns JDOQL strings that use this syntax. Example 1S: Find people with above average income using subquery (similar to example 1A) SELECT FROM Person WHERE income > averageIncome VARIABLES float averageIncome = SELECT avg(income) FROM Person; Example 2S: Find average income of fathers using a subquery (similar to example 2A) SELECT avg(income) FROM parents VARIABLES Collection parents = SELECT FROM Parent WHERE gender == 'M' && children.size() > 0; Pros: * Continues to use existing JDOQL keywords. * Grammar of the variables clause undergoes the same enhancements in both the single-string and the API. * Compatible with Query API proposal above. Cons: * More verbose than introducing "WITH" keyword. Implicit, unnamed variables would not be supported, unless the following syntax were supported (from example 2S), where the tokens "VARIABLES Collection parents" is implied. SELECT avg(income) FROM parents = SELECT FROM Parent WHERE gender == 'M' && children.size() > 0; This syntax is admittedly less verbose and more like SQL subqueries, but leaves open the typing and naming of the implicit, unnamed variables. * Possibility of using variables in place of both parameters and candidate collections. Open issues =========== * Can type derivation & coercion of JDOQL variables be performed in all cases? * These proposals use the assignment operator ("="). Should we use assignment via the JDOQL keyword "AS" instead or in addition to the assignment operator? * This proposal requires that JDOQL variables be allowed to substitute for both JDOQL parameters and candidate collections. Should this be allowed? >-----Original Message----- >From: Erik Bengtson [mailto:erik@jpox.org] >Sent: Wednesday, June 07, 2006 7:57 AM >To: jdo-experts-ext@sun.com; jdo-dev@db.apache.org >Subject: any plans to support subqueries with similar concept as in sql > >Hi, > >We need the ability to work with multiple sets in the same >query. It includes >performing operations between sets, numeric functions like >average or sum, >etc.. > >In JPOX it will implemented as exemplified here >http://www.jpox.org/servlet/jira/browse/CORE-2861 > >Sadly, JDOQL 2 is not capable to compete with JPQL in this aspect. > >Regardless the above issue, are there plans to expand the >JDOQL or even JDO 2 in >general based on new user requests/requirements? > >Regards, > >Erik Bengtson >