Search in sources :

Example 1 with CompiledFunction

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

the class FunctionJUnitTest method testCanonicalization.

@Test
public void testCanonicalization() throws Exception {
    CompiledValue cv1 = new CompiledLiteral("str1");
    CompiledValue cv2 = new CompiledLiteral("str2");
    CompiledValue cv3 = new CompiledLiteral(null);
    CompiledValue cv4 = new CompiledLiteral(null);
    CompiledValue cv5 = new CompiledLiteral(new Integer(10));
    CompiledValue cv6 = new CompiledLiteral(new Integer(5));
    CompiledValue[][] cvArr = new CompiledValue[][] { { cv1, cv2, cv3, cv4, cv5, cv6 }, { cv6, cv5, cv3, cv4, cv2, cv1 }, { cv1, cv3, cv5, cv2, cv4, cv6 }, { cv1 }, { cv3 }, { cv5 } };
    String[] canonicalizedArgs = { "('str1','str2',null,null,10,5)", "(5,10,null,null,'str2','str1')", "('str1',null,10,'str2',null,5)", "('str1')", "(null)", "(10)" };
    ExecutionContext context = null;
    for (int i = 0; i < 6; i++) {
        CompiledValue cf = new CompiledFunction(cvArr[i], OQLLexerTokenTypes.LITERAL_nvl);
        StringBuilder clauseBuffer = new StringBuilder();
        cf.generateCanonicalizedExpression(clauseBuffer, context);
        if (!clauseBuffer.toString().equals("NVL" + canonicalizedArgs[i])) {
            fail("Canonicalization not done properly");
        }
        cf = new CompiledFunction(cvArr[i], OQLLexerTokenTypes.LITERAL_element);
        clauseBuffer = new StringBuilder();
        cf.generateCanonicalizedExpression(clauseBuffer, context);
        if (!clauseBuffer.toString().equals("ELEMENT" + canonicalizedArgs[i])) {
            fail("Canonicalization not done properly");
        }
    }
}
Also used : CompiledFunction(org.apache.geode.cache.query.internal.CompiledFunction) ExecutionContext(org.apache.geode.cache.query.internal.ExecutionContext) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) CompiledLiteral(org.apache.geode.cache.query.internal.CompiledLiteral) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with CompiledFunction

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

the class FunctionalIndexCreationHelper method getModifiedDependentCompiledValue.

/**
   * This function is used to correct the complied value's dependency , in case the compiledValue is
   * dependent on the 0th RuntimeIterator in some way. Thus the dependent compiled value is prefixed
   * with the missing link so that it is derivable from the 0th iterator.
   */
