|
18 | 18 | import org.elasticsearch.transport.RemoteClusterAware;
|
19 | 19 | import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
|
20 | 20 | import org.elasticsearch.xpack.esql.capabilities.PostAnalysisPlanVerificationAware;
|
| 21 | +import org.elasticsearch.xpack.esql.capabilities.PostAnalysisVerificationAware; |
21 | 22 | import org.elasticsearch.xpack.esql.capabilities.TelemetryAware;
|
22 | 23 | import org.elasticsearch.xpack.esql.common.Failures;
|
23 | 24 | import org.elasticsearch.xpack.esql.core.capabilities.Resolvables;
|
|
50 | 51 | import static org.elasticsearch.xpack.esql.core.expression.Expressions.asAttributes;
|
51 | 52 | import static org.elasticsearch.xpack.esql.expression.NamedExpressions.mergeOutputAttributes;
|
52 | 53 |
|
53 |
| -public class Enrich extends UnaryPlan implements GeneratingPlan<Enrich>, PostAnalysisPlanVerificationAware, TelemetryAware, SortAgnostic { |
| 54 | +public class Enrich extends UnaryPlan |
| 55 | + implements |
| 56 | + GeneratingPlan<Enrich>, |
| 57 | + PostAnalysisPlanVerificationAware, |
| 58 | + PostAnalysisVerificationAware, |
| 59 | + TelemetryAware, |
| 60 | + SortAgnostic { |
54 | 61 | public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
|
55 | 62 | LogicalPlan.class,
|
56 | 63 | "Enrich",
|
@@ -335,4 +342,34 @@ private static void checkForPlansForbiddenBeforeRemoteEnrich(Enrich enrich, Fail
|
335 | 342 | failures.add(fail(enrich, "ENRICH with remote policy can't be executed after LOOKUP JOIN"));
|
336 | 343 | }
|
337 | 344 | }
|
| 345 | + |
| 346 | + /** |
| 347 | + * Remote ENRICH (and any remote operation in fact) is not compatible with MV_EXPAND + LIMIT. Consider: |
| 348 | + * `FROM *:events | SORT @timestamp | LIMIT 2 | MV_EXPAND ip | ENRICH _remote:clientip_policy ON ip` |
| 349 | + * Semantically, this must take two top events and then expand them. However, this can not be executed remotely, |
| 350 | + * because this means that we have to take top 2 events on each node, then expand them, then apply Enrich, |
| 351 | + * then bring them to the coordinator - but then we can not select top 2 of them - because that would be pre-expand! |
| 352 | + * We do not know which expanded rows are coming from the true top rows and which are coming from "false" top rows |
| 353 | + * which should have been thrown out. This is only possible to execute if MV_EXPAND executes on the coordinator |
| 354 | + * - which contradicts remote Enrich. |
| 355 | + * This could be fixed by the optimizer by moving MV_EXPAND past ENRICH, at least in some cases, but currently we do not do that. |
| 356 | + */ |
| 357 | + private void checkMvExpandAfterLimit(Failures failures) { |
| 358 | + this.forEachDown(MvExpand.class, u -> { |
| 359 | + u.forEachDown(p -> { |
| 360 | + if (p instanceof Limit || p instanceof TopN) { |
| 361 | + failures.add(fail(this, "MV_EXPAND after LIMIT is incompatible with remote ENRICH")); |
| 362 | + } |
| 363 | + }); |
| 364 | + }); |
| 365 | + |
| 366 | + } |
| 367 | + |
| 368 | + @Override |
| 369 | + public void postAnalysisVerification(Failures failures) { |
| 370 | + if (this.mode == Mode.REMOTE) { |
| 371 | + checkMvExpandAfterLimit(failures); |
| 372 | + } |
| 373 | + |
| 374 | + } |
338 | 375 | }
|
0 commit comments