Search in sources :

Example 1 with IndexRangeFilter

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

the class IndexResolver method composeEqualsFilter.

/**
 * Composes an equality filter from multiple single-column components.
 * <p>
 * If the number of single-column filters is equal to the number of index components, the resulting filter is a composite
 * equality filter.
 * <p>
 * If the number of single-column filters is less than the number of index components, the resulting filter is a range
 * filter, with missing components filled with negative/positive infinities for the left and right bounds respectively.
 * <p>
 * If the range filter is required, and the target index type is not {@link IndexType#SORTED}, the result is {@code null}.
 * <p>
 * Examples:
 * <ul>
 *     <li>SORTED(a, b), {a=1, b=2} => EQUALS(1), EQUALS(2) </li>
 *     <li>HASH(a, b), {a=1, b=2} => EQUALS(1), EQUALS(2) </li>
 *     <li>SORTED(a, b), {a=1} => EQUALS(1), RANGE(INF) </li>
 *     <li>HASH(a, b), {a=1} => null </li>
 * </ul>
 *
 * @return composite filter or {@code null}
 */
private static IndexFilter composeEqualsFilter(List<IndexFilter> filters, IndexEqualsFilter lastFilter, IndexType indexType, int indexComponentsCount) {
    // Flatten all known values.
    List<Expression> components = new ArrayList<>(filters.size());
    List<Boolean> allowNulls = new ArrayList<>(filters.size());
    fillNonTerminalComponents(filters, components, allowNulls);
    components.addAll(lastFilter.getValue().getComponents());
    allowNulls.addAll(lastFilter.getValue().getAllowNulls());
    if (indexComponentsCount == components.size()) {
        // If there is a full match, then leave it as equals filter
        return new IndexEqualsFilter(new IndexFilterValue(components, allowNulls));
    } else {
        // Otherwise convert it to a range request
        if (indexType == HASH) {
            return null;
        }
        List<Expression> fromComponents = components;
        List<Expression> toComponents = new ArrayList<>(components);
        List<Boolean> fromAllowNulls = allowNulls;
        List<Boolean> toAllowNulls = new ArrayList<>(fromAllowNulls);
        addInfiniteRanges(fromComponents, fromAllowNulls, true, toComponents, toAllowNulls, true, indexComponentsCount);
        return new IndexRangeFilter(new IndexFilterValue(fromComponents, fromAllowNulls), true, new IndexFilterValue(toComponents, toAllowNulls), true);
    }
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) Expression(com.hazelcast.sql.impl.expression.Expression) RexToExpression(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) ArrayList(java.util.ArrayList)

Example 2 with IndexRangeFilter

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

the class IndexResolver method createIndexFilterForSingleRange.

@SuppressWarnings("unchecked")
@Nonnull
private static IndexFilter createIndexFilterForSingleRange(Range<?> range, QueryDataType hazelcastType) {
    Expression<?> lowerBound = ConstantExpression.create(range.lowerEndpoint(), hazelcastType);
    IndexFilterValue lowerBound0 = new IndexFilterValue(singletonList(lowerBound), singletonList(false));
    if (isSingletonRange(range)) {
        return new IndexEqualsFilter(lowerBound0);
    }
    Expression<?> upperBound = ConstantExpression.create(range.upperEndpoint(), hazelcastType);
    IndexFilterValue upperBound0 = new IndexFilterValue(singletonList(upperBound), singletonList(false));
    return new IndexRangeFilter(lowerBound0, range.lowerBoundType() == BoundType.CLOSED, upperBound0, range.upperBoundType() == BoundType.CLOSED);
}
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) Nonnull(javax.annotation.Nonnull)

Example 3 with IndexRangeFilter

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

the class IndexResolver method prepareSingleColumnCandidateComparison.

/**
 * Try creating a candidate filter for comparison operator.
 *
 * @param exp               the original expression
 * @param kind              expression kine (=, >, <, >=, <=)
 * @param operand1          the first operand (CAST must be unwrapped before the method is invoked)
 * @param operand2          the second operand (CAST must be unwrapped before the method is invoked)
 * @param parameterMetadata parameter metadata for expressions like {a>?}
 * @return candidate or {@code null}
 */
