Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/123427.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 123427
summary: Reduce iteration complexity for plan traversal
area: ES|QL
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.ElementType;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand Down Expand Up @@ -116,7 +117,7 @@ public static Set<String> planConcreteIndices(PhysicalPlan plan) {
return Set.of();
}
var indices = new LinkedHashSet<String>();
plan.forEachUp(FragmentExec.class, f -> f.fragment().forEachUp(EsRelation.class, r -> indices.addAll(r.index().concreteIndices())));
forEachRelation(plan, relation -> indices.addAll(relation.concreteIndices()));
return indices;
}

Expand All @@ -128,21 +129,30 @@ public static String[] planOriginalIndices(PhysicalPlan plan) {
return Strings.EMPTY_ARRAY;
}
var indices = new LinkedHashSet<String>();
plan.forEachUp(
FragmentExec.class,
f -> f.fragment()
.forEachUp(EsRelation.class, r -> indices.addAll(asList(Strings.commaDelimitedListToStringArray(r.index().name()))))
);
forEachRelation(plan, relation -> indices.addAll(asList(Strings.commaDelimitedListToStringArray(relation.indexPattern()))));
return indices.toArray(String[]::new);
}

public static PhysicalPlan localPlan(List<SearchExecutionContext> searchContexts, Configuration configuration, PhysicalPlan plan) {
return localPlan(configuration, plan, SearchContextStats.from(searchContexts));
private static void forEachRelation(PhysicalPlan plan, Consumer<EsRelation> action) {
plan.forEachDown(FragmentExec.class, f -> f.fragment().forEachDown(EsRelation.class, r -> {
if (r.indexMode() != IndexMode.LOOKUP) {
action.accept(r);
}
}));
}

public static PhysicalPlan localPlan(
List<SearchExecutionContext> searchContexts,
Configuration configuration,
FoldContext foldCtx,
PhysicalPlan plan
) {
return localPlan(configuration, foldCtx, plan, SearchContextStats.from(searchContexts));
}

public static PhysicalPlan localPlan(Configuration configuration, PhysicalPlan plan, SearchStats searchStats) {
final var logicalOptimizer = new LocalLogicalPlanOptimizer(new LocalLogicalOptimizerContext(configuration, searchStats));
var physicalOptimizer = new LocalPhysicalPlanOptimizer(new LocalPhysicalOptimizerContext(configuration, searchStats));
public static PhysicalPlan localPlan(Configuration configuration, FoldContext foldCtx, PhysicalPlan plan, SearchStats searchStats) {
final var logicalOptimizer = new LocalLogicalPlanOptimizer(new LocalLogicalOptimizerContext(configuration, foldCtx, searchStats));
var physicalOptimizer = new LocalPhysicalPlanOptimizer(new LocalPhysicalOptimizerContext(configuration, foldCtx, searchStats));

return localPlan(plan, logicalOptimizer, physicalOptimizer);
}
Expand Down