Search in sources :

Example 11 with DefaultQuery

use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.

the class QueryUsingFunctionContextDUnitTest method runQueryOnServerLDS.

protected ArrayList runQueryOnServerLDS(String queryStr, Set filter) {
    Set buckets = getBucketsForFilter(filter);
    Region localDataSet = new LocalDataSet((PartitionedRegion) CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1), buckets);
    QueryService qservice = CacheFactory.getAnyInstance().getQueryService();
    Query query = qservice.newQuery(queryStr);
    SelectResults results;
    try {
        results = (SelectResults) ((LocalDataSet) localDataSet).executeQuery((DefaultQuery) query, null, buckets);
        return (ArrayList) results.asList();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) HashSet(java.util.HashSet) SelectResults(org.apache.geode.cache.query.SelectResults) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) QueryService(org.apache.geode.cache.query.QueryService) ArrayList(java.util.ArrayList) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) CacheException(org.apache.geode.cache.CacheException)

Example 12 with DefaultQuery

use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.

the class OrderByTestImplementation method testOrderByWithColumnAlias_Bug52041_2.

@Test
public void testOrderByWithColumnAlias_Bug52041_2() throws Exception {
    Region region = createRegion("portfolio", Portfolio.class);
    for (int i = 0; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.shortID = (short) ((short) i / 5);
        pf.status = "active";
        region.put("" + i, pf);
    }
    String queryStr = "select distinct p.ID as _id, p.shortID as short_id  from /portfolio p where p.ID >= 0 " + "order by short_id asc, p.ID desc";
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery(queryStr);
    SelectResults<Struct> results = (SelectResults<Struct>) query.execute();
    Iterator<Struct> iter = results.asList().iterator();
    int counter = 0;
    int k = 0;
    while (iter.hasNext()) {
        k = ((counter) / 5 + 1) * 5 - 1;
        Struct str = iter.next();
        assertEquals(counter / 5, ((Short) str.get("short_id")).intValue());
        assertEquals(k - (counter) % 5, ((Integer) str.get("_id")).intValue());
        ++counter;
    }
    CompiledSelect cs = ((DefaultQuery) query).getSimpleSelect();
    List<CompiledSortCriterion> orderbyAtts = cs.getOrderByAttrs();
    assertEquals(orderbyAtts.get(0).getColumnIndex(), 1);
    assertEquals(orderbyAtts.get(1).getColumnIndex(), 0);
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Example 13 with DefaultQuery

use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.

the class PdxGroupByTestImpl method testAggregateFuncMax.

@Override
@Test
public void testAggregateFuncMax() throws Exception {
    Region region = this.createRegion("portfolio", PortfolioPdx.class);
    for (int i = 1; i < 200; ++i) {
        PortfolioPdx pf = new PortfolioPdx(i);
        pf.shortID = (short) ((short) i / 5);
        region.put("key-" + i, pf);
    }
    String queryStr = "select  p.status as status, Max(p.ID) as Maxx from /portfolio p where p.ID > 0 group by status ";
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery(queryStr);
    CompiledSelect cs = ((DefaultQuery) query).getSelect();
    SelectResults sr = (SelectResults) query.execute();
    assertTrue(sr.getCollectionType().getElementType().isStructType());
    assertEquals(2, sr.size());
    Iterator iter = sr.iterator();
    Region rgn = CacheUtils.getRegion("portfolio");
    int activeMaxID = 0;
    int inactiveMaxID = 0;
    for (Object o : rgn.values()) {
        PortfolioPdx pf = (PortfolioPdx) o;
        if (pf.status.equals("active")) {
            if (pf.getID() > activeMaxID) {
                activeMaxID = pf.getID();
            }
        } else if (pf.status.equals("inactive")) {
            if (pf.getID() > inactiveMaxID) {
                inactiveMaxID = pf.getID();
            }
        } else {
            fail("unexpected value of status");
        }
    }
    while (iter.hasNext()) {
        Struct struct = (Struct) iter.next();
        StructType structType = struct.getStructType();
        ObjectType[] fieldTypes = structType.getFieldTypes();
        assertEquals("String", fieldTypes[0].getSimpleClassName());
        assertEquals("Number", fieldTypes[1].getSimpleClassName());
        if (struct.get("status").equals("active")) {
            assertEquals(activeMaxID, ((Integer) struct.get("Maxx")).intValue());
        } else if (struct.get("status").equals("inactive")) {
            assertEquals(inactiveMaxID, ((Integer) struct.get("Maxx")).intValue());
        } else {
            fail("unexpected value of status");
        }
    }
    ObjectType elementType = sr.getCollectionType().getElementType();
    assertTrue(elementType.isStructType());
    StructType structType = (StructType) elementType;
    ObjectType[] fieldTypes = structType.getFieldTypes();
    assertEquals("String", fieldTypes[0].getSimpleClassName());
    assertEquals("Number", fieldTypes[1].getSimpleClassName());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) StructType(org.apache.geode.cache.query.types.StructType) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) Struct(org.apache.geode.cache.query.Struct) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Example 14 with DefaultQuery

use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.

