99
1010import org .elasticsearch .action .ActionListener ;
1111import org .elasticsearch .action .OriginalIndices ;
12- import org .elasticsearch .action .search .ShardSearchFailure ;
1312import org .elasticsearch .action .support .IndicesOptions ;
1413import org .elasticsearch .action .support .SubscribableListener ;
1514import org .elasticsearch .common .Strings ;
1918import org .elasticsearch .compute .data .Page ;
2019import org .elasticsearch .compute .operator .DriverProfile ;
2120import org .elasticsearch .core .Releasables ;
22- import org .elasticsearch .core .TimeValue ;
2321import org .elasticsearch .index .IndexMode ;
2422import org .elasticsearch .index .mapper .IndexModeFieldMapper ;
2523import org .elasticsearch .index .query .BoolQueryBuilder ;
8583import org .elasticsearch .xpack .esql .telemetry .PlanTelemetry ;
8684
8785import java .util .ArrayList ;
88- import java .util .Arrays ;
8986import java .util .HashMap ;
9087import java .util .Iterator ;
9188import java .util .List ;
@@ -365,16 +362,10 @@ public void analyzedPlan(
365362 final List <IndexPattern > indices = preAnalysis .indices ;
366363
367364 EsqlCCSUtils .checkForCcsLicense (executionInfo , indices , indicesExpressionGrouper , configuredClusters , verifier .licenseState ());
368-
369- final Set <String > targetClusters = enrichPolicyResolver .groupIndicesPerCluster (
370- configuredClusters ,
371- indices .stream ()
372- .flatMap (index -> Arrays .stream (Strings .commaDelimitedListToStringArray (index .indexPattern ())))
373- .toArray (String []::new )
374- ).keySet ();
365+ initializeClusterData (indices , executionInfo );
375366
376367 var listener = SubscribableListener .<EnrichResolution >newForked (
377- l -> enrichPolicyResolver .resolvePolicies (targetClusters , unresolvedPolicies , l )
368+ l -> enrichPolicyResolver .resolvePolicies (unresolvedPolicies , executionInfo , l )
378369 )
379370 .<PreAnalysisResult >andThen ((l , enrichResolution ) -> resolveFieldNames (parsed , enrichResolution , l ))
380371 .<PreAnalysisResult >andThen ((l , preAnalysisResult ) -> resolveInferences (preAnalysis .inferencePlans , preAnalysisResult , l ));
@@ -400,12 +391,6 @@ public void analyzedPlan(
400391 }).<PreAnalysisResult >andThen ((l , result ) -> {
401392 assert requestFilter != null : "The second pre-analysis shouldn't take place when there is no index filter in the request" ;
402393
403- // "reset" execution information for all ccs or non-ccs (local) clusters, since we are performing the indices
404- // resolving one more time (the first attempt failed and the query had a filter)
405- for (String clusterAlias : executionInfo .clusterAliases ()) {
406- executionInfo .swapCluster (clusterAlias , (k , v ) -> null );
407- }
408-
409394 // here the requestFilter is set to null, performing the pre-analysis after the first step failed
410395 preAnalyzeMainIndices (preAnalysis , executionInfo , result , null , l );
411396 }).<LogicalPlan >andThen ((l , result ) -> {
@@ -435,6 +420,26 @@ private void preAnalyzeLookupIndex(IndexPattern table, PreAnalysisResult result,
435420 // TODO: Verify that the resolved index actually has indexMode: "lookup"
436421 }
437422
423+ private void initializeClusterData (List <IndexPattern > indices , EsqlExecutionInfo executionInfo ) {
424+ if (indices .isEmpty ()) {
425+ return ;
426+ }
427+ assert indices .size () == 1 : "Only single index pattern is supported" ;
428+ Map <String , OriginalIndices > clusterIndices = indicesExpressionGrouper .groupIndices (
429+ configuredClusters ,
430+ IndicesOptions .DEFAULT ,
431+ indices .getFirst ().indexPattern ()
432+ );
433+ for (Map .Entry <String , OriginalIndices > entry : clusterIndices .entrySet ()) {
434+ final String clusterAlias = entry .getKey ();
435+ String indexExpr = Strings .arrayToCommaDelimitedString (entry .getValue ().indices ());
436+ executionInfo .swapCluster (clusterAlias , (k , v ) -> {
437+ assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet" ;
438+ return new EsqlExecutionInfo .Cluster (clusterAlias , indexExpr , executionInfo .isSkipUnavailable (clusterAlias ));
439+ });
440+ }
441+ }
442+
438443 private void preAnalyzeMainIndices (
439444 PreAnalyzer .PreAnalysis preAnalysis ,
440445 EsqlExecutionInfo executionInfo ,
@@ -448,38 +453,8 @@ private void preAnalyzeMainIndices(
448453 // Note: JOINs are not supported but we detect them when
449454 listener .onFailure (new MappingException ("Queries with multiple indices are not supported" ));
450455 } else if (indices .size () == 1 ) {
451- // known to be unavailable from the enrich policy API call
452- Map <String , Exception > unavailableClusters = result .enrichResolution .getUnavailableClusters ();
453456 IndexPattern table = indices .getFirst ();
454457
455- Map <String , OriginalIndices > clusterIndices = indicesExpressionGrouper .groupIndices (
456- configuredClusters ,
457- IndicesOptions .DEFAULT ,
458- table .indexPattern ()
459- );
460- for (Map .Entry <String , OriginalIndices > entry : clusterIndices .entrySet ()) {
461- final String clusterAlias = entry .getKey ();
462- String indexExpr = Strings .arrayToCommaDelimitedString (entry .getValue ().indices ());
463- executionInfo .swapCluster (clusterAlias , (k , v ) -> {
464- assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet" ;
465- if (unavailableClusters .containsKey (k )) {
466- return new EsqlExecutionInfo .Cluster (
467- clusterAlias ,
468- indexExpr ,
469- executionInfo .isSkipUnavailable (clusterAlias ),
470- EsqlExecutionInfo .Cluster .Status .SKIPPED ,
471- 0 ,
472- 0 ,
473- 0 ,
474- 0 ,
475- List .of (new ShardSearchFailure (unavailableClusters .get (k ))),
476- new TimeValue (0 )
477- );
478- } else {
479- return new EsqlExecutionInfo .Cluster (clusterAlias , indexExpr , executionInfo .isSkipUnavailable (clusterAlias ));
480- }
481- });
482- }
483458 // if the preceding call to the enrich policy API found unavailable clusters, recreate the index expression to search
484459 // based only on available clusters (which could now be an empty list)
485460 String indexExpressionToResolve = EsqlCCSUtils .createIndexExpressionFromAvailableClusters (executionInfo );
0 commit comments