Skip to content

Commit 7284a69

Browse files
authored
[8.x] Do not serialize EsIndex in plan (#119580) (#120496)
Certain plan classes (such as EsRelation, EsSourceExec, EsQueryExec) contain and serialize the entire EsIndex instance. This instance might contain huge mapping that is never used in plan. This change replaces EsIndex usage with indexPattern and indexNameWithModes to minimize the size of the serialized plan.
1 parent ba57820 commit 7284a69

29 files changed

+398
-209
lines changed

docs/changelog/119580.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 119580
2+
summary: Do not serialize `EsIndex` in plan
3+
area: ES|QL
4+
type: enhancement
5+
issues: []

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ static TransportVersion def(int id) {
164164
public static final TransportVersion ESQL_PROFILE_ROWS_PROCESSED = def(8_824_00_0);
165165
public static final TransportVersion BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1 = def(8_825_00_0);
166166
public static final TransportVersion REVERT_BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1 = def(8_826_00_0);
167+
public static final TransportVersion ESQL_SKIP_ES_INDEX_SERIALIZATION = def(8_827_00_0);
167168

168169
/*
169170
* STOP! READ THIS FIRST! No, really,

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/EsqlTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public static Range rangeOf(Expression value, Expression lower, boolean includeL
215215
}
216216

217217
public static EsRelation relation() {
218-
return new EsRelation(EMPTY, new EsIndex(randomAlphaOfLength(8), emptyMap()), IndexMode.STANDARD, randomBoolean());
218+
return new EsRelation(EMPTY, new EsIndex(randomAlphaOfLength(8), emptyMap()), IndexMode.STANDARD);
219219
}
220220

221221
/**

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Analyzer.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,13 @@ private LogicalPlan resolveIndex(UnresolvedRelation plan, IndexResolution indexR
269269
}
270270
var attributes = mappingAsAttributes(plan.source(), esIndex.mapping());
271271
attributes.addAll(plan.metadataFields());
272-
return new EsRelation(plan.source(), esIndex, attributes.isEmpty() ? NO_FIELDS : attributes, plan.indexMode());
272+
return new EsRelation(
273+
plan.source(),
274+
esIndex.name(),
275+
plan.indexMode(),
276+
esIndex.indexNameWithModes(),
277+
attributes.isEmpty() ? NO_FIELDS : attributes
278+
);
273279
}
274280
}
275281

@@ -1371,9 +1377,13 @@ private LogicalPlan doRule(LogicalPlan plan) {
13711377
}
13721378

13731379
if (missing.isEmpty() == false) {
1374-
List<Attribute> newOutput = new ArrayList<>(esr.output());
1375-
newOutput.addAll(missing);
1376-
return new EsRelation(esr.source(), esr.index(), newOutput, esr.indexMode(), esr.frozen());
1380+
return new EsRelation(
1381+
esr.source(),
1382+
esr.indexPattern(),
1383+
esr.indexMode(),
1384+
esr.indexNameWithModes(),
1385+
CollectionUtils.combine(esr.output(), missing)
1386+
);
13771387
}
13781388
return esr;
13791389
});

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/PruneColumns.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ public LogicalPlan apply(LogicalPlan plan) {
102102
p = new Eval(eval.source(), eval.child(), remaining);
103103
}
104104
}
105-
} else if (p instanceof EsRelation esRelation && esRelation.indexMode() == IndexMode.LOOKUP) {
105+
} else if (p instanceof EsRelation esr && esr.indexMode() == IndexMode.LOOKUP) {
106106
// Normally, pruning EsRelation has no effect because InsertFieldExtraction only extracts the required fields, anyway.
107107
// However, InsertFieldExtraction can't be currently used in LOOKUP JOIN right index,
108108
// it works differently as we extract all fields (other than the join key) that the EsRelation has.
109-
var remaining = removeUnused(esRelation.output(), used);
109+
var remaining = removeUnused(esr.output(), used);
110110
if (remaining != null) {
111-
p = new EsRelation(esRelation.source(), esRelation.index(), remaining, esRelation.indexMode(), esRelation.frozen());
111+
p = new EsRelation(esr.source(), esr.indexPattern(), esr.indexMode(), esr.indexNameWithModes(), remaining);
112112
}
113113
}
114114
} while (recheck);

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/SkipQueryOnEmptyMappings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public final class SkipQueryOnEmptyMappings extends OptimizerRules.OptimizerRule
1616

1717
@Override
1818
protected LogicalPlan rule(EsRelation plan) {
19-
return plan.index().concreteIndices().isEmpty() ? new LocalRelation(plan.source(), plan.output(), LocalSupplier.EMPTY) : plan;
19+
return plan.concreteIndices().isEmpty() ? new LocalRelation(plan.source(), plan.output(), LocalSupplier.EMPTY) : plan;
2020
}
2121
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/TranslateMetricsAggregate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private static Aggregate toStandardAggregate(Aggregate metrics) {
220220
if (attributes.stream().noneMatch(a -> a.name().equals(MetadataAttribute.TIMESTAMP_FIELD))) {
221221
attributes.removeIf(a -> a.name().equals(MetadataAttribute.TIMESTAMP_FIELD));
222222
}
223-
return new EsRelation(r.source(), r.index(), new ArrayList<>(attributes), IndexMode.STANDARD);
223+
return new EsRelation(r.source(), r.indexPattern(), IndexMode.STANDARD, r.indexNameWithModes(), new ArrayList<>(attributes));
224224
});
225225
return new Aggregate(metrics.source(), child, Aggregate.AggregateType.STANDARD, metrics.groupings(), metrics.aggregates());
226226
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/physical/local/PushFiltersToSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ private static PhysicalPlan rewrite(
104104
var query = Queries.combine(Queries.Clause.FILTER, asList(queryExec.query(), planQuery));
105105
queryExec = new EsQueryExec(
106106
queryExec.source(),
107-
queryExec.index(),
107+
queryExec.indexPattern(),
108108
queryExec.indexMode(),
109+
queryExec.indexNameWithModes(),
109110
queryExec.output(),
110111
query,
111112
queryExec.limit(),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/physical/local/PushStatsToSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected PhysicalPlan rule(AggregateExec aggregateExec, LocalPhysicalOptimizerC
5959
if (tuple.v2().size() == aggregateExec.aggregates().size()) {
6060
plan = new EsStatsQueryExec(
6161
aggregateExec.source(),
62-
queryExec.index(),
62+
queryExec.indexPattern(),
6363
queryExec.query(),
6464
queryExec.limit(),
6565
tuple.v1(),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/physical/local/ReplaceSourceAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ protected PhysicalPlan rule(EsSourceExec plan) {
5353
attributes.add(ma);
5454
}
5555
});
56-
return new EsQueryExec(plan.source(), plan.index(), plan.indexMode(), attributes, plan.query());
56+
return new EsQueryExec(plan.source(), plan.indexPattern(), plan.indexMode(), plan.indexNameWithModes(), attributes, plan.query());
5757
}
5858
}

0 commit comments

Comments
 (0)