use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.
the class ValueIndexLikeExpansionVisitor method visitExpression.
@Nonnull
@Override
public GraphExpansion visitExpression(@Nonnull FieldKeyExpression fieldKeyExpression) {
final String fieldName = fieldKeyExpression.getFieldName();
final KeyExpression.FanType fanType = fieldKeyExpression.getFanType();
final VisitorState state = getCurrentState();
final List<String> fieldNamePrefix = state.getFieldNamePrefix();
final CorrelationIdentifier baseAlias = state.getBaseAlias();
final List<String> fieldNames = ImmutableList.<String>builder().addAll(fieldNamePrefix).add(fieldName).build();
final Value value;
final Placeholder predicate;
switch(fanType) {
case FanOut:
// explode this field and prefixes of this field
final Quantifier childBase = fieldKeyExpression.explodeField(baseAlias, fieldNamePrefix);
value = state.registerValue(QuantifiedObjectValue.of(childBase.getAlias()));
final GraphExpansion childExpansion;
if (state.isKey()) {
predicate = value.asPlaceholder(newParameterAlias());
childExpansion = GraphExpansion.ofPlaceholder(value, predicate);
} else {
childExpansion = GraphExpansion.ofResultValue(value);
}
final SelectExpression selectExpression = childExpansion.buildSelectWithBase(childBase);
final Quantifier childQuantifier = Quantifier.forEach(GroupExpressionRef.of(selectExpression));
final GraphExpansion.Sealed sealedChildExpansion = childExpansion.seal();
return sealedChildExpansion.derivedWithQuantifier(childQuantifier);
case None:
value = state.registerValue(new FieldValue(QuantifiedColumnValue.of(baseAlias, 0), fieldNames));
if (state.isKey()) {
predicate = value.asPlaceholder(newParameterAlias());
return GraphExpansion.ofPlaceholder(value, predicate);
}
return GraphExpansion.ofResultValue(value);
// TODO collect/concatenate function
case Concatenate:
default:
}
throw new UnsupportedOperationException();
}
use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.
the class OrderingProperty method deriveForPredicatesFilterFromOrderings.
public static Optional<Ordering> deriveForPredicatesFilterFromOrderings(@Nonnull final List<Optional<Ordering>> orderingOptionals, @Nonnull final RecordQueryPredicatesFilterPlan predicatesFilterPlan) {
final Optional<Ordering> childOrderingOptional = Iterables.getOnlyElement(orderingOptionals);
if (childOrderingOptional.isPresent()) {
final Ordering childOrdering = childOrderingOptional.get();
final SetMultimap<KeyExpression, Comparisons.Comparison> equalityBoundFileKeyExpressions = predicatesFilterPlan.getPredicates().stream().flatMap(queryPredicate -> {
if (!(queryPredicate instanceof ValuePredicate)) {
return Stream.empty();
}
final ValuePredicate valuePredicate = (ValuePredicate) queryPredicate;
if (!valuePredicate.getComparison().getType().isEquality()) {
return Stream.empty();
}
if (!(valuePredicate.getValue() instanceof FieldValue)) {
return Stream.of();
}
return Stream.of(Pair.of((FieldValue) valuePredicate.getValue(), valuePredicate.getComparison()));
}).map(valueComparisonPair -> {
final FieldValue fieldValue = valueComparisonPair.getLeft();
final String fieldName = fieldValue.getFieldName();
KeyExpression keyExpression = Key.Expressions.field(fieldName);
final List<String> fieldPrefix = fieldValue.getFieldPrefix();
for (int i = fieldPrefix.size() - 1; i >= 0; i--) {
keyExpression = Key.Expressions.field(fieldPrefix.get(i)).nest(keyExpression);
}
return Pair.of(keyExpression, valueComparisonPair.getRight());
}).collect(ImmutableSetMultimap.toImmutableSetMultimap(Pair::getLeft, Pair::getRight));
final ImmutableList<KeyPart> resultOrderingKeyParts = childOrdering.getOrderingKeyParts().stream().filter(keyPart -> !equalityBoundFileKeyExpressions.containsKey(keyPart.getNormalizedKeyExpression())).collect(ImmutableList.toImmutableList());
final SetMultimap<KeyExpression, Comparisons.Comparison> resultEqualityBoundKeyMap = HashMultimap.create(childOrdering.getEqualityBoundKeyMap());
equalityBoundFileKeyExpressions.forEach(resultEqualityBoundKeyMap::put);
return Optional.of(new Ordering(resultEqualityBoundKeyMap, resultOrderingKeyParts, childOrdering.isDistinct()));
} else {
return Optional.empty();
}
}
use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.
the class PushReferencedFieldsThroughSelectRule method onMatch.
@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
final PlannerBindings bindings = call.getBindings();
final SelectExpression selectExpression = bindings.get(root);
final List<? extends QueryPredicate> predicates = bindings.getAll(predicateMatcher);
final Set<FieldValue> fieldValuesFromPredicates = RelationalExpressionWithPredicates.fieldValuesFromPredicates(predicates);
final Set<FieldValue> fieldValuesFromResultValues = selectExpression.getFieldValuesFromResultValues();
final ExpressionRef<? extends RelationalExpression> lowerRef = bindings.get(lowerRefMatcher);
final ImmutableSet<FieldValue> allReferencedValues = ImmutableSet.<FieldValue>builder().addAll(call.getInterestingProperty(ReferencedFieldsAttribute.REFERENCED_FIELDS).map(ReferencedFields::getReferencedFieldValues).orElse(ImmutableSet.of())).addAll(fieldValuesFromPredicates).addAll(fieldValuesFromResultValues).build();
call.pushRequirement(lowerRef, ReferencedFieldsAttribute.REFERENCED_FIELDS, new ReferencedFields(allReferencedValues));
}
use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.
the class BooleanPredicateNormalizerTest method bigNonCnf.
@Test
void bigNonCnf() {
final QueryPredicate cnf = and(IntStream.rangeClosed(1, 9).boxed().map(i -> or(IntStream.rangeClosed(1, 9).boxed().map(j -> and(new ValuePredicate(new FieldValue(QuantifiedColumnValue.of(CorrelationIdentifier.UNGROUNDED, 0), ImmutableList.of("num_value_3_indexed")), new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 9 + j)), new ValuePredicate(new FieldValue(QuantifiedColumnValue.of(CorrelationIdentifier.UNGROUNDED, 0), ImmutableList.of("str_value_indexed")), new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "foo")))).collect(Collectors.toList()))).collect(Collectors.toList()));
final BooleanPredicateNormalizer plannerNormalizer = BooleanPredicateNormalizer.forConfiguration(DNF, RecordQueryPlannerConfiguration.builder().build());
assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> plannerNormalizer.normalize(cnf));
assertEquals(cnf, plannerNormalizer.normalizeIfPossible(cnf).orElse(cnf));
}
use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.
the class ValueIndexScanMatchCandidate method tryFetchCoveringIndexScan.
@Nonnull
private Optional<RelationalExpression> tryFetchCoveringIndexScan(@Nonnull final PartialMatch partialMatch, @Nonnull final List<ComparisonRange> comparisonRanges, final boolean isReverse) {
if (recordTypes.size() > 1) {
return Optional.empty();
}
final RecordType recordType = Iterables.getOnlyElement(recordTypes);
final IndexKeyValueToPartialRecord.Builder builder = IndexKeyValueToPartialRecord.newBuilder(recordType);
for (int i = 0; i < indexKeyValues.size(); i++) {
final Value keyValue = indexKeyValues.get(i);
if (keyValue instanceof FieldValue && keyValue.isFunctionallyDependentOn(recordValue)) {
final AvailableFields.FieldData fieldData = AvailableFields.FieldData.of(IndexKeyValueToPartialRecord.TupleSource.KEY, i);
addCoveringField(builder, (FieldValue) keyValue, fieldData);
}
}
for (int i = 0; i < indexValueValues.size(); i++) {
final Value valueValue = indexValueValues.get(i);
if (valueValue instanceof FieldValue && valueValue.isFunctionallyDependentOn(recordValue)) {
final AvailableFields.FieldData fieldData = AvailableFields.FieldData.of(IndexKeyValueToPartialRecord.TupleSource.VALUE, i);
addCoveringField(builder, (FieldValue) valueValue, fieldData);
}
}
if (!builder.isValid()) {
return Optional.empty();
}
final IndexScanParameters scanParameters = IndexScanComparisons.byValue(toScanComparisons(comparisonRanges));
final RecordQueryPlanWithIndex indexPlan = new RecordQueryIndexPlan(index.getName(), scanParameters, isReverse, false, (ValueIndexScanMatchCandidate) partialMatch.getMatchCandidate());
final RecordQueryCoveringIndexPlan coveringIndexPlan = new RecordQueryCoveringIndexPlan(indexPlan, recordType.getName(), // not used except for old planner properties
AvailableFields.NO_FIELDS, builder.build());
return Optional.of(new RecordQueryFetchFromPartialRecordPlan(coveringIndexPlan, coveringIndexPlan::pushValueThroughFetch));
}
Aggregations