Search in sources :

Example 1 with QCompiler

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

the class QueryDataFunction method compileQuery.

/**
   * Compile the query and return a set of regions involved in the query It throws an
   * QueryInvalidException if the query is not proper
   *
   * @param cache current cache
   * @param query input query
   *
   * @return a set of regions involved in the query
   */
private static Set<String> compileQuery(final InternalCache cache, final String query) throws QueryInvalidException {
    QCompiler compiler = new QCompiler();
    Set<String> regionsInQuery;
    try {
        CompiledValue compiledQuery = compiler.compileQuery(query);
        Set<String> regions = new HashSet<>();
        compiledQuery.getRegionsInQuery(regions, null);
        regionsInQuery = Collections.unmodifiableSet(regions);
        return regionsInQuery;
    } catch (QueryInvalidException qe) {
        logger.error("{} Failed, Error {}", query, qe.getMessage(), qe);
        throw qe;
    }
}
Also used : QCompiler(org.apache.geode.cache.query.internal.QCompiler) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) HashSet(java.util.HashSet)

Example 2 with QCompiler

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

the class RangeIndexAPIJUnitTest method bindIterators.

private void bindIterators(ExecutionContext context, String string) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    QCompiler compiler = new QCompiler();
    List compilerItrDefs = compiler.compileFromClause(string);
    context.newScope(0);
    for (Object itrDef : compilerItrDefs) {
        CompiledIteratorDef iterDef = (CompiledIteratorDef) itrDef;
        RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
        context.bindIterator(rIter);
    }
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) QCompiler(org.apache.geode.cache.query.internal.QCompiler) List(java.util.List) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator)

Example 3 with QCompiler

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

the class IndexCreationInternalsJUnitTest method testLoneProjectionAttributes.

@Test
public void testLoneProjectionAttributes() throws Exception {
    // compileProjectionAttributes returns a List or null.
    // null if '*', or a List<Object[2]>.
    // The two-element Object arrays are:
    // 0: a String, the field name, or null if no identifier provided
    // 1: The CompiledValue for the projection expression.
    QCompiler compiler = new QCompiler();
    List list = compiler.compileProjectionAttributes("*");
    assertNull(list);
    compiler = new QCompiler();
    list = compiler.compileProjectionAttributes("ID, status");
    assertEquals(2, list.size());
    Object[] firstProj = (Object[]) list.get(0);
    assertEquals(2, firstProj.length);
    // no field name
    assertNull(firstProj[0]);
    CompiledID id1 = (CompiledID) firstProj[1];
    assertEquals("ID", id1.getId());
    Object[] secondProj = (Object[]) list.get(1);
    assertEquals(2, secondProj.length);
    // no field name
    assertNull(secondProj[0]);
    CompiledID id2 = (CompiledID) secondProj[1];
    assertEquals("status", id2.getId());
    // test two ways of specifying the field names
    compiler = new QCompiler();
    list = compiler.compileProjectionAttributes("x: ID, y: status");
    assertEquals(2, list.size());
    firstProj = (Object[]) list.get(0);
    assertEquals(2, firstProj.length);
    assertEquals("x", firstProj[0]);
    id1 = (CompiledID) firstProj[1];
    assertEquals("ID", id1.getId());
    secondProj = (Object[]) list.get(1);
    assertEquals(2, secondProj.length);
    assertEquals("y", secondProj[0]);
    id2 = (CompiledID) secondProj[1];
    assertEquals("status", id2.getId());
    // the following is invalid
    try {
        compiler = new QCompiler();
        list = compiler.compileProjectionAttributes("ID x, status y");
        fail("Should have thrown a QueryInvalidException");
    } catch (QueryInvalidException e) {
    // pass
    }
    // test three ways of specifying the field names
    compiler = new QCompiler();
    list = compiler.compileProjectionAttributes("ID AS x, status as y");
    assertEquals(2, list.size());
    firstProj = (Object[]) list.get(0);
    assertEquals(2, firstProj.length);
    assertEquals("x", firstProj[0]);
    id1 = (CompiledID) firstProj[1];
    assertEquals("ID", id1.getId());
    secondProj = (Object[]) list.get(1);
    assertEquals(2, secondProj.length);
    assertEquals("y", secondProj[0]);
    id2 = (CompiledID) secondProj[1];
    assertEquals("status", id2.getId());
}
Also used : CompiledID(org.apache.geode.cache.query.internal.CompiledID) QCompiler(org.apache.geode.cache.query.internal.QCompiler) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) List(java.util.List) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 4 with QCompiler

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

