Skip to content

Commit ca581cc

Browse files
authored
resolves: #3487 restrict select expressions in rewrite matchers to be exploratory (#3486)
This fixes #3487
1 parent 8ee1c54 commit ca581cc

File tree

77 files changed

+2293
-2288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+2293
-2288
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/ConstraintsMap.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ public ConstraintsMap() {
9797
this.attributeToConstraintMap = Maps.newLinkedHashMap();
9898
}
9999

100+
public long getCurrentTick() {
101+
return currentTick;
102+
}
103+
104+
public long getWatermarkGoalTick() {
105+
return watermarkGoalTick;
106+
}
107+
108+
public long getWatermarkCommittedTick() {
109+
return watermarkCommittedTick;
110+
}
111+
100112
/**
101113
* Method to return if a particular attribute is contained in the map.
102114
* @param attribute the attribute key to check

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/PartialMatch.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class PartialMatch {
100100
private final Supplier<Map<CorrelationIdentifier, ComparisonRange>> boundParameterPrefixMapSupplier;
101101

102102
@Nonnull
103-
private final Supplier<Set<QueryPredicate>> boundPlaceholdersSupplier;
103+
private final Supplier<Set<Placeholder>> boundPlaceholdersSupplier;
104104

105105
@Nonnull
106106
private final Supplier<Set<Quantifier>> matchedQuantifiersSupplier;
@@ -217,14 +217,14 @@ private Set<Quantifier> computeUnmatchedQuantifiers() {
217217
}
218218

219219
@Nonnull
220-
public final Set<QueryPredicate> getBoundPlaceholders() {
220+
public final Set<Placeholder> getBoundPlaceholders() {
221221
return boundPlaceholdersSupplier.get();
222222
}
223223

224224
@Nonnull
225-
private Set<QueryPredicate> computeBoundPlaceholders() {
225+
private Set<Placeholder> computeBoundPlaceholders() {
226226
final var boundParameterPrefixMap = getBoundParameterPrefixMap();
227-
final var boundPlaceholders = Sets.<QueryPredicate>newIdentityHashSet();
227+
final var boundPlaceholders = Sets.<Placeholder>newIdentityHashSet();
228228

229229
//
230230
// Go through all accumulated parameter bindings -- find the query predicates binding the parameters. Those
@@ -246,7 +246,7 @@ private Set<QueryPredicate> computeBoundPlaceholders() {
246246

247247
final var placeholder = (Placeholder)candidatePredicate;
248248
if (boundParameterPrefixMap.containsKey(placeholder.getParameterAlias())) {
249-
boundPlaceholders.add(predicateMapping.getCandidatePredicate());
249+
boundPlaceholders.add(placeholder);
250250
}
251251
}
252252

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/Reference.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,11 @@ public String show(final boolean renderSingleGroups) {
717717
return PlannerGraphVisitor.show(renderSingleGroups, this);
718718
}
719719

720+
@Nonnull
721+
public String showExploratory() {
722+
return PlannerGraphVisitor.show(PlannerGraphVisitor.REMOVE_FINAL_EXPRESSIONS | PlannerGraphVisitor.RENDER_SINGLE_GROUPS, this);
723+
}
724+
720725
@SuppressWarnings("PMD.CompareObjectsWithEquals")
721726
private static boolean isMemoizedExpression(@Nonnull final RelationalExpression member,
722727
@Nonnull final RelationalExpression otherExpression,

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/expressions/RelationalExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,4 +853,9 @@ default <U> U acceptVisitor(@Nonnull SimpleExpressionVisitor<U> simpleExpression
853853
default String show(final boolean renderSingleGroups) {
854854
return PlannerGraphVisitor.show(renderSingleGroups, this);
855855
}
856+
857+
@Nonnull
858+
default String showExploratory() {
859+
return PlannerGraphVisitor.show(PlannerGraphVisitor.REMOVE_FINAL_EXPRESSIONS | PlannerGraphVisitor.RENDER_SINGLE_GROUPS, this);
860+
}
856861
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/expressions/SelectExpression.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,21 +626,31 @@ public Optional<MatchInfo> adjustMatch(@Nonnull final PartialMatch partialMatch)
626626
@Nonnull
627627
@Override
628628
public PlannerGraph rewriteInternalPlannerGraph(@Nonnull final List<? extends PlannerGraph> childGraphs) {
629+
final var predicateString = "WHERE " + getPredicates().stream()
630+
.map(Object::toString)
631+
.collect(Collectors.joining(" AND "));
632+
633+
final var abbreviatedPredicateString = predicateString.length() > 30 ? String.format("%02x", predicateString.hashCode()) : predicateString;
629634
return PlannerGraph.fromNodeAndChildGraphs(
630635
new PlannerGraph.LogicalOperatorNode(this,
631636
"SELECT " + resultValue,
632637
getPredicates().isEmpty()
633638
? ImmutableList.of()
634-
: ImmutableList.of("WHERE " + getPredicates().stream()
635-
.map(Object::toString)
636-
.collect(Collectors.joining(" AND "))),
639+
: ImmutableList.of(abbreviatedPredicateString),
637640
ImmutableMap.of()),
638641
childGraphs);
639642
}
640643

641644
@Override
642645
public String toString() {
643-
return "SELECT " + resultValue + " WHERE " + AndPredicate.and(getPredicates());
646+
final var selectFrom =
647+
"SELECT " + resultValue + " FROM " + Quantifiers.aliases(getQuantifiers())
648+
.stream()
649+
.map(CorrelationIdentifier::toString).collect(Collectors.joining(", "));
650+
if (!getPredicates().isEmpty()) {
651+
return selectFrom + " WHERE " + AndPredicate.and(getPredicates());
652+
}
653+
return selectFrom;
644654
}
645655

646656
/**

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/matching/structure/ExpressionsPartitionMatchers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private ExpressionsPartitionMatchers() {
4848
@Nonnull
4949
@SuppressWarnings("unchecked")
5050
public static BindingMatcher<Reference> expressionPartitions(@Nonnull final BindingMatcher<? extends Iterable<? extends ExpressionPartition<? extends RelationalExpression>>> downstream) {
51-
return TypedMatcherWithExtractAndDownstream.typedWithDownstream((Class<Reference>)(Class<?>)Reference.class,
51+
return TypedMatcherWithExtractAndDownstream.typedWithDownstream(Reference.class,
5252
Extractor.of(Reference::toExpressionPartitions, name -> "expressionPartitions(" + name + ")"),
5353
downstream);
5454
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/matching/structure/PlanPartitionMatchers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private PlanPartitionMatchers() {
4141

4242
@Nonnull
4343
@SuppressWarnings("unchecked")
44-
public static <R extends Reference> BindingMatcher<R> planPartitions(@Nonnull final BindingMatcher<? extends Iterable<PlanPartition>> downstream) {
45-
return TypedMatcherWithExtractAndDownstream.typedWithDownstream((Class<R>)(Class<?>)Reference.class,
44+
public static BindingMatcher<Reference> planPartitions(@Nonnull final BindingMatcher<? extends Iterable<PlanPartition>> downstream) {
45+
return TypedMatcherWithExtractAndDownstream.typedWithDownstream(Reference.class,
4646
Extractor.of(Reference::toPlanPartitions, name -> "planPartitions(" + name + ")"),
4747
downstream);
4848
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/matching/structure/RecordQueryPlanMatchers.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ public static BindingMatcher<RecordQueryUnionOnValuesPlan> unionOnValuesPlan(@No
511511

512512
@SuppressWarnings("unchecked")
513513
@Nonnull
514-
public static <C extends RecordQueryPlanWithComparisonKeyValues> BindingMatcher<C> comparisonKeyValues(@Nonnull CollectionMatcher<? extends Value> comparisonKeyMatcher) {
515-
return typedWithDownstream((Class<C>)(Class<?>)RecordQueryPlanWithComparisonKeyValues.class,
514+
public static BindingMatcher<RecordQueryPlanWithComparisonKeyValues> comparisonKeyValues(@Nonnull CollectionMatcher<? extends Value> comparisonKeyMatcher) {
515+
return typedWithDownstream(RecordQueryPlanWithComparisonKeyValues.class,
516516
Extractor.of(RecordQueryPlanWithComparisonKeyValues::getComparisonKeyValues, name -> "comparisonKeyValues(" + name + ")"),
517517
comparisonKeyMatcher);
518518
}
@@ -775,8 +775,8 @@ public static BindingMatcher<RecordQueryExplodePlan> explodePlan() {
775775

776776
@Nonnull
777777
@SuppressWarnings("unchecked")
778-
public static <M extends RecordQueryExplodePlan> BindingMatcher<M> collectionValue(@Nonnull BindingMatcher<? extends Value> downstream) {
779-
return typedWithDownstream((Class<M>)(Class<?>)RecordQueryExplodePlan.class,
778+
public static BindingMatcher<RecordQueryExplodePlan> collectionValue(@Nonnull BindingMatcher<? extends Value> downstream) {
779+
return typedWithDownstream(RecordQueryExplodePlan.class,
780780
Extractor.of(RecordQueryExplodePlan::getCollectionValue, name -> "collectionValue(" + name + ")"),
781781
downstream);
782782
}
@@ -803,8 +803,8 @@ public static BindingMatcher<RecordQueryUpdatePlan> updatePlan(@Nonnull final Bi
803803

804804
@Nonnull
805805
@SuppressWarnings("unchecked")
806-
public static <M extends RecordQueryAbstractDataModificationPlan> BindingMatcher<M> target(@Nonnull BindingMatcher<? extends String> downstream) {
807-
return typedWithDownstream((Class<M>)(Class<?>)RecordQueryAbstractDataModificationPlan.class,
806+
public static BindingMatcher<RecordQueryAbstractDataModificationPlan> target(@Nonnull BindingMatcher<? extends String> downstream) {
807+
return typedWithDownstream(RecordQueryAbstractDataModificationPlan.class,
808808
Extractor.of(RecordQueryAbstractDataModificationPlan::getTargetRecordType, name -> "target(" + name + ")"),
809809
downstream);
810810
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/matching/structure/ReferenceMatchers.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,32 @@ public static BindingMatcher<Reference> getCurrentReferenceMatcher() {
5858

5959
@Nonnull
6060
@SuppressWarnings("unchecked")
61-
public static <R extends Reference> BindingMatcher<R> anyRef() {
62-
return typed((Class<R>)(Class<?>)Reference.class);
61+
public static BindingMatcher<Reference> anyRef() {
62+
return typed(Reference.class);
6363
}
6464

6565
@Nonnull
66-
public static BindingMatcher<? extends Reference> anyRefOverOnlyPlans() {
66+
public static BindingMatcher<Reference> anyRefOverOnlyPlans() {
6767
return members(all(RelationalExpressionMatchers.ofType(RecordQueryPlan.class)));
6868
}
6969

7070
@Nonnull
7171
@SuppressWarnings("unchecked")
72-
public static <R extends Reference, E extends RelationalExpression> BindingMatcher<R> members(@Nonnull final CollectionMatcher<E> downstream) {
73-
return TypedMatcherWithExtractAndDownstream.typedWithDownstream((Class<R>)(Class<?>)Reference.class,
72+
public static <E extends RelationalExpression> BindingMatcher<Reference> members(@Nonnull final CollectionMatcher<E> downstream) {
73+
return TypedMatcherWithExtractAndDownstream.typedWithDownstream(Reference.class,
7474
Extractor.of(Reference::getAllMemberExpressions, name -> "allMembers(" + name + ")"),
7575
downstream);
7676
}
7777

7878
@Nonnull
7979
@SuppressWarnings("unchecked")
80-
public static <R extends Reference, E extends RelationalExpression> BindingMatcher<R> exploratoryMembers(@Nonnull final CollectionMatcher<E> downstream) {
81-
return TypedMatcherWithExtractAndDownstream.typedWithDownstream((Class<R>)Reference.class,
80+
public static <E extends RelationalExpression> BindingMatcher<Reference> exploratoryMembers(@Nonnull final CollectionMatcher<E> downstream) {
81+
return TypedMatcherWithExtractAndDownstream.typedWithDownstream(Reference.class,
8282
Extractor.of(Reference::getExploratoryExpressions, name -> "allMembers(" + name + ")"),
8383
downstream);
8484
}
8585

8686
@Nonnull
87-
@SuppressWarnings("unchecked")
8887
public static <E extends RelationalExpression> BindingMatcher<Reference> exploratoryMember(@Nonnull final BindingMatcher<E> downstream) {
8988
return TypedMatcherWithExtractAndDownstream.typedWithDownstream(Reference.class,
9089
Extractor.of(Reference::getExploratoryExpressions, name -> "exploratoryMember(" + name + ")"),

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/matching/structure/RelationalExpressionMatchers.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ public static <R extends RelationalExpression, C extends Collection<? extends Qu
9494
downstream);
9595
}
9696

97-
@SuppressWarnings("unchecked")
98-
public static <R extends RelationalExpression, C extends Collection<? extends Quantifier>> BindingMatcher<R> owning(@Nonnull final BindingMatcher<C> downstream) {
99-
return ofTypeOwning((Class<R>)(Class<?>)RelationalExpression.class, downstream);
100-
}
101-
10297
public static <R extends RelationalExpression> BindingMatcher<R> canBeImplemented() {
10398
return PrimitiveMatchers.satisfies(relationalExpression ->
10499
relationalExpression.getQuantifiers()
@@ -134,6 +129,11 @@ public static <R extends RelationalExpressionWithPredicates, C1 extends Collecti
134129
downstreamQuantifiers))));
135130
}
136131

132+
public static <E extends RelationalExpression> BindingMatcher<E> isExploratoryExpression() {
133+
return PrimitiveMatchers.satisfiesWithOuterBinding(ReferenceMatchers.getCurrentReferenceMatcher(),
134+
(expression, currentReference) -> !currentReference.isFinal(expression));
135+
}
136+
137137
@Nonnull
138138
public static BindingMatcher<FullUnorderedScanExpression> fullUnorderedScanExpression() {
139139
return ofTypeOwning(FullUnorderedScanExpression.class, CollectionMatcher.empty());

0 commit comments

Comments
 (0)