Search in sources :

Example 21 with DefaultQuery

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

the class IndexHintJUnitTest method testTwoIndexHint.

// Tests the grammar for a hint with two index names
@Test
public void testTwoIndexHint() throws Exception {
    createRegion();
    QueryService qs = CacheUtils.getQueryService();
    DefaultQuery query = (DefaultQuery) qs.newQuery("<hint 'FirstIndex', 'SecondIndex'> select * from /Portfolios p where p.ID > 10");
    QueryExecutionContext qec = new QueryExecutionContext(new Object[1], CacheUtils.getCache(), query);
    query.executeUsingContext(qec);
    assertTrue(qec.isHinted("FirstIndex"));
    assertTrue(qec.isHinted("SecondIndex"));
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 22 with DefaultQuery

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

the class LocalDataSet method query.

public SelectResults query(String queryPredicate) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    QueryService qs = getCache().getLocalQueryService();
    DefaultQuery query = (DefaultQuery) qs.newQuery("select * from " + getFullPath() + " this where " + queryPredicate);
    Object[] params = null;
    return (SelectResults) this.executeQuery(query, params, getBucketSet());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService)

Example 23 with DefaultQuery

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

the class QueryJUnitTest method testThreadSafetyOfCompiledSelectScopeId.

@Test
public void testThreadSafetyOfCompiledSelectScopeId() throws Exception {
    try {
        Cache cache = CacheUtils.getCache();
        RegionFactory<Integer, Portfolio> rf = cache.createRegionFactory(RegionShortcut.PARTITION);
        Region r = rf.create("keyzset");
        for (int i = 0; i < 100; i++) {
            r.put(i, new Portfolio(i));
        }
        ScopeThreadingTestHook scopeIDTestHook = new ScopeThreadingTestHook(3);
        DefaultQuery.testHook = scopeIDTestHook;
        QueryService qs = cache.getQueryService();
        Query q = qs.newQuery("SELECT DISTINCT * FROM /keyzset.keySet key WHERE key.id > 0 AND key.id <= 0 ORDER BY key asc LIMIT $3");
        Thread q1 = new Thread(new QueryRunnable(q, new Object[] { 10, 20, 10 }));
        Thread q2 = new Thread(new QueryRunnable(q, new Object[] { 5, 10, 5 }));
        Thread q3 = new Thread(new QueryRunnable(q, new Object[] { 2, 10, 8 }));
        q1.start();
        q2.start();
        q3.start();
        q1.join();
        q2.join();
        q3.join();
        assertEquals("Exceptions were thrown due to DefaultQuery not being thread-safe", true, scopeIDTestHook.isOk());
    } finally {
        DefaultQuery.testHook = null;
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) 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 24 with DefaultQuery

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

the class QueryJUnitTest method test006GetRegionsInQuery.

@Test
public void test006GetRegionsInQuery() {
    String[] queryStrs = new String[] { "SELECT DISTINCT * FROM /Portfolios where status='active'", "/Portfolios", "/Portfolios.values", "/Portfolios.keys()", "/Portfolios.entries(false)", "null = null", "select distinct * from /Employees where not (select distinct * from collect).isEmpty", "select distinct * from $2 where salary > $1", "SELECT DISTINCT key: key, iD: entry.value.iD, secId: posnVal.secId  FROM /pos.entries entry, entry.value.positions.values posnVal  WHERE entry.value.\"type\" = 'type0' AND posnVal.secId = 'YHOO'", "SELECT DISTINCT * FROM (SELECT DISTINCT * FROM /Portfolios ptf, positions pos) WHERE pos.value.secId = 'IBM'", "SELECT DISTINCT * FROM /Portfolios WHERE NOT(SELECT DISTINCT * FROM positions.values p WHERE p.secId = 'IBM').isEmpty", "SELECT DISTINCT * FROM /Portfolios where status = ELEMENT(SELECT DISTINCT * FROM /Portfolios p where p.ID = 0).status", "Select distinct * from /Portfolios pf, /Portfolios2, /Portfolios3, /Data where pf.status='active'", "select distinct * from /portfolios p, p.positions.values myPos, (select distinct * from /Employees x)  where myPos.secId = 'YHOO'", "select distinct * from /portfolios p, p.positions.values myPos, (select distinct * from /Employees x, /portfolios)  where myPos.secId = 'YHOO'", "select distinct * from /portfolios p, p.positions.values myPos, (select distinct * from /Employees x, /Portfolios)  where myPos.secId = 'YHOO'", "select distinct /Portfolios.size, key FROM /pos.entries", "select distinct /Portfolios2.size, key FROM /pos.entries WHERE (Select distinct * from /portfolios4, entries).size = 3" };
    String[][] regions = new String[][] { { "/Portfolios" }, { "/Portfolios" }, { "/Portfolios" }, { "/Portfolios" }, { "/Portfolios" }, {}, { "/Employees" }, { "/Portfolios" }, { "/pos" }, { "/Portfolios" }, { "/Portfolios" }, { "/Portfolios" }, { "/Portfolios", "/Portfolios2", "/Portfolios3", "/Data" }, { "/portfolios", "/Employees" }, { "/portfolios", "/Employees" }, { "/portfolios", "/Employees", "/Portfolios" }, { "/Portfolios", "/pos" }, { "/Portfolios2", "/pos", "/portfolios4" } };
    Object[] params = new Object[] { "", CacheUtils.createRegion("Portfolios", Portfolio.class) };
    for (int i = 0; i < queryStrs.length; ++i) {
        Query q = CacheUtils.getQueryService().newQuery(queryStrs[i]);
        Set set = ((DefaultQuery) q).getRegionsInQuery(params);
        String[] qRegions = regions[i];
        assertEquals("region names don't match in query #" + i + "(\"" + queryStrs[i] + "\"", new HashSet(Arrays.asList(qRegions)), set);
    }
    DefaultQuery q = (DefaultQuery) CacheUtils.getQueryService().newQuery(queryStrs[0]);
    Set set = q.getRegionsInQuery(params);
    try {
        set.add("test");
        fail("The set returned should not be modifiable");
    } catch (Exception e) {
    // Expected
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 25 with DefaultQuery

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

the class RangeIndexAPIJUnitTest method testQueryMethod_3.

/**
   * Tests the query method of RangeIndex with takes a bound defined ( lower as well as upper) & may
   * contain NotEqaul Keys
   */
@Test
public void testQueryMethod_3() 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();
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GT, new Integer(10), OQLLexerTokenTypes.TOK_LT, results, null, context);
    assertEquals(4, results.size());
    for (int i = 6; i < 10; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    keysToRemove.add(new Integer(10));
    keysToRemove.add(new Integer(9));
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GT, new Integer(10), OQLLexerTokenTypes.TOK_LT, results, keysToRemove, context);
    assertEquals(3, results.size());
    for (int i = 6; i < 9; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    keysToRemove.add(new Integer(10));
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GT, new Integer(10), OQLLexerTokenTypes.TOK_LE, results, keysToRemove, context);
    assertEquals(4, results.size());
    for (int i = 6; i < 10; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GT, new Integer(10), OQLLexerTokenTypes.TOK_LE, results, null, context);
    assertEquals(5, results.size());
    for (int i = 6; i < 11; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GE, new Integer(10), OQLLexerTokenTypes.TOK_LE, results, null, context);
    assertEquals(6, results.size());
    for (int i = 5; i < 11; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    keysToRemove.add(new Integer(5));
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GE, new Integer(10), OQLLexerTokenTypes.TOK_LE, results, keysToRemove, context);
    assertEquals(5, results.size());
    for (int i = 6; i < 11; ) {
        assertTrue(results.contains(region.get(new Integer(i))));
        ++i;
    }
    results.clear();
    keysToRemove.clear();
    keysToRemove.add(new Integer(5));
    keysToRemove.add(new Integer(10));
    keysToRemove.add(new Integer(7));
    i1.query(new Integer(5), OQLLexerTokenTypes.TOK_GE, new Integer(10), OQLLexerTokenTypes.TOK_LE, results, keysToRemove, context);
    assertEquals(3, results.size());
    assertTrue(results.contains(region.get(new Integer(6))));
    assertTrue(results.contains(region.get(new Integer(8))));
    assertTrue(results.contains(region.get(new Integer(9))));
}
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)

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