use of org.apache.geode.cache.query.internal.CompiledLiteral in project geode by apache.
the class FunctionalIndexCreationHelper method prepareIndexExpression.
/**
* This function is modified so that if the indexed expression has any dependency on the 0th
* iterator, then it needs to modified by using the missing link so that it is derivable from the
* 0th iterator.
* <p>
* TODO: refactor large method prepareIndexExpression
*/
private void prepareIndexExpression(String indexedExpression) throws IndexInvalidException {
CompiledValue expr = this.compiler.compileQuery(indexedExpression);
if (expr == null) {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression));
}
if (expr instanceof CompiledUndefined || expr instanceof CompiledLiteral || expr instanceof CompiledComparison || expr instanceof CompiledBindArgument || expr instanceof CompiledNegation) {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression));
}
try {
StringBuilder sb = new StringBuilder();
if (expr instanceof MapIndexable) {
MapIndexable mi = (MapIndexable) expr;
List<CompiledValue> indexingKeys = mi.getIndexingKeys();
if (indexingKeys.size() == 1 && indexingKeys.get(0) == CompiledValue.MAP_INDEX_ALL_KEYS) {
this.isMapTypeIndex = true;
this.isAllKeys = true;
// Strip the index operator
expr = mi.getRecieverSansIndexArgs();
expr.generateCanonicalizedExpression(sb, this.context);
sb.append('[').append('*').append(']');
} else if (indexingKeys.size() == 1) {
expr.generateCanonicalizedExpression(sb, this.context);
} else {
this.isMapTypeIndex = true;
this.multiIndexKeysPattern = new String[indexingKeys.size()];
this.mapKeys = new Object[indexingKeys.size()];
expr = mi.getRecieverSansIndexArgs();
expr.generateCanonicalizedExpression(sb, this.context);
sb.append('[');
String prefixStr = sb.toString();
StringBuilder sb2 = new StringBuilder();
int size = indexingKeys.size();
for (int j = 0; j < size; ++j) {
CompiledValue cv = indexingKeys.get(size - j - 1);
this.mapKeys[size - j - 1] = cv.evaluate(this.context);
StringBuilder sb3 = new StringBuilder();
cv.generateCanonicalizedExpression(sb3, this.context);
sb3.insert(0, prefixStr);
sb3.append(']');
this.multiIndexKeysPattern[j] = sb3.toString();
cv.generateCanonicalizedExpression(sb2, this.context);
sb2.insert(0, ',');
}
sb2.deleteCharAt(0);
sb.append(sb2);
sb.append(']');
}
} else {
expr.generateCanonicalizedExpression(sb, this.context);
}
this.indexedExpression = sb.toString();
this.modifiedIndexExpr = expr;
if (!this.isFirstIteratorRegionEntry && this.indexedExpression.contains(this.canonicalizedIteratorNames[0])) {
this.modifiedIndexExpr = getModifiedDependentCompiledValue(this.context, -1, expr, true);
}
} catch (Exception e) {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression), e);
}
this.indexedExpr = expr;
}
use of org.apache.geode.cache.query.internal.CompiledLiteral 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");
}
}
}
Aggregations