Skip to content

Commit d3184ca

Browse files
authored
Speedup lossy sum with constant group (#133779)
If the group is constant, we can compute the sum in a tight loop and update the group's value only once.
1 parent a977c95 commit d3184ca

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

x-pack/plugin/esql/compute/src/main/generated/org/elasticsearch/compute/aggregation/LossySumDoubleGroupingAggregatorFunction.java

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/LossySumDoubleAggregator.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.elasticsearch.compute.data.Block;
1616
import org.elasticsearch.compute.data.BlockFactory;
1717
import org.elasticsearch.compute.data.DoubleBlock;
18+
import org.elasticsearch.compute.data.DoubleVector;
19+
import org.elasticsearch.compute.data.IntArrayBlock;
20+
import org.elasticsearch.compute.data.IntBigArrayBlock;
1821
import org.elasticsearch.compute.data.IntVector;
1922
import org.elasticsearch.compute.operator.DriverContext;
2023
import org.elasticsearch.core.Releasables;
@@ -121,6 +124,43 @@ public static Block evaluateFinal(GroupingSumState state, IntVector selected, Gr
121124
}
122125
}
123126

127+
public static GroupingAggregatorFunction.AddInput wrapAddInput(
128+
GroupingAggregatorFunction.AddInput delegate,
129+
GroupingSumState state,
130+
DoubleVector values
131+
) {
132+
return new GroupingAggregatorFunction.AddInput() {
133+
@Override
134+
public void add(int positionOffset, IntArrayBlock groupIds) {
135+
delegate.add(positionOffset, groupIds);
136+
}
137+
138+
@Override
139+
public void add(int positionOffset, IntBigArrayBlock groupIds) {
140+
delegate.add(positionOffset, groupIds);
141+
}
142+
143+
@Override
144+
public void add(int positionOffset, IntVector groupIds) {
145+
if (groupIds.isConstant()) {
146+
double sum = 0.0;
147+
int positionCount = groupIds.getPositionCount();
148+
for (int i = 0; i < positionCount; i++) {
149+
sum += values.getDouble(i);
150+
}
151+
state.add(sum, groupIds.getInt(0));
152+
} else {
153+
delegate.add(positionOffset, groupIds);
154+
}
155+
}
156+
157+
@Override
158+
public void close() {
159+
Releasables.close(delegate);
160+
}
161+
};
162+
}
163+
124164
static final class SumState implements AggregatorState {
125165
private boolean seen;
126166
double value;
@@ -149,7 +189,7 @@ static final class GroupingSumState extends AbstractArrayState implements Groupi
149189
super(bigArrays);
150190
boolean success = false;
151191
try {
152-
this.values = bigArrays.newDoubleArray(1);
192+
this.values = bigArrays.newDoubleArray(128);
153193
success = true;
154194
} finally {
155195
if (success == false) {

0 commit comments

Comments
 (0)