@@ -132,18 +132,25 @@ long[] buildAndWritePostingsLists(
132132 OptimizedScalarQuantizer quantizer = new OptimizedScalarQuantizer (fieldInfo .getVectorSimilarityFunction ());
133133 int [] quantized = new int [fieldInfo .getVectorDimension ()];
134134 byte [] binary = new byte [BQVectorUtils .discretize (fieldInfo .getVectorDimension (), 64 ) / 8 ];
135+ float [] overspillScratch = new float [fieldInfo .getVectorDimension ()];
135136 for (int i = 0 ; i < assignments .length ; i ++) {
136137 int c = assignments [i ];
137138 float [] centroid = centroidSupplier .centroid (c );
138139 float [] vector = floatVectorValues .vectorValue (i );
140+ boolean overspill = overspillAssignments .length > i && overspillAssignments [i ] != -1 ;
141+ // if overspilling, this means we quantize twice, and quantization mutates the in-memory representation of the vector
142+ // so, make a copy of the vector to avoid mutating it
143+ if (overspill ) {
144+ System .arraycopy (vector , 0 , overspillScratch , 0 , fieldInfo .getVectorDimension ());
145+ }
146+
139147 OptimizedScalarQuantizer .QuantizationResult result = quantizer .scalarQuantize (vector , quantized , (byte ) 1 , centroid );
140148 BQVectorUtils .packAsBinary (quantized , binary );
141149 writeQuantizedValue (quantizedVectorsTemp , binary , result );
142- boolean overspill = overspillAssignments .length > i && overspillAssignments [i ] != -1 ;
143150 if (overspill ) {
144151 int s = overspillAssignments [i ];
145152 // write the overspill vector as well
146- result = quantizer .scalarQuantize (vector , quantized , (byte ) 1 , centroidSupplier .centroid (s ));
153+ result = quantizer .scalarQuantize (overspillScratch , quantized , (byte ) 1 , centroidSupplier .centroid (s ));
147154 BQVectorUtils .packAsBinary (quantized , binary );
148155 writeQuantizedValue (quantizedVectorsTemp , binary , result );
149156 } else {
0 commit comments