Search in sources :

Example 56 with SelectResults

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

the class OrderByPartitionedJUnitTest 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) ((Struct) list.get((i - 1))).getFieldValues()[0];
            // 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, pkid 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++) {
            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 8 - ID asc, 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 <= 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 9 - ID desc, 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 != (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) TestObject(org.apache.geode.cache.query.dunit.QueryUsingPoolDUnitTest.TestObject) List(java.util.List) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 57 with SelectResults

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

the class MultiIndexCreationJUnitTest method testMultiIndexCreationOnlyDefine.

@Test
public void testMultiIndexCreationOnlyDefine() throws Exception {
    Region r = CacheUtils.getRegion(regionName);
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("statusIndex", "status", r.getFullPath());
    qs.defineIndex("IDIndex", "ID", r.getFullPath());
    Index ind = qs.getIndex(r, "statusIndex");
    assertNull("Index should not have been created", ind);
    ind = qs.getIndex(r, "IDIndex");
    assertNull("Index should not have been created", ind);
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertFalse(indexCalled);
        }
    });
    String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        assertEquals(5, sr.size());
    }
    QueryObserverHolder.setInstance(old);
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 58 with SelectResults

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

the class MultiIndexCreationJUnitTest method testBasicMultiIndexCreationDifferentTypes.

@Test
public void testBasicMultiIndexCreationDifferentTypes() throws Exception {
    Region r = CacheUtils.getRegion(regionName);
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("statusIndex", "status", r.getFullPath());
    qs.defineHashIndex("IDIndex", "ID", r.getFullPath());
    qs.defineKeyIndex("keyIDIndex", "ID", r.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(r, "statusIndex");
    assertTrue(ind instanceof CompactRangeIndex);
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "IDIndex");
    assertTrue(ind instanceof HashIndex);
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "keyIDIndex");
    assertTrue(ind instanceof PrimaryKeyIndex);
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        assertEquals(5, sr.size());
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 59 with SelectResults

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

the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegions.

@Test
public void testIndexCreationOnMultipleRegions() throws Exception {
    Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
    for (int i = 0; i < 10; i++) {
        pr.put("" + i, new Portfolio(i));
    }
    Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
    for (int i = 0; i < 10; i++) {
        overflow.put("" + i, new Portfolio(i));
    }
    Region r = CacheUtils.getRegion(regionName);
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("IDIndex", "ID", pr.getFullPath());
    qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
    qs.defineIndex("statusIndex", "status", overflow.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(overflow, "statusIndex");
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(pr, "IDIndex");
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "secIDIndex");
    assertEquals(12, ind.getStatistics().getNumberOfKeys());
    assertEquals(20, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        if (i == 2) {
            assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
        } else {
            assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
        }
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 60 with SelectResults

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

the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegionsBeforePuts.

@Test
public void testIndexCreationOnMultipleRegionsBeforePuts() throws Exception {
    Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
    Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
    Region r = CacheUtils.getRegion(regionName);
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("IDIndex", "ID", pr.getFullPath());
    qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
    qs.defineIndex("statusIndex", "status", overflow.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    for (int i = 0; i < 10; i++) {
        pr.put("" + i, new Portfolio(i));
    }
    for (int i = 0; i < 10; i++) {
        overflow.put("" + i, new Portfolio(i));
    }
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(overflow, "statusIndex");
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(pr, "IDIndex");
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "secIDIndex");
    assertEquals(12, ind.getStatistics().getNumberOfKeys());
    assertEquals(20, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        if (i == 2) {
            assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
        } else {
            assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
        }
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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