private static IndexComponentCandidate prepareSingleColumnCandidateComparison(RexNode exp, SqlKind kind, RexNode operand1, RexNode operand2, QueryParameterMetadata parameterMetadata) {
    // The condition (kind) is changed accordingly (e.g. ">" to "<").
    if (operand1.getKind() != SqlKind.INPUT_REF && operand2.getKind() == SqlKind.INPUT_REF) {
        kind = inverseIndexConditionKind(kind);
        RexNode tmp = operand1;
        operand1 = operand2;
        operand2 = tmp;
    }
    if (operand1.getKind() != SqlKind.INPUT_REF) {
        // No columns in the expression, index cannot be used. E.g. {'a' > 'b'}
        return null;
    }
    int columnIndex = ((RexInputRef) operand1).getIndex();
    if (!IndexRexVisitor.isValid(operand2)) {
        // E.g. {column_a > column_b}.
        return null;
    }
    // Convert the second operand into Hazelcast expression. The expression will be evaluated once before index scan is
    // initiated, to construct the proper filter for index lookup.
    Expression<?> filterValue = convertToExpression(operand2, parameterMetadata);
    if (filterValue == null) {
        // tree to Hazelcast plan.
        return null;
    }
    // Create the value that will be passed to filters. Not that "allowNulls=false" here, because any NULL in the comparison
    // operator never returns "TRUE" and hence always returns an empty result set.
    IndexFilterValue filterValue0 = new IndexFilterValue(singletonList(filterValue), singletonList(false));
    IndexFilter filter;
    switch(kind) {
        case EQUALS:
            filter = new IndexEqualsFilter(filterValue0);
            break;
        case GREATER_THAN:
            filter = new IndexRangeFilter(filterValue0, false, null, false);
            break;
        case GREATER_THAN_OR_EQUAL:
            filter = new IndexRangeFilter(filterValue0, true, null, false);
            break;
        case LESS_THAN:
            filter = new IndexRangeFilter(null, false, filterValue0, false);
            break;
        default:
            assert kind == SqlKind.LESS_THAN_OR_EQUAL;
            filter = new IndexRangeFilter(null, false, filterValue0, true);
    }
    return new IndexComponentCandidate(exp, columnIndex, filter);
}
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) RexInputRef(org.apache.calcite.rex.RexInputRef) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with IndexRangeFilter

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

the class IndexResolver method composeRangeFilter.

/**
 * Create the composite range filter from the given per-column filters.
 * <p>
 * If the number of per-column filters if less than the number of index components, then infinite ranges are added
 * to the missing components.
 * <p>
 * Consider that we have two per-column filter as input: {@code {a=1}, {b>2 AND b<3}}.
 * <p>
 * If the index is defined as {@code {a, b}}, then the resulting filter would be {@code {a=1, b>2 AND a=1, b<3}}.
 * <p>
 * If the index is defined as {@code {a, b, c}}, then the resulting filter would be
 * {@code {a=1, b>2, c>NEGATIVE_INFINITY AND a=1, b<3, c<POSITIVE_INFINITY}}.
 *
 * @param filters         all per-column filters
 * @param lastFilter      the last filter (range)
 * @param componentsCount number of components in the filter
 * @return range filter
 */
private static IndexFilter composeRangeFilter(List<IndexFilter> filters, IndexRangeFilter lastFilter, int componentsCount) {
    // Flatten non-terminal components.
    List<Expression> components = new ArrayList<>(filters.size());
    List<Boolean> allowNulls = new ArrayList<>();
    fillNonTerminalComponents(filters, components, allowNulls);
    // Add value of the current filter.
    List<Expression> fromComponents = components;
    List<Expression> toComponents = new ArrayList<>(components);
    List<Boolean> fromAllowNulls = allowNulls;
    List<Boolean> toAllowNulls = new ArrayList<>(fromAllowNulls);
    if (lastFilter.getFrom() != null) {
        fromComponents.add(lastFilter.getFrom().getComponents().get(0));
        fromAllowNulls.add(false);
    } else {
        if (componentsCount == 1) {
            fromComponents.add(ConstantExpression.create(NEGATIVE_INFINITY, QueryDataType.OBJECT));
            fromAllowNulls.add(false);
        } else {
            // In composite indexes null values are not stored separately. Therefore, we need to filter them out.
            fromComponents.add(ConstantExpression.create(null, QueryDataType.OBJECT));
            fromAllowNulls.add(true);
        }
    }
    if (lastFilter.getTo() != null) {
        toComponents.add(lastFilter.getTo().getComponents().get(0));
    } else {
        toComponents.add(ConstantExpression.create(POSITIVE_INFINITY, QueryDataType.OBJECT));
    }
    toAllowNulls.add(false);
    // Fill missing part of the range request.
    addInfiniteRanges(fromComponents, fromAllowNulls, lastFilter.isFromInclusive(), toComponents, toAllowNulls, lastFilter.isToInclusive(), componentsCount);
    return new IndexRangeFilter(new IndexFilterValue(fromComponents, fromAllowNulls), lastFilter.isFromInclusive(), new IndexFilterValue(toComponents, toAllowNulls), lastFilter.isToInclusive());
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) Expression(com.hazelcast.sql.impl.expression.Expression) RexToExpression(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) ArrayList(java.util.ArrayList)

Example 5 with IndexRangeFilter

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

the class IndexRangeFilterTest method testComparable.

@Test
public void testComparable() {
    ExpressionEvalContext evalContext = createExpressionEvalContext();
    assertEquals(1, new IndexRangeFilter(intValue(1, true), true, intValue(2, true), true).getComparable(evalContext));
    assertEquals(AbstractIndex.NULL, new IndexRangeFilter(intValue(null, true), true, intValue(2, true), true).getComparable(evalContext));
    assertNull(new IndexRangeFilter(intValue(null, false), true, intValue(2, true), true).getComparable(evalContext));
    assertEquals(2, new IndexRangeFilter(null, true, intValue(2, true), true).getComparable(evalContext));
    assertEquals(AbstractIndex.NULL, new IndexRangeFilter(null, true, intValue(null, true), true).getComparable(evalContext));
    assertNull(new IndexRangeFilter(null, true, intValue(null, false), true).getComparable(evalContext));
}
Also used : ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) 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)

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