Search in sources :

Example 16 with IndexRangeFilter

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));
}
Also used : ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) InternalIndex(com.hazelcast.query.impl.InternalIndex) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IndexConfig(com.hazelcast.config.IndexConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 17 with IndexRangeFilter

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());
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with IndexRangeFilter

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);
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 19 with IndexRangeFilter

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);
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with IndexRangeFilter

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;
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) ArrayList(java.util.ArrayList) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

IndexRangeFilter (com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter)20 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 Test (org.junit.Test)13 IndexConfig (com.hazelcast.config.IndexConfig)9 ArrayList (java.util.ArrayList)8 IndexFilter (com.hazelcast.sql.impl.exec.scan.index.IndexFilter)7 IndexFilterValue (com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue)7 IndexEqualsFilter (com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter)6 JobConfig (com.hazelcast.jet.config.JobConfig)5 MapIndexScanMetadata (com.hazelcast.sql.impl.exec.scan.MapIndexScanMetadata)5 ExpressionEvalContext (com.hazelcast.sql.impl.expression.ExpressionEvalContext)5 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 InternalIndex (com.hazelcast.query.impl.InternalIndex)4 IndexInFilter (com.hazelcast.sql.impl.exec.scan.index.IndexInFilter)3 RexToExpression (com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression)2 ConstantExpression (com.hazelcast.sql.impl.expression.ConstantExpression)2 Expression (com.hazelcast.sql.impl.expression.Expression)2 ArrayDataSerializableFactory (com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory)1