Skip to content

Conversation

kanoshiou
Copy link
Contributor

@kanoshiou kanoshiou commented Oct 13, 2025

When the optimizer creates a LocalRelation using LocalSupplier.of() (which delegates to ImmediateLocalSupplier), the same page instance can be referenced and released multiple times during inline stats execution. This happens because the LocalRelation serves as a source for both sides of the InlineJoin - once for the left-hand side and again for the right-hand side, causing a double release error and connection closure.

This PR introduces a MaterializeInlineJoinData optimizer rule that runs in the cleanup phase and transforms any LocalRelation nodes on the InlineJoin left-hand side to use CopyingLocalSupplier if they don't already.

Closes #135679

- Replace `LocalSupplier.of` with new `CopyingLocalSupplier` in `PruneColumns` - Add `INLINE_STATS_AFTER_PRUNED_AGGREGATE` capability - Add test cases for inline stats after aggregate pruning
@elasticsearchmachine elasticsearchmachine added needs:triage Requires assignment of a team area label v9.3.0 external-contributor Pull request authored by a developer outside the Elasticsearch team labels Oct 13, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Oct 13, 2025
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine elasticsearchmachine removed the needs:triage Requires assignment of a team area label label Oct 13, 2025
@bpintea bpintea requested review from astefan and bpintea October 13, 2025 11:23
@kanoshiou kanoshiou changed the title ESQL: Fix connection closed in inline stats after pruned aggregates ESQL: Fix double release in inline stats when LocalRelation is reused Oct 14, 2025
@bpintea
Copy link
Contributor

bpintea commented Oct 14, 2025

@kanoshiou thanks for this patch!

I'm however thinking if we shouldn't have another approach here: instead of patching the optimiser rules to use the CopyingLocalSupplier, shouldn't we rather patch theLocalRelation leaf that InlineJoin get to run on the RHS? (Possibly apply the fix only to InlineJoin#firstSubPlan.)
#135679 is INLINE STATS-specific and we could probably contain the fix to it. We could then also check if we'd need a CopyingLocalSupplier (but that's secondary).

@astefan, WDYT?

@astefan
Copy link
Contributor

astefan commented Oct 15, 2025

@kanoshiou thanks for this patch!

I'm however thinking if we shouldn't have another approach here: instead of patching the optimiser rules to use the CopyingLocalSupplier, shouldn't we rather patch theLocalRelation leaf that InlineJoin get to run on the RHS? (Possibly apply the fix only to InlineJoin#firstSubPlan.) #135679 is INLINE STATS-specific and we could probably contain the fix to it. We could then also check if we'd need a CopyingLocalSupplier (but that's secondary).

@astefan, WDYT?

@kanoshiou inline stats is different than any other command in that part of its workings live in EsqlSession. Whenever we add something that is specific to inline stats we try to limit its spread and make it as seamless as possible in the existing code.
This means that modifications that happen outside EsqlSession and InlineJoin itself should be limited. Your changes to optimizer rules that have nothing to do with inline stats (as opposed to PruneInlineJoinOnEmptyRightSide for example which is inline stats specific) should have a very good reason to be there.

Your changes to PruneColumns and PropagateEmptyRelation take the shotgun approach and are too invasive, they will also apply to non-inline stats use cases which is unnecessary. @bpintea's suggestion is something you should look into, instead.

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

Please, look into @bpintea's suggestion for a less invasive and more targeted approach to this bug fix. Please, let us know if you are able to work on this bug fix in short time, otherwise we are ready to take it over. Thank you.

@kanoshiou
Copy link
Contributor Author

Thank you for the review and insightful suggestions, @bpintea @astefan! I'll update the branch today with a new logical optimizer rule.

@kanoshiou kanoshiou requested a review from astefan October 15, 2025 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL external-contributor Pull request authored by a developer outside the Elasticsearch team Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.3.0

5 participants