Search in sources :

Example 86 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class OrderByTestImplementation method testOrderByForUndefined.

@Test
public void testOrderByForUndefined() throws Exception {
    String[] queries = { // 0
    "SELECT DISTINCT position1.secId FROM /test ORDER BY position1.secId", // 1
    "SELECT DISTINCT position1.secId FROM /test ORDER BY position1.secId desc", // 2
    "SELECT DISTINCT position1.secId FROM /test where ID > 0  ORDER BY position1.secId", // 3
    "SELECT DISTINCT position1.secId FROM /test where ID > 0  ORDER BY position1.secId desc", // 4
    "SELECT DISTINCT position1.secId, ID FROM /test ORDER BY position1.secId, ID", // 5
    "SELECT DISTINCT position1.secId, ID FROM /test ORDER BY position1.secId desc, ID" };
    Region r1 = this.createRegion("test", Portfolio.class);
    for (int i = 0; i < 10; i++) {
        Portfolio pf = new Portfolio(i);
        if (i % 2 == 0) {
            pf.position1 = null;
        }
        r1.put(i + "", pf);
    }
    QueryService qs = CacheUtils.getQueryService();
    SelectResults[][] sr = new SelectResults[queries.length][2];
    Object[] srArr = null;
    for (int i = 0; i < queries.length; i++) {
        try {
            sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute();
            srArr = sr[i][0].toArray();
            if (i == 0) {
                assertEquals("First result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[0]);
            } else if (i == 1) {
                assertEquals("Last result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[srArr.length - 1]);
            } else if (i == 2) {
                assertEquals("First result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[0]);
            } else if (i == 3) {
                assertEquals("Last result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[srArr.length - 1]);
            } else if (i == 4) {
                for (int j = 0; j < srArr.length / 2; j++) {
                    assertEquals("Undefined should  have been returned for query " + queries[i], QueryService.UNDEFINED, ((Struct) srArr[j]).getFieldValues()[0]);
                }
            } else if (i == 5) {
                for (int j = srArr.length - 1; j > srArr.length / 2; j--) {
                    assertEquals("Undefined should  have been returned for query " + queries[i], QueryService.UNDEFINED, ((Struct) srArr[j]).getFieldValues()[0]);
                }
            }
        } catch (Exception e) {
            fail("Query execution failed for: " + queries[i] + " : " + e);
        }
    }
    this.createIndex("secIndex", "position1.secId", r1.getFullPath());
    this.createIndex("IDIndex", "ID", r1.getFullPath());
    for (int i = 0; i < queries.length; i++) {
        try {
            sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute();
            srArr = sr[i][1].toArray();
            if (i == 0) {
                assertEquals("First result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[0]);
            } else if (i == 1) {
                assertEquals("Last result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[srArr.length - 1]);
            } else if (i == 2) {
                assertEquals("First result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[0]);
            } else if (i == 3) {
                assertEquals("Last result should be undefined for query " + queries[i], QueryService.UNDEFINED, srArr[srArr.length - 1]);
            } else if (i == 4) {
                for (int j = 0; j < srArr.length / 2; j++) {
                    assertEquals("Undefined should  have been returned for query " + queries[i], QueryService.UNDEFINED, ((Struct) srArr[j]).getFieldValues()[0]);
                }
            } else if (i == 5) {
                for (int j = srArr.length - 1; j > srArr.length / 2; j--) {
                    assertEquals("Undefined should  have been returned for query " + queries[i], QueryService.UNDEFINED, ((Struct) srArr[j]).getFieldValues()[0]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail("Query execution failed for: " + queries[i] + " : " + e);
        }
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Struct(org.apache.geode.cache.query.Struct) Test(org.junit.Test)

Example 87 with QueryService

use of org.apache.geode.cache.query.QueryService 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 88 with QueryService

use of org.apache.geode.cache.query.QueryService 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 89 with QueryService

use of org.apache.geode.cache.query.QueryService 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 90 with QueryService

use of org.apache.geode.cache.query.QueryService 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

QueryService (org.apache.geode.cache.query.QueryService)532 Test (org.junit.Test)383 SelectResults (org.apache.geode.cache.query.SelectResults)331 Region (org.apache.geode.cache.Region)316 Query (org.apache.geode.cache.query.Query)316 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)215 Portfolio (org.apache.geode.cache.query.data.Portfolio)199 CacheException (org.apache.geode.cache.CacheException)139 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)118 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)110 Index (org.apache.geode.cache.query.Index)109 Host (org.apache.geode.test.dunit.Host)104 VM (org.apache.geode.test.dunit.VM)104 Iterator (java.util.Iterator)102 Cache (org.apache.geode.cache.Cache)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)86 Struct (org.apache.geode.cache.query.Struct)70 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)68 IOException (java.io.IOException)67 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)65