Skip to content

Commit a0e2ba7

Browse files
authored
Merge pull request #9612 from neo-technology/neo4j-values-crusade
Neo4j values, round 42
2 parents 983334f + ddfb0ba commit a0e2ba7

File tree

58 files changed

+2858
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2858
-350
lines changed

algo/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
implementation project(':core')
3434
implementation project(':core-utils')
3535
implementation project(':core-write')
36+
implementation project(':gds-values')
3637
implementation project(':graph-schema-api')
3738
implementation project(':licensing')
3839
implementation project(':logging')

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
implementation project(':annotations')
2424
implementation project(':config-api')
2525
implementation project(':core-utils')
26+
implementation project(':gds-values')
2627
implementation project(':licensing')
2728
implementation project(':logging')
2829
implementation project(':graph-schema-api')
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.core.loading;
21+
22+
import org.jetbrains.annotations.NotNull;
23+
import org.neo4j.gds.values.Array;
24+
import org.neo4j.gds.values.GdsNoValue;
25+
import org.neo4j.gds.values.GdsValue;
26+
import org.neo4j.gds.values.primitive.PrimitiveValues;
27+
import org.neo4j.values.AnyValue;
28+
import org.neo4j.values.SequenceValue;
29+
import org.neo4j.values.storable.ArrayValue;
30+
import org.neo4j.values.storable.IntegralValue;
31+
import org.neo4j.values.storable.NoValue;
32+
import org.neo4j.values.storable.Value;
33+
import org.neo4j.values.storable.ValueGroup;
34+
import org.neo4j.values.virtual.ListValue;
35+
36+
import static org.neo4j.gds.utils.StringFormatting.formatWithLocale;
37+
38+
public final class GdsNeo4jValueConverter {
39+
40+
public static GdsValue toValue(@NotNull AnyValue value) {
41+
if (value == NoValue.NO_VALUE) {
42+
return GdsNoValue.NO_VALUE;
43+
}
44+
if (value.isSequenceValue()) { // ArrayValue or ListValue
45+
return convertSequenceValueOrFail((SequenceValue) value);
46+
}
47+
if (value instanceof Value storableValue && storableValue.valueGroup() == ValueGroup.NUMBER) {
48+
if (storableValue instanceof org.neo4j.values.storable.FloatValue floatValue) {
49+
return PrimitiveValues.floatingPointValue(floatValue.floatValue());
50+
} else if (storableValue instanceof org.neo4j.values.storable.DoubleValue doubleValue) {
51+
return PrimitiveValues.floatingPointValue(doubleValue.doubleValue());
52+
} else if (storableValue instanceof IntegralValue integralValue) {
53+
return PrimitiveValues.longValue(integralValue.longValue());
54+
}
55+
}
56+
throw new IllegalArgumentException(formatWithLocale(
57+
"Unsupported conversion to GDS Value from Neo4j Value with type `%s`.",
58+
value.getTypeName()
59+
));
60+
}
61+
62+
private static GdsValue convertSequenceValueOrFail(SequenceValue value) {
63+
if (value instanceof ListValue listValue) {
64+
return convertListValueOrFail(listValue);
65+
} else if (value instanceof ArrayValue arrayValue) {
66+
return convertArrayValueOrFail(arrayValue);
67+
} else {
68+
throw failOnBadInput(value);
69+
}
70+
}
71+
72+
@NotNull
73+
private static Array convertListValueOrFail(ListValue listValue) {
74+
if (listValue.isEmpty()) {
75+
// encode as long array
76+
return PrimitiveValues.EMPTY_LONG_ARRAY;
77+
}
78+
try {
79+
return convertArrayValueOrFail(listValue.toStorableArray());
80+
} catch (RuntimeException e) {
81+
throw failOnBadInput(listValue);
82+
}
83+
}
84+
85+
@NotNull
86+
private static Array convertArrayValueOrFail(ArrayValue array) {
87+
if (array.valueGroup() != ValueGroup.NUMBER_ARRAY) {
88+
throw failOnBadInput(array);
89+
}
90+
if (array.isEmpty()) {
91+
return PrimitiveValues.EMPTY_LONG_ARRAY;
92+
}
93+
var arrayCopy = array.asObjectCopy();
94+
if (arrayCopy instanceof long[]) {
95+
return PrimitiveValues.longArray((long[]) arrayCopy);
96+
} else if (arrayCopy instanceof double[]) {
97+
return PrimitiveValues.doubleArray((double[]) arrayCopy);
98+
} else {
99+
throw failOnBadInput(array);
100+
}
101+
}
102+
103+
private static IllegalArgumentException failOnBadInput(SequenceValue badInput) {
104+
return new IllegalArgumentException(
105+
formatWithLocale(
106+
"Unsupported conversion to GDS Value from Neo4j Value `%s`.",
107+
badInput
108+
)
109+
);
110+
}
111+
112+
private GdsNeo4jValueConverter() {}
113+
}

