Search in sources :

Example 76 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.

the class NonDistinctOrderByReplicatedJUnitTest method testLimitApplicationOnPrimaryKeyIndex.

@Test
public void testLimitApplicationOnPrimaryKeyIndex() throws Exception {
    String[] queries = { // from index
    "SELECT   ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != $1 limit 10" };
    Object[][] r = new Object[queries.length][2];
    QueryService qs;
    qs = CacheUtils.getQueryService();
    Position.resetCounter();
    // Create Regions
    Region r1 = this.createRegion("portfolio1", Portfolio.class);
    for (int i = 0; i < 50; i++) {
        r1.put(i + "", new Portfolio(i));
    }
    // Execute Queries without Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            r[i][0] = q.execute(new Object[] { new Integer(10) });
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create Indexes
    qs.createIndex("PKIDIndexPf1", IndexType.PRIMARY_KEY, "ID", "/portfolio1");
    // Execute Queries with Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            r[i][1] = q.execute(new Object[] { "10" });
            int indexLimit = queries[i].indexOf("limit");
            int limit = -1;
            boolean limitQuery = indexLimit != -1;
            if (limitQuery) {
                limit = Integer.parseInt(queries[i].substring(indexLimit + 5).trim());
            }
            boolean orderByQuery = queries[i].indexOf("order by") != -1;
            SelectResults rcw = (SelectResults) r[i][1];
            if (orderByQuery) {
                assertEquals("Ordered", rcw.getCollectionType().getSimpleClassName());
            }
            if (!observer.isIndexesUsed) {
                fail("Index is NOT uesd");
            }
            int indexDistinct = queries[i].indexOf("distinct");
            boolean distinctQuery = indexDistinct != -1;
            if (limitQuery) {
                if (orderByQuery) {
                    assertFalse(observer.limitAppliedAtIndex);
                } else {
                    assertTrue(observer.limitAppliedAtIndex);
                }
            } else {
                assertFalse(observer.limitAppliedAtIndex);
            }
            Iterator itr = observer.indexesUsed.iterator();
            while (itr.hasNext()) {
                String indexUsed = itr.next().toString();
                if (!(indexUsed).equals("PKIDIndexPf1")) {
                    fail("<PKIDIndexPf1> was expected but found " + indexUsed);
                }
            // assertIndexDetailsEquals("statusIndexPf1",itr.next().toString());
            }
            int indxs = observer.indexesUsed.size();
            System.out.println("**************************************************Indexes Used :::::: " + indxs + " Index Name: " + observer.indexName);
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Result set verification
    Collection coll1 = null;
    Collection coll2 = null;
    Iterator itert1 = null;
    Iterator itert2 = null;
    ObjectType type1, type2;
    type1 = ((SelectResults) r[0][0]).getCollectionType().getElementType();
    type2 = ((SelectResults) r[0][1]).getCollectionType().getElementType();
    if ((type1.getClass().getName()).equals(type2.getClass().getName())) {
        CacheUtils.log("Both SelectResults are of the same Type i.e.--> " + ((SelectResults) r[0][0]).getCollectionType().getElementType());
    } else {
        CacheUtils.log("Classes are : " + type1.getClass().getName() + " " + type2.getClass().getName());
        fail("FAILED:Select result Type is different in both the cases." + "; failed query=" + queries[0]);
    }
    if (((SelectResults) r[0][0]).size() == ((SelectResults) r[0][1]).size()) {
        CacheUtils.log("Both SelectResults are of Same Size i.e.  Size= " + ((SelectResults) r[0][1]).size());
    } else {
        fail("FAILED:SelectResults size is different in both the cases. Size1=" + ((SelectResults) r[0][0]).size() + " Size2 = " + ((SelectResults) r[0][1]).size() + "; failed query=" + queries[0]);
    }
    coll2 = (((SelectResults) r[0][1]).asSet());
    coll1 = (((SelectResults) r[0][0]).asSet());
    itert1 = coll1.iterator();
    itert2 = coll2.iterator();
    while (itert1.hasNext()) {
        Object[] values1 = ((Struct) itert1.next()).getFieldValues();
        Object[] values2 = ((Struct) itert2.next()).getFieldValues();
        assertEquals(values1.length, values2.length);
        assertTrue((((Integer) values1[0]).intValue() != 10));
        assertTrue((((Integer) values2[0]).intValue() != 10));
    }
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) 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) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 77 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.

the class OrderByReplicatedJUnitTest method testLimitApplicationOnPrimaryKeyIndex.

