|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.esql.optimizer.rules.physical.local;
|
9 | 9 |
|
| 10 | +import org.elasticsearch.index.IndexMode; |
| 11 | +import org.elasticsearch.index.query.BoolQueryBuilder; |
| 12 | +import org.elasticsearch.index.query.FuzzyQueryBuilder; |
| 13 | +import org.elasticsearch.index.query.MatchAllQueryBuilder; |
| 14 | +import org.elasticsearch.index.query.MatchNoneQueryBuilder; |
| 15 | +import org.elasticsearch.index.query.MultiTermQueryBuilder; |
| 16 | +import org.elasticsearch.index.query.PrefixQueryBuilder; |
10 | 17 | import org.elasticsearch.index.query.QueryBuilder;
|
| 18 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
| 19 | +import org.elasticsearch.index.query.RegexpQueryBuilder; |
| 20 | +import org.elasticsearch.index.query.TermsQueryBuilder; |
| 21 | +import org.elasticsearch.index.query.WildcardQueryBuilder; |
11 | 22 | import org.elasticsearch.logging.LogManager;
|
12 | 23 | import org.elasticsearch.logging.Logger;
|
13 | 24 | import org.elasticsearch.xpack.esql.core.expression.Alias;
|
|
30 | 41 | import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec;
|
31 | 42 | import org.elasticsearch.xpack.esql.plan.physical.EvalExec;
|
32 | 43 | import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan;
|
| 44 | +import org.elasticsearch.xpack.esql.querydsl.query.SingleValueQuery; |
| 45 | +import org.elasticsearch.xpack.esql.stats.SearchStats; |
33 | 46 |
|
34 | 47 | import java.time.ZoneId;
|
35 | 48 | import java.util.ArrayList;
|
@@ -275,7 +288,12 @@ protected PhysicalPlan rule(EvalExec evalExec, LocalPhysicalOptimizerContext ctx
|
275 | 288 | if (roundTos.size() == 1) {
|
276 | 289 | RoundTo roundTo = roundTos.get(0);
|
277 | 290 | int count = roundTo.points().size();
|
278 |
| - int roundingPointsUpperLimit = roundingPointsThreshold(ctx); |
| 291 | + int roundingPointsUpperLimit = adjustedRoundingPointsThreshold( |
| 292 | + ctx.searchStats(), |
| 293 | + roundingPointsThreshold(ctx), |
| 294 | + queryExec.query(), |
| 295 | + queryExec.indexMode() |
| 296 | + ); |
279 | 297 | if (count > roundingPointsUpperLimit) {
|
280 | 298 | logger.debug(
|
281 | 299 | "Skipping RoundTo push down for [{}], as it has [{}] points, which is more than [{}]",
|
@@ -485,4 +503,63 @@ private int roundingPointsThreshold(LocalPhysicalOptimizerContext ctx) {
|
485 | 503 | }
|
486 | 504 | return roundingPointsThreshold;
|
487 | 505 | }
|
| 506 | + |
| 507 | + /** |
| 508 | + * If the main query is expensive (such as including wildcard queries), executing more queries with tags is slower and more costly |
| 509 | + * than executing fewer queries without tags and then reading points and rounding. The rounding points threshold is treated as the |
| 510 | + * maximum number of clauses allowed to execute. We estimate the number of clauses in the main query and adjust the threshold so |
| 511 | + * that the total number of clauses does not exceed the limit by too much. Some expensive queries count as more than one clause; |
| 512 | + * for example, a wildcard query counts as 5 clauses, and a terms query counts as the number of terms. |
| 513 | + */ |
| 514 | + static int adjustedRoundingPointsThreshold(SearchStats stats, int threshold, QueryBuilder query, IndexMode indexMode) { |
| 515 | + int clauses = estimateQueryClauses(stats, query) + 1; |
| 516 | + if (indexMode == IndexMode.TIME_SERIES) { |
| 517 | + // No doc partitioning for time_series sources; increase the threshold to trade overhead for parallelism. |
| 518 | + threshold *= 2; |
| 519 | + } |
| 520 | + return Math.ceilDiv(threshold, clauses); |
| 521 | + } |
| 522 | + |
| 523 | + static int estimateQueryClauses(SearchStats stats, QueryBuilder q) { |
| 524 | + if (q == null || q instanceof MatchAllQueryBuilder || q instanceof MatchNoneQueryBuilder) { |
| 525 | + return 0; |
| 526 | + } |
| 527 | + if (q instanceof WildcardQueryBuilder |
| 528 | + || q instanceof RegexpQueryBuilder |
| 529 | + || q instanceof PrefixQueryBuilder |
| 530 | + || q instanceof FuzzyQueryBuilder) { |
| 531 | + return 5; |
| 532 | + } |
| 533 | + if (q instanceof RangeQueryBuilder r) { |
| 534 | + // with points count 1, without count 3 |
| 535 | + return stats.min(new FieldAttribute.FieldName(r.fieldName())) != null ? 1 : 3; |
| 536 | + } |
| 537 | + if (q instanceof MultiTermQueryBuilder) { |
| 538 | + return 3; |
| 539 | + } |
| 540 | + if (q instanceof TermsQueryBuilder terms && terms.values() != null) { |
| 541 | + return terms.values().size(); |
| 542 | + } |
| 543 | + if (q instanceof SingleValueQuery.Builder b) { |
| 544 | + // ignore the single_value clause |
| 545 | + return Math.max(1, estimateQueryClauses(stats, b.next())); |
| 546 | + } |
| 547 | + if (q instanceof BoolQueryBuilder bq) { |
| 548 | + int total = 0; |
| 549 | + for (var c : bq.filter()) { |
| 550 | + total += estimateQueryClauses(stats, c); |
| 551 | + } |
| 552 | + for (var c : bq.must()) { |
| 553 | + total += estimateQueryClauses(stats, c); |
| 554 | + } |
| 555 | + for (var c : bq.should()) { |
| 556 | + total += estimateQueryClauses(stats, c); |
| 557 | + } |
| 558 | + for (var c : bq.mustNot()) { |
| 559 | + total += Math.max(2, estimateQueryClauses(stats, c)); |
| 560 | + } |
| 561 | + return total; |
| 562 | + } |
| 563 | + return 1; |
| 564 | + } |
488 | 565 | }
|
0 commit comments