kaivalnp commented on code in PR #15553:
URL: https://github.com/apache/lucene/pull/15553#discussion_r2673490493


##########
lucene/core/src/test/org/apache/lucene/codecs/TestMergedByteVectorValues.java:
##########
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.codecs;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.KnnByteVectorField;
+import org.apache.lucene.index.ByteVectorValues;
+import org.apache.lucene.index.CodecReader;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.KnnVectorValues;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.MergeState;
+import org.apache.lucene.index.SegmentReadState;
+import org.apache.lucene.index.SegmentWriteState;
+import org.apache.lucene.index.Sorter;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.tests.codecs.asserting.AssertingCodec;
+import org.apache.lucene.tests.util.LuceneTestCase;
+import org.apache.lucene.tests.util.TestUtil;
+
+/** Tests for MergedByteVectorValues to ensure lastOrd is properly incremented 
during iteration. */
+public class TestMergedByteVectorValues extends LuceneTestCase {
+
+  /**
+   * Tests that skipping vectors via nextDoc() and then loading a vector works 
correctly during
+   * merge. This verifies the fix for the lastOrd tracking bug in 
MergedByteVectorValues.
+   *
+   * <p>The bug: MergedByteVectorValues.nextDoc() does not increment lastOrd, 
so when you skip N
+   * vectors and then try to load vectorValue(N), it fails because lastOrd is 
still -1.
+   */
+  public void testSkipThenLoadByteVectorDuringMerge() throws IOException {

Review Comment:
   The setup of this test seems complicated to me -- considering the gist is:
   
   ```java
   // Run the test
   ByteVectorValues values =
       KnnVectorsWriter.MergedVectorValues.mergeByteVectorValues(
           fieldInfo, mergeState);
   
   // Skip doc 0 and load doc 1
   values.iterator().nextDoc(); // doc 0
   values.iterator().nextDoc(); // doc 1
   
   // Read vector for doc 1
   assertArrayEquals(expected, values.vectorValue(1)); // throws 
IllegalArgumentException on main
   ```
   
   It's probably because `ByteVectorValuesSub` and `MergedByteVectorValues` are 
private, so we have to go through public APIs to instantiate them + execute the 
test?
   
   Perhaps we can make them package-private for testing? It would simplify the 
test to something like:
   
   ```java
   /**
    * Test that skipping vectors in MergedByteVectorValues via nextDoc() and 
then loading a
    * subsequent vector via vectorValue() works correctly.
    */
   public void testSkipsInMergedByteVectorValues() throws IOException {
     // Data
     List<byte[]> vectors = List.of(new byte[] {0}, new byte[] {1});
   
     // Setup
     KnnVectorsWriter.ByteVectorValuesSub sub =
         new KnnVectorsWriter.ByteVectorValuesSub(x -> x, 
ByteVectorValues.fromBytes(vectors, 1));
     MergeState state =
         new MergeState(
             null, null, null, null, null, null, null, null, null, null, null, 
null, null, null,
             null, false);
   
     // Run the test
     ByteVectorValues values =
         new 
KnnVectorsWriter.MergedVectorValues.MergedByteVectorValues(List.of(sub), state);
   
     // Skip doc 0 and load doc 1
     values.iterator().nextDoc(); // doc 0
     values.iterator().nextDoc(); // doc 1
   
     // Read vector for doc 1
     assertArrayEquals(vectors.get(1), values.vectorValue(1));
   }
   ```
   
   Although I'll wait for someone else to weigh in on this..



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to