-
Notifications
You must be signed in to change notification settings - Fork 25.8k
ESQL: Improve Lookup Join performance with CachedDirectoryReader #139314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
julian-elastic
merged 15 commits into
elastic:main
from
julian-elastic:CachedDirectoryReader_v3
Jan 9, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5398571
Improve Lookup Join performance with CachedDirectoryReader
julian-elastic 68b6123
Fix a data issue when a field is used multiple times
julian-elastic 828b757
Refactor CachedDirectoryReader to its own file
julian-elastic cc251a3
Remove termsCache completely
julian-elastic 82e8c70
Update docs/changelog/139314.yaml
julian-elastic 4c364de
Fix Comments
julian-elastic 6364bc3
Merge remote-tracking branch 'upstream/main' into CachedDirectoryRead…
julian-elastic 01ef288
Fix an issue with ParallelCompositeReader
julian-elastic 3cff54f
Merge remote-tracking branch 'upstream/main' into CachedDirectoryRead…
julian-elastic 3b17b03
Address code review comments
julian-elastic 6b78236
Address more code review comments
julian-elastic 7815133
Merge remote-tracking branch 'upstream/main' into CachedDirectoryRead…
julian-elastic e22711b
Always get cost from super.getNumericDocValues(field)
julian-elastic 87d4fc4
Merge remote-tracking branch 'upstream/main' into CachedDirectoryRead…
julian-elastic 04d636b
[CI] Auto commit changes from spotless
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 139314 | ||
| summary: Improve Lookup Join performance with `CachedDirectoryReader` | ||
| area: ES|QL | ||
| type: enhancement | ||
| issues: | ||
| - 137268 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ompute/src/main/java/org/elasticsearch/compute/operator/lookup/CachedDirectoryReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * 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.compute.operator.lookup; | ||
|
|
||
| import org.apache.lucene.index.DirectoryReader; | ||
| import org.apache.lucene.index.FilterDirectoryReader; | ||
| import org.apache.lucene.index.FilterLeafReader; | ||
| import org.apache.lucene.index.LeafReader; | ||
| import org.apache.lucene.index.NumericDocValues; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.IntFunction; | ||
| import java.util.function.LongSupplier; | ||
|
|
||
| /** | ||
| * A DirectoryReader that caches NumericDocValues per field. | ||
| */ | ||
| class CachedDirectoryReader extends FilterDirectoryReader { | ||
| CachedDirectoryReader(DirectoryReader in) throws IOException { | ||
| super(in, new SubReaderWrapper() { | ||
| @Override | ||
| public LeafReader wrap(LeafReader reader) { | ||
| return new CachedLeafReader(reader); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| protected DirectoryReader doWrapDirectoryReader(DirectoryReader in) throws IOException { | ||
| return new CachedDirectoryReader(in); | ||
| } | ||
|
|
||
| @Override | ||
| public CacheHelper getReaderCacheHelper() { | ||
| return in.getReaderCacheHelper(); | ||
| } | ||
|
|
||
| static class CachedLeafReader extends FilterLeafReader { | ||
| final Map<String, NumericDocValues> docValues = new HashMap<>(); | ||
|
|
||
| CachedLeafReader(LeafReader in) { | ||
| super(in); | ||
| } | ||
|
|
||
| @Override | ||
| public NumericDocValues getNumericDocValues(String field) throws IOException { | ||
| NumericDocValues dv = super.getNumericDocValues(field); | ||
| if (dv == null) { | ||
| // It's important to return null here if the field doesn't have doc values - and the only way | ||
| // to get that consistently is to call super.getNumericDocValues. There are other ways to try, | ||
| // but I don't believe they'll work consistently. So that means we prepare the reader each time, | ||
| // but we don't use it. This still is faster than not caching at all. | ||
| return null; | ||
| } | ||
| return new CachedNumericDocValues(dv::cost, docId -> docValues.compute(field, (k, curr) -> { | ||
| if (curr == null || curr.docID() > docId) { | ||
| return dv; | ||
| } | ||
| return curr; | ||
| })); | ||
| } | ||
|
|
||
| @Override | ||
| public CacheHelper getCoreCacheHelper() { | ||
| return in.getCoreCacheHelper(); | ||
| } | ||
|
|
||
| @Override | ||
| public CacheHelper getReaderCacheHelper() { | ||
| return in.getCoreCacheHelper(); | ||
| } | ||
| } | ||
|
|
||
| static class CachedNumericDocValues extends NumericDocValues { | ||
| private NumericDocValues delegate = null; | ||
| private final LongSupplier cost; | ||
| private final IntFunction<NumericDocValues> fromCache; | ||
|
|
||
| CachedNumericDocValues(LongSupplier cost, IntFunction<NumericDocValues> fromCache) { | ||
| this.cost = cost; | ||
| this.fromCache = fromCache; | ||
| } | ||
|
|
||
| NumericDocValues getOrOverwriteDelegate(int docID) { | ||
| if (delegate == null) { | ||
| // This will return the cached delegate if present | ||
| // However, it could return a new one if the current one is ahead of docID | ||
| // Sometimes, we call with -1 docID to specifically request a new one | ||
| delegate = fromCache.apply(docID); | ||
| } | ||
| return delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public long longValue() throws IOException { | ||
| return getOrOverwriteDelegate(-1).longValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean advanceExact(int target) throws IOException { | ||
| return getOrOverwriteDelegate(target).advanceExact(target); | ||
| } | ||
|
|
||
| @Override | ||
| public int advance(int target) throws IOException { | ||
| return getOrOverwriteDelegate(target).advance(target); | ||
| } | ||
|
|
||
| /** | ||
| * If there is a delegate, we will return its docId, | ||
| * otherwise we return -1 to indicate there is no delegate | ||
| */ | ||
| @Override | ||
| public int docID() { | ||
| return delegate == null ? -1 : delegate.docID(); | ||
| } | ||
|
|
||
| @Override | ||
| public int nextDoc() throws IOException { | ||
| return getOrOverwriteDelegate(-1).nextDoc(); | ||
| } | ||
|
|
||
| @Override | ||
| public long cost() { | ||
| return cost.getAsLong(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.