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;
}
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);
}
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;
}
}
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));
}
Aggregations