@Test
public void testLimitApplicationOnPrimaryKeyIndex() throws Exception {
    String[] queries = { // from index
    "SELECT  distinct ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != $1 limit 10" };
    Object[][] r = new Object[queries.length][2];
    QueryService qs;
    qs = CacheUtils.getQueryService();
    Position.resetCounter();
    // Create Regions
    Region r1 = createRegion("portfolio1", Portfolio.class);
    for (int i = 0; i < 50; i++) {
        r1.put(i + "", new Portfolio(i));
    }
    // Execute Queries without Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            r[i][0] = q.execute(new Object[] { new Integer(10) });
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create Indexes
    this.createIndex("PKIDIndexPf1", IndexType.PRIMARY_KEY, "ID", "/portfolio1");
    // Execute Queries with Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            r[i][1] = q.execute(new Object[] { "10" });
            int indexLimit = queries[i].indexOf("limit");
            int limit = -1;
            boolean limitQuery = indexLimit != -1;
            if (limitQuery) {
                limit = Integer.parseInt(queries[i].substring(indexLimit + 5).trim());
            }
            boolean orderByQuery = queries[i].indexOf("order by") != -1;
            SelectResults rcw = (SelectResults) r[i][1];
            if (orderByQuery) {
                assertEquals("Ordered", rcw.getCollectionType().getSimpleClassName());
            }
            if (assertIndexUsedOnQueryNode() && !observer.isIndexesUsed) {
                fail("Index is NOT uesd");
            }
            int indexDistinct = queries[i].indexOf("distinct");
            boolean distinctQuery = indexDistinct != -1;
            if (limitQuery) {
                if (orderByQuery) {
                    assertFalse(observer.limitAppliedAtIndex);
                } else {
                    assertTrue(observer.limitAppliedAtIndex);
                }
            } else {
                assertFalse(observer.limitAppliedAtIndex);
            }
            Iterator itr = observer.indexesUsed.iterator();
            while (itr.hasNext()) {
                String indexUsed = itr.next().toString();
                if (!(indexUsed).equals("PKIDIndexPf1")) {
                    fail("<PKIDIndexPf1> was expected but found " + indexUsed);
                }
            // assertIndexDetailsEquals("statusIndexPf1",itr.next().toString());
            }
            int indxs = observer.indexesUsed.size();
            System.out.println("**************************************************Indexes Used :::::: " + indxs + " Index Name: " + observer.indexName);
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Result set verification
    Collection coll1 = null;
    Collection coll2 = null;
    Iterator itert1 = null;
    Iterator itert2 = null;
    ObjectType type1, type2;
    type1 = ((SelectResults) r[0][0]).getCollectionType().getElementType();
    type2 = ((SelectResults) r[0][1]).getCollectionType().getElementType();
    if ((type1.getClass().getName()).equals(type2.getClass().getName())) {
        CacheUtils.log("Both SelectResults are of the same Type i.e.--> " + ((SelectResults) r[0][0]).getCollectionType().getElementType());
    } else {
        CacheUtils.log("Classes are : " + type1.getClass().getName() + " " + type2.getClass().getName());
        fail("FAILED:Select result Type is different in both the cases." + "; failed query=" + queries[0]);
    }
    if (((SelectResults) r[0][0]).size() == ((SelectResults) r[0][1]).size()) {
        CacheUtils.log("Both SelectResults are of Same Size i.e.  Size= " + ((SelectResults) r[0][1]).size());
    } else {
        fail("FAILED:SelectResults size is different in both the cases. Size1=" + ((SelectResults) r[0][0]).size() + " Size2 = " + ((SelectResults) r[0][1]).size() + "; failed query=" + queries[0]);
    }
    coll2 = (((SelectResults) r[0][1]).asSet());
    coll1 = (((SelectResults) r[0][0]).asSet());
    itert1 = coll1.iterator();
    itert2 = coll2.iterator();
    while (itert1.hasNext()) {
        Object[] values1 = ((Struct) itert1.next()).getFieldValues();
        Object[] values2 = ((Struct) itert2.next()).getFieldValues();
        assertEquals(values1.length, values2.length);
        assertTrue((((Integer) values1[0]).intValue() != 10));
        assertTrue((((Integer) values2[0]).intValue() != 10));
    }
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) 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) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 78 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.

the class PRQueryDUnitHelper method compareTwoQueryResults.

/*
   * This function compares the two result sets passed based on <br> 1. Type <br> 2. Size <br> 3.
   * Contents <br>
   * 
   * @param Object[][] @param length @return
   */
