Skip to content

Commit ef555a1

Browse files
committed
Do not prune attributes from EsRelation
This can lead to empty output, which leads to the EsRelation being replaced by a LocalRelation with 0 rows.
1 parent f69a231 commit ef555a1

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@ private LogicalPlan missingToNull(LogicalPlan plan, Predicate<FieldAttribute> sh
6767
// Project[field1, field2, field3] <- keeps the ordering intact
6868
// \_Eval[field1 = null, field3 = null]
6969
// \_EsRelation[field2]
70-
// TODO: Double check that we test when there are 0 fields remaining
71-
List<Attribute> initialOutput = relation.output();
72-
List<Attribute> remainingFields = new ArrayList<>(initialOutput.size());
70+
List<Attribute> relationOutput = relation.output();
7371
Map<DataType, Alias> nullLiterals = Maps.newLinkedHashMapWithExpectedSize(DataType.types().size());
74-
List<NamedExpression> newProjections = new ArrayList<>(initialOutput.size());
75-
for (int i = 0, size = initialOutput.size(); i < size; i++) {
76-
Attribute attr = initialOutput.get(i);
72+
List<NamedExpression> newProjections = new ArrayList<>(relationOutput.size());
73+
for (int i = 0, size = relationOutput.size(); i < size; i++) {
74+
Attribute attr = relationOutput.get(i);
7775
NamedExpression projection;
7876
if (attr instanceof FieldAttribute f && (shouldBeRetained.test(f) == false)) {
7977
DataType dt = f.dataType();
@@ -90,18 +88,16 @@ private LogicalPlan missingToNull(LogicalPlan plan, Predicate<FieldAttribute> sh
9088
projection = new Alias(f.source(), f.name(), nullAlias.toAttribute(), f.id());
9189
}
9290
} else {
93-
remainingFields.add(attr);
9491
projection = attr;
9592
}
9693
newProjections.add(projection);
9794
}
9895

99-
if (remainingFields.size() == initialOutput.size()) {
96+
if (nullLiterals.size() == 0) {
10097
return plan;
10198
}
10299

103-
EsRelation newRelation = relation.withAttributes(remainingFields);
104-
Eval eval = new Eval(plan.source(), newRelation, new ArrayList<>(nullLiterals.values()));
100+
Eval eval = new Eval(plan.source(), relation, new ArrayList<>(nullLiterals.values()));
105101
// This projection is redundant if there's another projection downstream (and no commands depend on the order until we hit it).
106102
return new Project(plan.source(), eval, newProjections);
107103
}

0 commit comments

Comments
 (0)