You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "bugmakerrrrrr (via GitHub)" <gi...@apache.org> on 2024/03/06 16:04:49 UTC
[PR] Fix NPE when LeafReader return null VectorValues [lucene]
bugmakerrrrrr opened a new pull request, #13162:
URL: https://github.com/apache/lucene/pull/13162
### Description
`LeafReader#getXXXVectorValues` may return null value.
**Reproduction**:
```
public class TestKnnByteVectorQuery extends BaseKnnVectorQueryTestCase {
public void testVectorEncodingMismatch() throws IOException {
try (Directory indexStore =
getIndexStore("field", new float[] {0, 1}, new float[] {1, 2}, new float[] {0, 0});
IndexReader reader = DirectoryReader.open(indexStore)) {
AbstractKnnVectorQuery query =
new KnnFloatVectorQuery("field", new float[] {0, 1}, 10);
IndexSearcher searcher = newSearcher(reader);
searcher.search(query, 10);
}
}
}
```
**Output**:
```
java.lang.NullPointerException: Cannot invoke "org.apache.lucene.index.FloatVectorValues.size()" because the return value of "org.apache.lucene.index.LeafReader.getFloatVectorValues(String)" is null
at __randomizedtesting.SeedInfo.seed([A63D89233AB7E539:F77A9739A165CB8D]:0)
at org.apache.lucene.search.KnnFloatVectorQuery.approximateSearch(KnnFloatVectorQuery.java:92)
at org.apache.lucene.search.AbstractKnnVectorQuery.getLeafResults(AbstractKnnVectorQuery.java:120)
at org.apache.lucene.search.AbstractKnnVectorQuery.searchLeaf(AbstractKnnVectorQuery.java:104)
at org.apache.lucene.search.AbstractKnnVectorQuery.lambda$rewrite$0(AbstractKnnVectorQuery.java:89)
at org.apache.lucene.search.TaskExecutor$TaskGroup.lambda$createTask$0(TaskExecutor.java:117)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at org.apache.lucene.search.TaskExecutor$TaskGroup.invokeAll(TaskExecutor.java:152)
at org.apache.lucene.search.TaskExecutor.invokeAll(TaskExecutor.java:76)
at org.apache.lucene.search.AbstractKnnVectorQuery.rewrite(AbstractKnnVectorQuery.java:91)
at org.apache.lucene.search.KnnFloatVectorQuery.rewrite(KnnFloatVectorQuery.java:45)
at org.apache.lucene.search.IndexSearcher.rewrite(IndexSearcher.java:731)
at org.apache.lucene.tests.search.AssertingIndexSearcher.rewrite(AssertingIndexSearcher.java:69)
at org.apache.lucene.search.IndexSearcher.rewrite(IndexSearcher.java:742)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:626)
at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:483)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:498)
at org.apache.lucene.search.TestKnnByteVectorQuery.testVectorEncodingMismatch(TestKnnByteVectorQuery.java:47)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
```
<!--
If this is your first contribution to Lucene, please make sure you have reviewed the contribution guide.
https://github.com/apache/lucene/blob/main/CONTRIBUTING.md
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Fix NPE when LeafReader return null VectorValues [lucene]
Posted by "bugmakerrrrrr (via GitHub)" <gi...@apache.org>.
bugmakerrrrrr commented on code in PR #13162:
URL: https://github.com/apache/lucene/pull/13162#discussion_r1517586857
##########
lucene/core/src/java/org/apache/lucene/search/FloatVectorSimilarityValuesSource.java:
##########
@@ -40,6 +40,9 @@ public FloatVectorSimilarityValuesSource(float[] vector, String fieldName) {
@Override
public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
final FloatVectorValues vectorValues = ctx.reader().getFloatVectorValues(fieldName);
+ if (vectorValues == null) {
+ return DoubleValues.EMPTY;
+ }
Review Comment:
Nice catch. I will fix it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Fix NPE when LeafReader return null VectorValues [lucene]
Posted by "hossman (via GitHub)" <gi...@apache.org>.
hossman commented on PR #13162:
URL: https://github.com/apache/lucene/pull/13162#issuecomment-2010107912
FWIW: This commit seems to have duplicated `checkField` logic that was previously added in #13105 -- compare `VectorFieldFunction.checkField` with `FloatVectorValues.checkField` and `ByteVectorValues.checkField`.
These should probably be refactored to eliminate the duplication?
```
hossman@slate:~/lucene/lucene [j11] [main] $ tail -20 ./lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/VectorFieldFunction.java
* @lucene.internal
* @lucene.experimental
*/
static void checkField(LeafReader in, String field, VectorEncoding expectedEncoding) {
FieldInfo fi = in.getFieldInfos().fieldInfo(field);
if (fi != null) {
final VectorEncoding actual = fi.hasVectorValues() ? fi.getVectorEncoding() : null;
if (expectedEncoding != actual) {
throw new IllegalStateException(
"Unexpected vector encoding ("
+ actual
+ ") for field "
+ field
+ "(expected="
+ expectedEncoding
+ ")");
}
}
}
}
hossman@slate:~/lucene/lucene [j11] [main] $ tail -20 ./lucene/core/src/java/org/apache/lucene/index/FloatVectorValues.java
* Checks the Vector Encoding of a field
*
* @throws IllegalStateException if {@code field} has vectors, but using a different encoding
* @lucene.internal
* @lucene.experimental
*/
public static void checkField(LeafReader in, String field) {
FieldInfo fi = in.getFieldInfos().fieldInfo(field);
if (fi != null && fi.hasVectorValues() && fi.getVectorEncoding() != VectorEncoding.FLOAT32) {
throw new IllegalStateException(
"Unexpected vector encoding ("
+ fi.getVectorEncoding()
+ ") for field "
+ field
+ "(expected="
+ VectorEncoding.FLOAT32
+ ")");
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Fix NPE when LeafReader return null VectorValues [lucene]
Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent merged PR #13162:
URL: https://github.com/apache/lucene/pull/13162
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Fix NPE when LeafReader return null VectorValues [lucene]
Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on code in PR #13162:
URL: https://github.com/apache/lucene/pull/13162#discussion_r1516111922
##########
lucene/core/src/java/org/apache/lucene/search/FloatVectorSimilarityValuesSource.java:
##########
@@ -40,6 +40,9 @@ public FloatVectorSimilarityValuesSource(float[] vector, String fieldName) {
@Override
public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
final FloatVectorValues vectorValues = ctx.reader().getFloatVectorValues(fieldName);
+ if (vectorValues == null) {
+ return DoubleValues.EMPTY;
+ }
Review Comment:
Why do you throw in the byte similarity source, but not here? We need to be consistent. I think throwing here is acceptable as well (via `FloatVectorValues.check`).
##########
lucene/core/src/java/org/apache/lucene/index/LeafReader.java:
##########
@@ -246,11 +246,12 @@ public final PostingsEnum postings(Term term) throws IOException {
public final TopDocs searchNearestVectors(
String field, float[] target, int k, Bits acceptDocs, int visitedLimit) throws IOException {
FieldInfo fi = getFieldInfos().fieldInfo(field);
- if (fi == null || fi.getVectorDimension() == 0) {
- // The field does not exist or does not index vectors
+ FloatVectorValues floatVectorValues = getFloatVectorValues(fi.name);
+ if (floatVectorValues == null) {
+ FloatVectorValues.checkField(this, field);
Review Comment:
The leaf reader here shouldn't throw. Especially since the companion method that accepts a KnnCollector doesn't.
##########
lucene/core/src/java/org/apache/lucene/search/FieldExistsQuery.java:
##########
@@ -128,12 +128,12 @@ public Query rewrite(IndexSearcher indexSearcher) throws IOException {
break;
}
} else if (fieldInfo.getVectorDimension() != 0) { // the field indexes vectors
- int numVectors =
+ DocIdSetIterator vectorValues =
switch (fieldInfo.getVectorEncoding()) {
- case FLOAT32 -> leaf.getFloatVectorValues(field).size();
- case BYTE -> leaf.getByteVectorValues(field).size();
+ case FLOAT32 -> leaf.getFloatVectorValues(field);
+ case BYTE -> leaf.getByteVectorValues(field);
};
- if (numVectors != leaf.maxDoc()) {
+ if (vectorValues != null && vectorValues.cost() != leaf.maxDoc()) {
Review Comment:
```suggestion
assert vectorValues != null : "unexpected null vector values";
if (vectorValues != null && vectorValues.cost() != leaf.maxDoc()) {
```
I think being safe here is fine. But, this should never be null. An assertion should be added to ensure that it isn't (keeping the null check in the if statement is ok).
##########
lucene/core/src/java/org/apache/lucene/index/LeafReader.java:
##########
@@ -287,11 +288,12 @@ public final TopDocs searchNearestVectors(
public final TopDocs searchNearestVectors(
String field, byte[] target, int k, Bits acceptDocs, int visitedLimit) throws IOException {
FieldInfo fi = getFieldInfos().fieldInfo(field);
- if (fi == null || fi.getVectorDimension() == 0) {
- // The field does not exist or does not index vectors
+ ByteVectorValues byteVectorValues = getByteVectorValues(fi.name);
+ if (byteVectorValues == null) {
+ ByteVectorValues.checkField(this, field);
Review Comment:
The leaf reader here shouldn't throw. Especially since the companion method that accepts a KnnCollector doesn't.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org