jduo commented on code in PR #3616:
URL: https://github.com/apache/calcite/pull/3616#discussion_r1529081391
##########
core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java:
##########
@@ -649,6 +661,96 @@ assert rowTypesAreEquivalent(
return substitutions;
}
+ /**
+ * Find out the replacements belongs to the real substitution and undo the
rest.
+ * The rest replacements are exploratory, they are no more needed when this
attempt finished.
+ */
+ private List<Replacement> undoAndRemoveExploratoryReplacements(
+ List<Replacement> attempted, List<MutableRel> targetDescendants,
+ MutableRel replacement, boolean lastIsFinalReplacement) {
+ final List<Replacement> realReplacements = new ArrayList<>();
+ final List<Replacement> exploratoryReplacements =
+ new ArrayList<>(lastIsFinalReplacement
+ ? attempted.subList(0, attempted.size() - 1)
+ : attempted);
+ findOutRealReplacements(
+ exploratoryReplacements,
+ realReplacements,
+ null,
+ targetDescendants,
+ replacement);
+ Collections.reverse(realReplacements);
+ exploratoryReplacements.removeAll(realReplacements);
+ undoReplacement(exploratoryReplacements);
+ if (lastIsFinalReplacement) {
+ realReplacements.add(attempted.get(attempted.size() - 1));
+ }
+ return realReplacements;
+ }
+
+ private void findOutRealReplacements(
+ List<Replacement> src, List<Replacement> result, @Nullable Replacement
last,
+ List<MutableRel> targetDescendants, MutableRel replacement) {
+ if (src == null || src.isEmpty()) {
+ return;
+ }
+ final Replacement current = src.get(src.size() - 1);
+ if (last == null || isPrevReplacement(current, last)) {
+ result.add(current);
+ last = current;
+ }
+ if (isFirstReplacement(current, targetDescendants, replacement)) {
+ return;
+ }
+ findOutRealReplacements(
+ src.subList(0, src.size() - 1),
+ result,
+ last,
+ targetDescendants,
+ replacement);
+ }
+
+ /**
+ * Return true when x replacement is the previous one of y.
+ */
+ private boolean isPrevReplacement(Replacement x, Replacement y) {
+ final MutableRel prev = x.after;
+ final MutableRel next = y.before;
+ if (prev == next) {
+ return true;
+ }
+ final MutableRel prevParent = prev.getParent();
+ assert prevParent != null;
+ for (MutableRel cur : MutableRels.descendants(next)) {
+ if (cur == prevParent) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Return true when x is the first one of real replacements chain,
+ * which the before of it is not the part of the targetDescendants and is
not replacement.
+ */
+ private boolean isFirstReplacement(
+ Replacement r,
+ List<MutableRel> targetDescendants,
+ MutableRel replacement) {
+ final MutableRel before = r.before;
+ for (MutableRel cur : MutableRels.descendants(before)) {
+ if (replacement.equals(cur)) {
+ return false;
+ }
+ for (MutableRel targetDescendant : targetDescendants) {
Review Comment:
Does this need to descend again to MutableRels.descendants(targetDescendant)?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]