Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ public static QueryPredicate and(@Nonnull final Collection<? extends QueryPredic

@Nonnull
public static QueryPredicate and(@Nonnull final Collection<? extends QueryPredicate> conjuncts, final boolean isAtomic) {
// Keep only conjuncts that aren’t tautologies; except for placeholders (where tautology means "no range
// constraint"). All placeholders must be retained for the index-matching machinery to work correctly.
final var filteredConjuncts =
conjuncts.stream()
.filter(queryPredicate -> !queryPredicate.isTautology())
.filter(queryPredicate -> (queryPredicate instanceof Placeholder) || !queryPredicate.isTautology())
.collect(ImmutableList.toImmutableList());

if (filteredConjuncts.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase;
import com.apple.foundationdb.record.query.expressions.Comparisons;
import com.apple.foundationdb.record.query.plan.cascades.AliasMap;
import com.apple.foundationdb.record.query.plan.cascades.CorrelationIdentifier;
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
import com.apple.foundationdb.record.query.plan.cascades.predicates.simplification.DefaultQueryPredicateRuleSet;
import com.apple.foundationdb.record.query.plan.cascades.predicates.simplification.QueryPredicateWithCnfRuleSet;
Expand Down Expand Up @@ -58,6 +59,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand Down Expand Up @@ -391,4 +393,31 @@ void simplifyPredicateTestRemovalRedundantDeepSubPredicates() {
QueryPredicateWithDnfRuleSet.ofSimplificationRules()).get();
assertEquals(expectedSimplifiedPredicate, simplifiedPredicate);
}

/**
* Tests that {@link AndPredicate#and} retains placeholders.
*
* <p>A {@link Placeholder} with no range constraints reports {@code isTautology() == true} because it accepts every
* value. The tautology filter in {@code and()} must not drop such placeholders, as they are necessary for correct
* index matching.
*/
@Test
void andRetainsPlaceholders() {
final var alias = CorrelationIdentifier.of("p0");
final var value = LiteralValue.ofScalar(42);
final Placeholder placeholder = Placeholder.newInstanceWithoutRanges(value, alias);

// Sanity-check that the placeholder indeed claims to be a tautology.
assertTrue(placeholder.isTautology());

// A conjunction of only an unconstrained placeholder is simplified to just that (not collapsed to TRUE).
final QueryPredicate pred1 = AndPredicate.and(List.of(placeholder));
assertSame(placeholder, pred1);

// Mixing a placeholder with a real predicate. The resulting `AndPredicate` must contain the placeholder.
final var pred2 = new ValuePredicate(value, new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, 42));
final QueryPredicate pred3 = AndPredicate.and(List.of(placeholder, pred2));
assertTrue(pred3 instanceof AndPredicate);
assertTrue(((AndPredicate)pred3).getChildren().contains(placeholder));
}
}
Loading