Skip to content

Commit

Permalink
Add support for new Vector type (#74)
Browse files Browse the repository at this point in the history
* add support for new Vector type

---------

Co-authored-by: Barak Bar Orion <[email protected]>
  • Loading branch information
gkorland and barakb authored Oct 10, 2024
1 parent f5180cd commit bda3abc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
# Label used to access the service container
falkordb:
# Docker Hub image
image: falkordb/falkordb:latest
image: falkordb/falkordb:edge
# Map port 6379 on the Docker host to port 6379 on the FalkorDB container
ports:
- 6379:6379
Expand Down
3 changes: 3 additions & 0 deletions .sdkmanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=17.0.12-graal
21 changes: 19 additions & 2 deletions src/main/java/com/falkordb/impl/resultset/ResultSetImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import redis.clients.jedis.exceptions.JedisDataException;

import java.util.*;
import java.util.stream.Collectors;

public class ResultSetImpl implements ResultSet {

Expand All @@ -25,7 +26,7 @@ public class ResultSetImpl implements ResultSet {
/**
* @param rawResponse the raw representation of response is at most 3 lists of
* objects. The last list is the statistics list.
* @param graph the graph connection
* @param graph the graph connection
* @param cache the graph local cache
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -157,7 +158,7 @@ private Node deserializeNode(List<Object> rawNodeData) {

/**
* @param graphEntity graph entity
* @param id entity id to be set to the graph entity
* @param id entity id to be set to the graph entity
*/
private void deserializeGraphEntityId(GraphEntity graphEntity, long id) {
graphEntity.setId(id);
Expand Down Expand Up @@ -246,6 +247,8 @@ private Object deserializeScalar(List<Object> rawScalarData) {
return deserializeMap(obj);
case VALUE_POINT:
return deserializePoint(obj);
case VALUE_VECTORF32:
return deserializeVector(obj);
case VALUE_UNKNOWN:
default:
return obj;
Expand All @@ -256,6 +259,20 @@ private Object deserializePoint(Object rawScalarData) {
return new Point(BuilderFactory.DOUBLE_LIST.build(rawScalarData));
}

private List<Float> deserializeVector(Object rawScalarData) {
List<byte[]> array = (List<byte[]>) rawScalarData;
return array.stream()
.map(val -> {
try {
return Float.parseFloat(SafeEncoder.encode(val));
} catch (NumberFormatException e) {
// Handle the exception appropriately
throw new GraphException("Invalid float value in vector data", e);
}
})
.collect(Collectors.toList());
}

@SuppressWarnings("unchecked")
private Map<String, Object> deserializeMap(Object rawScalarData) {
List<Object> keyTypeValueEntries = (List<Object>) rawScalarData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum ResultSetScalarTypes {
VALUE_NODE,
VALUE_PATH,
VALUE_MAP,
VALUE_POINT;
VALUE_POINT,
VALUE_VECTORF32;

private static final ResultSetScalarTypes[] values = values();

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/falkordb/GraphAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,19 @@ public void test64bitnumber() {
Assert.assertEquals(Long.valueOf(value), r.getValue(0));
}

@Test
public void testVecf32() {
ResultSet resultSet = client.query("RETURN vecf32([2.1, -0.82, 1.3, 4.5]) AS vector");
Assert.assertEquals(1, resultSet.size());
Record r = resultSet.iterator().next();
List<Float> vector = r.getValue(0);
Assert.assertEquals(4, vector.size());
Assert.assertEquals(2.1f, vector.get(0), 0.01);
Assert.assertEquals(-0.82f, vector.get(1), 0.01);
Assert.assertEquals(1.3f, vector.get(2), 0.01);
Assert.assertEquals(4.5f, vector.get(3), 0.01);
}

@Test
public void testCachedExecution() {
client.query("CREATE (:N {val:1}), (:N {val:2})");
Expand Down

0 comments on commit bda3abc

Please sign in to comment.