Another questions and my findings on using optional in subqueries:

1) Using Optional inside subqueries works just fine, as expected.

        String singleStringJDOQL1 =
"SELECT FROM " + OptionalSample.class.getName() + " WHERE " + "(select max(a.id) from " + OptionalSample.class.getName() + " a " +
                "where a.optionalPC.isPresent() ) == id";

        String singleStringJDOQL2 =
"SELECT FROM " + OptionalSample.class.getName() + " WHERE " + "(select max(a.id) from " + OptionalSample.class.getName() + " a " +
                "where a.optionalPC.get() != null) == id";

 I will upload a modified test harness that checks this functionality.


2) Another question.
According to https://docs.oracle.com/cd/E13189_01/kodo/docs324/ref_guide_subqueries.html (last example), subqueries can return collections. I tried to to call ".isEmpty()" and ".get(0)" on them and I get (I hope I didn't screw up my tests again):

"Cannot perform operation ".isEmpty" on org.datanucleus.store.rdbms.sql.expression.SubqueryExpression"

"Cannot perform operation ".get" on org.datanucleus.store.rdbms.sql.expression.SubqueryExpression"

Query example:

        String singleStringJDOQL1 =
"SELECT FROM " + OptionalSample.class.getName() + " WHERE " +
                "(select from " + OptionalSample.class.getName() + " a " +
                "where a.optionalPC != null).isEmpty()";

        String singleStringJDOQL4 =
            "SELECT FROM " + OptionalSample.class.getName() + " WHERE " +
            "(select from " + OptionalSample.class.getName() + " a " +
            "where a.optionalPC != null).get(0).optionalPC.isPresent()";

In the spec I couldn't find a definition of what subqueries should return (examples in 14.10.18 and 14.10.19 return aggregates). So I suppose they should be able to return collections? It seems that the TCK tests for subqueries use exclusively aggregates, so I'm not sure whether this is an omission or whether it is nor supposed to be possible.

Below is a code snippet that can be simply plugged in into the SupportedOptionalMethods.java test.

Cheers,
Tilmann

    /**
     * This methods tests that subqueries as collections.
     */
    public void testSubqueriesNoOptional() {
        //TODO
        //this gives:
//Cannot perform operation ".isEmpty" on org.datanucleus.store.rdbms.sql.expression.SubqueryExpression
        //However, it should be possible according to:
//https://docs.oracle.com/cd/E13189_01/kodo/docs324/ref_guide_subqueries.html
        String singleStringJDOQL1 =
"SELECT FROM " + OptionalSample.class.getName() + " WHERE " +
                "(select from " + OptionalSample.class.getName() + " a " +
                "where a.optionalPC != null).isEmpty()";
        Object[] expectedResult1 = new Object[]{oidReferencedPC1};
        checkSingleStringQuery(singleStringJDOQL1, expectedResult1);

        //TODO
        //This gives:
//Cannot perform operation ".get" on org.datanucleus.store.rdbms.sql.expression.SubqueryExpression
        String singleStringJDOQL4 =
            "SELECT FROM " + OptionalSample.class.getName() + " WHERE " +
            "(select from " + OptionalSample.class.getName() + " a " +
            "where a.optionalPC != null).get(0).optionalPC.isPresent()";
        Object[] expectedResult4 = new Object[]{oidReferencedPC1};
        checkSingleStringQuery(singleStringJDOQL4, expectedResult4);
    }



On 24.09.2016 18:59, Tilmann wrote:
Thanks!

Looks like I made a mistake during results checks, it works now, sorry for that.

Tilmann


On 24.09.2016 18:32, Craig Russell wrote:
The subselect would return 88 and then the outer select would become

"SELECT FROM " + OptionalSample.class.getName() + " WHERE " +
                “(88) == id”;

Seems to me this should select the object whose id is 88.

Craig

On Sep 24, 2016, at 9:22 AM, Tilmann <zaesc...@gmx.de> wrote:

Hi,

could I get some feedback?

I tried the following subquery:

"SELECT FROM " + OptionalSample.class.getName() + " WHERE " + "(select max(a.id) from " + OptionalSample.class.getName() + " a " +
                "where a.optionalPC != null) == id";

This returns '88' (an integer) instead of the object with id==88. Is this expected to return an integer?

Full code (can be used in SupportedOptionalMethods.java):
       String singleStringJDOQL2 =
"SELECT FROM " + OptionalSample.class.getName() + " WHERE " + "(select max(a.id) from " + OptionalSample.class.getName() + " a " +
                "where a.optionalPC != null) == id";
        Object[] expectedResult2 = new Object[]{oidReferencedPC1};
        // single String JDOQL
        Query singleStringQuery2 = pm.newQuery(singleStringJDOQL2);
executeJDOQuery(ASSERTION_FAILED, singleStringQuery2, singleStringJDOQL2,
                            false, null, expectedResult2, true);


Kind regards,
Tilmann

Reply via email to