Search in sources :

Example 1 with MatchableSortExpression

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

the class ValueIndexExpansionVisitor method expand.

@Nonnull
@Override
public MatchCandidate expand(@Nonnull final Quantifier.ForEach baseQuantifier, @Nullable final KeyExpression primaryKey, final boolean isReverse) {
    Debugger.updateIndex(ValueComparisonRangePredicate.Placeholder.class, old -> 0);
    final ImmutableList.Builder<GraphExpansion> allExpansionsBuilder = ImmutableList.builder();
    // add the value for the flow of records
    final QuantifiedColumnValue recordValue = QuantifiedColumnValue.of(baseQuantifier.getAlias(), 0);
    allExpansionsBuilder.add(GraphExpansion.ofResultValueAndQuantifier(recordValue, baseQuantifier));
    KeyExpression rootExpression = index.getRootExpression();
    final int keyValueSplitPoint;
    if (rootExpression instanceof KeyWithValueExpression) {
        final KeyWithValueExpression keyWithValueExpression = (KeyWithValueExpression) rootExpression;
        keyValueSplitPoint = keyWithValueExpression.getSplitPoint();
        rootExpression = keyWithValueExpression.getInnerKey();
    } else {
        keyValueSplitPoint = -1;
    }
    final List<Value> keyValues = Lists.newArrayList();
    final List<Value> valueValues = Lists.newArrayList();
    final VisitorState initialState = VisitorState.of(keyValues, valueValues, baseQuantifier.getAlias(), ImmutableList.of(), keyValueSplitPoint, 0);
    final GraphExpansion keyValueExpansion = pop(rootExpression.expand(push(initialState)));
    allExpansionsBuilder.add(keyValueExpansion);
    final int keySize = keyValues.size();
    if (primaryKey != null) {
        // unfortunately we must copy as the returned list is not guaranteed to be mutable which is needed for the
        // trimPrimaryKey() function as it is causing a side-effect
        final List<KeyExpression> trimmedPrimaryKeys = Lists.newArrayList(primaryKey.normalizeKeyForPositions());
        index.trimPrimaryKey(trimmedPrimaryKeys);
        for (int i = 0; i < trimmedPrimaryKeys.size(); i++) {
            final KeyExpression primaryKeyPart = trimmedPrimaryKeys.get(i);
            final VisitorState initialStateForKeyPart = VisitorState.of(keyValues, Lists.newArrayList(), baseQuantifier.getAlias(), ImmutableList.of(), -1, keySize + i);
            final GraphExpansion primaryKeyPartExpansion = pop(primaryKeyPart.expand(push(initialStateForKeyPart)));
            allExpansionsBuilder.add(primaryKeyPartExpansion);
        }
    }
    final GraphExpansion completeExpansion = GraphExpansion.ofOthers(allExpansionsBuilder.build());
    final List<CorrelationIdentifier> parameters = completeExpansion.getPlaceholderAliases();
    final MatchableSortExpression matchableSortExpression = new MatchableSortExpression(parameters, isReverse, completeExpansion.buildSelect());
    return new ValueIndexScanMatchCandidate(index, recordTypes, ExpressionRefTraversal.withRoot(GroupExpressionRef.of(matchableSortExpression)), parameters, recordValue, keyValues, valueValues, fullKey(index, primaryKey));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ValueComparisonRangePredicate(com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate) MatchableSortExpression(com.apple.foundationdb.record.query.plan.temp.expressions.MatchableSortExpression) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) Value(com.apple.foundationdb.record.query.predicates.Value) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) KeyWithValueExpression(com.apple.foundationdb.record.metadata.expressions.KeyWithValueExpression) Nonnull(javax.annotation.Nonnull)

Aggregations

KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)1 KeyWithValueExpression (com.apple.foundationdb.record.metadata.expressions.KeyWithValueExpression)1 MatchableSortExpression (com.apple.foundationdb.record.query.plan.temp.expressions.MatchableSortExpression)1 QuantifiedColumnValue (com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue)1 Value (com.apple.foundationdb.record.query.predicates.Value)1 ValueComparisonRangePredicate (com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 Nonnull (javax.annotation.Nonnull)1