Search in sources :

Example 91 with QueryService

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

the class PdxGroupByTestImpl method testAggregateFuncWithOrderBy.

@Override
@Test
public void testAggregateFuncWithOrderBy() throws Exception {
    Region region = this.createRegion("portfolio", PortfolioPdx.class);
    for (int i = 1; i < 600; ++i) {
        PortfolioPdx pf = new PortfolioPdx(i);
        if (pf.status.equals("active")) {
            pf.shortID = (short) ((short) i % 5);
        } else {
            pf.shortID = (short) ((short) i % 11);
        }
        region.put("key-" + i, pf);
    }
    String[] queries = { "select   p.status as status,  avg(distinct p.shortID) as average from /portfolio p where p.ID > 0 group by status order by average", "select   p.shortID as shid,  avg( p.ID) as average from /portfolio p where p.ID > 0 group by shid order by average desc", "select   p.shortID as shid,  avg( p.ID) as average from /portfolio p where p.ID > 0 group by shid order by  avg(p.ID)", "select   p.shortID as shid,  avg( p.ID) as average from /portfolio p where p.ID > 0 group by shid order by  avg(p.ID) desc, shid asc", "select   p.shortID as shid,  avg( p.ID) as average from /portfolio p where p.ID > 0 group by shid order by  avg(p.ID) desc, shid desc", "select   p.status as status,  p.shortID as shid  from /portfolio p where p.ID > 0 group by status, shid order by  shid desc", "select   p.shortID as shid,  count(*) as countt  from /portfolio p where p.ID > 0 group by p.shortID order by  count(*) desc" };
    Object[][] r = new Object[queries.length][2];
    QueryService qs = CacheUtils.getQueryService();
    for (int i = 0; i < queries.length; ++i) {
        Query query = qs.newQuery(queries[i]);
        SelectResults sr = (SelectResults) query.execute();
        r[i][0] = sr;
    }
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    // Compare each of the query results with queries fired without order by ,
    // but without order by
    ssOrrs.compareExternallySortedQueriesWithOrderBy(queries, r);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) 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) Test(org.junit.Test)

Example 92 with QueryService

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

the class QueryREUpdateInProgressJUnitTest method testCompactMapIndexQueriesOnREWhenUpdateInProgressWithOneIndex.

