Skip to content

Conversation

benwtrent
Copy link
Member

When using a particular leaf reader (possibly for a MemoryIndex), parent can be null.

Let's verify that and prevent an NPE

Copy link
Contributor

This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.

throws IOException {
// The delegate supports optimistic collection
if (delegate.isOptimistic()) {
if (delegate.isOptimistic() && context.parent != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am assuming the same reasoning applies here as well? // ctx.parent could be null if this is a MemoryIndex. If yes, can be add the comment here as well?

@msokolov
Copy link
Contributor

If this is a real problem, why hasn't it surfaced in unit test failures? Or ... maybe it did?

@benwtrent
Copy link
Member Author

@msokolov I am not sure, I noticed it in ES:

elastic/elasticsearch#136427

Maybe we just exit early there (which I think we will do, e.g. InMemoryIndex just cannot have kNN happen), but it seems to me better to fix it here as well.

@msokolov
Copy link
Contributor

I guess what I'm wondering is does this point a testing gap in Lucene? I thought that we would randomize the index directory type when running tests ...

@benwtrent
Copy link
Member Author

Looking at MemoryIndex:

    @Override
    public void searchNearestVectors(
        String field, float[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) {}

    @Override
    public void searchNearestVectors(
        String field, byte[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) {}

So, returning no hits and forcing exact (or the caller to change), is the way. But we should definitely not throw an NPE. I can add a test really quick to make sure this won't throw an NPE.

@github-actions github-actions bot added this to the 10.4.0 milestone Oct 13, 2025
@benwtrent benwtrent merged commit 8e8e37d into apache:main Oct 14, 2025
12 checks passed
@benwtrent benwtrent deleted the bugfix/handle-npe-optimistic-knn branch October 14, 2025 11:29
benwtrent added a commit that referenced this pull request Oct 14, 2025
* Fix potential NPE for KnnVectorQuery

* adding test

* removing unused test code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants