Skip to content

Commit 9ac61e3

Browse files
committed
Prove bug in current size() usage
1 parent bc07630 commit 9ac61e3

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ static final class SortedLongArrayPropertyValues implements LongArrayNodePropert
5757
SortedLongArrayPropertyValues(NodePropertyValues nodePropertyValues) {
5858
this.properties = HugeObjectArray.newArray(long[].class, nodePropertyValues.valuesStored());
5959
this.properties.setAll(i -> {
60-
var value = nodePropertyValues.longArrayValue(i).clone();
60+
long[] input = nodePropertyValues.longArrayValue(i);
61+
62+
if (input == null) {
63+
return null;
64+
}
65+
66+
var value = input.clone();
6167
Arrays.parallelSort(value);
6268
return value;
6369
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.similarity.knn.metrics;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.neo4j.gds.api.properties.nodes.LongArrayNodePropertyValues;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
class LongArrayPropertySimilarityComputerTest {
28+
29+
@Test
30+
void usingSparseProperties() {
31+
long[][] inputValues = {{42, 24}, null, {84, 48}};
32+
33+
var sparseProperties = new LongArrayNodePropertyValues() {
34+
@Override
35+
public long[] longArrayValue(long nodeId) {
36+
return inputValues[(int) nodeId];
37+
}
38+
39+
@Override
40+
public long valuesStored() {
41+
return 2;
42+
}
43+
};
44+
45+
var sortedValues = new LongArrayPropertySimilarityComputer.SortedLongArrayPropertyValues(sparseProperties);
46+
47+
assertThat(sortedValues.longArrayValue(0)).containsExactly(24, 42);
48+
assertThat(sortedValues.longArrayValue(1)).isNull();
49+
assertThat(sortedValues.longArrayValue(2)).containsExactly(48, 84);
50+
}
51+
52+
}

proc/similarity/src/test/java/org/neo4j/gds/similarity/knn/KnnStreamProcTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,32 @@ void shouldStreamWithFilteredNodes() {
9494
Map.of("node1", 7L, "node2", 6L, "similarity", 1.0)
9595
));
9696
}
97+
98+
@Test
99+
void computeOverSparseNodeProperties() {
100+
String nodeCreateQuery =
101+
"CREATE " +
102+
" (alice:Person {grades: [24, 4]})" +
103+
" ,(carol:Person)" +
104+
" ,(eve:Person)" +
105+
" ,(bob:Foo {grades: [24, 4, 42]})";
106+
107+
runQuery(nodeCreateQuery);
108+
109+
String createQuery = "CALL gds.graph.project('graph', " +
110+
"'Person', '*', {nodeProperties: {grades: {defaultValue: [1, 1]}}})";
111+
runQuery(createQuery);
112+
113+
String algoQuery = GdsCypher.call("graph")
114+
.algo("gds.knn")
115+
.streamMode()
116+
.addParameter("nodeLabels", List.of("Person"))
117+
.addParameter("nodeProperties", List.of("grades"))
118+
.yields("node1", "node2", "similarity");
119+
120+
assertCypherResult(algoQuery, List.of(
121+
Map.of("node1", 6L, "node2", 7L, "similarity", 1.0),
122+
Map.of("node1", 7L, "node2", 6L, "similarity", 1.0)
123+
));
124+
}
97125
}

0 commit comments

Comments
 (0)