private CompiledValue getModifiedDependentCompiledValue(ExecutionContext context, int currItrID, CompiledValue cv, boolean isDependent) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    if (cv instanceof CompiledIteratorDef) {
        CompiledIteratorDef iterDef = (CompiledIteratorDef) cv;
        RuntimeIterator rItr = (RuntimeIterator) context.getCurrentIterators().get(currItrID);
        String canonFrmClause = rItr.getDefinition();
        // TODO: original value of isDependent is always ignored
        isDependent = canonFrmClause.startsWith(this.canonicalizedIteratorNames[0]);
        return new CompiledIteratorDef(iterDef.getName(), rItr.getElementType(), getModifiedDependentCompiledValue(context, currItrID, iterDef.getCollectionExpr(), isDependent));
    } else if (cv instanceof CompiledPath) {
        CompiledPath path = (CompiledPath) cv;
        return new CompiledPath(getModifiedDependentCompiledValue(context, currItrID, path.getReceiver(), isDependent), path.getTailID());
    } else if (cv instanceof CompiledOperation) {
        CompiledOperation oper = (CompiledOperation) cv;
        List list = oper.getArguments();
        List newList = new ArrayList();
        for (Object aList : list) {
            CompiledValue cv1 = (CompiledValue) aList;
            StringBuilder sb = new StringBuilder();
            cv1.generateCanonicalizedExpression(sb, context);
            if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
                newList.add(getModifiedDependentCompiledValue(context, currItrID, cv1, true));
            } else {
                newList.add(getModifiedDependentCompiledValue(context, currItrID, cv1, false));
            }
        }
        // What if the receiver is null?
        CompiledValue rec = oper.getReceiver(context);
        if (rec == null) {
            if (isDependent) {
                rec = this.missingLink;
            }
            return new CompiledOperation(rec, oper.getMethodName(), newList);
        } else {
            return new CompiledOperation(getModifiedDependentCompiledValue(context, currItrID, rec, isDependent), oper.getMethodName(), newList);
        }
    } else if (cv instanceof CompiledFunction) {
        CompiledFunction cf = (CompiledFunction) cv;
        CompiledValue[] cvArray = cf.getArguments();
        int function = cf.getFunction();
        int len = cvArray.length;
        CompiledValue[] newCvArray = new CompiledValue[len];
        for (int i = 0; i < len; ++i) {
            CompiledValue cv1 = cvArray[i];
            StringBuilder sb = new StringBuilder();
            cv1.generateCanonicalizedExpression(sb, context);
            if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
                newCvArray[i] = getModifiedDependentCompiledValue(context, currItrID, cv1, true);
            } else {
                newCvArray[i] = getModifiedDependentCompiledValue(context, currItrID, cv1, false);
            }
        }
        return new CompiledFunction(newCvArray, function);
    } else if (cv instanceof CompiledID) {
        CompiledID id = (CompiledID) cv;
        RuntimeIterator rItr0 = (RuntimeIterator) context.getCurrentIterators().get(0);
        if (isDependent) {
            String name;
            if ((name = rItr0.getName()) != null && name.equals(id.getId())) {
                // The CompiledID is a RuneTimeIterator & so it needs to be replaced by the missing link
                return this.missingLink;
            } else {
                // The compiledID is a compiledPath
                return new CompiledPath(this.missingLink, id.getId());
            }
        } else {
            return cv;
        }
    } else if (cv instanceof CompiledIndexOperation) {
        CompiledIndexOperation co = (CompiledIndexOperation) cv;
        CompiledValue cv1 = co.getExpression();
        StringBuilder sb = new StringBuilder();
        cv1.generateCanonicalizedExpression(sb, context);
        if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
            cv1 = getModifiedDependentCompiledValue(context, currItrID, cv1, true);
        } else {
            cv1 = getModifiedDependentCompiledValue(context, currItrID, cv1, false);
        }
        return new CompiledIndexOperation(getModifiedDependentCompiledValue(context, currItrID, co.getReceiver(), isDependent), cv1);
    } else {
        return cv;
    }
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) CompiledID(org.apache.geode.cache.query.internal.CompiledID) CompiledFunction(org.apache.geode.cache.query.internal.CompiledFunction) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) ArrayList(java.util.ArrayList) CompiledPath(org.apache.geode.cache.query.internal.CompiledPath) CompiledIndexOperation(org.apache.geode.cache.query.internal.CompiledIndexOperation) CompiledOperation(org.apache.geode.cache.query.internal.CompiledOperation) ArrayList(java.util.ArrayList) List(java.util.List) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator)

Aggregations

CompiledFunction (org.apache.geode.cache.query.internal.CompiledFunction)2 CompiledValue (org.apache.geode.cache.query.internal.CompiledValue)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompiledID (org.apache.geode.cache.query.internal.CompiledID)1 CompiledIndexOperation (org.apache.geode.cache.query.internal.CompiledIndexOperation)1 CompiledIteratorDef (org.apache.geode.cache.query.internal.CompiledIteratorDef)1 CompiledLiteral (org.apache.geode.cache.query.internal.CompiledLiteral)1 CompiledOperation (org.apache.geode.cache.query.internal.CompiledOperation)1 CompiledPath (org.apache.geode.cache.query.internal.CompiledPath)1 ExecutionContext (org.apache.geode.cache.query.internal.ExecutionContext)1 RuntimeIterator (org.apache.geode.cache.query.internal.RuntimeIterator)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1 Test (org.junit.Test)1