|
| 1 | +/* |
| 2 | + * ImplementRecursiveRule.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2024 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.record.query.plan.cascades.rules; |
| 22 | + |
| 23 | +import com.apple.foundationdb.annotation.API; |
| 24 | +import com.apple.foundationdb.record.logging.KeyValueLogMessage; |
| 25 | +import com.apple.foundationdb.record.query.plan.cascades.CascadesRule; |
| 26 | +import com.apple.foundationdb.record.query.plan.cascades.CascadesRuleCall; |
| 27 | +import com.apple.foundationdb.record.query.plan.cascades.PlanPartition; |
| 28 | +import com.apple.foundationdb.record.query.plan.cascades.Quantifier; |
| 29 | +import com.apple.foundationdb.record.query.plan.cascades.Reference; |
| 30 | +import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger; |
| 31 | +import com.apple.foundationdb.record.query.plan.cascades.expressions.RecursiveExpression; |
| 32 | +import com.apple.foundationdb.record.query.plan.cascades.matching.structure.BindingMatcher; |
| 33 | +import com.apple.foundationdb.record.query.plan.plans.RecordQueryRecursivePlan; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +import javax.annotation.Nonnull; |
| 38 | + |
| 39 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.ListMatcher.exactly; |
| 40 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.MultiMatcher.all; |
| 41 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.QuantifierMatchers.anyQuantifierOverRef; |
| 42 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.ReferenceMatchers.anyPlanPartition; |
| 43 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.ReferenceMatchers.planPartitions; |
| 44 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.ReferenceMatchers.rollUp; |
| 45 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.RelationalExpressionMatchers.canBeImplemented; |
| 46 | +import static com.apple.foundationdb.record.query.plan.cascades.matching.structure.RelationalExpressionMatchers.recursiveExpression; |
| 47 | + |
| 48 | +/** |
| 49 | + * A rule that implements an existential nested loop join of its (already implemented) children. |
| 50 | + */ |
| 51 | +@API(API.Status.EXPERIMENTAL) |
| 52 | +public class ImplementRecursiveRule extends CascadesRule<RecursiveExpression> { |
| 53 | + @Nonnull |
| 54 | + private static final Logger logger = LoggerFactory.getLogger(ImplementRecursiveRule.class); |
| 55 | + |
| 56 | + @Nonnull |
| 57 | + private static final BindingMatcher<PlanPartition> rootPlanPartitionsMatcher = anyPlanPartition(); |
| 58 | + |
| 59 | + @Nonnull |
| 60 | + private static final BindingMatcher<Reference> rootReferenceMatcher = planPartitions(rollUp(all(rootPlanPartitionsMatcher))); |
| 61 | + @Nonnull |
| 62 | + private static final BindingMatcher<Quantifier> rootQuantifierMatcher = anyQuantifierOverRef(rootReferenceMatcher); |
| 63 | + @Nonnull |
| 64 | + private static final BindingMatcher<PlanPartition> childPlanPartitionsMatcher = anyPlanPartition(); |
| 65 | + |
| 66 | + @Nonnull |
| 67 | + private static final BindingMatcher<Reference> childReferenceMatcher = planPartitions(rollUp(all(childPlanPartitionsMatcher))); |
| 68 | + @Nonnull |
| 69 | + private static final BindingMatcher<Quantifier> childQuantifierMatcher = anyQuantifierOverRef(childReferenceMatcher); |
| 70 | + @Nonnull |
| 71 | + private static final BindingMatcher<RecursiveExpression> root = |
| 72 | + recursiveExpression(exactly(rootQuantifierMatcher, childQuantifierMatcher)).where(canBeImplemented()); |
| 73 | + |
| 74 | + public ImplementRecursiveRule() { |
| 75 | + super(root); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onMatch(@Nonnull final CascadesRuleCall call) { |
| 80 | + final var bindings = call.getBindings(); |
| 81 | + final var recursiveExpression = bindings.get(root); |
| 82 | + Debugger.withDebugger(debugger -> logger.debug(KeyValueLogMessage.of("matched RecursiveExpression", "legs", recursiveExpression.getQuantifiers().size()))); |
| 83 | + |
| 84 | + final var rootQuantifier = bindings.get(rootQuantifierMatcher); |
| 85 | + final var childQuantifier = bindings.get(childQuantifierMatcher); |
| 86 | + |
| 87 | + final var rootReference = bindings.get(rootReferenceMatcher); |
| 88 | + final var childReference = bindings.get(childReferenceMatcher); |
| 89 | + |
| 90 | + final var rootPartition = bindings.get(rootPlanPartitionsMatcher); |
| 91 | + final var childPartition = bindings.get(childPlanPartitionsMatcher); |
| 92 | + |
| 93 | + final var rootAlias = rootQuantifier.getAlias(); |
| 94 | + final var childAlias = childQuantifier.getAlias(); |
| 95 | + |
| 96 | + var rootRef = call.memoizeMemberPlans(rootReference, rootPartition.getPlans()); |
| 97 | + final var newRootQuantifier = Quantifier.physicalBuilder().withAlias(rootAlias).build(rootRef); |
| 98 | + |
| 99 | + var childRef = call.memoizeMemberPlans(childReference, childPartition.getPlans()); |
| 100 | + final var newChildQuantifier = Quantifier.physicalBuilder().withAlias(childAlias).build(childRef); |
| 101 | + |
| 102 | + call.yieldExpression(new RecordQueryRecursivePlan(newRootQuantifier, newChildQuantifier, recursiveExpression.getResultValue(), true)); |
| 103 | + } |
| 104 | +} |
0 commit comments