Search in sources :

Example 46 with SelectResults

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

the class DistinctResultsWithDupValuesInRegionJUnitTest method testQueriesOnPartitionedRegionWithIndex.

/**
   * Test on Partitioned Region data
   */
@Test
public void testQueriesOnPartitionedRegionWithIndex() {
    Cache cache = CacheUtils.getCache();
    createPartitionedRegion();
    assertNotNull(cache.getRegion(regionName));
    assertEquals(numElem * 2, cache.getRegion(regionName).size());
    QueryService queryService = cache.getQueryService();
    Query query1 = null;
    try {
        queryService.createIndex("idIndex", "p.ID", "/" + regionName + " p");
        for (String queryStr : queries) {
            query1 = queryService.newQuery(queryStr);
            SelectResults result1 = (SelectResults) query1.execute();
            assertEquals(queryStr, numElem * 2, result1.size());
            verifyDistinctResults(result1);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Query " + query1 + " Execution Failed!");
    }
    // Destroy current Region for other tests
    cache.getRegion(regionName).destroyRegion();
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 47 with SelectResults

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

the class DistinctResultsWithDupValuesInRegionJUnitTest method testQueriesOnReplicatedRegionWithIndex.

/**
   * Test on Replicated Region data
   */
@Test
public void testQueriesOnReplicatedRegionWithIndex() {
    Cache cache = CacheUtils.getCache();
    createReplicatedRegion();
    assertNotNull(cache.getRegion(regionName));
    assertEquals(numElem * 2, cache.getRegion(regionName).size());
    QueryService queryService = cache.getQueryService();
    Query query1 = null;
    try {
        queryService.createIndex("idIndex", "p.ID", "/" + regionName + " p");
        for (String queryStr : queries) {
            query1 = queryService.newQuery(queryStr);
            SelectResults result1 = (SelectResults) query1.execute();
            assertEquals(queryStr, numElem * 2, result1.size());
            verifyDistinctResults(result1);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Query " + query1 + " Execution Failed!");
    }
    // Destroy current Region for other tests
    cache.getRegion(regionName).destroyRegion();
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 48 with SelectResults

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

the class NonDistinctOrderByReplicatedJUnitTest method testNonDistinctOrderbyResultSetForReplicatedRegion.

@Test
public void testNonDistinctOrderbyResultSetForReplicatedRegion() throws Exception {
    final int numElements = 200;
    CacheUtils.getCache();
    Region region = this.createRegion("portfolios", Portfolio.class);
    Short[] expectedArray = new Short[numElements - 1];
    for (int i = 1; i < numElements; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.shortID = (short) ((short) i / 5);
        region.put("" + i, pf);
        expectedArray[i - 1] = pf.shortID;
    }
    Arrays.sort(expectedArray, new Comparator<Short>() {

        @Override
        public int compare(Short o1, Short o2) {
            return o1.shortValue() - o2.shortValue();
        }
    });
    String query = "select pf.shortID from /portfolios pf order by pf.shortID";
    QueryService qs = CacheUtils.getQueryService();
    SelectResults sr = (SelectResults) qs.newQuery(query).execute();
    Object[] results = sr.toArray();
    assertTrue(Arrays.equals(expectedArray, results));
}
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) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 49 with SelectResults

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

the class NonDistinctOrderByReplicatedJUnitTest method testLimitAndOrderByApplicationOnPrimaryKeyIndexQuery.

@Test
public void testLimitAndOrderByApplicationOnPrimaryKeyIndexQuery() throws Exception {
    String[] queries = { // from index
    "SELECT   ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != '10' order by ID desc limit 5 ", "SELECT   ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != $1 order by ID " };
    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) {
                assertTrue(rcw.getCollectionType().isOrdered());
            }
            if (!observer.isIndexesUsed) {
                fail("Index is NOT uesd");
            }
            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());
        }
    }
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, true, queries);
}
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) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 50 with SelectResults

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

the class NonDistinctOrderByTestImplementation method testLimitNotAppliedIfOrderByNotUsingIndex.

