Skip to content

Commit 7f8d913

Browse files
committed
ES|QL: Add PRESENT ES|QL function
- Change intermediate state for using boolean - Add unit tests for PresentAggregatorFunctionTests and PresentGroupingAggregatorFunctionTests Part of #131069
1 parent 06632d3 commit 7f8d913

File tree

11 files changed

+223
-146
lines changed

11 files changed

+223
-146
lines changed

docs/reference/query-languages/esql/_snippets/functions/description/present.md

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

docs/reference/query-languages/esql/_snippets/functions/types/present.md

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

docs/reference/query-languages/esql/kibana/definition/functions/present.json

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

docs/reference/query-languages/esql/kibana/docs/functions/present.md

Lines changed: 1 addition & 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/PresentAggregatorFunction.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.elasticsearch.compute.data.BooleanBlock;
1212
import org.elasticsearch.compute.data.BooleanVector;
1313
import org.elasticsearch.compute.data.ElementType;
14-
import org.elasticsearch.compute.data.IntBlock;
15-
import org.elasticsearch.compute.data.IntVector;
1614
import org.elasticsearch.compute.data.Page;
1715
import org.elasticsearch.compute.operator.DriverContext;
1816

@@ -49,22 +47,22 @@ public String describe() {
4947
}
5048

5149
private static final List<IntermediateStateDesc> INTERMEDIATE_STATE_DESC = List.of(
52-
new IntermediateStateDesc("count", ElementType.INT),
50+
new IntermediateStateDesc("present", ElementType.BOOLEAN),
5351
new IntermediateStateDesc("seen", ElementType.BOOLEAN)
5452
);
5553

5654
public static List<IntermediateStateDesc> intermediateStateDesc() {
5755
return INTERMEDIATE_STATE_DESC;
5856
}
5957

60-
private final IntState state;
58+
private final BooleanState state;
6159
private final List<Integer> channels;
6260

6361
public static PresentAggregatorFunction create(List<Integer> inputChannels) {
64-
return new PresentAggregatorFunction(inputChannels, new IntState(0));
62+
return new PresentAggregatorFunction(inputChannels, new BooleanState(false));
6563
}
6664

67-
private PresentAggregatorFunction(List<Integer> channels, IntState state) {
65+
private PresentAggregatorFunction(List<Integer> channels, BooleanState state) {
6866
this.channels = channels;
6967
this.state = state;
7068
}
@@ -81,26 +79,26 @@ private int blockIndex() {
8179
@Override
8280
public void addRawInput(Page page, BooleanVector mask) {
8381
Block block = page.getBlock(blockIndex());
84-
IntState state = this.state;
85-
int count;
82+
BooleanState state = this.state;
83+
boolean present;
8684
if (mask.isConstant()) {
8785
if (mask.getBoolean(0) == false) {
8886
return;
8987
}
90-
count = block.getTotalValueCount();
88+
present = block.getTotalValueCount() > 0;
9189
} else {
92-
count = countMasked(block, mask);
90+
present = presentMasked(block, mask);
9391
}
94-
state.intValue(Math.min(count, 1));
92+
state.booleanValue(present);
9593
}
9694

97-
private int countMasked(Block block, BooleanVector mask) {
95+
private boolean presentMasked(Block block, BooleanVector mask) {
9896
for (int p = 0; p < block.getPositionCount(); p++) {
9997
if (mask.getBoolean(p)) {
100-
return 1;
98+
return true;
10199
}
102100
}
103-
return 0;
101+
return false;
104102
}
105103

106104
@Override
@@ -112,11 +110,11 @@ public void addIntermediateInput(Page page) {
112110
if (uncastBlock.areAllValuesNull()) {
113111
return;
114112
}
115-
IntVector count = page.<IntBlock>getBlock(channels.get(0)).asVector();
113+
BooleanVector present = page.<BooleanBlock>getBlock(channels.get(0)).asVector();
116114
BooleanVector seen = page.<BooleanBlock>getBlock(channels.get(1)).asVector();
117-
assert count.getPositionCount() == 1;
118-
assert count.getPositionCount() == seen.getPositionCount();
119-
state.intValue(Math.min(state.intValue() + count.getInt(0), 1));
115+
assert present.getPositionCount() == 1;
116+
assert present.getPositionCount() == seen.getPositionCount();
117+
state.booleanValue(state.booleanValue() || present.getBoolean(0));
120118
}
121119

122120
@Override
@@ -126,7 +124,7 @@ public void evaluateIntermediate(Block[] blocks, int offset, DriverContext drive
126124

127125
@Override
128126
public void evaluateFinal(Block[] blocks, int offset, DriverContext driverContext) {
129-
blocks[offset] = driverContext.blockFactory().newConstantIntBlockWith(state.intValue(), 1);
127+
blocks[offset] = driverContext.blockFactory().newConstantBooleanBlockWith(state.booleanValue(), 1);
130128
}
131129

132130
@Override

0 commit comments

Comments
 (0)