Skip to content

Commit d73e253

Browse files
jjaderbergDarthMax
andcommitted
Prevent using wrapper with primitive properties
Co-Authored-By: Max Kiessling <[email protected]>
1 parent a427c96 commit d73e253

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

algo/src/main/java/org/neo4j/gds/similarity/knn/metrics/NullCheckingNodeProperties.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@
2727

2828
import static org.neo4j.gds.utils.StringFormatting.formatWithLocale;
2929

30-
public class NullCheckingNodeProperties implements NodeProperties {
30+
public final class NullCheckingNodeProperties implements NodeProperties {
31+
32+
public static NullCheckingNodeProperties create(NodeProperties properties, String name, IdMap idMap) {
33+
var valueType = properties.valueType();
34+
assert valueType != ValueType.DOUBLE && valueType != ValueType.LONG: "Don't use NullCheckingNodeProperties for primitive properties";
35+
return new NullCheckingNodeProperties(properties, name, idMap);
36+
}
3137

3238
private final NodeProperties properties;
3339
private final String name;
3440
private final IdMap idMap;
3541

36-
NullCheckingNodeProperties(NodeProperties properties, String name, IdMap idMap) {
42+
private NullCheckingNodeProperties(NodeProperties properties, String name, IdMap idMap) {
3743
this.properties = properties;
3844
this.name = name;
3945
this.idMap = idMap;

algo/src/main/java/org/neo4j/gds/similarity/knn/metrics/SimilarityComputer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ static SimilarityComputer ofProperty(
7878
case DOUBLE_ARRAY:
7979
return ofDoubleArrayProperty(
8080
name,
81-
new NullCheckingNodeProperties(properties, name, idMap),
81+
NullCheckingNodeProperties.create(properties, name, idMap),
8282
defaultSimilarityMetric
8383
);
8484
case FLOAT_ARRAY:
8585
return ofFloatArrayProperty(
8686
name,
87-
new NullCheckingNodeProperties(properties, name, idMap),
87+
NullCheckingNodeProperties.create(properties, name, idMap),
8888
defaultSimilarityMetric
8989
);
9090
case LONG_ARRAY:
9191
return ofLongArrayProperty(
9292
name,
93-
new SortedLongArrayProperties(new NullCheckingNodeProperties(properties, name, idMap)),
93+
new SortedLongArrayProperties(NullCheckingNodeProperties.create(properties, name, idMap)),
9494
defaultSimilarityMetric
9595
);
9696
default:

algo/src/test/java/org/neo4j/gds/similarity/knn/metrics/NullCheckingNodePropertiesTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class NullCheckingNodePropertiesTest {
4343
@Test
4444
void shouldWrapDoubleArrayPropertyAccess() {
4545
var props = new DoubleArrayTestProperties(nodeId -> new double[]{nodeId});
46-
var wrappedProps = new NullCheckingNodeProperties(props, "propertyName", graph);
46+
var wrappedProps = NullCheckingNodeProperties.create(props, "propertyName", graph);
4747
graph.forEachNode(nodeId -> {
4848
assertThat(wrappedProps.doubleArrayValue(nodeId))
4949
.isEqualTo(props.doubleArrayValue(nodeId));
@@ -55,7 +55,7 @@ void shouldWrapDoubleArrayPropertyAccess() {
5555
@Test
5656
void shouldWrapFloatArrayPropertyAccess() {
5757
var props = new FloatArrayTestProperties(nodeId -> new float[]{nodeId});
58-
var wrappedProps = new NullCheckingNodeProperties(props, "propertyName", graph);
58+
var wrappedProps = NullCheckingNodeProperties.create(props, "propertyName", graph);
5959
graph.forEachNode(nodeId -> {
6060
assertThat(wrappedProps.floatArrayValue(nodeId))
6161
.isEqualTo(props.floatArrayValue(nodeId));
@@ -67,7 +67,7 @@ void shouldWrapFloatArrayPropertyAccess() {
6767
@Test
6868
void shouldWrapLongArrayPropertyAccess() {
6969
var props = new LongArrayTestProperties(nodeId -> new long[]{nodeId});
70-
var wrappedProps = new NullCheckingNodeProperties(props, "propertyName", graph);
70+
var wrappedProps = NullCheckingNodeProperties.create(props, "propertyName", graph);
7171
graph.forEachNode(nodeId -> {
7272
assertThat(wrappedProps.longArrayValue(nodeId))
7373
.isEqualTo(props.longArrayValue(nodeId));
@@ -79,7 +79,7 @@ void shouldWrapLongArrayPropertyAccess() {
7979
@Test
8080
void shouldThrowUsefullyForNullDoubleArrayValues() {
8181
var nullProps = new DoubleArrayTestProperties(nodeId -> null);
82-
var nonNullProps = new NullCheckingNodeProperties(nullProps, "propertyName", graph);
82+
var nonNullProps = NullCheckingNodeProperties.create(nullProps, "propertyName", graph);
8383
assertThatThrownBy(() -> nonNullProps.doubleArrayValue(1))
8484
.isInstanceOf(IllegalArgumentException.class)
8585
.hasMessageContaining("Missing `List of Float` node property `propertyName` for node with id `1`.");
@@ -88,7 +88,7 @@ void shouldThrowUsefullyForNullDoubleArrayValues() {
8888
@Test
8989
void shouldThrowUsefullyForNullFloatArrayValues() {
9090
var nullProps = new FloatArrayTestProperties(nodeId -> null);
91-
var nonNullProps = new NullCheckingNodeProperties(nullProps, "propertyName", graph);
91+
var nonNullProps = NullCheckingNodeProperties.create(nullProps, "propertyName", graph);
9292
assertThatThrownBy(() -> nonNullProps.floatArrayValue(1))
9393
.isInstanceOf(IllegalArgumentException.class)
9494
.hasMessageContaining("Missing `List of Float` node property `propertyName` for node with id `1`.");
@@ -97,9 +97,18 @@ void shouldThrowUsefullyForNullFloatArrayValues() {
9797
@Test
9898
void shouldThrowUsefullyForNullLongArrayValues() {
9999
var nullProps = new LongArrayTestProperties(nodeId -> null);
100-
var nonNullProps = new NullCheckingNodeProperties(nullProps, "propertyName", graph);
100+
var nonNullProps = NullCheckingNodeProperties.create(nullProps, "propertyName", graph);
101101
assertThatThrownBy(() -> nonNullProps.longArrayValue(1))
102102
.isInstanceOf(IllegalArgumentException.class)
103103
.hasMessageContaining("Missing `List of Integer` node property `propertyName` for node with id `1`.");
104104
}
105+
106+
@Test
107+
void shouldThrowNormallyIfTheWrongPropertyIsAccessed() {
108+
var longProps = new LongArrayTestProperties(i -> new long[]{i});
109+
var wrappedProps = NullCheckingNodeProperties.create(longProps, "propertyName", graph);
110+
assertThatThrownBy(() -> wrappedProps.doubleArrayValue(0))
111+
.isInstanceOf(UnsupportedOperationException.class)
112+
.hasMessage("Tried to retrieve a value of type DOUBLE_ARRAY value from properties of type LONG_ARRAY");
113+
}
105114
}

0 commit comments

Comments
 (0)