Skip to content

Conversation

smalyshev
Copy link
Contributor

@smalyshev smalyshev commented Sep 19, 2025

Remote ENRICH (and any remote operation in fact) is not compatible with MV_EXPAND + LIMIT. Consider:

FROM *:events | SORT @timestamp | LIMIT 2 | MV_EXPAND ip | ENRICH _remote:clientip_policy ON ip

Semantically, this must take two top events and then expand them. However, this can not be executed remotely, because this means that we have to take top 2 events on each node, then expand them, then apply Enrich, then bring them to the coordinator - but then we can not select top 2 of them - because that would be pre-expand! We do not know which expanded rows are coming from the true top rows and which are coming from "false" top rows which should have been thrown out. This is only possible to execute if MV_EXPAND executes on the coordinator - which contradicts remote Enrich.

With current hack it would silently return wrong data (as it would apply LIMIT after joining remote data without caring for MvExpand) but even if we fix the hack I don't think it can be semantically executed, at least without subplans.

The same problem would happen with remote join - except the limits are already banned before lookup join, so we're good there. And the same probably would happen for any other expanding operation - but I think joins and MV_EXPAND are the only ones that exist right now.

@elasticsearchmachine
Copy link
Collaborator

Hi @smalyshev, I've created a changelog YAML for you.

@smalyshev smalyshev marked this pull request as ready for review September 19, 2025 13:57
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Sep 19, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

Copy link
Contributor

@luigidellaquila luigidellaquila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @smalyshev, the implementation looks correct for this specific case.
I just left a comment about validation in general, and on other cases we could be missing, see below

private void checkMvExpandAfterLimit(Failures failures) {
this.forEachDown(MvExpand.class, u -> {
u.forEachDown(p -> {
if (p instanceof Limit || p instanceof TopN) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the logic for JOIN is a bit different; in particular, post optimization, it also checks for the presence of a PipelineBreaker, while ENRICH only checks for ExecutesOn.
Do you think it makes sense to unify the two, or at least to make these two checks consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Join and Enrich are different, as Enrich is cardinality-preserving while Join is not. That makes some pipeline breakers compatible with Enrich but not with Join. I agree that PipelineBreaker usage is not ideal there are it's not exactly meant for that, and in the future we may change that to refine the meanings of each, but Enrich and Join will probably stay different. Unless we move to handling them with subplans which would resolve the cardinality problem (not for free, of course). For now I think PipelineBreaker is a good stand-in for what we need, but longer term it probably will need to be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also the reason for this particular change, btw - MV_EXPAND changes cardinality, which leads Enrich to essentially have the same issue that remote JOIN has from the start - the order of LIMIT and cardinality-changing operation comes out wrong, as semantically we expect that the LIMIT is global over all the dataset, but in reality we only do it per-node and delay the global one until we're back at the coordinator. This only works if none of the operations in between is cardinality-changing.

Copy link
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice catch!

pr: 135051
summary: Ban Limit + `MvExpand` before remote Enrich
area: ES|QL
type: enhancement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe that's more of a bug fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess though we don't have a bug filed for it...

@Override
public void postAnalysisVerification(Failures failures) {
if (this.mode == Mode.REMOTE) {
checkMvExpandAfterLimit(failures);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: since this triggers after analysis, the condition p instanceof TopN (while correct) will never be true - we don't create TopN nodes during analysis, only OrderBy nodes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have to do it on analysis stage to avoid confusion with synthetic limits that are pushed down, but I wasn't sure if there's any possible way to have topN on analysis stage.

| LIMIT 2
| eval ip= TO_STR(host)
| MV_EXPAND host
| %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add tests that have random nodes in between the mv expand and the enrich. Or between the limit and the mv expand (although we already have this to some extent).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have any "random harmless commands" code anywhere in the tests? Or just add a couple of fixed ones?

this.forEachDown(MvExpand.class, u -> {
u.forEachDown(p -> {
if (p instanceof Limit || p instanceof TopN) {
failures.add(fail(this, "MV_EXPAND after LIMIT is incompatible with remote ENRICH"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: technically, that's only true if we cannot push the remote enrich past the mv_expand. Which we sometimes could! (There are more optimizations that could be applied to MV_EXPAND, in general.)

But since we currently don't do this, this check will strictly prohibit only queries that we can't properly run anyway, so this is fine.

Maybe we could add a comment, though?

@elasticsearchmachine
Copy link
Collaborator

Hi @smalyshev, I've updated the changelog YAML for you.

@smalyshev
Copy link
Contributor Author

@alex-spies do you think we should backport it to 9.1/8.19?

@smalyshev smalyshev merged commit 7f1d2dc into elastic:main Sep 22, 2025
34 checks passed
@smalyshev smalyshev deleted the remote-enrich-limit branch September 22, 2025 19:29
gmjehovich pushed a commit to gmjehovich/elasticsearch that referenced this pull request Sep 22, 2025
* Ban Limit + MvExpand before remote Enrich
DonalEvans pushed a commit to DonalEvans/elasticsearch that referenced this pull request Sep 22, 2025
* Ban Limit + MvExpand before remote Enrich
smalyshev added a commit to smalyshev/elasticsearch that referenced this pull request Sep 23, 2025
* Ban Limit + MvExpand before remote Enrich

(cherry picked from commit 7f1d2dc)

# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Enrich.java
@smalyshev
Copy link
Contributor Author

💚 All backports created successfully

Status Branch Result
9.1
8.19

Questions ?

Please refer to the Backport tool documentation

smalyshev added a commit to smalyshev/elasticsearch that referenced this pull request Sep 23, 2025
* Ban Limit + MvExpand before remote Enrich

(cherry picked from commit 7f1d2dc)

# Conflicts:
#	x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Enrich.java
smalyshev added a commit that referenced this pull request Sep 25, 2025
* Ban Limit + MvExpand before remote Enrich (#135051)

* Ban Limit + MvExpand before remote Enrich

(cherry picked from commit 7f1d2dc)
smalyshev added a commit that referenced this pull request Sep 25, 2025
* Ban Limit + MvExpand before remote Enrich (#135051)

* Ban Limit + MvExpand before remote Enrich

(cherry picked from commit 7f1d2dc)
smalyshev added a commit to smalyshev/elasticsearch that referenced this pull request Sep 25, 2025
smalyshev added a commit that referenced this pull request Sep 29, 2025
smalyshev added a commit to smalyshev/elasticsearch that referenced this pull request Sep 29, 2025
smalyshev added a commit to smalyshev/elasticsearch that referenced this pull request Sep 29, 2025
elasticsearchmachine pushed a commit that referenced this pull request Sep 29, 2025
elasticsearchmachine pushed a commit that referenced this pull request Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants