Class Approximation

java.lang.Object
org.elasticsearch.xpack.esql.approximation.Approximation
All Implemented Interfaces:
ApproximationDriver

public class Approximation extends Object implements ApproximationDriver
Generates subplans to obtain a good sample probability for approximation and the final main plan.

To obtain an appropriate sample probability, first a target number of rows is set. This is determined via the ApproximationSettings. Next, the total number of rows in the source index is counted via the subplan sourceCountSubPlan(). This plan always executes fast. When there are no commands that can change the number of rows, the sample probability can be directly computed as a ratio of the target number of rows and this total number.

In the presence of commands that can change the number of rows (e.g. filtering), another step is needed. The first goal is to find a sample probability that leads to approximately ROW_COUNT_FOR_COUNT_ESTIMATION rows, and when this probability is found, a sample probability leading to the target number of rows is computed.

This is done by setting the initial sample probability to the ratio of ROW_COUNT_FOR_COUNT_ESTIMATION and the total number of rows in the source index, and a number of rows is sampled with the subplan countSubPlan(double). As long as the sampled number of rows is too small, the probability is increased until a good probability is reached. This final probability is used to compute the probability using for approximating the original query.