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