Source:
void mergeWith( VolcanoPlanner planner, RelSet otherSet) { assert this != otherSet; assert this.equivalentSet == null; assert otherSet.equivalentSet == null; LOGGER.trace("Merge set#{} into set#{}", otherSet.id, id); otherSet.equivalentSet = this; final RelMetadataQuery mq = rel.getCluster().getMetadataQuery(); boolean existed = planner.allSets.remove(otherSet); assert existed : "merging with a dead otherSet"; final Map<RelSubset, RelNode> changedSubsets = Maps.newIdentityHashMap(); for (RelSubset otherSubset : otherSet.subsets) { planner.ruleQueue.subsetImportances.remove(otherSubset); RelSubset subset = getOrCreateSubset( otherSubset.getCluster(), otherSubset.getTraitSet()); if (otherSubset.bestCost.isLt(subset.bestCost)) { changedSubsets.put(subset, otherSubset.best); } for (RelNode otherRel : otherSubset.getRels()) { planner.reregister(this, otherRel); } } assert equivalentSet == null; final Set<RelSubset> activeSet = new HashSet<>(); for (Map.Entry<RelSubset, RelNode> subsetBestPair : changedSubsets.entrySet()) { RelSubset relSubset = subsetBestPair.getKey(); relSubset.propagateCostImprovements( planner, mq, subsetBestPair.getValue(), activeSet); } assert activeSet.isEmpty(); final List<RelNode> previousParents = ImmutableList.copyOf(otherSet.getParentRels()); for (RelNode parentRel : previousParents) { planner.rename(parentRel); } if (equivalentSet != null) { return; } for (RelNode parentRel : getParentRels()) { final RelSubset parentSubset = planner.getSubset(parentRel); parentSubset.propagateCostImprovements( planner, mq, parentRel, activeSet); } assert activeSet.isEmpty(); assert equivalentSet == null; for (RelNode rel : rels) { assert planner.getSet(rel) == this; planner.fireRules(rel, true); } }

Target:
void mergeWith( VolcanoPlanner planner, RelSet otherSet) { assert this != otherSet; assert this.equivalentSet == null; assert otherSet.equivalentSet == null; LOGGER.trace("Merge set#{} into set#{}", otherSet.id, id); otherSet.equivalentSet = this; RelMetadataQuery mq = rel.getCluster().getMetadataQuery(); boolean existed = planner.allSets.remove(otherSet); assert existed : "merging with a dead otherSet"; Map<RelSubset, RelNode> changedSubsets = new IdentityHashMap<>(); for (RelSubset otherSubset : otherSet.subsets) { planner.ruleQueue.subsetImportances.remove(otherSubset); RelSubset subset = getOrCreateSubset( otherSubset.getCluster(), otherSubset.getTraitSet()); if (otherSubset.bestCost.isLt(subset.bestCost)) { changedSubsets.put(subset, otherSubset.best); } for (RelNode otherRel : otherSubset.getRels()) { planner.reregister(this, otherRel); } } assert equivalentSet == null; Set<RelSubset> activeSet = new HashSet<>(); for (Map.Entry<RelSubset, RelNode> subsetBestPair : changedSubsets.entrySet()) { RelSubset relSubset = subsetBestPair.getKey(); relSubset.propagateCostImprovements( planner, mq, subsetBestPair.getValue(), activeSet); } assert activeSet.isEmpty(); final List<RelNode> previousParents = ImmutableList.copyOf(otherSet.getParentRels()); for (RelNode parentRel : previousParents) { planner.rename(parentRel); } if (equivalentSet != null) { return; } for (RelNode parentRel : getParentRels()) { final RelSubset parentSubset = planner.getSubset(parentRel); parentSubset.propagateCostImprovements( planner, mq, parentRel, activeSet); } assert activeSet.isEmpty(); assert equivalentSet == null; for (RelNode rel : rels) { assert planner.getSet(rel) == this; planner.fireRules(rel, true); } }