99
1010import org .apache .logging .log4j .LogManager ;
1111import org .apache .logging .log4j .Logger ;
12+ import org .elasticsearch .ExceptionsHelper ;
1213import org .elasticsearch .action .ActionListener ;
1314import org .elasticsearch .common .util .set .Sets ;
1415import org .elasticsearch .compute .data .LongBlock ;
@@ -382,11 +383,9 @@ public void onResponse(Result result) {
382383 boolean esStatsQueryExecuted = result .executionInfo () != null
383384 && result .executionInfo ().clusterInfo .values ()
384385 .stream ()
385- .noneMatch (
386- cluster -> cluster .getFailures ().stream ().anyMatch (e -> e .getCause () instanceof UnsupportedOperationException )
387- );
386+ .noneMatch (cluster -> cluster .getFailures ().stream ().anyMatch (Approximate ::isCausedByUnsupported ));
388387 if (esStatsQueryExecuted ) {
389- logger .debug ("not approximating stats query" );
388+ logger .debug ("stats query succeeded; returning exact result " );
390389 listener .onResponse (result );
391390 } else {
392391 result .pages ().forEach (Page ::close );
@@ -396,15 +395,20 @@ public void onResponse(Result result) {
396395
397396 @ Override
398397 public void onFailure (Exception e ) {
399- if (e instanceof UnsupportedOperationException ) {
398+ if (isCausedByUnsupported ( e ) ) {
400399 runner .run (toPhysicalPlan .apply (sourceCountPlan ()), configuration , foldContext , sourceCountListener (listener ));
401400 } else {
401+ logger .debug ("stats query failed; returning error" , e );
402402 listener .onFailure (e );
403403 }
404404 }
405405 };
406406 }
407407
408+ private static boolean isCausedByUnsupported (Exception ex ) {
409+ return ExceptionsHelper .unwrapCausesAndSuppressed (ex , e -> e instanceof UnsupportedOperationException ).isPresent ();
410+ }
411+
408412 /**
409413 * Plan that counts the number of rows in the source index.
410414 * This is the ES|QL query:
0 commit comments