Search in sources :

Example 21 with IndexEqualsFilter

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

the class IndexResolver method createIndexScan.

/**
 * Create index scan for the given index if possible.
 *
 * @param scan         the original map scan
 * @param index        index to be considered
 * @param conjunctions CNF components of the original map filter
 * @param candidates   resolved candidates
 * @param ascs         a list of index field collations
 * @return index scan or {@code null}.
 */
public static RelNode createIndexScan(FullScanLogicalRel scan, MapTableIndex index, List<RexNode> conjunctions, Map<Integer, List<IndexComponentCandidate>> candidates, List<Boolean> ascs) {
    List<IndexComponentFilter> filters = new ArrayList<>(index.getFieldOrdinals().size());
    // Iterate over every index component from the beginning and try to form a filter to it
    for (int i = 0; i < index.getFieldOrdinals().size(); i++) {
        int fieldOrdinal = index.getFieldOrdinals().get(i);
        QueryDataType fieldConverterType = index.getFieldConverterTypes().get(i);
        List<IndexComponentCandidate> fieldCandidates = candidates.get(fieldOrdinal);
        if (fieldCandidates == null) {
            // used for index filter.
            break;
        }
        // Create the filter for the given index component if possible.
        // Separate candidates are possibly merged into a single complex filter at this stage.
        // Consider the index {a}, and the condition "WHERE a>1 AND a<5". In this case two distinct range candidates
        // {>1} and {<5} are combined into a single RANGE filter {>1 AND <5}
        IndexComponentFilter filter = selectComponentFilter(index.getType(), fieldCandidates, fieldConverterType);
        if (filter == null) {
            // Cannot create a filter for the given candidates, stop.
            break;
        }
        filters.add(filter);
        if (!(filter.getFilter() instanceof IndexEqualsFilter)) {
            // {a>1} filter.
            break;
        }
    }
    if (filters.isEmpty()) {
        // Failed to build any filters. The index cannot be used.
        return null;
    }
    // Now as filters are determined, construct the physical entity.
    return createIndexScan(scan, index, conjunctions, filters, ascs);
}
Also used : IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList)

Example 22 with IndexEqualsFilter

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

the class IndexResolver method prepareSingleColumnCandidateIsNull.

/**
 * Try creating a candidate filter for the "IS NULL" expression.
 * <p>
 * Returns the filter EQUALS(null) with "allowNulls-true".
 *
 * @param exp     original expression, e.g. {col IS NULL}
 * @param operand operand, e.g. {col}; CAST must be unwrapped before the method is invoked
 * @return candidate or {@code null}
 */
private static IndexComponentCandidate prepareSingleColumnCandidateIsNull(RexNode exp, RexNode operand) {
    if (operand.getKind() != SqlKind.INPUT_REF) {
        // The operand is not a column, e.g. {'literal' IS NULL}, index cannot be used
        return null;
    }
    int columnIndex = ((RexInputRef) operand).getIndex();
    QueryDataType type = HazelcastTypeUtils.toHazelcastType(operand.getType());
    // Create a value with "allowNulls=true"
    IndexFilterValue filterValue = new IndexFilterValue(singletonList(ConstantExpression.create(null, type)), singletonList(true));
    IndexFilter filter = new IndexEqualsFilter(filterValue);
    return new IndexComponentCandidate(exp, columnIndex, filter);
}
Also used : IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) RexInputRef(org.apache.calcite.rex.RexInputRef) 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