Search in sources :

Example 76 with QueryService

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

the class CountStarJUnitTest method testCountStarWithDuplicateValues.

@Test
public void testCountStarWithDuplicateValues() throws Exception {
    Cache cache = CacheUtils.getCache();
    createLocalRegion();
    assertNotNull(cache.getRegion(regionName));
    assertEquals(numElem, cache.getRegion(regionName).size());
    HashMap<String, Integer> countStarDistinctQueries = new HashMap<String, Integer>();
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName, 100);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0", 100);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID < 0", 0);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 LIMIT 50", 50);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 AND status='active'", 50);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 OR status='active'", 100);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 AND status LIKE 'act%'", 50);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 OR status LIKE 'ina%'", 100);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID IN SET(1, 2, 3, 4, 5)", 5);
    countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where NOT (ID > 5)", 5);
    QueryService queryService = cache.getQueryService();
    // Run queries
    Query query1 = null;
    Query query2 = null;
    // Populate the region.
    Region region = cache.getRegion(regionName);
    for (int i = 1; i <= 100; i++) {
        region.put(i, new Portfolio(i, i));
    }
    // Duplicate values
    for (int i = 1; i <= 100; i++) {
        region.put(i + 100, new Portfolio(i, i));
    }
    // Without Indexes
    for (String queryStr : countStarDistinctQueries.keySet()) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute();
        SelectResults result2 = (SelectResults) query2.execute();
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
    // assertIndexDetailsEquals("Query: "+ queryStr,
    // countStarDistinctQueries.get(queryStr).intValue(), count);
    }
    // CReate Index on status and ID
    queryService.createIndex("sampleIndex-1", IndexType.FUNCTIONAL, "p.ID", "/" + regionName + " p");
    queryService.createIndex("sampleIndex-2", IndexType.FUNCTIONAL, "p.status", "/" + regionName + " p");
    queryService.createIndex("sampleIndex-3", IndexType.FUNCTIONAL, "pos.secId", "/" + regionName + " p, p.positions.values pos");
    // Verify Index Creation
    assertNotNull(queryService.getIndex(region, "sampleIndex-1"));
    assertNotNull(queryService.getIndex(region, "sampleIndex-2"));
    assertEquals(3, queryService.getIndexes().size());
    // Without Indexes
    for (String queryStr : countStarDistinctQueries.keySet()) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute();
        SelectResults result2 = (SelectResults) query2.execute();
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
    // assertIndexDetailsEquals("Query: "+ queryStr,
    // countStarDistinctQueries.get(queryStr).intValue(), count);
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) HashMap(java.util.HashMap) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 77 with QueryService

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

the class CountStarJUnitTest method testCountStartQueriesOnLocalRegion.

// Queries without indexes.
/**
   * Test on Local Region data
   */
@Test
public void testCountStartQueriesOnLocalRegion() throws Exception {
    Cache cache = CacheUtils.getCache();
    createLocalRegion();
    assertNotNull(cache.getRegion(regionName));
    assertEquals(numElem, cache.getRegion(regionName).size());
    QueryService queryService = cache.getQueryService();
    Query query1 = null;
    Query query2 = null;
    for (String queryStr : countStarQueries.keySet()) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute();
        SelectResults result2 = (SelectResults) query2.execute();
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
        assertEquals("Query: " + queryStr, countStarQueries.get(queryStr).intValue(), count);
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 78 with QueryService

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

the class ComparisonOperatorsJUnitTest method testCompareWithInt.

@Test
public void testCompareWithInt() throws Exception {
    String var = "ID";
    int value = 2;
    QueryService qs = CacheUtils.getQueryService();
    for (int i = 0; i < operators.length; i++) {
        Query query = qs.newQuery("SELECT DISTINCT * FROM /Portfolios where " + var + operators[i] + value);
        Object result = query.execute();
        if (result instanceof Collection) {
            Iterator iter = ((Collection) result).iterator();
            while (iter.hasNext()) {
                boolean isPassed = false;
                Portfolio p = (Portfolio) iter.next();
                switch(i) {
                    case 0:
                        isPassed = (p.getID() == value);
                        break;
                    case 1:
                        isPassed = (p.getID() != value);
                        break;
                    case 2:
                        isPassed = (p.getID() != value);
                        break;
                    case 3:
                        isPassed = (p.getID() < value);
                        break;
                    case 4:
                        isPassed = (p.getID() <= value);
                        break;
                    case 5:
                        isPassed = (p.getID() > value);
                        break;
                    case 6:
                        isPassed = (p.getID() >= value);
                        break;
                }
                if (!isPassed)
                    fail(this.getName() + " failed for operator " + operators[i]);
            }
        } else {
            fail(this.getName() + " failed for operator " + operators[i]);
        }
    }
}
Also used : Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) Iterator(java.util.Iterator) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 79 with QueryService

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