core/src/main/java/org/neo4j/gds/core/loading/RelationshipImportResult.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030
import org.neo4j.gds.api.PropertyState;
3131
import org.neo4j.gds.api.RelationshipProperty;
3232
import org.neo4j.gds.api.RelationshipPropertyStore;
33-
import org.neo4j.gds.api.ValueTypes;
3433
import org.neo4j.gds.api.nodeproperties.ValueType;
3534
import org.neo4j.gds.api.schema.Direction;
3635
import org.neo4j.gds.api.schema.MutableRelationshipSchema;
3736
import org.neo4j.gds.api.schema.MutableRelationshipSchemaEntry;
38-
import org.neo4j.values.storable.NumberType;
3937

4038
import java.util.Collection;
4139
import java.util.HashMap;
@@ -157,7 +155,7 @@ private static RelationshipPropertyStore constructRelationshipPropertyStore(
157155
),
158156
propertyMapping.defaultValue().isUserDefined()
159157
? propertyMapping.defaultValue()
160-
: ValueTypes.fromNumberType(NumberType.FLOATING_POINT).fallbackValue(),
158+
: ValueType.DOUBLE.fallbackValue(),
161159
propertyMapping.aggregation()
162160
)
163161
);

core/src/main/java/org/neo4j/gds/core/loading/ValueConverter.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

core/src/main/java/org/neo4j/gds/core/loading/construction/NodesBuilder.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
import org.neo4j.gds.core.loading.nodeproperties.NodePropertiesFromStoreBuilder;
3838
import org.neo4j.gds.core.utils.paged.HugeAtomicBitSet;
3939
import org.neo4j.gds.core.utils.paged.HugeAtomicGrowingBitSet;
40-
import org.neo4j.values.storable.Value;
41-
import org.neo4j.values.storable.Values;
40+
import org.neo4j.gds.values.GdsValue;
4241

43-
import java.util.HashMap;
4442
import java.util.HashSet;
4543
import java.util.Map;
4644
import java.util.concurrent.atomic.LongAdder;
@@ -148,33 +146,19 @@ public void addNode(long originalId, NodeLabel nodeLabel) {
148146
this.addNode(originalId, NodeLabelTokens.ofNodeLabel(nodeLabel));
149147
}
150148

151-
public void addNodeWithPropertiesAsObjects(long originalId, Map<String, Object> propertiesAsObjects) {
152-
this.addNodeWithPropertiesAsObjects(originalId, propertiesAsObjects, NodeLabelTokens.empty());
153-
}
154-
155-
public void addNodeWithPropertiesAsObjects(long originalId, Map<String, Object> propertiesAsObjects, NodeLabel... nodeLabels) {
156-
this.addNodeWithPropertiesAsObjects(originalId, propertiesAsObjects, NodeLabelTokens.ofNodeLabels(nodeLabels));
157-
}
158-
159-
public void addNodeWithPropertiesAsObjects(long originalId, Map<String, Object> propertiesAsObjects, NodeLabelToken nodeLabels) {
160-
var properties = new HashMap<String, Value>(propertiesAsObjects.size());
161-
propertiesAsObjects.forEach((key, value) -> properties.put(key, Values.of(value)));
162-
this.addNode(originalId, properties, nodeLabels);
163-
}
164-
165-
public void addNode(long originalId, Map<String, Value> properties) {
149+
public void addNode(long originalId, Map<String, GdsValue> properties) {
166150
this.addNode(originalId, properties, NodeLabelTokens.empty());
167151
}
168152

169-
public void addNode(long originalId, Map<String, Value> properties, NodeLabelToken nodeLabels) {
153+
public void addNode(long originalId, Map<String, GdsValue> properties, NodeLabelToken nodeLabels) {
170154
this.addNode(originalId, nodeLabels, PropertyValues.of(properties));
171155
}
172156

173-
public void addNode(long originalId, Map<String, Value> properties, NodeLabel... nodeLabels) {
157+
public void addNode(long originalId, Map<String, GdsValue> properties, NodeLabel... nodeLabels) {
174158
this.addNode(originalId, properties, NodeLabelTokens.ofNodeLabels(nodeLabels));
175159
}
176160

177-
public void addNode(long originalId, Map<String, Value> properties, NodeLabel nodeLabel) {
161+
public void addNode(long originalId, Map<String, GdsValue> properties, NodeLabel nodeLabel) {
178162
this.addNode(originalId, properties, NodeLabelTokens.ofNodeLabel(nodeLabel));
179163
}
180164

0 commit comments

Comments
 (0)