@Test
public void testLimitNotAppliedIfOrderByNotUsingIndex() throws Exception {
    String[] queries = { // Test case No. IUMR021
    "SELECT    description, pkid FROM /portfolio1 pf1 where pkid = '12' and ID > 10  order by pkid asc ", "SELECT    description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID > 10 order by  pkid desc ", "SELECT    description, pkid FROM /portfolio1 pf1 where pkid = '13'and  ID > 10 and ID < 20 order by  pkid asc ", "SELECT    description, pkid FROM /portfolio1 pf1 where pkid <'9' and ID > 10 and ID < 20 order by  pkid desc", "SELECT    description, pkid FROM /portfolio1 pf1 where pkid = '15' and ID >= 10 and ID <= 20 order by  pkid desc ", "SELECT    description, pkid FROM /portfolio1 pf1 where pkid > '1' and pkid <='9' and ID >= 10 and ID <= 20 order by  pkid asc", "SELECT   description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID != 10 order by  pkid asc ", "SELECT   description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID != 10 order by  pkid desc ", "SELECT  description, pkid FROM /portfolio1 pf1 where pkid = '17' and ID > 10 order by  pkid asc limit 5", "SELECT    description,  pkid FROM /portfolio1 pf1 where pkid > '17' and ID > 10 order by  pkid desc limit 5", "SELECT    description,  pkid FROM /portfolio1 pf1 where pkid < '7' and ID > 10 and ID < 20 order by  pkid asc limit 5 ", "SELECT   description, pkid FROM /portfolio1 pf1 where pkid = '18' and ID > 10 and ID < 20 order by  pkid desc limit 5", "SELECT    description,  pkid FROM /portfolio1 pf1 where pkid > '1' and ID >= 10 and ID <= 20 order by pkid asc limit 5", "SELECT   description,  pkid FROM /portfolio1 pf1 where pkid != '17' and ID >= 10 and ID <= 20 order by pkid desc limit 5", "SELECT   description,  pkid FROM /portfolio1 pf1 where pkid > '0' and ID != 10 order by  pkid asc limit 10", "SELECT   description, createTime, pkid FROM /portfolio1 pf1 where pkid > '9' and ID != 10 order by  pkid desc 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 = 1; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.shortID = (short) ((short) i / 5);
        r1.put("" + i, pf);
    }
    // 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();
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create Indexes
    this.createIndex("PKIDIndexPf1", IndexType.FUNCTIONAL, "pkid", "/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();
            int indexLimit = queries[i].indexOf("limit");
            int limit = -1;
            boolean limitQuery = indexLimit != -1;
            if (limitQuery) {
                limit = Integer.parseInt(queries[i].substring(indexLimit + 5).trim());
            }
            SelectResults rcw = (SelectResults) r[i][1];
            assertTrue(rcw.getCollectionType().isOrdered());
            if (assertIndexUsedOnQueryNode() && !observer.isIndexesUsed) {
                fail("Index is NOT uesd");
            }
            // assertTrue(!limitQuery || !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());
        }
    }
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, true, queries);
    ssOrrs.compareExternallySortedQueriesWithOrderBy(queries, r);
}
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) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Aggregations

SelectResults (org.apache.geode.cache.query.SelectResults)577 Test (org.junit.Test)423 Query (org.apache.geode.cache.query.Query)360 Region (org.apache.geode.cache.Region)336 QueryService (org.apache.geode.cache.query.QueryService)331 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)256 Portfolio (org.apache.geode.cache.query.data.Portfolio)249 Index (org.apache.geode.cache.query.Index)133 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 Host (org.apache.geode.test.dunit.Host)107 VM (org.apache.geode.test.dunit.VM)107 CacheException (org.apache.geode.cache.CacheException)105 Iterator (java.util.Iterator)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)101 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)89 Cache (org.apache.geode.cache.Cache)84 Struct (org.apache.geode.cache.query.Struct)80 LocalRegion (org.apache.geode.internal.cache.LocalRegion)67 ObjectType (org.apache.geode.cache.query.types.ObjectType)66