use of com.apple.foundationdb.record.query.predicates.ValuePredicate in project fdb-record-layer by FoundationDB.
the class InComparisonToExplodeRule method onMatch.
@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
final PlannerBindings bindings = call.getBindings();
final SelectExpression selectExpression = bindings.get(root);
// we don't need iteration stability
final List<? extends ValuePredicate> inPredicatesList = bindings.getAll(inPredicateMatcher);
if (inPredicatesList.isEmpty()) {
return;
}
final Set<QueryPredicate> inPredicates = Sets.newIdentityHashSet();
inPredicates.addAll(inPredicatesList);
final ImmutableList.Builder<Quantifier> transformedQuantifiers = ImmutableList.builder();
final ImmutableList.Builder<QueryPredicate> transformedPredicates = ImmutableList.builder();
for (final QueryPredicate predicate : selectExpression.getPredicates()) {
if (inPredicates.contains(predicate)) {
final ValuePredicate valuePredicate = (ValuePredicate) predicate;
final Comparisons.Comparison comparison = valuePredicate.getComparison();
Verify.verify(comparison.getType() == Comparisons.Type.IN);
final ExplodeExpression explodeExpression;
if (comparison instanceof Comparisons.ListComparison) {
explodeExpression = new ExplodeExpression(new LiteralValue<>(comparison.getComparand()));
} else if (comparison instanceof Comparisons.ParameterComparison) {
explodeExpression = new ExplodeExpression(QuantifiedColumnValue.of(CorrelationIdentifier.of(((Comparisons.ParameterComparison) comparison).getParameter()), 0));
} else {
throw new RecordCoreException("unknown in comparison " + comparison.getClass().getSimpleName());
}
final Quantifier.ForEach newQuantifier = Quantifier.forEach(GroupExpressionRef.of(explodeExpression));
transformedPredicates.add(new ValuePredicate(((ValuePredicate) predicate).getValue(), new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, Bindings.Internal.CORRELATION.bindingName(newQuantifier.getAlias().toString()), Bindings.Internal.CORRELATION)));
transformedQuantifiers.add(newQuantifier);
} else {
transformedPredicates.add(predicate);
}
}
transformedQuantifiers.addAll(bindings.getAll(innerQuantifierMatcher));
call.yield(call.ref(new SelectExpression(selectExpression.getResultValues(), transformedQuantifiers.build(), transformedPredicates.build())));
}
use of com.apple.foundationdb.record.query.predicates.ValuePredicate 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.ValuePredicate in project fdb-record-layer by FoundationDB.
the class BooleanPredicateNormalizerTest method bigCnfThatWouldOverflow.
@Test
void bigCnfThatWouldOverflow() {
// A CNF who's DNF size doesn't fit in an int.
final List<QueryPredicate> conjuncts = new ArrayList<>();
for (int i = 0; i < 32; i++) {
final List<QueryPredicate> disjuncts = new ArrayList<>();
for (int j = 0; j < 2; j++) {
disjuncts.add(new ValuePredicate(F, new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 100 + j)));
}
conjuncts.add(or(disjuncts));
}
final QueryPredicate cnf = and(conjuncts);
final BooleanPredicateNormalizer normalizer = BooleanPredicateNormalizer.getDefaultInstanceForDnf();
assertThrows(ArithmeticException.class, () -> normalizer.getNormalizedSize(cnf));
assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> normalizer.normalize(cnf));
assertEquals(cnf, normalizer.normalizeIfPossible(cnf).orElse(cnf));
}
use of com.apple.foundationdb.record.query.predicates.ValuePredicate in project fdb-record-layer by FoundationDB.
the class BooleanPredicateNormalizerTest method cnf.
@Test
void cnf() {
final List<QueryPredicate> conjuncts = new ArrayList<>();
for (int i = 0; i < 5; i++) {
final List<QueryPredicate> disjuncts = new ArrayList<>();
for (int j = 0; j < 5; j++) {
disjuncts.add(new ValuePredicate(F, new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 4 + j)));
}
conjuncts.add(or(disjuncts));
}
final QueryPredicate cnf = and(conjuncts);
final BooleanPredicateNormalizer normalizer = BooleanPredicateNormalizer.getDefaultInstanceForDnf();
assertNotEquals(cnf, normalizer.normalize(cnf).orElse(cnf));
assertTrue(numberOfOrTerms(Objects.requireNonNull(normalizer.normalize(cnf).orElse(cnf))) <= normalizer.getNormalizedSize(cnf));
final BooleanPredicateNormalizer lowLimitNormalizer = BooleanPredicateNormalizer.withLimit(DNF, 2);
assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> lowLimitNormalizer.normalize(cnf));
assertEquals(cnf, lowLimitNormalizer.normalizeIfPossible(cnf).orElse(cnf));
}
use of com.apple.foundationdb.record.query.predicates.ValuePredicate 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));
}
Aggregations