@Test
public void testCompactMapIndexQueriesOnREWhenUpdateInProgressWithOneIndex() throws Exception {
    // Create Indexes.
    Cache cache = CacheUtils.getCache();
    QueryService qs = cache.getQueryService();
    String[] queries = new String[] { "select * from /" + regionName + " pf where pf.positions['IBM'] != null", "select * from /" + regionName + " pf, pf.positions.values pos where pf.positions['IBM'] != null" };
    Object[][] results = new Object[queries.length][2];
    // Put values in Region.
    putREWithUpdateInProgressTrue(regionName);
    putREWithUpdateInProgressTrue(exampleRegionName);
    putREWithUpdateInProgressTrue(regionForAsyncIndex);
    // Run all queries.
    for (int i = 0; i < queries.length; i++) {
        try {
            results[i][0] = qs.newQuery("" + queries[i]).execute();
        } catch (Exception e) {
            throw new RuntimeException("Query execution failed for query: " + queries[i], e);
        }
    }
    qs.createIndex("mapIndex", "pf.positions['IBM','YHOO','SUN']", "/" + regionName + " pf");
    // Run all queries with Indexes.
    for (int i = 0; i < queries.length; i++) {
        try {
            results[i][1] = qs.newQuery("" + queries[i]).execute();
        } catch (Exception e) {
            throw new RuntimeException("Query execution failed for query: " + queries[i] + "\n ResultSet 01: " + results[i][0] + "\n" + "ResultSet 02: " + results[i][1], e);
        }
    }
    // Compare query results
    GemFireCacheImpl.getInstance().getLogger().fine("\n Result 01: " + results[queries.length - 1][0] + "\n\n Result 02: " + results[queries.length - 1][1]);
    new StructSetOrResultsSet().CompareQueryResultsWithoutAndWithIndexes(results, queries.length, false, queries);
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 93 with QueryService

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

the class QueryREUpdateInProgressJUnitTest method testQueriesOnREWhenUpdateInProgressWithOneIndex.

@Test
public void testQueriesOnREWhenUpdateInProgressWithOneIndex() throws Exception {
    // Create Indexes.
    Cache cache = CacheUtils.getCache();
    QueryService qs = cache.getQueryService();
    // Get Queries.
    String[] queries = getQueries();
    Object[][] results = new Object[queries.length][2];
    // Put values in Region.
    putREWithUpdateInProgressTrue(regionName);
    putREWithUpdateInProgressTrue(exampleRegionName);
    putREWithUpdateInProgressTrue(regionForAsyncIndex);
    // Run all queries.
    for (int i = 0; i < queries.length; i++) {
        try {
            results[i][0] = qs.newQuery("<trace> " + queries[i]).execute();
        } catch (Exception e) {
            throw new RuntimeException("Query executio failed for query: " + queries[i], e);
        }
    }
    qs.createIndex("idIndex", "p.ID", "/" + regionName + " p");
    // Run all queries with Indexes.
    for (int i = 0; i < queries.length; i++) {
        try {
            results[i][1] = qs.newQuery("<trace> " + queries[i]).execute();
        } catch (Exception e) {
            throw new RuntimeException("Query executio failed for query: " + queries[i] + "\n ResultSet 01: " + results[i][0] + "\n" + "ResultSet 02: " + results[i][1], e);
        }
    }
    // Compare query results
    GemFireCacheImpl.getInstance().getLogger().fine("\n Result 01: " + results[queries.length - 1][0] + "\n\n Result 02: " + results[queries.length - 1][1]);
    new StructSetOrResultsSet().CompareQueryResultsWithoutAndWithIndexes(results, queries.length, false, queries);
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 94 with QueryService

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

the class QueryREUpdateInProgressJUnitTest method testQueriesOnREWhenUpdateInProgress.

/**
   * This tests queries without limit clause.
   */
@Test
public void testQueriesOnREWhenUpdateInProgress() throws Exception {
    // Create Indexes.
    Cache cache = CacheUtils.getCache();
    QueryService qs = cache.getQueryService();
    // Get Queries.
    String[] queries = getQueries();
    Object[][] results = new Object[queries.length][2];
    // Put values in Region.
    putREWithUpdateInProgressTrue(regionName);
    putREWithUpdateInProgressTrue(exampleRegionName);
    putREWithUpdateInProgressTrue(regionForAsyncIndex);
    // Run all queries.
    for (int i = 0; i < queries.length; i++) {
        try {
            results[i][0] = qs.newQuery("<trace> " + queries[i]).execute();
        } catch (Exception e) {
            throw new RuntimeException("Query execution failed for query: " + queries[i], e);
        }
    }
    try {
        qs.createIndex("idIndex", "p.ID", "/" + regionName + " p");
        qs.createIndex("statusIndex", "p.status", "/" + regionName + " p");
        qs.createIndex("secIdIndex", "pos.secId", "/" + regionName + " p, p.positions.values pos");
        qs.createIndex("pentryKeyIndex", "p_ent.key", "/" + regionName + ".entries p_ent");
        qs.createIndex("pentryValueIndex", "ppos.secId", "/" + regionName + ".entries p_ent, p_ent.value.positions.values ppos");
        qs.createIndex("eidIndex", "e.ID", "/" + exampleRegionName + " e");
        qs.createIndex("estatusIndex", "e.status", "/" + exampleRegionName + " e");
        qs.createIndex("eentryKeyIndex", "e_ent.key", "/" + exampleRegionName + ".entries e_ent");
        qs.createIndex("eentryValueIndex", "epos.secId", "/" + exampleRegionName + ".entries e_ent, e_ent.value.positions.values epos");
        qs.createIndex("keyIndex", "toString", "/" + regionForAsyncIndex + ".keys");
    } catch (Exception e) {
        throw new RuntimeException("Index creation failed!", e);
    }
    // Run all queries with Indexes.
    for (int i = 0; i < queries.length; i++) {
        try {
            results[i][1] = qs.newQuery("<trace> " + queries[i]).execute();
        } catch (Exception e) {
            throw new RuntimeException("Query execution failed for query: " + queries[i] + "\n ResultSet 01: " + results[i][0] + "\n" + "ResultSet 02: " + results[i][1] + "\n", e);
        }
    }
    // Compare query results
    GemFireCacheImpl.getInstance().getLogger().fine("\n Result 01: " + results[queries.length - 1][0] + "\n\n Result 02: " + results[queries.length - 1][1]);
    new StructSetOrResultsSet().CompareQueryResultsWithoutAndWithIndexes(results, queries.length, false, queries);
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 95 with QueryService

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

the class OrderByReplicatedJUnitTest method testOrderByWithNullValues.

@Test
public void testOrderByWithNullValues() throws Exception {
    // IN ORDER BY NULL values are treated as smallest. E.g For an ascending
    // order by field
    // its null values are reported first and then the values in ascending
    // order.
    String[] queries = getQueriesForOrderByWithNullValues();
    Object[][] r = new Object[queries.length][2];
    QueryService qs;
    qs = CacheUtils.getQueryService();
    // Create Regions
    final int size = 9;
    final int numNullValues = 3;
    Region r1 = createRegion("portfolio1", Portfolio.class);
    for (int i = 1; i <= size; i++) {
        Portfolio pf = new Portfolio(i);
        // Add numNullValues null values.
        if (i <= numNullValues) {
            pf.pkid = null;
            pf.status = "a" + i;
        }
        r1.put(i + "", pf);
    }
    Query q = null;
    SelectResults results = null;
    List list = null;
    String str = "";
    try {
        // Query 0 - null values are first in the order.
        str = queries[0];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        r[0][0] = results;
        list = results.asList();
        for (int i = 1; i <= size; i++) {
            Portfolio p = (Portfolio) list.get((i - 1));
            if (i <= numNullValues) {
                assertNull("Expected null value for pkid, p: " + p, p.pkid);
            } else {
                assertNotNull("Expected not null value for pkid", p.pkid);
                if (!p.pkid.equals("" + i)) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 1 - null values are first in the order.
        str = queries[1];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= size; i++) {
            Portfolio p = (Portfolio) list.get((i - 1));
            if (i <= numNullValues) {
                assertNull("Expected null value for pkid", p.pkid);
            } else {
                assertNotNull("Expected not null value for pkid", p.pkid);
                if (!p.pkid.equals("" + i)) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 2 - null values are last in the order.
        str = queries[2];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= size; i++) {
            Portfolio p = (Portfolio) list.get((i - 1));
            if (i > (size - numNullValues)) {
                assertNull("Expected null value for pkid", p.pkid);
            } else {
                assertNotNull("Expected not null value for pkid", p.pkid);
                if (!p.pkid.equals("" + (size - (i - 1)))) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 3 - 1 distinct null value with pkid.
        str = queries[3];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            String pkid = (String) list.get((i - 1));
            if (i == 1) {
                assertNull("Expected null value for pkid", pkid);
            } else {
                assertNotNull("Expected not null value for pkid", pkid);
                if (!pkid.equals("" + (numNullValues + (i - 1)))) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 4 - 1 distinct null value with pkid.
        str = queries[4];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            String pkid = (String) list.get((i - 1));
            if (i == 1) {
                assertNull("Expected null value for pkid", pkid);
            } else {
                assertNotNull("Expected not null value for pkid", pkid);
                if (!pkid.equals("" + (numNullValues + (i - 1)))) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 5 - 1 distinct null value with pkid at the end.
        str = queries[5];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            String pkid = (String) list.get((i - 1));
            if (i == (list.size())) {
                assertNull("Expected null value for pkid", pkid);
            } else {
                assertNotNull("Expected not null value for pkid", pkid);
                if (!pkid.equals("" + (size - (i - 1)))) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 6 - ID field values should be in the same order.
        str = queries[6];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= size; i++) {
            int id = ((Integer) list.get((i - 1))).intValue();
            // ID should be one of 1, 2, 3 because of distinct
            if (i <= numNullValues) {
                if (!(id == 1 || id == 2 || id == 3)) {
                    fail(" Value of ID is not as expected " + id);
                }
            } else {
                if (id != i) {
                    fail(" Value of ID is not as expected " + id);
                }
            }
        }
        // Query 7 - ID field values should be in the same order.
        str = queries[7];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            int id = ((Integer) list.get((i - 1))).intValue();
            if (id != (numNullValues + i)) {
                fail(" Value of ID is not as expected, " + id);
            }
        }
        // Query 8 - ID, pkid field values should be in the same order.
        str = queries[8];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= size; i++) {
            Struct vals = (Struct) list.get((i - 1));
            int id = ((Integer) vals.get("ID")).intValue();
            String pkid = (String) vals.get("pkid");
            // ID should be one of 1, 2, 3 because of distinct
            if (i <= numNullValues) {
                if (!(id == 1 || id == 2 || id == 3)) {
                    fail(" Value of ID is not as expected " + id);
                }
                assertNull("Expected null value for pkid", pkid);
            } else {
                if (id != i) {
                    fail(" Value of ID is not as expected " + id);
                }
                assertNotNull("Expected not null value for pkid", pkid);
                if (!pkid.equals("" + i)) {
                    fail(" Value of pkid is not in expected order.");
                }
            }
        }
        // Query 9 - ID, pkid field values should be in the same order.
        str = queries[9];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            Struct vals = (Struct) list.get((i - 1));
            int id = ((Integer) vals.get("ID")).intValue();
            String pkid = (String) vals.get("pkid");
            if (i <= numNullValues) {
                assertNull("Expected null value for pkid, " + pkid, pkid);
                if (!(id == 1 || id == 2 || id == 3)) {
                    fail(" Value of ID is not as expected " + id);
                }
            } else {
                if (!pkid.equals("" + i)) {
                    fail(" Value of pkid is not as expected, " + pkid);
                }
                if (id != i) {
                    fail(" Value of ID is not as expected, " + id);
                }
            }
        }
        // Query 10 - ID asc, pkid field values should be in the same order.
        str = queries[10];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            Struct vals = (Struct) list.get((i - 1));
            int id = ((Integer) vals.get("ID")).intValue();
            String pkid = (String) vals.get("pkid");
            if (i <= numNullValues) {
                assertNull("Expected null value for pkid, " + pkid, pkid);
                if (id != i) {
                    fail(" Value of ID is not as expected, it is: " + id + " expected :" + i);
                }
            } else {
                if (!pkid.equals("" + i)) {
                    fail(" Value of pkid is not as expected, " + pkid);
                }
                if (id != i) {
                    fail(" Value of ID is not as expected, " + id);
                }
            }
        }
        // Query 11 - ID desc, pkid field values should be in the same order.
        str = queries[11];
        q = CacheUtils.getQueryService().newQuery(str);
        CacheUtils.getLogger().info("Executing query: " + str);
        results = (SelectResults) q.execute();
        list = results.asList();
        for (int i = 1; i <= list.size(); i++) {
            Struct vals = (Struct) list.get((i - 1));
            int id = ((Integer) vals.get("ID")).intValue();
            String pkid = (String) vals.get("pkid");
            if (i <= numNullValues) {
                assertNull("Expected null value for pkid, " + pkid, pkid);
                if (id != (numNullValues - (i - 1))) {
                    fail(" Value of ID is not as expected " + id);
                }
            } else {
                if (!pkid.equals("" + i)) {
                    fail(" Value of pkid is not as expected, " + pkid);
                }
                if (id != i) {
                    fail(" Value of ID is not as expected, " + id);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(q.getQueryString());
    }
}
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) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) List(java.util.List) 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