-
Notifications
You must be signed in to change notification settings - Fork 25.8k
ES|QL query approximation: move sample correction to data node #144005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jan-elastic
merged 10 commits into
main
from
esql-approximation-move-sample-correction-to-data-node
Mar 13, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1eb413e
looser bounds
jan-elastic ce9cfa8
Move sample correction to data nodes.
jan-elastic 8ae54ec
Buckets equal best estimate for exact counts (-> zero-width CI).
jan-elastic d66448e
update capability
jan-elastic edabb55
ignore test
jan-elastic 7617263
Don't round before divisions.
jan-elastic 58b4ea6
CSV test with approximation=false
jan-elastic 6170d37
looser CSV test bounds
jan-elastic e84f3ae
Fix ApproximationTests
jan-elastic 2bb5e65
small refactor / renaming
jan-elastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
...ompute/src/main/generated-src/org/elasticsearch/compute/aggregation/DoubleArrayState.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
197 changes: 197 additions & 0 deletions
197
...c/main/java/org/elasticsearch/compute/aggregation/CountApproximateAggregatorFunction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.compute.aggregation; | ||
|
|
||
| import org.elasticsearch.compute.data.Block; | ||
| import org.elasticsearch.compute.data.BooleanBlock; | ||
| import org.elasticsearch.compute.data.BooleanVector; | ||
| import org.elasticsearch.compute.data.DoubleBlock; | ||
| import org.elasticsearch.compute.data.DoubleVector; | ||
| import org.elasticsearch.compute.data.ElementType; | ||
| import org.elasticsearch.compute.data.Page; | ||
| import org.elasticsearch.compute.operator.DriverContext; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class CountApproximateAggregatorFunction implements AggregatorFunction { | ||
| private static final List<IntermediateStateDesc> INTERMEDIATE_STATE_DESC = List.of( | ||
| new IntermediateStateDesc("count", ElementType.DOUBLE), | ||
| new IntermediateStateDesc("seen", ElementType.BOOLEAN) | ||
| ); | ||
|
|
||
| public static List<IntermediateStateDesc> intermediateStateDesc() { | ||
| return INTERMEDIATE_STATE_DESC; | ||
| } | ||
|
|
||
| private final DoubleState state; | ||
| private final List<Integer> channels; | ||
| private final boolean countAll; | ||
|
|
||
| public static CountApproximateAggregatorFunction create(List<Integer> inputChannels) { | ||
| return new CountApproximateAggregatorFunction(inputChannels, new DoubleState(0)); | ||
| } | ||
|
|
||
| protected CountApproximateAggregatorFunction(List<Integer> channels, DoubleState state) { | ||
| this.channels = channels; | ||
| this.state = state; | ||
| // no channels specified means count-all/count(*) | ||
| this.countAll = channels.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public int intermediateBlockCount() { | ||
| return intermediateStateDesc().size(); | ||
| } | ||
|
|
||
| private int blockIndex() { | ||
| // In case of countAll, block index is irrelevant. | ||
| // Page.positionCount should be used instead, | ||
| // because the page could have zero blocks | ||
| // (drop all columns scenario) | ||
| return countAll ? -1 : channels.get(0); | ||
| } | ||
|
|
||
| @Override | ||
| public void addRawInput(Page page, BooleanVector mask) { | ||
| if (countAll) { | ||
| // this will work also when the page has no blocks | ||
| if (mask.isConstant() && mask.getBoolean(0)) { | ||
| state.doubleValue(state.doubleValue() + page.getPositionCount()); | ||
| } else { | ||
| int count = 0; | ||
| for (int i = 0; i < mask.getPositionCount(); i++) { | ||
| if (mask.getBoolean(i)) { | ||
| count++; | ||
| } | ||
| } | ||
| state.doubleValue(state.doubleValue() + count); | ||
| } | ||
| } else { | ||
| Block block = page.getBlock(blockIndex()); | ||
| DoubleState state = this.state; | ||
| int count; | ||
| if (mask.isConstant()) { | ||
| if (mask.getBoolean(0) == false) { | ||
| return; | ||
| } | ||
| count = getBlockTotalValueCount(block); | ||
| } else { | ||
| count = countMasked(block, mask); | ||
| } | ||
| state.doubleValue(state.doubleValue() + count); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the number of total values in a block | ||
| * @param block block to count values for | ||
| * @return number of total values present in the block | ||
| */ | ||
| protected int getBlockTotalValueCount(Block block) { | ||
| return block.getTotalValueCount(); | ||
| } | ||
|
|
||
| private int countMasked(Block block, BooleanVector mask) { | ||
| int count = 0; | ||
| if (countAll) { | ||
| for (int p = 0; p < block.getPositionCount(); p++) { | ||
| if (mask.getBoolean(p)) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| for (int p = 0; p < block.getPositionCount(); p++) { | ||
| if (mask.getBoolean(p)) { | ||
| count += getBlockValueCountAtPosition(block, p); | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the number of values at a given position in a block | ||
| * @param block block | ||
| * @param position position to get the number of values | ||
| * @return | ||
| */ | ||
| protected int getBlockValueCountAtPosition(Block block, int position) { | ||
| return block.getValueCount(position); | ||
| } | ||
|
|
||
| @Override | ||
| public void addIntermediateInput(Page page) { | ||
| assert channels.size() == intermediateBlockCount(); | ||
| var blockIndex = blockIndex(); | ||
| assert page.getBlockCount() >= blockIndex + intermediateStateDesc().size(); | ||
| Block uncastBlock = page.getBlock(channels.get(0)); | ||
| if (uncastBlock.areAllValuesNull()) { | ||
| return; | ||
| } | ||
| DoubleVector count = page.<DoubleBlock>getBlock(channels.get(0)).asVector(); | ||
| BooleanVector seen = page.<BooleanBlock>getBlock(channels.get(1)).asVector(); | ||
| assert count.getPositionCount() == 1; | ||
| assert count.getPositionCount() == seen.getPositionCount(); | ||
| state.doubleValue(state.doubleValue() + count.getDouble(0)); | ||
| } | ||
|
|
||
| @Override | ||
| public void evaluateIntermediate(Block[] blocks, int offset, DriverContext driverContext) { | ||
| state.toIntermediate(blocks, offset, driverContext); | ||
| } | ||
|
|
||
| @Override | ||
| public void evaluateFinal(Block[] blocks, int offset, DriverContext driverContext) { | ||
| blocks[offset] = driverContext.blockFactory().newConstantDoubleBlockWith(state.doubleValue(), 1); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append(this.getClass().getSimpleName()).append("["); | ||
| sb.append("channels=").append(channels); | ||
| sb.append("]"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| state.close(); | ||
| } | ||
|
|
||
| public static AggregatorFunctionSupplier supplier() { | ||
| return new CountApproximateAggregatorFunctionSupplier(); | ||
| } | ||
|
|
||
| protected static class CountApproximateAggregatorFunctionSupplier implements AggregatorFunctionSupplier { | ||
| @Override | ||
| public List<IntermediateStateDesc> nonGroupingIntermediateStateDesc() { | ||
| return CountApproximateAggregatorFunction.intermediateStateDesc(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<IntermediateStateDesc> groupingIntermediateStateDesc() { | ||
| return CountApproximateGroupingAggregatorFunction.intermediateStateDesc(); | ||
| } | ||
|
|
||
| @Override | ||
| public AggregatorFunction aggregator(DriverContext driverContext, List<Integer> channels) { | ||
| return CountApproximateAggregatorFunction.create(channels); | ||
| } | ||
|
|
||
| @Override | ||
| public GroupingAggregatorFunction groupingAggregator(DriverContext driverContext, List<Integer> channels) { | ||
| return CountApproximateGroupingAggregatorFunction.create(driverContext, channels); | ||
| } | ||
|
|
||
| @Override | ||
| public String describe() { | ||
| return "count"; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically
CountAggregatorFunctionwithLong->Double.Do we need to prevent this code duplication somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion, apart from
countMasked()andblockIndex()the rest of the code looks the same but the types are different, so we won't save much with a refactoringThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll leave it like this. The one thing I can see doing is generating both from a StringTemplate file, like some other classes are.