Search in sources :

Example 61 with DefaultQuery

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

the class GroupByTestImpl method testAggregateFuncCountDistinctStar_2.

@Test
public void testAggregateFuncCountDistinctStar_2() 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 62 with DefaultQuery

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

the class RangeIndexAPIJUnitTest method testQueryMethod_4.

/**
   * Tests the query method of RangeIndex which just contains not equal keys. That is if the where
   * clause looks like a != 7 and a != 8 & a!=9
   */
@Test
public void testQueryMethod_4() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    AbstractIndex i1 = (AbstractIndex) qs.createIndex("idIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
    AbstractIndex i2 = (AbstractIndex) qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
    AbstractIndex i3 = (AbstractIndex) qs.createIndex("status.toString()", IndexType.FUNCTIONAL, "status.toString", "/portfolios");
    Set results = new HashSet();
    DefaultQuery q = new DefaultQuery("select * from /portfolios", CacheUtils.getCache(), false);
    q.setRemoteQuery(false);
    ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache(), q);
    bindIterators(context, "/portfolios");
    Set keysToRemove = new HashSet();
    keysToRemove.add(new Integer(5));
    i1.query(results, keysToRemove, context);
    assertEquals(11, results.size());
    for (int i = 0; i < 12; ) {
        if (i != 5) {
            assertTrue(results.contains(region.get(new Integer(i))));
        }
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    keysToRemove.add(new Integer(5));
    keysToRemove.add(new Integer(8));
    i1.query(results, keysToRemove, context);
    assertEquals(10, results.size());
    for (int i = 0; i < 12; ) {
        if (i != 5 && i != 8) {
            assertTrue(results.contains(region.get(new Integer(i))));
        }
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    keysToRemove.add("active");
    keysToRemove.add("inactive");
    i2.query(results, keysToRemove, context);
    assertEquals(2, results.size());
    for (int i = 10; i < 12; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) ExecutionContext(org.apache.geode.cache.query.internal.ExecutionContext) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) QueryService(org.apache.geode.cache.query.QueryService) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 63 with DefaultQuery

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

the class BugJUnitTest method testEquijoinPRColocatedQuery_2.

@Test
public void testEquijoinPRColocatedQuery_2() throws Exception {
    AttributesFactory factory = new AttributesFactory();
    factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {

        public String getName() {
            return "blah";
        }

        public Serializable getRoutingObject(EntryOperation opDetails) {
            return (Serializable) opDetails.getKey();
        }

        public void close() {
        }
    }).create());
    PartitionedRegion pr1 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr1", factory.create());
    factory = new AttributesFactory();
    factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {

        public String getName() {
            return "blah";
        }

        public Serializable getRoutingObject(EntryOperation opDetails) {
            return (Serializable) opDetails.getKey();
        }

        public void close() {
        }
    }).setColocatedWith(pr1.getName()).create());
    final PartitionedRegion pr2 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr2", factory.create());
    createAllNumPRAndEvenNumPR(pr1, pr2, 80);
    Set<Integer> set = createAndPopulateSet(15);
    LocalDataSet lds = new LocalDataSet(pr1, set);
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserverHolder.setInstance(observer);
    QueryService qs = pr1.getCache().getQueryService();
    qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr1.entries e");
    qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr2.entries e");
    String query = "select distinct e1.key from /pr1.entries e1,/pr2.entries  e2" + " where e1.value=e2.value";
    DefaultQuery cury = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
    SelectResults r = (SelectResults) lds.executeQuery(cury, null, set);
    if (!observer.isIndexesUsed) {
        fail("Indexes should have been used");
    }
}
Also used : Serializable(java.io.Serializable) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 64 with DefaultQuery

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

the class BugJUnitTest method testEquijoinPRColocatedQuery_1.

@Test
public void testEquijoinPRColocatedQuery_1() throws Exception {
    AttributesFactory factory = new AttributesFactory();
    factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {

        public String getName() {
            return "blah";
        }

        public Serializable getRoutingObject(EntryOperation opDetails) {
            return (Serializable) opDetails.getKey();
        }

        public void close() {
        }
    }).create());
    PartitionedRegion pr1 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr1", factory.create());
    factory = new AttributesFactory();
    factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {

        public String getName() {
            return "blah";
        }

        public Serializable getRoutingObject(EntryOperation opDetails) {
            return (Serializable) opDetails.getKey();
        }

        public void close() {
        }
    }).setColocatedWith(pr1.getName()).create());
    final PartitionedRegion pr2 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr2", factory.create());
    createAllNumPRAndEvenNumPR(pr1, pr2, 80);
    Set<Integer> set = createAndPopulateSet(15);
    LocalDataSet lds = new LocalDataSet(pr1, set);
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserverHolder.setInstance(observer);
    QueryService qs = pr1.getCache().getQueryService();
    qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr1 e");
    qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr2 e");
    String query = "select distinct e1.value from /pr1 e1, " + "/pr2  e2" + " where e1.value=e2.value";
    DefaultQuery cury = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
    SelectResults r = (SelectResults) lds.executeQuery(cury, null, set);
    if (!observer.isIndexesUsed) {
        fail("Indexes should have been used");
    }
}
Also used : Serializable(java.io.Serializable) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) LocalDataSet(org.apache.geode.internal.cache.LocalDataSet) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 65 with DefaultQuery

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

the class ServerCQImpl method constructServerSideQuery.

/**
   * Returns parameterized query used by the server. This method replaces Region name with $1 and if
   * type is not specified in the query, looks for type from cqattributes and appends into the
   * query.
   * 
   * @return String modified query.
   */
private Query constructServerSideQuery() throws QueryException {
    InternalCache cache = cqService.getInternalCache();
    DefaultQuery locQuery = (DefaultQuery) cache.getLocalQueryService().newQuery(this.queryString);
    CompiledSelect select = locQuery.getSimpleSelect();
    CompiledIteratorDef from = (CompiledIteratorDef) select.getIterators().get(0);
    // WARNING: ASSUMES QUERY WAS ALREADY VALIDATED FOR PROPER "FORM" ON CLIENT;
    // THIS VALIDATION WILL NEED TO BE DONE ON THE SERVER FOR NATIVE CLIENTS,
    // BUT IS NOT DONE HERE FOR JAVA CLIENTS.
    // The query was already checked on the client that the sole iterator is a
    // CompiledRegion
    this.regionName = ((CompiledRegion) from.getCollectionExpr()).getRegionPath();
    from.setCollectionExpr(new CompiledBindArgument(1));
    return locQuery;
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) CompiledBindArgument(org.apache.geode.cache.query.internal.CompiledBindArgument) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) InternalCache(org.apache.geode.internal.cache.InternalCache)

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