@@ -311,7 +311,7 @@ public void testCountPlan_smallDataAfterMvExpanding() throws Exception {
311311 // - one pass to get the number of expanded rows (which determines the sample probability)
312312 // - one pass to execute the original query
313313 assertThat (runner .invocations , hasSize (4 ));
314- assertThat (runner .invocations .get (0 ), allOf (hasMvExpand ("emp_no" )));
314+ assertThat (runner .invocations .get (0 ), allOf (hasMvExpand ("emp_no" ), not ( hasSample ()) ));
315315 assertThat (runner .invocations .get (1 ), allOf (not (hasMvExpand ("emp_no" )), not (hasSample ())));
316316 assertThat (runner .invocations .get (2 ), allOf (hasMvExpand ("emp_no" ), not (hasSample ())));
317317 assertThat (runner .invocations .get (3 ), allOf (hasMvExpand ("emp_no" ), not (hasSample ())));
@@ -327,7 +327,7 @@ public void testCountPlan_largeDataAfterMvExpanding() throws Exception {
327327 // - one pass to get the number of expanded rows (which determines the sample probability)
328328 // - one pass to approximate the query
329329 assertThat (runner .invocations , hasSize (4 ));
330- assertThat (runner .invocations .get (0 ), allOf (hasMvExpand ("emp_no" )));
330+ assertThat (runner .invocations .get (0 ), allOf (hasMvExpand ("emp_no" ), not ( hasSample ()) ));
331331 assertThat (runner .invocations .get (1 ), allOf (not (hasMvExpand ("emp_no" )), not (hasSample ())));
332332 assertThat (runner .invocations .get (2 ), allOf (hasMvExpand ("emp_no" ), not (hasSample ())));
333333 assertThat (runner .invocations .get (3 ), allOf (hasMvExpand ("emp_no" ), hasSample (1e-4 )));
@@ -349,6 +349,37 @@ public void testCountPlan_largeDataBeforeMvExpanding() throws Exception {
349349 assertThat (runner .invocations .get (3 ), allOf (hasMvExpand ("emp_no" ), hasSample (1e-7 )));
350350 }
351351
352+ public void testCountPlan_sampleProbabilityThreshold_noFilter () throws Exception {
353+ TestRunner runner = new TestRunner (500_000 , 500_000 );
354+ Approximate approximate = createApproximate ("FROM test | STATS SUM(emp_no)" , runner );
355+ approximate .approximate (TestRunner .resultCloser );
356+ // This plan needs three passes:
357+ // - one pass to check whether it's an ES stats query (always no for the test runner)
358+ // - one pass to get the total number of rows
359+ // - one pass to execute the original query (because the sample probability is 20%)
360+ assertThat (runner .invocations , hasSize (3 ));
361+ assertThat (runner .invocations .get (0 ), allOf (not (hasSample ())));
362+ assertThat (runner .invocations .get (1 ), allOf (not (hasSample ())));
363+ assertThat (runner .invocations .get (1 ), allOf (not (hasSample ())));
364+ }
365+
366+ public void testCountPlan_sampleProbabilityThreshold_withFilter () throws Exception {
367+ TestRunner runner = new TestRunner (1_000_000_000_000L , 200_000 );
368+ Approximate approximate = createApproximate ("FROM test | WHERE emp_no > 1 | STATS SUM(emp_no)" , runner );
369+ approximate .approximate (TestRunner .resultCloser );
370+ // This plan needs three passes:
371+ // - one pass to check whether it's an ES stats query (always no for the test runner)
372+ // - one pass to get the total number of rows
373+ // - two passes to get the number of filtered rows (which determines the sample probability)
374+ // - one pass to execute the original query (because the sample probability is 50%)
375+ assertThat (runner .invocations , hasSize (5 ));
376+ assertThat (runner .invocations .get (0 ), allOf (not (hasSample ()), hasFilter ("emp_no" )));
377+ assertThat (runner .invocations .get (1 ), allOf (not (hasSample ()), not (hasFilter ("emp_no" ))));
378+ assertThat (runner .invocations .get (2 ), allOf (hasSample (1e-7 ), hasFilter ("emp_no" )));
379+ assertThat (runner .invocations .get (3 ), allOf (hasSample (1e-2 ), hasFilter ("emp_no" )));
380+ assertThat (runner .invocations .get (4 ), allOf (not (hasSample ()), hasFilter ("emp_no" )));
381+ }
382+
352383 public void testApproximatePlan_createsConfidenceInterval_withoutGrouping () throws Exception {
353384 TestRunner runner = new TestRunner (1_000_000_000 , 1_000_000_000 );
354385 Approximate approximate = createApproximate ("FROM test | STATS COUNT(), SUM(emp_no)" , runner );
0 commit comments