Search in sources :

Example 16 with IndexEqualsFilter

use of com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter in project hazelcast by hazelcast.

the class IndexEqualsFilterTest method testEquals.

@Test
public void testEquals() {
    IndexEqualsFilter filter = new IndexEqualsFilter(intValue(1, true));
    checkEquals(filter, new IndexEqualsFilter(intValue(1, true)), true);
    checkEquals(filter, new IndexEqualsFilter(intValue(2, true)), false);
}
Also used : IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 17 with IndexEqualsFilter

use of com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter in project hazelcast by hazelcast.

the class IndexEqualsFilterTest method testContent.

@Test
public void testContent() {
    IndexFilterValue value = intValue(1, true);
    IndexEqualsFilter filter = new IndexEqualsFilter(value);
    assertSame(value, filter.getValue());
}
Also used : IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) 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 IndexEqualsFilter

use of com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter 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)

Example 19 with IndexEqualsFilter

use of com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter in project hazelcast by hazelcast.

the class IndexResolver method fillNonTerminalComponents.

/**
 * Given the list of column filters, flatten their expressions and allow-null flags.
 * <p>
 * The operation is performed for all filters except for the last one, because treatment of the last filter might differ
 * depending on the total number of components in the index.
 *
 * @param filters    column filters
 * @param components expressions that would form the final filter
 * @param allowNulls allow-null collection relevant to components
 */
private static void fillNonTerminalComponents(List<IndexFilter> filters, List<Expression> components, List<Boolean> allowNulls) {
    for (int i = 0; i < filters.size() - 1; i++) {
        IndexEqualsFilter filter0 = (IndexEqualsFilter) filters.get(i);
        IndexFilterValue value = filter0.getValue();
        assert value.getComponents().size() == 1;
        components.add(value.getComponents().get(0));
        allowNulls.add(value.getAllowNulls().get(0));
    }
    assert components.size() == filters.size() - 1;
    assert allowNulls.size() == filters.size() - 1;
}
Also used : IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue)

Example 20 with IndexEqualsFilter

use of com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter in project hazelcast by hazelcast.

the class IndexResolver method composeInFilter.

/**
 * Create the final IN filter from the collection of per-column filters.
 * <p>
 * Consider the expression {@code {a=1 AND b IN (2,3)}}. After the conversion, the composite filter will be
 * {@code {a,b} IN {{1, 2}, {1, 3}}}.
 *
 * @param filters              per-column filters
 * @param lastFilter           the last IN filter
 * @param indexComponentsCount the number of index components
 * @return composite IN filter
 */
private static IndexFilter composeInFilter(List<IndexFilter> filters, IndexInFilter lastFilter, IndexType indexType, int indexComponentsCount) {
    List<IndexFilter> newFilters = new ArrayList<>(lastFilter.getFilters().size());
    for (IndexFilter filter : lastFilter.getFilters()) {
        assert filter instanceof IndexEqualsFilter;
        IndexFilter newFilter = composeEqualsFilter(filters, (IndexEqualsFilter) filter, indexType, indexComponentsCount);
        if (newFilter == null) {
            // Cannot create a filter for one of the values of the IN clause. Stop.
            return null;
        }
        newFilters.add(newFilter);
    }
    return new IndexInFilter(newFilters);
}
Also used : IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) ArrayList(java.util.ArrayList) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter)

Aggregations

IndexEqualsFilter (com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter)22 IndexFilterValue (com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue)9 IndexInFilter (com.hazelcast.sql.impl.exec.scan.index.IndexInFilter)9 IndexFilter (com.hazelcast.sql.impl.exec.scan.index.IndexFilter)8 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 IndexRangeFilter (com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter)6 ArrayList (java.util.ArrayList)6 IndexConfig (com.hazelcast.config.IndexConfig)3 ExpressionEvalContext (com.hazelcast.sql.impl.expression.ExpressionEvalContext)3 RexInputRef (org.apache.calcite.rex.RexInputRef)3 RexNode (org.apache.calcite.rex.RexNode)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 InternalIndex (com.hazelcast.query.impl.InternalIndex)2 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)2 ArrayDataSerializableFactory (com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory)1 ConstructorFunction (com.hazelcast.internal.util.ConstructorFunction)1 JobConfig (com.hazelcast.jet.config.JobConfig)1 RexToExpression (com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression)1