github-actions[bot] commented on code in PR #70917:
URL: https://github.com/apache/doris/pull/70917#discussion_r4675349609
##########
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:
[P2] Preserve mandatory suffix evaluation after compaction
Consider this reduced input plan, with at least one row where v <= 0:
```text
Project(TRUE IN (TRUE repeated 201 times, assert_true(v > 0, 'x')) AS ok)
Scan(v INT)
```
Returning the compacted two-option predicate here newly enables
`InPredicateExtractNonConstant` in the same normalizer. It creates `(TRUE =
TRUE) OR (TRUE = assert_true(...))`; `FoldConstantRuleOnFE.visitEqualTo` folds
the first child, then `visitOr` replaces the entire expression with TRUE. The
resulting `Project(TRUE AS ok)` has removed the assertion.
Before this change, the 202-option input bypasses deduplication and
extraction. `visitInPredicate` retains its nonliteral suffix, and BE
`VInPredicate` evaluates all arguments, so `assert_true` raises 'x' for that
row. `AssertTrue` implements `NoneMovableFunction`, whose evaluation must not
be pruned. Please preserve that evaluation when compaction enables later
rewrites and add a full-normalization regression; the current suffix test uses
Abs, stays above 200 after compaction, and runs only dedup/equality rules.
##########
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:
[P2] Avoid quadratic hashing after the repetitive sample
A valid reduced input is:
```text
Filter(k IN (S0 repeated 200 times, S1, ..., S9799))
Scan(k STRING)
```
Let these be literal strings with the same first 36 characters and distinct
suffixes. The 9,999-option list passes the sample, then this insertion
accumulates 9,800 distinct literals. `StringLikeLiteral.doComputeHashCode`
hashes only those first 36 characters, so every key collides. Nereids literals
implement `Comparable<ComparableLiteral>`, which does not supply HashMap's
concrete self-typed key ordering; its tree-bin insertion still searches
colliding subtrees. See [OpenJDK HashMap's ordering and insertion
logic](https://github.com/openjdk/jdk17u/blob/master/src/java.base/share/classes/java/util/HashMap.java#L2016-L2041).
Consequently this new pass performs quadratic equality work (roughly 48
million key comparisons at the default width limit), whereas the previous >200
guard skipped it. Hash caching and early rejection on the next rewrite cannot
avoid that first pass. Please use collision-safe accumulation or a bounded
fallback, and cover a repetitive sample followed by this largely distinct tail.
This is a static operation-count finding, not a benchmark result.
--
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]