the class SelectStarQueryDUnitTest method functionWithStructTypeInInnerQueryShouldNotThrowExceptionWhenRunOnMultipleNodes.

@Test
public void functionWithStructTypeInInnerQueryShouldNotThrowExceptionWhenRunOnMultipleNodes() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM server3 = host.getVM(2);
    final VM client = host.getVM(3);
    PortfolioPdx[] portfolios = new PortfolioPdx[10];
    for (int i = 0; i < portfolios.length; i++) {
        portfolios[i] = new PortfolioPdx(i);
    }
    // create servers and regions
    final int port1 = startPartitionedCacheServer(server1, portfolios);
    final int port2 = startPartitionedCacheServer(server2, portfolios);
    final int port3 = startPartitionedCacheServer(server3, portfolios);
    // create client
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(getServerHostName(server1.getHost()), port1);
            cf.addPoolServer(getServerHostName(server2.getHost()), port2);
            cf.addPoolServer(getServerHostName(server3.getHost()), port3);
            ClientCache cache = getClientCache(cf);
            cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regName);
            return null;
        }
    });
    // put serialized PortfolioPdx objects
    client.invoke(new SerializableCallable("Put objects") {

        @Override
        public Object call() throws Exception {
            Region r1 = getRootRegion(regName);
            for (int i = 10; i < 100; i++) {
                r1.put("key-" + i, new PortfolioPdx(i));
            }
            return null;
        }
    });
    // query remotely from client
    client.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            getLogWriter().info("Querying remotely from client");
            QueryService remoteQS = null;
            try {
                remoteQS = ((ClientCache) getCache()).getQueryService();
                SelectResults sr = (SelectResults) remoteQS.newQuery("select distinct oP.ID, oP.status, oP.getType from /" + regName + " oP where element(select distinct p.ID, p.status, p.getType from /" + regName + " p where p.ID = oP.ID).status = 'inactive'").execute();
                assertEquals(50, sr.size());
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            return null;
        }
    });
    closeCache(client);
    closeCache(server1);
    closeCache(server2);
    closeCache(server3);
}
Also used : Host(org.apache.geode.test.dunit.Host) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 80 with QueryService

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

the class PdxOrderByJUnitTest method testPartitionRangeIndex.

@Test
public void testPartitionRangeIndex() throws Exception {
    final int numberOfEntries = 10;
    Region pr = this.configurePR();
    // create a local query service
    QueryService localQueryService = null;
    try {
        localQueryService = CacheUtils.getQueryService();
    } catch (Exception e) {
        fail(e.toString());
    }
    for (int i = 0; i < numberOfEntries; i++) {
        pr.put("key-" + i, new PortfolioPdx(i));
    }
    localQueryService = CacheUtils.getCache().getQueryService();
    SelectResults[][] rs = new SelectResults[queryString.length][2];
    for (int i = 0; i < queryString.length; i++) {
        try {
            Query query = localQueryService.newQuery(queryString[i]);
            rs[i][0] = (SelectResults) query.execute();
            checkForPdxString(rs[i][0].asList(), queryString[i]);
        } catch (Exception e) {
            fail("Failed executing " + queryString[i]);
        }
    }
    Index index = null;
    try {
        index = localQueryService.createIndex("secIdIndex", "pos.secId", regName + " p, p.positions.values pos");
        if (index instanceof PartitionedIndex) {
            for (Object o : ((PartitionedIndex) index).getBucketIndexes()) {
                if (!(o instanceof RangeIndex)) {
                    fail("Range Index should have been created instead of " + index.getClass());
                }
            }
        } else {
            fail("Partitioned index expected");
        }
    } catch (Exception ex) {
        fail("Failed to create index." + ex.getMessage());
    }
    for (int i = 0; i < queryString.length; i++) {
        try {
            Query query = localQueryService.newQuery(queryString[i]);
            rs[i][1] = (SelectResults) query.execute();
            checkForPdxString(rs[i][1].asList(), queryString[i]);
        } catch (Exception e) {
            fail("Failed executing " + queryString[i]);
        }
    }
    for (int i = 0; i < queryString.length; i++) {
        try {
            if (i < 7) {
                // Compare local and remote query results.
                if (!compareResultsOfWithAndWithoutIndex(rs[i])) {
                    fail("Local and Remote Query Results are not matching for query :" + queryString[i]);
                }
            } else {
                // compare the order of results returned
                compareResultsOrder(rs[i], true);
            }
        } catch (Exception e) {
            fail("Failed executing " + queryString[i]);
        }
    }
}
Also used : PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) Index(org.apache.geode.cache.query.Index) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) RegionExistsException(org.apache.geode.cache.RegionExistsException) TimeoutException(org.apache.geode.cache.TimeoutException) CacheException(org.apache.geode.cache.CacheException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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