the class IndexCreationInternalsJUnitTest method testLoneFromClause.

@Test
public void testLoneFromClause() throws Exception {
    // compileFromClause returns a List<CompiledIteratorDef>
    QCompiler compiler = new QCompiler();
    List list = compiler.compileFromClause("/pos p, p.positions");
    assertEquals(2, list.size());
    CompiledIteratorDef first = (CompiledIteratorDef) list.get(0);
    assertEquals("p", first.getName());
    assertEquals("/pos", ((CompiledRegion) first.getCollectionExpr()).getRegionPath());
    assertEquals(TypeUtils.OBJECT_TYPE, first.getElementType());
    CompiledIteratorDef second = (CompiledIteratorDef) list.get(1);
    assertNull(second.getName());
    CompiledPath path = (CompiledPath) second.getCollectionExpr();
    assertEquals("p", ((CompiledID) path.getReceiver()).getId());
    assertEquals("positions", path.getTailID());
    assertEquals(TypeUtils.OBJECT_TYPE, second.getElementType());
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) QCompiler(org.apache.geode.cache.query.internal.QCompiler) List(java.util.List) CompiledPath(org.apache.geode.cache.query.internal.CompiledPath) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with QCompiler

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

the class PartitionedRegionDataStore method getIndexes.

private List getIndexes(String rootRegion, String bucketRegion) {
    List indexes = null;
    if (!this.partitionedRegion.isIndexed()) {
        return indexes;
    }
    // Get PR indexes.
    Map indexMap = this.partitionedRegion.getIndex();
    if (indexMap == null || indexMap.isEmpty()) {
        return indexes;
    }
    // Build index info thats used to create indexes on bucket regions.
    indexes = new ArrayList();
    Set indexSet = indexMap.entrySet();
    for (Iterator it = indexSet.iterator(); it.hasNext(); ) {
        try {
            Map.Entry indexEntry = (Map.Entry) it.next();
            PartitionedIndex index = (PartitionedIndex) indexEntry.getValue();
            IndexCreationData icd = new IndexCreationData(index.getName());
            new QCompiler();
            String imports = index.getImports();
            icd.setIndexData(index.getType(), index.getCanonicalizedFromClause(), index.getCanonicalizedIndexedExpression(), index.getImports());
            icd.setPartitionedIndex(index);
            indexes.add(icd);
        } catch (Exception ignor) {
            // since bucket creation should not fail.
            logger.info(LocalizedMessage.create(LocalizedStrings.PartitionedRegionDataStore_EXCPETION__IN_BUCKET_INDEX_CREATION_, ignor.getLocalizedMessage()), ignor);
        }
    }
    return indexes;
}
Also used : IndexCreationData(org.apache.geode.cache.query.internal.index.IndexCreationData) PartitionedIndex(org.apache.geode.cache.query.internal.index.PartitionedIndex) Entry(org.apache.geode.cache.Region.Entry) SizeEntry(org.apache.geode.internal.cache.PartitionedRegion.SizeEntry) QCompiler(org.apache.geode.cache.query.internal.QCompiler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BucketMovedException(org.apache.geode.internal.cache.execute.BucketMovedException) InternalGemFireException(org.apache.geode.InternalGemFireException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException)

Aggregations

QCompiler (org.apache.geode.cache.query.internal.QCompiler)6 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)4 List (java.util.List)3 HashSet (java.util.HashSet)2 CompiledIteratorDef (org.apache.geode.cache.query.internal.CompiledIteratorDef)2 CompiledValue (org.apache.geode.cache.query.internal.CompiledValue)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 InternalGemFireException (org.apache.geode.InternalGemFireException)1 Entry (org.apache.geode.cache.Region.Entry)1 FunctionException (org.apache.geode.cache.execute.FunctionException)1 CompiledID (org.apache.geode.cache.query.internal.CompiledID)1 CompiledPath (org.apache.geode.cache.query.internal.CompiledPath)1 RuntimeIterator (org.apache.geode.cache.query.internal.RuntimeIterator)1 IndexCreationData (org.apache.geode.cache.query.internal.index.IndexCreationData)1 PartitionedIndex (org.apache.geode.cache.query.internal.index.PartitionedIndex)1