englefly commented on code in PR #70917:
URL: https://github.com/apache/doris/pull/70917#discussion_r4704531644
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/InPredicateDedup.java:
##########
@@ -36,31 +38,62 @@
public class InPredicateDedup implements ExpressionPatternRuleFactory {
public static final InPredicateDedup INSTANCE = new InPredicateDedup();
- // In many BI scenarios, the sql is auto-generated, and hence there may be
thousands of options.
- // It takes a long time to apply this rule. So set a threshold for the max
number.
+ // Size limit for other IN rewrites and sample size for large-list
deduplication.
public static final int REWRITE_OPTIONS_MAX_SIZE = 200;
+ private static final int LARGE_IN_PREFIX_DISTINCT_LIMIT = 100;
@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
matchesType(InPredicate.class)
- .when(inPredicate -> inPredicate.getOptions().size() <=
REWRITE_OPTIONS_MAX_SIZE)
.then(InPredicateDedup::dedup)
.toRule(ExpressionRuleType.IN_PREDICATE_DEDUP)
);
}
/** dedup */
public static Expression dedup(InPredicate inPredicate) {
+ List<Expression> options = inPredicate.getOptions();
+ if (options.size() > REWRITE_OPTIONS_MAX_SIZE) {
+ return dedupLiteralPrefix(inPredicate, options);
+ }
+
ImmutableSet.Builder<Expression> newOptionsBuilder =
ImmutableSet.builderWithExpectedSize(inPredicate.arity());
- for (Expression option : inPredicate.getOptions()) {
+ for (Expression option : options) {
newOptionsBuilder.add(option);
}
Set<Expression> newOptions = newOptionsBuilder.build();
- if (newOptions.size() == inPredicate.getOptions().size()) {
+ if (newOptions.size() == options.size()) {
return inPredicate;
}
return new InPredicate(inPredicate.getCompareExpr(), newOptions);
}
+
+ private static Expression dedupLiteralPrefix(InPredicate inPredicate,
List<Expression> options) {
+ Set<Literal> distinctPrefix = new LinkedHashSet<>();
+ int prefixEnd = 0;
+ for (; prefixEnd < options.size(); prefixEnd++) {
+ Expression option = options.get(prefixEnd);
+ if (!(option instanceof Literal)) {
+ break;
+ }
+ distinctPrefix.add((Literal) option);
Review Comment:
这不是这个rule的问题. 如果不采用限制的hash计算方式,也会有性能恶化的反例
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]