-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
LocalExecutionPlanner.java
796 lines (711 loc) · 36.3 KB
/
LocalExecutionPlanner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.planner;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.iterable.Iterables;
import org.elasticsearch.compute.Describable;
import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.ElementType;
import org.elasticsearch.compute.data.Page;
import org.elasticsearch.compute.lucene.LuceneCountOperator;
import org.elasticsearch.compute.lucene.LuceneOperator;
import org.elasticsearch.compute.operator.ColumnExtractOperator;
import org.elasticsearch.compute.operator.Driver;
import org.elasticsearch.compute.operator.DriverContext;
import org.elasticsearch.compute.operator.EvalOperator.EvalOperatorFactory;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.compute.operator.FilterOperator.FilterOperatorFactory;
import org.elasticsearch.compute.operator.LocalSourceOperator;
import org.elasticsearch.compute.operator.LocalSourceOperator.LocalSourceFactory;
import org.elasticsearch.compute.operator.MvExpandOperator;
import org.elasticsearch.compute.operator.Operator;
import org.elasticsearch.compute.operator.Operator.OperatorFactory;
import org.elasticsearch.compute.operator.OutputOperator.OutputOperatorFactory;
import org.elasticsearch.compute.operator.RowOperator.RowOperatorFactory;
import org.elasticsearch.compute.operator.ShowOperator;
import org.elasticsearch.compute.operator.SinkOperator;
import org.elasticsearch.compute.operator.SinkOperator.SinkOperatorFactory;
import org.elasticsearch.compute.operator.SourceOperator;
import org.elasticsearch.compute.operator.SourceOperator.SourceOperatorFactory;
import org.elasticsearch.compute.operator.StringExtractOperator;
import org.elasticsearch.compute.operator.exchange.ExchangeSinkHandler;
import org.elasticsearch.compute.operator.exchange.ExchangeSinkOperator.ExchangeSinkOperatorFactory;
import org.elasticsearch.compute.operator.exchange.ExchangeSourceHandler;
import org.elasticsearch.compute.operator.exchange.ExchangeSourceOperator.ExchangeSourceOperatorFactory;
import org.elasticsearch.compute.operator.topn.TopNEncoder;
import org.elasticsearch.compute.operator.topn.TopNOperator;
import org.elasticsearch.compute.operator.topn.TopNOperator.TopNOperatorFactory;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
import org.elasticsearch.xpack.esql.enrich.EnrichLookupOperator;
import org.elasticsearch.xpack.esql.enrich.EnrichLookupService;
import org.elasticsearch.xpack.esql.evaluator.EvalMapper;
import org.elasticsearch.xpack.esql.evaluator.command.GrokEvaluatorExtracter;
import org.elasticsearch.xpack.esql.plan.logical.local.LocalSupplier;
import org.elasticsearch.xpack.esql.plan.physical.AggregateExec;
import org.elasticsearch.xpack.esql.plan.physical.DissectExec;
import org.elasticsearch.xpack.esql.plan.physical.EnrichExec;
import org.elasticsearch.xpack.esql.plan.physical.EsQueryExec;
import org.elasticsearch.xpack.esql.plan.physical.EsStatsQueryExec;
import org.elasticsearch.xpack.esql.plan.physical.EvalExec;
import org.elasticsearch.xpack.esql.plan.physical.ExchangeExec;
import org.elasticsearch.xpack.esql.plan.physical.ExchangeSinkExec;
import org.elasticsearch.xpack.esql.plan.physical.ExchangeSourceExec;
import org.elasticsearch.xpack.esql.plan.physical.FieldExtractExec;
import org.elasticsearch.xpack.esql.plan.physical.FilterExec;
import org.elasticsearch.xpack.esql.plan.physical.GrokExec;
import org.elasticsearch.xpack.esql.plan.physical.LimitExec;
import org.elasticsearch.xpack.esql.plan.physical.LocalSourceExec;
import org.elasticsearch.xpack.esql.plan.physical.MvExpandExec;
import org.elasticsearch.xpack.esql.plan.physical.OutputExec;
import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan;
import org.elasticsearch.xpack.esql.plan.physical.ProjectExec;
import org.elasticsearch.xpack.esql.plan.physical.RowExec;
import org.elasticsearch.xpack.esql.plan.physical.ShowExec;
import org.elasticsearch.xpack.esql.plan.physical.TopNExec;
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
import org.elasticsearch.xpack.esql.session.EsqlConfiguration;
import org.elasticsearch.xpack.ql.expression.Alias;
import org.elasticsearch.xpack.ql.expression.Attribute;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Expressions;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.NameId;
import org.elasticsearch.xpack.ql.expression.NamedExpression;
import org.elasticsearch.xpack.ql.expression.Order;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.util.Holder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
import static org.elasticsearch.compute.lucene.LuceneOperator.NO_LIMIT;
import static org.elasticsearch.compute.operator.LimitOperator.Factory;
import static org.elasticsearch.compute.operator.ProjectOperator.ProjectOperatorFactory;
/**
* The local execution planner takes a plan (represented as PlanNode tree / digraph) as input and creates the corresponding
* drivers that are used to execute the given plan.
*/
public class LocalExecutionPlanner {
private static final Logger logger = LogManager.getLogger(LocalExecutionPlanner.class);
private final String sessionId;
private final CancellableTask parentTask;
private final BigArrays bigArrays;
private final BlockFactory blockFactory;
private final EsqlConfiguration configuration;
private final ExchangeSourceHandler exchangeSourceHandler;
private final ExchangeSinkHandler exchangeSinkHandler;
private final EnrichLookupService enrichLookupService;
private final PhysicalOperationProviders physicalOperationProviders;
public LocalExecutionPlanner(
String sessionId,
CancellableTask parentTask,
BigArrays bigArrays,
BlockFactory blockFactory,
EsqlConfiguration configuration,
ExchangeSourceHandler exchangeSourceHandler,
ExchangeSinkHandler exchangeSinkHandler,
EnrichLookupService enrichLookupService,
PhysicalOperationProviders physicalOperationProviders
) {
this.sessionId = sessionId;
this.parentTask = parentTask;
this.bigArrays = bigArrays;
this.blockFactory = blockFactory;
this.exchangeSourceHandler = exchangeSourceHandler;
this.exchangeSinkHandler = exchangeSinkHandler;
this.enrichLookupService = enrichLookupService;
this.physicalOperationProviders = physicalOperationProviders;
this.configuration = configuration;
}
/**
* turn the given plan into a list of drivers to execute
*/
public LocalExecutionPlan plan(PhysicalPlan node) {
var context = new LocalExecutionPlannerContext(
new ArrayList<>(),
new Holder<>(DriverParallelism.SINGLE),
configuration.pragmas(),
bigArrays,
blockFactory
);
// workaround for https://github.com/elastic/elasticsearch/issues/99782
node = node.transformUp(
AggregateExec.class,
a -> a.getMode() == AggregateExec.Mode.FINAL ? new ProjectExec(a.source(), a, Expressions.asAttributes(a.aggregates())) : a
);
PhysicalOperation physicalOperation = plan(node, context);
context.addDriverFactory(
new DriverFactory(
new DriverSupplier(context.bigArrays, context.blockFactory, physicalOperation, configuration.pragmas().statusInterval()),
context.driverParallelism().get()
)
);
return new LocalExecutionPlan(context.driverFactories);
}
private PhysicalOperation plan(PhysicalPlan node, LocalExecutionPlannerContext context) {
if (node instanceof AggregateExec aggregate) {
return planAggregation(aggregate, context);
} else if (node instanceof FieldExtractExec fieldExtractExec) {
return planFieldExtractNode(context, fieldExtractExec);
} else if (node instanceof ExchangeExec exchangeExec) {
return planExchange(exchangeExec, context);
} else if (node instanceof TopNExec topNExec) {
return planTopN(topNExec, context);
} else if (node instanceof EvalExec eval) {
return planEval(eval, context);
} else if (node instanceof DissectExec dissect) {
return planDissect(dissect, context);
} else if (node instanceof GrokExec grok) {
return planGrok(grok, context);
} else if (node instanceof ProjectExec project) {
return planProject(project, context);
} else if (node instanceof FilterExec filter) {
return planFilter(filter, context);
} else if (node instanceof LimitExec limit) {
return planLimit(limit, context);
} else if (node instanceof MvExpandExec mvExpand) {
return planMvExpand(mvExpand, context);
}
// source nodes
else if (node instanceof EsQueryExec esQuery) {
return planEsQueryNode(esQuery, context);
} else if (node instanceof EsStatsQueryExec statsQuery) {
return planEsStats(statsQuery, context);
} else if (node instanceof RowExec row) {
return planRow(row, context);
} else if (node instanceof LocalSourceExec localSource) {
return planLocal(localSource, context);
} else if (node instanceof ShowExec show) {
return planShow(show);
} else if (node instanceof ExchangeSourceExec exchangeSource) {
return planExchangeSource(exchangeSource, context);
}
// lookups and joins
else if (node instanceof EnrichExec enrich) {
return planEnrich(enrich, context);
}
// output
else if (node instanceof OutputExec outputExec) {
return planOutput(outputExec, context);
} else if (node instanceof ExchangeSinkExec exchangeSink) {
return planExchangeSink(exchangeSink, context);
}
throw new EsqlIllegalArgumentException("unknown physical plan node [" + node.nodeName() + "]");
}
private PhysicalOperation planAggregation(AggregateExec aggregate, LocalExecutionPlannerContext context) {
var source = plan(aggregate.child(), context);
return physicalOperationProviders.groupingPhysicalOperation(aggregate, source, context);
}
private PhysicalOperation planEsQueryNode(EsQueryExec esQueryExec, LocalExecutionPlannerContext context) {
return physicalOperationProviders.sourcePhysicalOperation(esQueryExec, context);
}
private PhysicalOperation planEsStats(EsStatsQueryExec statsQuery, LocalExecutionPlannerContext context) {
if (physicalOperationProviders instanceof EsPhysicalOperationProviders == false) {
throw new EsqlIllegalArgumentException("EsStatsQuery should only occur against a Lucene backend");
}
if (statsQuery.stats().size() > 1) {
throw new EsqlIllegalArgumentException("EsStatsQuery currently supports only one field statistic");
}
// for now only one stat is supported
EsStatsQueryExec.Stat stat = statsQuery.stats().get(0);
EsPhysicalOperationProviders esProvider = (EsPhysicalOperationProviders) physicalOperationProviders;
Function<SearchContext, Query> querySupplier = EsPhysicalOperationProviders.querySupplier(stat.filter(statsQuery.query()));
Expression limitExp = statsQuery.limit();
int limit = limitExp != null ? (Integer) limitExp.fold() : NO_LIMIT;
final LuceneOperator.Factory luceneFactory = new LuceneCountOperator.Factory(
esProvider.searchContexts(),
querySupplier,
context.queryPragmas.dataPartitioning(),
context.queryPragmas.taskConcurrency(),
limit
);
Layout.Builder layout = new Layout.Builder();
layout.append(statsQuery.outputSet());
int instanceCount = Math.max(1, luceneFactory.taskConcurrency());
context.driverParallelism(new DriverParallelism(DriverParallelism.Type.DATA_PARALLELISM, instanceCount));
return PhysicalOperation.fromSource(luceneFactory, layout.build());
}
private PhysicalOperation planFieldExtractNode(LocalExecutionPlannerContext context, FieldExtractExec fieldExtractExec) {
return physicalOperationProviders.fieldExtractPhysicalOperation(fieldExtractExec, plan(fieldExtractExec.child(), context));
}
private PhysicalOperation planOutput(OutputExec outputExec, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(outputExec.child(), context);
var output = outputExec.output();
return source.withSink(
new OutputOperatorFactory(
Expressions.names(output),
alignPageToAttributes(output, source.layout),
outputExec.getPageConsumer()
),
source.layout
);
}
private static Function<Page, Page> alignPageToAttributes(List<Attribute> attrs, Layout layout) {
// align the page layout with the operator output
// extraction order - the list ordinal is the same as the column one
// while the value represents the position in the original page
final int[] mappedPosition = new int[attrs.size()];
int index = -1;
boolean transformRequired = false;
for (var attribute : attrs) {
mappedPosition[++index] = layout.get(attribute.id()).channel();
transformRequired |= mappedPosition[index] != index;
}
Function<Page, Page> transformer = transformRequired ? p -> {
var blocks = new Block[mappedPosition.length];
for (int i = 0; i < blocks.length; i++) {
blocks[i] = p.getBlock(mappedPosition[i]);
blocks[i].incRef();
}
p.releaseBlocks();
return new Page(blocks);
} : Function.identity();
return transformer;
}
private PhysicalOperation planExchange(ExchangeExec exchangeExec, LocalExecutionPlannerContext context) {
throw new UnsupportedOperationException("Exchange needs to be replaced with a sink/source");
}
private PhysicalOperation planExchangeSink(ExchangeSinkExec exchangeSink, LocalExecutionPlannerContext context) {
Objects.requireNonNull(exchangeSinkHandler, "ExchangeSinkHandler wasn't provided");
var child = exchangeSink.child();
// see https://github.com/elastic/elasticsearch/issues/100807 - handle case where the plan has been fully minimized
// to a local relation and the aggregate intermediate data erased. For this scenario, match the output the exchange output
// with that of the local relation
if (child instanceof LocalSourceExec localExec) {
var output = exchangeSink.output();
var localOutput = localExec.output();
if (output.equals(localOutput) == false) {
// the outputs are going to be similar except for the bool "seen" flags which are added in below
List<Block> blocks = new ArrayList<>(asList(localExec.supplier().get()));
if (blocks.size() > 0) {
for (int i = 0, s = output.size(); i < s; i++) {
var out = output.get(i);
if (out.dataType() == DataTypes.BOOLEAN) {
blocks.add(i, BlockFactory.getNonBreakingInstance().newConstantBooleanBlockWith(true, 1));
}
}
}
var newSupplier = LocalSupplier.of(blocks.toArray(Block[]::new));
child = new LocalSourceExec(localExec.source(), output, newSupplier);
}
}
PhysicalOperation source = plan(child, context);
Function<Page, Page> transformer = exchangeSink.isIntermediateAgg()
? Function.identity()
: alignPageToAttributes(exchangeSink.output(), source.layout);
return source.withSink(new ExchangeSinkOperatorFactory(exchangeSinkHandler::createExchangeSink, transformer), source.layout);
}
private PhysicalOperation planExchangeSource(ExchangeSourceExec exchangeSource, LocalExecutionPlannerContext context) {
Objects.requireNonNull(exchangeSourceHandler, "ExchangeSourceHandler wasn't provided");
var builder = new Layout.Builder();
builder.append(exchangeSource.output());
// decorate the layout
var l = builder.build();
var layout = exchangeSource.isIntermediateAgg() ? new ExchangeLayout(l) : l;
return PhysicalOperation.fromSource(new ExchangeSourceOperatorFactory(exchangeSourceHandler::createExchangeSource), layout);
}
private PhysicalOperation planTopN(TopNExec topNExec, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(topNExec.child(), context);
ElementType[] elementTypes = new ElementType[source.layout.numberOfChannels()];
TopNEncoder[] encoders = new TopNEncoder[source.layout.numberOfChannels()];
List<Layout.ChannelSet> inverse = source.layout.inverse();
for (int channel = 0; channel < inverse.size(); channel++) {
elementTypes[channel] = PlannerUtils.toElementType(inverse.get(channel).type());
encoders[channel] = switch (inverse.get(channel).type().typeName()) {
case "ip" -> TopNEncoder.IP;
case "text", "keyword" -> TopNEncoder.UTF8;
case "version" -> TopNEncoder.VERSION;
case "boolean", "null", "byte", "short", "integer", "long", "double", "float", "half_float", "datetime", "date_period",
"time_duration", "object", "nested", "scaled_float", "unsigned_long", "_doc", "geo_point", "cartesian_point" ->
TopNEncoder.DEFAULT_SORTABLE;
// unsupported fields are encoded as BytesRef, we'll use the same encoder; all values should be null at this point
case "unsupported" -> TopNEncoder.UNSUPPORTED;
default -> throw new EsqlIllegalArgumentException("No TopN sorting encoder for type " + inverse.get(channel).type());
};
}
List<TopNOperator.SortOrder> orders = topNExec.order().stream().map(order -> {
int sortByChannel;
if (order.child() instanceof Attribute a) {
sortByChannel = source.layout.get(a.id()).channel();
} else {
throw new EsqlIllegalArgumentException("order by expression must be an attribute");
}
return new TopNOperator.SortOrder(
sortByChannel,
order.direction().equals(Order.OrderDirection.ASC),
order.nullsPosition().equals(Order.NullsPosition.FIRST)
);
}).toList();
int limit;
if (topNExec.limit() instanceof Literal literal) {
limit = Integer.parseInt(literal.value().toString());
} else {
throw new EsqlIllegalArgumentException("limit only supported with literal values");
}
// TODO Replace page size with passing estimatedRowSize down
/*
* The 2000 below is a hack to account for incoming size and to make
* sure the estimated row size is never 0 which'd cause a divide by 0.
* But we should replace this with passing the estimate into the real
* topn and letting it actually measure the size of rows it produces.
* That'll be more accurate. And we don't have a path for estimating
* incoming rows. And we don't need one because we can estimate.
*/
return source.with(
new TopNOperatorFactory(
limit,
asList(elementTypes),
asList(encoders),
orders,
context.pageSize(2000 + topNExec.estimatedRowSize())
),
source.layout
);
}
private PhysicalOperation planEval(EvalExec eval, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(eval.child(), context);
for (Alias field : eval.fields()) {
var evaluatorSupplier = EvalMapper.toEvaluator(field.child(), source.layout);
Layout.Builder layout = source.layout.builder();
layout.append(field.toAttribute());
source = source.with(new EvalOperatorFactory(evaluatorSupplier), layout.build());
}
return source;
}
private PhysicalOperation planDissect(DissectExec dissect, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(dissect.child(), context);
Layout.Builder layoutBuilder = source.layout.builder();
layoutBuilder.append(dissect.extractedFields());
final Expression expr = dissect.inputExpression();
String[] attributeNames = Expressions.names(dissect.extractedFields()).toArray(new String[0]);
Layout layout = layoutBuilder.build();
source = source.with(
new StringExtractOperator.StringExtractOperatorFactory(
attributeNames,
EvalMapper.toEvaluator(expr, layout),
() -> (input) -> dissect.parser().parser().parse(input)
),
layout
);
return source;
}
private PhysicalOperation planGrok(GrokExec grok, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(grok.child(), context);
Layout.Builder layoutBuilder = source.layout.builder();
List<Attribute> extractedFields = grok.extractedFields();
layoutBuilder.append(extractedFields);
Map<String, Integer> fieldToPos = new HashMap<>(extractedFields.size());
Map<String, ElementType> fieldToType = new HashMap<>(extractedFields.size());
ElementType[] types = new ElementType[extractedFields.size()];
for (int i = 0; i < extractedFields.size(); i++) {
Attribute extractedField = extractedFields.get(i);
ElementType type = PlannerUtils.toElementType(extractedField.dataType());
fieldToPos.put(extractedField.name(), i);
fieldToType.put(extractedField.name(), type);
types[i] = type;
}
Layout layout = layoutBuilder.build();
source = source.with(
new ColumnExtractOperator.Factory(
types,
EvalMapper.toEvaluator(grok.inputExpression(), layout),
() -> new GrokEvaluatorExtracter(grok.pattern().grok(), grok.pattern().pattern(), fieldToPos, fieldToType)
),
layout
);
return source;
}
private PhysicalOperation planEnrich(EnrichExec enrich, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(enrich.child(), context);
Layout.Builder layoutBuilder = source.layout.builder();
layoutBuilder.append(enrich.enrichFields());
Layout layout = layoutBuilder.build();
Set<String> indices = enrich.enrichIndex().concreteIndices();
if (indices.size() != 1) {
throw new EsqlIllegalArgumentException("Resolved enrich should have one concrete index; got " + indices);
}
String enrichIndex = Iterables.get(indices, 0);
return source.with(
new EnrichLookupOperator.Factory(
sessionId,
parentTask,
context.queryPragmas().enrichMaxWorkers(),
source.layout.get(enrich.matchField().id()).channel(),
enrichLookupService,
enrichIndex,
"match", // TODO: enrich should also resolve the match_type
enrich.policyMatchField(),
enrich.enrichFields()
),
layout
);
}
private ExpressionEvaluator.Factory toEvaluator(Expression exp, Layout layout) {
return EvalMapper.toEvaluator(exp, layout);
}
private PhysicalOperation planRow(RowExec row, LocalExecutionPlannerContext context) {
List<Object> obj = row.fields().stream().map(f -> f.child().fold()).toList();
Layout.Builder layout = new Layout.Builder();
layout.append(row.output());
return PhysicalOperation.fromSource(new RowOperatorFactory(obj), layout.build());
}
private PhysicalOperation planLocal(LocalSourceExec localSourceExec, LocalExecutionPlannerContext context) {
Layout.Builder layout = new Layout.Builder();
layout.append(localSourceExec.output());
LocalSourceOperator.BlockSupplier supplier = () -> localSourceExec.supplier().get();
var operator = new LocalSourceOperator(supplier);
return PhysicalOperation.fromSource(new LocalSourceFactory(() -> operator), layout.build());
}
private PhysicalOperation planShow(ShowExec showExec) {
Layout.Builder layout = new Layout.Builder();
layout.append(showExec.output());
return PhysicalOperation.fromSource(new ShowOperator.ShowOperatorFactory(showExec.values()), layout.build());
}
private PhysicalOperation planProject(ProjectExec project, LocalExecutionPlannerContext context) {
var source = plan(project.child(), context);
List<? extends NamedExpression> projections = project.projections();
List<Integer> projectionList = new ArrayList<>(projections.size());
Layout.Builder layout = new Layout.Builder();
Map<Integer, Layout.ChannelSet> inputChannelToOutputIds = new HashMap<>();
for (int index = 0, size = projections.size(); index < size; index++) {
NamedExpression ne = projections.get(index);
NameId inputId;
if (ne instanceof Alias a) {
inputId = ((NamedExpression) a.child()).id();
} else {
inputId = ne.id();
}
Layout.ChannelAndType input = source.layout.get(inputId);
Layout.ChannelSet channelSet = inputChannelToOutputIds.get(input.channel());
if (channelSet == null) {
channelSet = new Layout.ChannelSet(new HashSet<>(), input.type());
channelSet.nameIds().add(ne.id());
layout.append(channelSet);
} else {
channelSet.nameIds().add(ne.id());
}
if (channelSet.type() != input.type()) {
throw new IllegalArgumentException("type mismatch for aliases");
}
projectionList.add(input.channel());
}
return source.with(new ProjectOperatorFactory(projectionList), layout.build());
}
private PhysicalOperation planFilter(FilterExec filter, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(filter.child(), context);
// TODO: should this be extracted into a separate eval block?
return source.with(new FilterOperatorFactory(toEvaluator(filter.condition(), source.layout)), source.layout);
}
private PhysicalOperation planLimit(LimitExec limit, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(limit.child(), context);
return source.with(new Factory((Integer) limit.limit().fold()), source.layout);
}
private PhysicalOperation planMvExpand(MvExpandExec mvExpandExec, LocalExecutionPlannerContext context) {
PhysicalOperation source = plan(mvExpandExec.child(), context);
int blockSize = 5000;// TODO estimate row size and use context.pageSize()
Layout.Builder layout = source.layout.builder();
layout.replace(mvExpandExec.target().id(), mvExpandExec.expanded().id());
return source.with(
new MvExpandOperator.Factory(source.layout.get(mvExpandExec.target().id()).channel(), blockSize),
layout.build()
);
}
/**
* Immutable physical operation.
*/
public static class PhysicalOperation implements Describable {
final SourceOperatorFactory sourceOperatorFactory;
final List<OperatorFactory> intermediateOperatorFactories;
final SinkOperatorFactory sinkOperatorFactory;
final Layout layout; // maps field names to channels
/** Creates a new physical operation with the given source and layout. */
static PhysicalOperation fromSource(SourceOperatorFactory sourceOperatorFactory, Layout layout) {
return new PhysicalOperation(sourceOperatorFactory, layout);
}
/** Creates a new physical operation from this operation with the given layout. */
PhysicalOperation with(Layout layout) {
return new PhysicalOperation(this, Optional.empty(), Optional.empty(), layout);
}
/** Creates a new physical operation from this operation with the given intermediate operator and layout. */
PhysicalOperation with(OperatorFactory operatorFactory, Layout layout) {
return new PhysicalOperation(this, Optional.of(operatorFactory), Optional.empty(), layout);
}
/** Creates a new physical operation from this operation with the given sink and layout. */
PhysicalOperation withSink(SinkOperatorFactory sink, Layout layout) {
return new PhysicalOperation(this, Optional.empty(), Optional.of(sink), layout);
}
private PhysicalOperation(SourceOperatorFactory sourceOperatorFactory, Layout layout) {
this.sourceOperatorFactory = sourceOperatorFactory;
this.intermediateOperatorFactories = List.of();
this.sinkOperatorFactory = null;
this.layout = layout;
}
private PhysicalOperation(
PhysicalOperation physicalOperation,
Optional<OperatorFactory> intermediateOperatorFactory,
Optional<SinkOperatorFactory> sinkOperatorFactory,
Layout layout
) {
sourceOperatorFactory = physicalOperation.sourceOperatorFactory;
intermediateOperatorFactories = new ArrayList<>();
intermediateOperatorFactories.addAll(physicalOperation.intermediateOperatorFactories);
intermediateOperatorFactory.ifPresent(intermediateOperatorFactories::add);
this.sinkOperatorFactory = sinkOperatorFactory.isPresent() ? sinkOperatorFactory.get() : null;
this.layout = layout;
}
public SourceOperator source(DriverContext driverContext) {
return sourceOperatorFactory.get(driverContext);
}
public void operators(List<Operator> operators, DriverContext driverContext) {
intermediateOperatorFactories.stream().map(opFactory -> opFactory.get(driverContext)).forEach(operators::add);
}
public SinkOperator sink(DriverContext driverContext) {
return sinkOperatorFactory.get(driverContext);
}
@Override
public String describe() {
return Stream.concat(
Stream.concat(Stream.of(sourceOperatorFactory), intermediateOperatorFactories.stream()),
Stream.of(sinkOperatorFactory)
).map(Describable::describe).collect(joining("\n\\_", "\\_", ""));
}
}
/**
* The count and type of driver parallelism.
*/
record DriverParallelism(Type type, int instanceCount) {
DriverParallelism {
if (instanceCount <= 0) {
throw new IllegalArgumentException("instance count must be greater than zero; got: " + instanceCount);
}
}
static final DriverParallelism SINGLE = new DriverParallelism(Type.SINGLETON, 1);
enum Type {
SINGLETON,
DATA_PARALLELISM,
TASK_LEVEL_PARALLELISM
}
}
/**
* Context object used while generating a local plan. Currently only collects the driver factories as well as
* maintains information how many driver instances should be created for a given driver.
*/
public record LocalExecutionPlannerContext(
List<DriverFactory> driverFactories,
Holder<DriverParallelism> driverParallelism,
QueryPragmas queryPragmas,
BigArrays bigArrays,
BlockFactory blockFactory
) {
void addDriverFactory(DriverFactory driverFactory) {
driverFactories.add(driverFactory);
}
void driverParallelism(DriverParallelism parallelism) {
driverParallelism.set(parallelism);
}
int pageSize(Integer estimatedRowSize) {
if (estimatedRowSize == null) {
throw new IllegalStateException("estimated row size hasn't been set");
}
if (estimatedRowSize == 0) {
throw new IllegalStateException("estimated row size can't be 0");
}
if (queryPragmas.pageSize() != 0) {
return queryPragmas.pageSize();
}
return Math.max(SourceOperator.MIN_TARGET_PAGE_SIZE, SourceOperator.TARGET_PAGE_SIZE / estimatedRowSize);
}
}
record DriverSupplier(BigArrays bigArrays, BlockFactory blockFactory, PhysicalOperation physicalOperation, TimeValue statusInterval)
implements
Function<String, Driver>,
Describable {
@Override
public Driver apply(String sessionId) {
SourceOperator source = null;
List<Operator> operators = new ArrayList<>();
SinkOperator sink = null;
boolean success = false;
var driverContext = new DriverContext(bigArrays, blockFactory);
try {
source = physicalOperation.source(driverContext);
physicalOperation.operators(operators, driverContext);
sink = physicalOperation.sink(driverContext);
success = true;
return new Driver(sessionId, driverContext, physicalOperation::describe, source, operators, sink, statusInterval, () -> {});
} finally {
if (false == success) {
Releasables.close(source, () -> Releasables.close(operators), sink);
}
}
}
@Override
public String describe() {
return physicalOperation.describe();
}
}
record DriverFactory(DriverSupplier driverSupplier, DriverParallelism driverParallelism) implements Describable {
@Override
public String describe() {
return "DriverFactory(instances = "
+ driverParallelism.instanceCount()
+ ", type = "
+ driverParallelism.type()
+ ")\n"
+ driverSupplier.describe();
}
}
/**
* Plan representation that is geared towards execution on a single node
*/
public static class LocalExecutionPlan implements Describable {
final List<DriverFactory> driverFactories;
LocalExecutionPlan(List<DriverFactory> driverFactories) {
this.driverFactories = driverFactories;
}
public List<Driver> createDrivers(String sessionId) {
List<Driver> drivers = new ArrayList<>();
boolean success = false;
try {
for (DriverFactory df : driverFactories) {
for (int i = 0; i < df.driverParallelism.instanceCount; i++) {
logger.trace("building {} {}", i, df);
drivers.add(df.driverSupplier.apply(sessionId));
}
}
success = true;
return drivers;
} finally {
if (success == false) {
Releasables.close(Releasables.wrap(drivers));
}
}
}
@Override
public String describe() {
StringBuilder sb = new StringBuilder();
sb.append(driverFactories.stream().map(DriverFactory::describe).collect(joining("\n")));
return sb.toString();
}
}
}