englefly commented on code in PR #70917:
URL: https://github.com/apache/doris/pull/70917#discussion_r4704571505
##########
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);
+ // Continue past the sample only when it contains enough
duplicates.
+ if (prefixEnd < REWRITE_OPTIONS_MAX_SIZE && distinctPrefix.size()
> LARGE_IN_PREFIX_DISTINCT_LIMIT) {
+ return inPredicate;
+ }
+ }
+ if (distinctPrefix.size() == prefixEnd) {
+ return inPredicate;
+ }
+
+ // Keep the first non-literal option and every option after it in
their original order.
+ List<Expression> compacted =
ImmutableList.<Expression>builderWithExpectedSize(
+ distinctPrefix.size() + (options.size() - prefixEnd))
+ .addAll(distinctPrefix)
+ .addAll(options.subList(prefixEnd, options.size()))
+ .build();
+ return new InPredicate(inPredicate.getCompareExpr(), compacted,
inPredicate.isInferred());
Review Comment:
这是 InPredicateExtractNonConstant 的bug, 和本pr 无关. 在
https://github.com/apache/doris/pull/71052 中解决
--
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]