From a2709197bf4cd6da01d60e5eece6b59ff5c3b54a Mon Sep 17 00:00:00 2001 From: minghong Date: Tue, 24 Sep 2024 19:17:45 +0800 Subject: [PATCH] keep-order --- .../rules/expression/rules/OrToIn.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/OrToIn.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/OrToIn.java index 493b5395fc96030..f59b4c819eb9831 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/OrToIn.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/OrToIn.java @@ -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 @@ -118,31 +118,27 @@ private Expression rewrite(Or or) { } if (!candidates.isEmpty()) { Expression conjunct = candidatesToFinalResult(candidates); - List 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 cleanDisjunctsByCandidates(List disjuncts, Set candidateKeys) { - List cleanedDisjuncts = Lists.newArrayList(); + private boolean hasOtherExpressionExceptCandidates(List disjuncts, Set candidateKeys) { for (Expression disjunct : disjuncts) { List conjuncts = ExpressionUtils.extractConjunction(disjunct); - List 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) { @@ -258,11 +254,18 @@ private Map> getCandidate(Expression disjunct) { } } else if (conjunct instanceof InPredicate) { InPredicate inPredicate = (InPredicate) conjunct; - Set literalOptions = inPredicate.getOptions().stream() - .filter(Literal.class::isInstance) - .map(e -> (Literal) e) - .collect(Collectors.toSet()); - if (literalOptions.size() == inPredicate.getOptions().size()) { + Set 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 alreadyMappedLiterals = candidates.get(inPredicate.getCompareExpr()); if (alreadyMappedLiterals == null) { candidates.put(inPredicate.getCompareExpr(), literalOptions);