Skip to content

Commit

Permalink
keep-order
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Sep 24, 2024
1 parent 7f06720 commit a270919
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* dependends on SimplifyRange rule
Expand Down Expand Up @@ -118,31 +118,27 @@ private Expression rewrite(Or or) {
}
if (!candidates.isEmpty()) {
Expression conjunct = candidatesToFinalResult(candidates);
List<Expression> cleanedDisjuncts = cleanDisjunctsByCandidates(disjuncts, candidates.keySet());
if (cleanedDisjuncts.isEmpty()) {
return conjunct;
} else {
boolean hasOtherExpr = hasOtherExpressionExceptCandidates(disjuncts, candidates.keySet());
if (hasOtherExpr) {
return new And(conjunct, or);
} else {
return conjunct;
}
}
return or;
}

private List<Expression> cleanDisjunctsByCandidates(List<Expression> disjuncts, Set<Expression> candidateKeys) {
List<Expression> cleanedDisjuncts = Lists.newArrayList();
private boolean hasOtherExpressionExceptCandidates(List<Expression> disjuncts, Set<Expression> candidateKeys) {
for (Expression disjunct : disjuncts) {
List<Expression> conjuncts = ExpressionUtils.extractConjunction(disjunct);
List<Expression> cleanedConjuncts = Lists.newArrayList();
for (Expression conjunct : conjuncts) {
if (!containsAny(conjunct.getInputSlots(), candidateKeys)) {
cleanedConjuncts.add(conjunct);
return true;
}
}
if (!cleanedConjuncts.isEmpty()) {
cleanedDisjuncts.add(ExpressionUtils.and(cleanedConjuncts));
}

}
return cleanedDisjuncts;
return false;
}

private boolean containsAny(Set a, Set b) {
Expand Down Expand Up @@ -258,11 +254,18 @@ private Map<Expression, Set<Literal>> getCandidate(Expression disjunct) {
}
} else if (conjunct instanceof InPredicate) {
InPredicate inPredicate = (InPredicate) conjunct;
Set<Literal> literalOptions = inPredicate.getOptions().stream()
.filter(Literal.class::isInstance)
.map(e -> (Literal) e)
.collect(Collectors.toSet());
if (literalOptions.size() == inPredicate.getOptions().size()) {
Set<Literal> literalOptions = new LinkedHashSet<>();
boolean allLiteralOpts = true;
for (Expression opt : inPredicate.getOptions()) {
if (opt instanceof Literal) {
literalOptions.add((Literal) opt);
} else {
allLiteralOpts = false;
break;
}
}

if (allLiteralOpts) {
Set<Literal> alreadyMappedLiterals = candidates.get(inPredicate.getCompareExpr());
if (alreadyMappedLiterals == null) {
candidates.put(inPredicate.getCompareExpr(), literalOptions);
Expand Down

0 comments on commit a270919

Please sign in to comment.