public void compareTwoQueryResults(Object[][] r, int len) {
    Set set1 = null;
    Set set2 = null;
    ObjectType type1, type2;
    for (int j = 0; j < len; j++) {
        if ((r[j][0] != null) && (r[j][1] != null)) {
            type1 = ((SelectResults) r[j][0]).getCollectionType().getElementType();
            assertNotNull("PRQueryDUnitHelper#compareTwoQueryResults: Type 1 is NULL " + type1, type1);
            type2 = ((SelectResults) r[j][1]).getCollectionType().getElementType();
            assertNotNull("PRQueryDUnitHelper#compareTwoQueryResults: Type 2 is NULL " + type2, type2);
            if ((type1.getClass().getName()).equals(type2.getClass().getName())) {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("PRQueryDUnitHelper#compareTwoQueryResults: Both Search Results are of the same Type i.e.--> " + ((SelectResults) r[j][0]).getCollectionType().getElementType());
            } else {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().error("PRQueryDUnitHelper#compareTwoQueryResults: Classes are : " + type1.getClass().getName() + " " + type2.getClass().getName());
                fail("PRQueryDUnitHelper#compareTwoQueryResults: FAILED:Search result Type is different in both the cases");
            }
            int size0 = ((SelectResults) r[j][0]).size();
            int size1 = ((SelectResults) r[j][1]).size();
            if (size0 == size1) {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("PRQueryDUnitHelper#compareTwoQueryResults: Both Search Results are non-zero and are of Same Size i.e.  Size= " + size1 + ";j=" + j);
            } else {
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().error("PRQueryDUnitHelper#compareTwoQueryResults: FAILED:Search resultSet size are different in both cases; size0=" + size0 + ";size1=" + size1 + ";j=" + j);
                fail("PRQueryDUnitHelper#compareTwoQueryResults: FAILED:Search resultSet size are different in both cases; size0=" + size0 + ";size1=" + size1 + ";j=" + j);
            }
            set2 = (((SelectResults) r[j][1]).asSet());
            set1 = (((SelectResults) r[j][0]).asSet());
            assertEquals("PRQueryDUnitHelper#compareTwoQueryResults: FAILED: " + "result contents are not equal, ", set1, set2);
        }
    }
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) Set(java.util.Set) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) SelectResults(org.apache.geode.cache.query.SelectResults)

Example 79 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.

the class GroupByTestImpl method testAggregateFuncCountDistinctStar_1.

@Test
public void testAggregateFuncCountDistinctStar_1() throws Exception {
    Region region = this.createRegion("portfolio", Portfolio.class);
    for (int i = 1; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.shortID = (short) ((short) i % 5);
        region.put("" + i, pf);
    }
    String queryStr = "select  p.status as status, count(distinct p.shortID) as countt 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");
    Set<Object> distinctShortIDActive = new HashSet<Object>();
    Set<Object> distinctShortIDInactive = new HashSet<Object>();
    int inactiveCount = 0;
    for (Object o : rgn.values()) {
        Portfolio pf = (Portfolio) o;
        if (pf.status.equals("active")) {
            distinctShortIDActive.add(pf.shortID);
        } else if (pf.status.equals("inactive")) {
            distinctShortIDInactive.add(pf.shortID);
        } else {
            fail("unexpected status");
        }
    }
    while (iter.hasNext()) {
        Struct struct = (Struct) iter.next();
        StructType structType = struct.getStructType();
        ObjectType[] fieldTypes = structType.getFieldTypes();
        assertEquals("String", fieldTypes[0].getSimpleClassName());
        assertEquals("Integer", fieldTypes[1].getSimpleClassName());
        if (struct.get("status").equals("active")) {
            assertEquals(distinctShortIDActive.size(), ((Integer) struct.get("countt")).intValue());
        } else if (struct.get("status").equals("inactive")) {
            assertEquals(distinctShortIDInactive.size(), ((Integer) struct.get("countt")).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("Integer", 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) Portfolio(org.apache.geode.cache.query.data.Portfolio) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.

the class GroupByTestImpl method testAggregateFuncSumDistinct.

@Test
public void testAggregateFuncSumDistinct() throws Exception {
    Region region = this.createRegion("portfolio", Portfolio.class);
    for (int i = 1; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.shortID = (short) ((short) i / 5);
        region.put("" + i, pf);
    }
    String queryStr = "select  p.status as status, SUM (distinct p.shortID) as summ 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");
    Set<Short> activeSum = new HashSet<Short>();
    Set<Short> inactiveSum = new HashSet<Short>();
    for (Object o : rgn.values()) {
        Portfolio pf = (Portfolio) o;
        if (pf.status.equals("active")) {
            activeSum.add(pf.shortID);
        } else if (pf.status.equals("inactive")) {
            inactiveSum.add(pf.shortID);
        } else {
            fail("unexpected value of status");
        }
    }
    int activeSumm = 0, inactiveSumm = 0;
    for (Short val : activeSum) {
        activeSumm += val.intValue();
    }
    for (Short val : inactiveSum) {
        inactiveSumm += val.intValue();
    }
    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(activeSumm, ((Integer) struct.get("summ")).intValue());
        } else if (struct.get("status").equals("inactive")) {
            assertEquals(inactiveSumm, ((Integer) struct.get("summ")).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) Portfolio(org.apache.geode.cache.query.data.Portfolio) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ObjectType (org.apache.geode.cache.query.types.ObjectType)95 SelectResults (org.apache.geode.cache.query.SelectResults)66 QueryService (org.apache.geode.cache.query.QueryService)46 Test (org.junit.Test)46 Query (org.apache.geode.cache.query.Query)45 Iterator (java.util.Iterator)44 Region (org.apache.geode.cache.Region)40 StructType (org.apache.geode.cache.query.types.StructType)38 Portfolio (org.apache.geode.cache.query.data.Portfolio)32 Struct (org.apache.geode.cache.query.Struct)30 StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)29 ObjectTypeImpl (org.apache.geode.cache.query.internal.types.ObjectTypeImpl)25 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)24 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)23 ArrayList (java.util.ArrayList)21 List (java.util.List)20 Index (org.apache.geode.cache.query.Index)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)18 HashSet (java.util.HashSet)14