Search in sources :

Example 11 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class TextScanPlanner method getScanForQuery.

/**
 * Get a scan that matches a filter in the list of filters provided. It looks to satisfy the grouping
 * key of the index, and then it looks for a text filter within the list of filters and checks to
 * see if the given index is compatible with the filter. If it is, it will construct a scan that
 * satisfies that filter using the index.
 *
 * @param index the text index to check
 * @param filter a filter that the query must satisfy
 * @param hasSort whether the query has a sort associated with it
 * @param filterMask a mask over the filter containing state about which filters have been satisfied
 * @return a text scan or <code>null</code> if none is found
 */
@Nullable
public static TextScan getScanForQuery(@Nonnull Index index, @Nonnull QueryComponent filter, boolean hasSort, @Nonnull FilterSatisfiedMask filterMask) {
    final KeyExpression indexExpression = index.getRootExpression();
    final KeyExpression groupedKey;
    final FilterSatisfiedMask localMask = FilterSatisfiedMask.of(filter);
    final ScanComparisons groupingComparisons;
    if (indexExpression instanceof GroupingKeyExpression) {
        // Grouping expression present. Make sure this is satisfied.
        final KeyExpression groupingKey = ((GroupingKeyExpression) indexExpression).getGroupingSubKey();
        groupedKey = ((GroupingKeyExpression) indexExpression).getGroupedSubKey();
        QueryToKeyMatcher groupingQueryMatcher = new QueryToKeyMatcher(filter);
        QueryToKeyMatcher.Match groupingMatch = groupingQueryMatcher.matchesCoveringKey(groupingKey, localMask);
        if (!groupingMatch.getType().equals(QueryToKeyMatcher.MatchType.EQUALITY)) {
            return null;
        }
        groupingComparisons = new ScanComparisons(groupingMatch.getEqualityComparisons(), Collections.emptySet());
    } else {
        // Grouping expression not present. Use first column.
        groupedKey = indexExpression;
        groupingComparisons = null;
    }
    final KeyExpression textExpression = groupedKey.getSubKey(0, 1);
    final TextScan foundScan = getScanForFilter(index, textExpression, filter, groupingComparisons, hasSort, localMask);
    if (foundScan != null) {
        filterMask.mergeWith(localMask);
        return foundScan;
    }
    return null;
}
Also used : ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) TextScan(com.apple.foundationdb.record.query.plan.TextScan) QueryToKeyMatcher(com.apple.foundationdb.record.query.QueryToKeyMatcher) Nullable(javax.annotation.Nullable)

Example 12 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class TimeWindowForFunction method prependLeaderboardKeys.

@Nonnull
public ScanComparisons prependLeaderboardKeys(@Nonnull ScanComparisons scanComparisons) {
    final Comparisons.Comparison typeComparison = leaderboardTypeParameter == null ? new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, leaderboardType) : new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, leaderboardTypeParameter);
    final Comparisons.Comparison timestampComparison = leaderboardTimestampParameter == null ? new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, leaderboardTimestamp) : new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, leaderboardTimestampParameter);
    return new ScanComparisons(Arrays.asList(typeComparison, timestampComparison), Collections.emptySet()).append(scanComparisons);
}
Also used : ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Nonnull(javax.annotation.Nonnull)

Example 13 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class UnmatchedFieldsCountProperty method evaluateAtExpression.

@Nonnull
@Override
public Integer evaluateAtExpression(@Nonnull RelationalExpression expression, @Nonnull List<Integer> childResults) {
    int total = 0;
    for (Integer result : childResults) {
        if (result != null) {
            total += result;
        }
    }
    if (expression instanceof RecordQueryCoveringIndexPlan) {
        expression = ((RecordQueryCoveringIndexPlan) expression).getIndexPlan();
    }
    final int columnSize;
    if (expression instanceof RecordQueryPlanWithComparisons) {
        final ScanComparisons comparisons = ((RecordQueryPlanWithComparisons) expression).getComparisons();
        if (expression instanceof RecordQueryPlanWithIndex) {
            final String indexName = ((RecordQueryPlanWithIndex) expression).getIndexName();
            columnSize = planContext.getIndexByName(indexName).getRootExpression().getColumnSize();
        } else if (expression instanceof RecordQueryScanPlan) {
            columnSize = planContext.getGreatestPrimaryKeyWidth();
        } else {
            throw new RecordCoreException("unhandled plan with comparisons: can't find key expression");
        }
        return total + columnSize - (comparisons.getEqualitySize() + (comparisons.isEquality() ? 0 : 1));
    } else {
        return total;
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) RecordQueryPlanWithComparisons(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithComparisons) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryCoveringIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) Nonnull(javax.annotation.Nonnull)

Example 14 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class OrderingProperty method fromKeyAndScanComparisons.

@Nonnull
private static Optional<Ordering> fromKeyAndScanComparisons(@Nullable final KeyExpression keyExpression, @Nonnull final ScanComparisons scanComparisons, final boolean isReverse, final boolean isDistinct) {
    if (keyExpression == null) {
        return Optional.empty();
    }
    final ImmutableSetMultimap.Builder<KeyExpression, Comparisons.Comparison> equalityBoundKeyMapBuilder = ImmutableSetMultimap.builder();
    final List<KeyExpression> normalizedKeyExpressions = keyExpression.normalizeKeyForPositions();
    final List<Comparisons.Comparison> equalityComparisons = scanComparisons.getEqualityComparisons();
    for (int i = 0; i < equalityComparisons.size(); i++) {
        final KeyExpression normalizedKeyExpression = normalizedKeyExpressions.get(i);
        final Comparisons.Comparison comparison = equalityComparisons.get(i);
        equalityBoundKeyMapBuilder.put(normalizedKeyExpression, comparison);
    }
    final ImmutableList.Builder<KeyPart> result = ImmutableList.builder();
    for (int i = scanComparisons.getEqualitySize(); i < normalizedKeyExpressions.size(); i++) {
        final KeyExpression currentKeyExpression = normalizedKeyExpressions.get(i);
        // 
        // Note that it is not really important here if the keyExpression can be normalized in a lossless way
        // or not. A key expression containing repeated fields is sort-compatible with its normalized key
        // expression. We used to refuse to compute the sort order in the presence of repeats, however,
        // I think that restriction can be relaxed.
        // 
        result.add(KeyPart.of(currentKeyExpression, isReverse));
    }
    return Optional.of(new Ordering(equalityBoundKeyMapBuilder.build(), result.build(), isDistinct));
}
Also used : ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableList(com.google.common.collect.ImmutableList) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) Nonnull(javax.annotation.Nonnull)

Aggregations

ScanComparisons (com.apple.foundationdb.record.query.plan.ScanComparisons)14 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)10 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)9 Test (org.junit.jupiter.api.Test)7 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)6 IndexScanType (com.apple.foundationdb.record.IndexScanType)5 Index (com.apple.foundationdb.record.metadata.Index)5 RecordQueryIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan)5 Nonnull (javax.annotation.Nonnull)5 IndexEntry (com.apple.foundationdb.record.IndexEntry)4 ScanProperties (com.apple.foundationdb.record.ScanProperties)4 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)4 TupleRange (com.apple.foundationdb.record.TupleRange)4 Tuple (com.apple.foundationdb.tuple.Tuple)4 Tags (com.apple.test.Tags)4 Message (com.google.protobuf.Message)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4