use of com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter in project hazelcast by hazelcast.
the class IndexRangeFilterIteratorTest method testIterator_simple_to.
@Test
public void testIterator_simple_to() {
HazelcastInstance instance = factory.newHazelcastInstance(getConfig());
IMap<Integer, Value> map = instance.getMap(MAP_NAME);
map.addIndex(new IndexConfig().setName(INDEX_NAME).setType(SORTED).addAttribute("value1"));
InternalIndex index = getIndex(instance);
ExpressionEvalContext evalContext = createExpressionEvalContext();
// Check missing value.
map.put(0, new Value(10));
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(2), false).getEntries(index, descendingDirection, evalContext));
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(2), true).getEntries(index, descendingDirection, evalContext));
// Check single value.
map.put(1, new Value(2));
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(2), false).getEntries(index, descendingDirection, evalContext));
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(2), true).getEntries(index, descendingDirection, evalContext), 1);
// Check multiple values.
map.put(2, new Value(2));
map.put(3, new Value(1));
map.put(4, new Value(1));
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(2), false).getEntries(index, descendingDirection, evalContext), 3, 4);
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(2), true).getEntries(index, descendingDirection, evalContext), 1, 2, 3, 4);
// Check null value.
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(null, false), false).getEntries(index, descendingDirection, evalContext));
checkIterator(SORTED, descendingDirection, new IndexRangeFilter(null, false, intValue(null, false), true).getEntries(index, descendingDirection, evalContext));
}
use of com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter in project hazelcast by hazelcast.
the class IndexRangeFilterTest method testContent.
@Test
public void testContent() {
IndexFilterValue from = intValue(1, true);
IndexFilterValue to = intValue(2, true);
IndexRangeFilter filter = new IndexRangeFilter(from, true, to, true);
assertSame(from, filter.getFrom());
assertTrue(filter.isFromInclusive());
assertSame(to, filter.getTo());
assertTrue(filter.isToInclusive());
}
use of com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter in project hazelcast by hazelcast.
the class IndexRangeFilterTest method testEquals.
@Test
public void testEquals() {
IndexRangeFilter filter = new IndexRangeFilter(intValue(1, true), true, intValue(2, true), true);
checkEquals(filter, new IndexRangeFilter(intValue(1, true), true, intValue(2, true), true), true);
checkEquals(filter, new IndexRangeFilter(intValue(2, true), true, intValue(2, true), true), false);
checkEquals(filter, new IndexRangeFilter(null, true, intValue(2, true), true), false);
checkEquals(filter, new IndexRangeFilter(intValue(1, true), false, intValue(2, true), true), false);
checkEquals(filter, new IndexRangeFilter(intValue(1, true), true, intValue(3, true), true), false);
checkEquals(filter, new IndexRangeFilter(intValue(1, true), true, null, true), false);
checkEquals(filter, new IndexRangeFilter(intValue(1, true), true, intValue(2, true), false), false);
}
use of com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter in project hazelcast by hazelcast.
the class IndexRangeFilterTest method testSerialization.
@Test
public void testSerialization() {
IndexRangeFilter original = new IndexRangeFilter(intValue(1, true), true, intValue(2, true), true);
IndexRangeFilter restored = serializeAndCheck(original, SqlDataSerializerHook.INDEX_FILTER_RANGE);
checkEquals(original, restored, true);
}
use of com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter in project hazelcast by hazelcast.
the class IndexResolver method selectComponentFilter.
/**
* This method selects the best expression to be used as index filter from the list of candidates.
*
* @param type type of the index (SORTED, HASH)
* @param candidates candidates that might be used as a filter
* @param converterType expected converter type for the given component of the index
* @return filter for the index component or {@code null} if no candidate could be applied
*/
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity" })
private static IndexComponentFilter selectComponentFilter(IndexType type, List<IndexComponentCandidate> candidates, QueryDataType converterType) {
// First look for equality conditions, assuming that it is the most restrictive
for (IndexComponentCandidate candidate : candidates) {
if (candidate.getFilter() instanceof IndexEqualsFilter) {
return new IndexComponentFilter(candidate.getFilter(), singletonList(candidate.getExpression()), converterType);
}
}
// Next look for IN, as it is worse than equality on a single value, but better than range
for (IndexComponentCandidate candidate : candidates) {
if (candidate.getFilter() instanceof IndexInFilter) {
return new IndexComponentFilter(candidate.getFilter(), singletonList(candidate.getExpression()), converterType);
}
}
// Last, look for ranges
if (type == SORTED) {
IndexFilterValue from = null;
boolean fromInclusive = false;
IndexFilterValue to = null;
boolean toInclusive = false;
List<RexNode> expressions = new ArrayList<>(2);
for (IndexComponentCandidate candidate : candidates) {
if (!(candidate.getFilter() instanceof IndexRangeFilter)) {
continue;
}
IndexRangeFilter candidateFilter = (IndexRangeFilter) candidate.getFilter();
if (from == null && candidateFilter.getFrom() != null) {
from = candidateFilter.getFrom();
fromInclusive = candidateFilter.isFromInclusive();
expressions.add(candidate.getExpression());
}
if (to == null && candidateFilter.getTo() != null) {
to = candidateFilter.getTo();
toInclusive = candidateFilter.isToInclusive();
expressions.add(candidate.getExpression());
}
}
if (from != null || to != null) {
IndexRangeFilter filter = new IndexRangeFilter(from, fromInclusive, to, toInclusive);
return new IndexComponentFilter(filter, expressions, converterType);
}
}
// Cannot create an index request for the given candidates
return null;
}
Aggregations