9
9
10
10
import org .elasticsearch .action .ActionListener ;
11
11
import org .elasticsearch .action .OriginalIndices ;
12
- import org .elasticsearch .action .search .ShardSearchFailure ;
13
12
import org .elasticsearch .action .support .IndicesOptions ;
14
13
import org .elasticsearch .action .support .SubscribableListener ;
15
14
import org .elasticsearch .common .Strings ;
19
18
import org .elasticsearch .compute .data .Page ;
20
19
import org .elasticsearch .compute .operator .DriverProfile ;
21
20
import org .elasticsearch .core .Releasables ;
22
- import org .elasticsearch .core .TimeValue ;
23
21
import org .elasticsearch .index .IndexMode ;
24
22
import org .elasticsearch .index .query .QueryBuilder ;
25
23
import org .elasticsearch .indices .IndicesExpressionGrouper ;
78
76
import org .elasticsearch .xpack .esql .telemetry .PlanTelemetry ;
79
77
80
78
import java .util .ArrayList ;
81
- import java .util .Arrays ;
82
79
import java .util .HashMap ;
83
80
import java .util .Iterator ;
84
81
import java .util .List ;
@@ -319,16 +316,10 @@ public void analyzedPlan(
319
316
final List <TableInfo > indices = preAnalysis .indices ;
320
317
321
318
EsqlCCSUtils .checkForCcsLicense (executionInfo , indices , indicesExpressionGrouper , configuredClusters , verifier .licenseState ());
322
-
323
- final Set <String > targetClusters = enrichPolicyResolver .groupIndicesPerCluster (
324
- configuredClusters ,
325
- indices .stream ()
326
- .flatMap (t -> Arrays .stream (Strings .commaDelimitedListToStringArray (t .id ().indexPattern ())))
327
- .toArray (String []::new )
328
- ).keySet ();
319
+ initializeClusterData (indices , executionInfo );
329
320
330
321
var listener = SubscribableListener .<EnrichResolution >newForked (
331
- l -> enrichPolicyResolver .resolvePolicies (targetClusters , unresolvedPolicies , l )
322
+ l -> enrichPolicyResolver .resolvePolicies (unresolvedPolicies , executionInfo , l )
332
323
).<PreAnalysisResult >andThen ((l , enrichResolution ) -> resolveFieldNames (parsed , enrichResolution , l ));
333
324
// first resolve the lookup indices, then the main indices
334
325
for (TableInfo lookupIndex : preAnalysis .lookupIndices ) {
@@ -352,12 +343,6 @@ public void analyzedPlan(
352
343
}).<PreAnalysisResult >andThen ((l , result ) -> {
353
344
assert requestFilter != null : "The second pre-analysis shouldn't take place when there is no index filter in the request" ;
354
345
355
- // "reset" execution information for all ccs or non-ccs (local) clusters, since we are performing the indices
356
- // resolving one more time (the first attempt failed and the query had a filter)
357
- for (String clusterAlias : executionInfo .clusterAliases ()) {
358
- executionInfo .swapCluster (clusterAlias , (k , v ) -> null );
359
- }
360
-
361
346
// here the requestFilter is set to null, performing the pre-analysis after the first step failed
362
347
preAnalyzeIndices (preAnalysis .indices , executionInfo , result , null , l );
363
348
}).<LogicalPlan >andThen ((l , result ) -> {
@@ -388,6 +373,26 @@ private void preAnalyzeLookupIndex(TableInfo tableInfo, PreAnalysisResult result
388
373
// TODO: Verify that the resolved index actually has indexMode: "lookup"
389
374
}
390
375
376
+ private void initializeClusterData (List <TableInfo > indices , EsqlExecutionInfo executionInfo ) {
377
+ if (indices .isEmpty ()) {
378
+ return ;
379
+ }
380
+ assert indices .size () == 1 : "Only single index pattern is supported" ;
381
+ Map <String , OriginalIndices > clusterIndices = indicesExpressionGrouper .groupIndices (
382
+ configuredClusters ,
383
+ IndicesOptions .DEFAULT ,
384
+ indices .get (0 ).id ().indexPattern ()
385
+ );
386
+ for (Map .Entry <String , OriginalIndices > entry : clusterIndices .entrySet ()) {
387
+ final String clusterAlias = entry .getKey ();
388
+ String indexExpr = Strings .arrayToCommaDelimitedString (entry .getValue ().indices ());
389
+ executionInfo .swapCluster (clusterAlias , (k , v ) -> {
390
+ assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet" ;
391
+ return new EsqlExecutionInfo .Cluster (clusterAlias , indexExpr , executionInfo .isSkipUnavailable (clusterAlias ));
392
+ });
393
+ }
394
+ }
395
+
391
396
private void preAnalyzeIndices (
392
397
List <TableInfo > indices ,
393
398
EsqlExecutionInfo executionInfo ,
@@ -400,39 +405,9 @@ private void preAnalyzeIndices(
400
405
// Note: JOINs are not supported but we detect them when
401
406
listener .onFailure (new MappingException ("Queries with multiple indices are not supported" ));
402
407
} else if (indices .size () == 1 ) {
403
- // known to be unavailable from the enrich policy API call
404
- Map <String , Exception > unavailableClusters = result .enrichResolution .getUnavailableClusters ();
405
408
TableInfo tableInfo = indices .get (0 );
406
409
IndexPattern table = tableInfo .id ();
407
410
408
- Map <String , OriginalIndices > clusterIndices = indicesExpressionGrouper .groupIndices (
409
- configuredClusters ,
410
- IndicesOptions .DEFAULT ,
411
- table .indexPattern ()
412
- );
413
- for (Map .Entry <String , OriginalIndices > entry : clusterIndices .entrySet ()) {
414
- final String clusterAlias = entry .getKey ();
415
- String indexExpr = Strings .arrayToCommaDelimitedString (entry .getValue ().indices ());
416
- executionInfo .swapCluster (clusterAlias , (k , v ) -> {
417
- assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet" ;
418
- if (unavailableClusters .containsKey (k )) {
419
- return new EsqlExecutionInfo .Cluster (
420
- clusterAlias ,
421
- indexExpr ,
422
- executionInfo .isSkipUnavailable (clusterAlias ),
423
- EsqlExecutionInfo .Cluster .Status .SKIPPED ,
424
- 0 ,
425
- 0 ,
426
- 0 ,
427
- 0 ,
428
- List .of (new ShardSearchFailure (unavailableClusters .get (k ))),
429
- new TimeValue (0 )
430
- );
431
- } else {
432
- return new EsqlExecutionInfo .Cluster (clusterAlias , indexExpr , executionInfo .isSkipUnavailable (clusterAlias ));
433
- }
434
- });
435
- }
436
411
// if the preceding call to the enrich policy API found unavailable clusters, recreate the index expression to search
437
412
// based only on available clusters (which could now be an empty list)
438
413
String indexExpressionToResolve = EsqlCCSUtils .createIndexExpressionFromAvailableClusters (executionInfo );
0 commit comments