Skip to content

Commit 55dcb0b

Browse files
committed
ES|QL: Add PRESENT ES|QL function
Optimize AggregatorFunction Part of #131069
1 parent d916601 commit 55dcb0b

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.compute.operator.DriverContext;
1616

1717
import java.util.List;
18-
import java.util.concurrent.atomic.AtomicBoolean;
1918

2019
public class PresentAggregatorFunction implements AggregatorFunction {
2120
public static AggregatorFunctionSupplier supplier() {
@@ -55,14 +54,15 @@ public static List<IntermediateStateDesc> intermediateStateDesc() {
5554
return INTERMEDIATE_STATE_DESC;
5655
}
5756

58-
private final AtomicBoolean state;
5957
private final List<Integer> channels;
6058

59+
private boolean state;
60+
6161
public static PresentAggregatorFunction create(List<Integer> inputChannels) {
62-
return new PresentAggregatorFunction(inputChannels, new AtomicBoolean(false));
62+
return new PresentAggregatorFunction(inputChannels, false);
6363
}
6464

65-
private PresentAggregatorFunction(List<Integer> channels, AtomicBoolean state) {
65+
private PresentAggregatorFunction(List<Integer> channels, boolean state) {
6666
this.channels = channels;
6767
this.state = state;
6868
}
@@ -78,17 +78,10 @@ private int blockIndex() {
7878

7979
@Override
8080
public void addRawInput(Page page, BooleanVector mask) {
81+
if (mask.isConstant() && mask.getBoolean(0) == false) return;
82+
8183
Block block = page.getBlock(blockIndex());
82-
boolean present;
83-
if (mask.isConstant()) {
84-
if (mask.getBoolean(0) == false) {
85-
return;
86-
}
87-
present = block.getTotalValueCount() > 0;
88-
} else {
89-
present = presentMasked(block, mask);
90-
}
91-
this.state.set(present);
84+
this.state = mask.isConstant() ? block.getTotalValueCount() > 0 : presentMasked(block, mask);
9285
}
9386

9487
private boolean presentMasked(Block block, BooleanVector mask) {
@@ -112,7 +105,7 @@ public void addIntermediateInput(Page page) {
112105
BooleanVector present = page.<BooleanBlock>getBlock(channels.get(0)).asVector();
113106
assert present.getPositionCount() == 1;
114107
if (present.getBoolean(0)) {
115-
state.set(true);
108+
this.state = true;
116109
}
117110
}
118111

@@ -123,7 +116,7 @@ public void evaluateIntermediate(Block[] blocks, int offset, DriverContext drive
123116

124117
@Override
125118
public void evaluateFinal(Block[] blocks, int offset, DriverContext driverContext) {
126-
blocks[offset] = driverContext.blockFactory().newConstantBooleanBlockWith(state.get(), 1);
119+
blocks[offset] = driverContext.blockFactory().newConstantBooleanBlockWith(state, 1);
127120
}
128121

129122
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.aggregate;
9+
10+
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests;
11+
12+
import java.io.IOException;
13+
14+
public class PresentSerializationTests extends AbstractExpressionSerializationTests<Present> {
15+
@Override
16+
protected Present createTestInstance() {
17+
return new Present(randomSource(), randomChild());
18+
}
19+
20+
@Override
21+
protected Present mutateInstance(Present instance) throws IOException {
22+
return new Present(instance.source(), randomValueOtherThan(instance.field(), AbstractExpressionSerializationTests::randomChild));
23+
}
24+
}

0 commit comments

Comments
 (0)