From de7f1190f6290c351def6907a4e1eb4da1235db5 Mon Sep 17 00:00:00 2001 From: minghong Date: Mon, 11 Nov 2024 15:59:08 +0800 Subject: [PATCH] equal --- .../doris/nereids/stats/FilterEstimation.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java index 471c3a3f2586e47..1d252aa39cbc00d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/FilterEstimation.java @@ -20,6 +20,7 @@ import org.apache.doris.analysis.DateLiteral; import org.apache.doris.analysis.LiteralExpr; import org.apache.doris.analysis.StringLiteral; +import org.apache.doris.common.Pair; import org.apache.doris.nereids.stats.FilterEstimation.EstimationContext; import org.apache.doris.nereids.trees.expressions.And; import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; @@ -54,6 +55,7 @@ import org.apache.doris.statistics.StatisticsBuilder; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import java.util.HashMap; @@ -76,7 +78,7 @@ public class FilterEstimation extends ExpressionVisitor aggSlots; private boolean isOnBaseTable = false; @@ -115,11 +117,26 @@ public Statistics visit(Expression expr, EstimationContext context) { return context.statistics.withSel(DEFAULT_INEQUALITY_COEFFICIENT); } - private Statistics estimateLargeAnd(List conjuncts, EstimationContext context) { - StatisticsBuilder builder = new StatisticsBuilder(context.statistics); + Pair, List> extractEquals(List conjuncts) { + List equals = Lists.newArrayList(); + List nonEquals = Lists.newArrayList(); + conjuncts.stream().forEach( + expr -> { + if (expr instanceof EqualPredicate) { + equals.add((EqualPredicate) expr); + } else { + nonEquals.add(expr); + } + } + ); + return Pair.of(equals, nonEquals); + } + + private Statistics estimateMultiEqualConjuncts(List conjuncts, EstimationContext context) { if (context.statistics.getRowCount() <= 1) { - return builder.build(); + return context.statistics; } + StatisticsBuilder builder = new StatisticsBuilder(context.statistics); List orderedSelections = conjuncts.stream() .mapToDouble(conjunct -> conjunct.accept(this, context).getBENumber() / context.statistics.getRowCount()) @@ -154,8 +171,10 @@ private Statistics estimateBasicAnd(List conjuncts, EstimationContex public Statistics visitAnd(And and, EstimationContext context) { List conjuncts = ExpressionUtils.extractConjunction(and); Statistics outputStats; - if (conjuncts.size() >= LARGE_COMPOUND_PREDICATE) { - outputStats = estimateLargeAnd(conjuncts, context); + Pair, List> equalAndNonEqual = extractEquals(conjuncts); + if (equalAndNonEqual.first.size() >= EQUAL_PREDICATE_LARGE_THRESHOLD) { + outputStats = estimateBasicAnd(equalAndNonEqual.second, context); + outputStats = estimateMultiEqualConjuncts(equalAndNonEqual.first, new EstimationContext(outputStats)); } else { outputStats = estimateBasicAnd(conjuncts, context); }