Skip to content
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

refactor: Use float[], int[] QdrantVectorHandler #26

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/main/java/io/qdrant/spark/QdrantVectorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.qdrant.client.grpc.Points.Vectors;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.types.StructType;
Expand All @@ -26,8 +25,7 @@ public static Vectors prepareVectors(

// Maitaining support for the "embedding_field" and "vector_name" options
if (!options.embeddingField.isEmpty()) {
int embeddingFieldIndex = schema.fieldIndex(options.embeddingField);
float[] embeddings = record.getArray(embeddingFieldIndex).toFloatArray();
float[] embeddings = extractFloatArray(record, schema, options.embeddingField);
// 'options.vectorName' defaults to ""
vectorsBuilder.mergeFrom(
namedVectors(Collections.singletonMap(options.vectorName, vector(embeddings))));
Expand All @@ -42,9 +40,10 @@ private static Vectors prepareSparseVectors(

for (int i = 0; i < options.sparseVectorNames.length; i++) {
String name = options.sparseVectorNames[i];
List<Float> values = extractFloatArray(record, schema, options.sparseVectorValueFields[i]);
List<Integer> indices = extractIntArray(record, schema, options.sparseVectorIndexFields[i]);
sparseVectors.put(name, vector(values, indices));
float[] values = extractFloatArray(record, schema, options.sparseVectorValueFields[i]);
int[] indices = extractIntArray(record, schema, options.sparseVectorIndexFields[i]);

sparseVectors.put(name, vector(Floats.asList(values), Ints.asList(indices)));
}

return namedVectors(sparseVectors);
Expand All @@ -56,22 +55,21 @@ private static Vectors prepareDenseVectors(

for (int i = 0; i < options.vectorNames.length; i++) {
String name = options.vectorNames[i];
List<Float> values = extractFloatArray(record, schema, options.vectorFields[i]);
float[] values = extractFloatArray(record, schema, options.vectorFields[i]);
denseVectors.put(name, vector(values));
}

return namedVectors(denseVectors);
}

private static List<Float> extractFloatArray(
private static float[] extractFloatArray(
InternalRow record, StructType schema, String fieldName) {
int fieldIndex = schema.fieldIndex(fieldName);
return Floats.asList(record.getArray(fieldIndex).toFloatArray());
return record.getArray(fieldIndex).toFloatArray();
}

private static List<Integer> extractIntArray(
InternalRow record, StructType schema, String fieldName) {
private static int[] extractIntArray(InternalRow record, StructType schema, String fieldName) {
int fieldIndex = schema.fieldIndex(fieldName);
return Ints.asList(record.getArray(fieldIndex).toIntArray());
return record.getArray(fieldIndex).toIntArray();
}
}
Loading