the class PdxGroupByTestImpl method testSumWithMultiColumnGroupBy.

@Override
@Test
public void testSumWithMultiColumnGroupBy() throws Exception {
    Region region = this.createRegion("portfolio", PortfolioPdx.class);
    for (int i = 1; i < 200; ++i) {
        PortfolioPdx pf = new PortfolioPdx(i);
        pf.shortID = (short) ((short) i / 5);
        region.put("key-" + i, pf);
    }
    Map<String, Integer> expectedData = new HashMap<String, Integer>();
    for (Object o : region.values()) {
        PortfolioPdx pf = (PortfolioPdx) o;
        String key = pf.status + "_" + pf.shortID;
        if (expectedData.containsKey(key)) {
            int sum = expectedData.get(key).intValue() + pf.getID();
            expectedData.put(key, sum);
        } else {
            expectedData.put(key, pf.getID());
        }
    }
    String queryStr = "select  p.status as status, p.shortID as shortID, sum(p.ID) as summ from /portfolio p" + " where p.ID > 0 group by status, shortID ";
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery(queryStr);
    CompiledSelect cs = ((DefaultQuery) query).getSelect();
    SelectResults sr = (SelectResults) query.execute();
    assertTrue(sr.getCollectionType().getElementType().isStructType());
    assertEquals(expectedData.size(), sr.size());
    Iterator iter = sr.iterator();
    while (iter.hasNext()) {
        Struct struct = (Struct) iter.next();
        StructType structType = struct.getStructType();
        ObjectType[] fieldTypes = structType.getFieldTypes();
        assertEquals("String", fieldTypes[0].getSimpleClassName());
        assertEquals("short", fieldTypes[1].getSimpleClassName());
        assertEquals("Number", fieldTypes[2].getSimpleClassName());
        String key = struct.get("status") + "_" + struct.get("shortID");
        int sum = ((Integer) struct.get("summ")).intValue();
        assertTrue(expectedData.containsKey(key));
        assertEquals(expectedData.get(key).intValue(), sum);
    }
    ObjectType elementType = sr.getCollectionType().getElementType();
    assertTrue(elementType.isStructType());
    StructType structType = (StructType) elementType;
    ObjectType[] fieldTypes = structType.getFieldTypes();
    assertEquals("String", fieldTypes[0].getSimpleClassName());
    assertEquals("short", fieldTypes[1].getSimpleClassName());
    assertEquals("Number", fieldTypes[2].getSimpleClassName());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) StructType(org.apache.geode.cache.query.types.StructType) HashMap(java.util.HashMap) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) Struct(org.apache.geode.cache.query.Struct) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Example 15 with DefaultQuery

use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.

the class PdxGroupByTestImpl method testAggregateFuncNoGroupBy.

@Override
@Test
public void testAggregateFuncNoGroupBy() throws Exception {
    Region region = this.createRegion("portfolio", PortfolioPdx.class);
    for (int i = 1; i < 200; ++i) {
        PortfolioPdx pf = new PortfolioPdx(i);
        pf.shortID = (short) ((short) i / 5);
        region.put("key-" + i, pf);
    }
    String queryStr = "select   sum(p.ID) as summ , Max(p.ID) as maxx, min(p.ID) as minn," + " avg(p.ID) as average from /portfolio p where p.ID > 0 ";
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery(queryStr);
    CompiledSelect cs = ((DefaultQuery) query).getSelect();
    SelectResults sr = (SelectResults) query.execute();
    assertTrue(sr.getCollectionType().getElementType().isStructType());
    assertEquals(1, sr.size());
    Iterator iter = sr.iterator();
    Region rgn = CacheUtils.getRegion("portfolio");
    int sum = 0;
    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    for (Object o : rgn.values()) {
        PortfolioPdx pf = (PortfolioPdx) o;
        sum += pf.getID();
        if (pf.getID() > max) {
            max = pf.getID();
        }
        if (pf.getID() < min) {
            min = pf.getID();
        }
    }
    float avg = sum / rgn.size();
    while (iter.hasNext()) {
        Struct struct = (Struct) iter.next();
        assertEquals(sum, ((Integer) struct.get("summ")).intValue());
        assertEquals(max, ((Integer) struct.get("maxx")).intValue());
        assertEquals(min, ((Integer) struct.get("minn")).intValue());
        assertEquals(avg, ((Number) struct.get("average")).floatValue(), 0.0f);
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Aggregations

DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)66 Test (org.junit.Test)49 QueryService (org.apache.geode.cache.query.QueryService)40 SelectResults (org.apache.geode.cache.query.SelectResults)38 Query (org.apache.geode.cache.query.Query)37 Region (org.apache.geode.cache.Region)33 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)32 Struct (org.apache.geode.cache.query.Struct)27 Iterator (java.util.Iterator)26 HashSet (java.util.HashSet)23 ObjectType (org.apache.geode.cache.query.types.ObjectType)23 StructType (org.apache.geode.cache.query.types.StructType)22 Portfolio (org.apache.geode.cache.query.data.Portfolio)18 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 Set (java.util.Set)11 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)11 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)9