use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for ResultBag wrapped by a SelectResults if the iteration included
* duplicate elements. If the distinct clause is present then duplicate elements even if
* satisfying the where clause should not be considered as part as distinct will eliminate them.
* This test validates the above behaviour if projection sttribute is present and the projection
* attribute may be duplicate
*
* Tests ResultBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag() {
try {
Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
// Add 5 pairs of same Object starting from 11 to 20
for (int i = 11; i < 21; ) {
region1.put(Integer.toString(i), new Portfolio(i));
region1.put(Integer.toString(i + 1), new Portfolio(i));
i += 2;
}
Query query;
SelectResults result;
final int[] num = new int[1];
num[0] = 0;
final int[] numRepeat = new int[1];
numRepeat[0] = 0;
final Set data = new HashSet();
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
if (data.contains(currObject)) {
numRepeat[0] += 1;
} else {
data.add(currObject);
}
}
});
String queryString = "SELECT DISTINCT pf.ID FROM /portfolios1 pf WHERE pf.ID > 10 limit 5";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals((5 + numRepeat[0]), num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBag.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for StructBag wrapped by a SelectResults if the iteration included
* duplicate elements. If the distinct clause is present then duplicate elements even if
* satisfying the where clause should not be considered as part of the resultset as distinct will
* eliminate them
*
* Tests StructBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBag() {
try {
Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
// Add 5 pairs of same Object starting from 11 to 20
for (int i = 11; i < 21; ) {
region1.put(Integer.toString(i), new Portfolio(i));
region1.put(Integer.toString(i + 1), new Portfolio(i));
i += 2;
}
Query query;
SelectResults result;
final int[] num = new int[1];
num[0] = 0;
final int[] numRepeat = new int[1];
numRepeat[0] = 0;
final Set data = new HashSet();
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
if (data.contains(currObject)) {
numRepeat[0] += 1;
} else if (currObject instanceof Portfolio) {
data.add(currObject);
}
}
});
String queryString = "SELECT DISTINCT * FROM /portfolios1 pf, pf.collectionHolderMap.keySet WHERE pf.ID > 10 limit 20";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals((20 + 4 * numRepeat[0]), num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(20, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(20, wrapper.asSet().size());
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for StructBag wrapped by a SelectResults if the iteration included
* duplicate elements. If the distinct clause is present then duplicate elements even if
* satisfying the where clause should not be considered as part as distinct will eliminate them.
* This test validates the above behaviour if projection attribute is present and the projection
* attribute may be duplicate
*
* Tests StructBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag() {
try {
Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
// Add 5 pairs of same Object starting from 11 to 20
for (int i = 11; i < 21; ) {
region1.put(Integer.toString(i), new Portfolio(i));
region1.put(Integer.toString(i + 1), new Portfolio(i));
i += 2;
}
Query query;
SelectResults result;
final int[] num = new int[1];
num[0] = 0;
final int[] numRepeat = new int[1];
numRepeat[0] = 0;
final Set data = new HashSet();
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
if (data.contains(currObject)) {
numRepeat[0] += 1;
} else {
data.add(currObject);
}
}
});
String queryString = "SELECT DISTINCT pf.ID , pf.createTime FROM /portfolios1 pf WHERE pf.ID > 10 limit 5";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals((5 + numRepeat[0]), num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for ResultBag wrapped by a SelectResults if the iteration included
* duplicate elements. If the distinct clause is present then duplicate elements even if
* satisfying the where clause should not be considered as part of the resultset as distinct will
* eliminate them
*
* Tests ResultBag behaviour
*/
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag() {
try {
Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
// Add 5 pairs of same Object starting from 11 to 20
for (int i = 11; i < 21; ) {
region1.put(Integer.toString(i), new Portfolio(i));
region1.put(Integer.toString(i + 1), new Portfolio(i));
i += 2;
}
Query query;
SelectResults result;
final int[] num = new int[1];
final int[] numRepeat = new int[1];
numRepeat[0] = 0;
final Set data = new HashSet();
num[0] = 0;
// In the worst possible case all the unique values come in
// consecutive order & hence only 5 iterations will yield the
// result
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
if (data.contains(currObject)) {
numRepeat[0] += 1;
} else {
data.add(currObject);
}
}
});
String queryString = "SELECT DISTINCT * FROM /portfolios1 pf WHERE pf.ID > 10 limit 5";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals((5 + numRepeat[0]), num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.CompiledValue in project geode by apache.
the class StructSetOrResultsSet method getOrderByComparatorAndLimitForQuery.
public Wrapper getOrderByComparatorAndLimitForQuery(String orderByQuery, int unorderedResultSize) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
DefaultQuery q = (DefaultQuery) CacheUtils.getQueryService().newQuery(orderByQuery);
CompiledSelect cs = q.getSimpleSelect();
List<CompiledSortCriterion> orderByAttribs = null;
if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
Field originalOrderByMethod = CompiledGroupBySelect.class.getDeclaredField("originalOrderByClause");
originalOrderByMethod.setAccessible(true);
orderByAttribs = (List<CompiledSortCriterion>) originalOrderByMethod.get(cs);
} else {
orderByAttribs = cs.getOrderByAttrs();
}
ObjectType resultType = cs.getElementTypeForOrderByQueries();
ExecutionContext context = new ExecutionContext(null, CacheUtils.getCache());
final OrderByComparator obc = new OrderByComparator(orderByAttribs, resultType, context);
Comparator baseComparator = obc;
if (resultType.isStructType()) {
baseComparator = new Comparator<Struct>() {
@Override
public int compare(Struct o1, Struct o2) {
return obc.compare(o1.getFieldValues(), o2.getFieldValues());
}
};
}
final Comparator secondLevelComparator = baseComparator;
final Comparator finalComparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
final boolean[] orderByColsEqual = new boolean[] { false };
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
@Override
public void orderByColumnsEqual() {
orderByColsEqual[0] = true;
}
});
int result = secondLevelComparator.compare(o1, o2);
if (result != 0 && orderByColsEqual[0]) {
result = 0;
}
return result;
}
};
Field hasUnmappedOrderByColsField = CompiledSelect.class.getDeclaredField("hasUnmappedOrderByCols");
hasUnmappedOrderByColsField.setAccessible(true);
boolean skip = ((Boolean) hasUnmappedOrderByColsField.get(cs)).booleanValue();
ValidationLevel validationLevel = ValidationLevel.ALL;
int limit;
if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
Field limitCVField = CompiledGroupBySelect.class.getDeclaredField("limit");
limitCVField.setAccessible(true);
CompiledValue limitCV = (CompiledValue) limitCVField.get(cs);
Method evaluateLimitMethod = CompiledSelect.class.getDeclaredMethod("evaluateLimitValue", ExecutionContext.class, CompiledValue.class);
evaluateLimitMethod.setAccessible(true);
limit = ((Integer) evaluateLimitMethod.invoke(null, context, limitCV)).intValue();
} else {
limit = cs.getLimitValue(null);
}
if (limit != -1 && limit < unorderedResultSize) {
// chances are that results will not match
if (skip) {
validationLevel = ValidationLevel.NONE;
} else {
validationLevel = ValidationLevel.ORDER_BY_ONLY;
}
} else {
if (skip) {
validationLevel = ValidationLevel.MATCH_ONLY;
}
}
return new Wrapper(finalComparator, limit, validationLevel);
}
Aggregations