Skip to content

Commit 9cced6a

Browse files
committed
Infer default value from ValueType
1 parent 53eb899 commit 9cced6a

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,7 @@
4949
// TODO: should be named LoadablePropertyMappings
5050
final class IndexPropertyMappings {
5151

52-
static LoadablePropertyMappings prepareProperties(
53-
GraphProjectFromStoreConfig graphProjectConfig,
54-
GraphDimensions graphDimensions,
55-
TransactionContext transaction
56-
) {
57-
Map<NodeLabel, PropertyMappings> storeLoadedProperties = graphProjectConfig
58-
.nodeProjections()
59-
.projections()
60-
.entrySet()
61-
.stream()
62-
.collect(toMap(
63-
Map.Entry::getKey,
64-
entry -> entry.getValue().properties()
65-
));
66-
67-
return prepareLoadableProperties(graphDimensions, transaction, storeLoadedProperties);
68-
}
69-
70-
static Map<NodeLabel, PropertyMappings> propertyMappingsByLabel(GraphProjectFromStoreConfig graphProjectConfig) {
52+
static Map<NodeLabel, PropertyMappings> propertyMappings(GraphProjectFromStoreConfig graphProjectConfig) {
7153
return graphProjectConfig
7254
.nodeProjections()
7355
.projections()
@@ -81,6 +63,14 @@ static Map<NodeLabel, PropertyMappings> propertyMappingsByLabel(GraphProjectFrom
8163
));
8264
}
8365

66+
static LoadablePropertyMappings prepareProperties(
67+
GraphProjectFromStoreConfig graphProjectConfig,
68+
GraphDimensions graphDimensions,
69+
TransactionContext transaction
70+
) {
71+
return prepareLoadableProperties(graphDimensions, transaction, propertyMappings(graphProjectConfig));
72+
}
73+
8474
private static LoadablePropertyMappings prepareLoadableProperties(
8575
GraphDimensions dimensions,
8676
TransactionContext transaction,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.neo4j.gds.PropertyMapping;
2626
import org.neo4j.gds.PropertyMappings;
2727
import org.neo4j.gds.annotation.ValueClass;
28+
import org.neo4j.gds.api.DefaultValue;
2829
import org.neo4j.gds.api.IdMap;
2930
import org.neo4j.gds.api.PropertyState;
3031
import org.neo4j.gds.api.properties.nodes.ImmutableNodeProperty;
@@ -73,7 +74,7 @@ static Nodes of(
7374
var propertySchema = ImmutablePropertySchema.builder()
7475
.key(propertyMapping.propertyKey())
7576
.valueType(nodePropertyValues.valueType())
76-
.defaultValue(propertyMapping.defaultValue())
77+
.defaultValue(DefaultValue.of(nodePropertyValues.valueType()))
7778
.state(propertyState)
7879
.build();
7980

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ public static ScanningNodesImporter scanningNodesImporter(
9090
);
9191
}
9292

93-
var propertyMappings = IndexPropertyMappings.prepareProperties(
93+
var propertyMappings = IndexPropertyMappings.propertyMappings(graphProjectConfig);
94+
95+
var loadablePropertyMappings = IndexPropertyMappings.prepareProperties(
9496
graphProjectConfig,
9597
dimensions,
9698
loadingContext.transactionContext()
9799
);
98100

99-
var propertyMappingsByLabel = IndexPropertyMappings.propertyMappingsByLabel(graphProjectConfig);
100-
101101
var nodePropertyImporter = initializeNodePropertyImporter(
102-
propertyMappings,
102+
loadablePropertyMappings,
103103
dimensions,
104104
concurrency
105105
);
@@ -111,7 +111,7 @@ public static ScanningNodesImporter scanningNodesImporter(
111111
progressTracker,
112112
concurrency,
113113
propertyMappings,
114-
propertyMappingsByLabel,
114+
loadablePropertyMappings,
115115
nodePropertyImporter,
116116
idMapBuilder,
117117
labelInformationBuilder
@@ -124,8 +124,8 @@ private ScanningNodesImporter(
124124
GraphDimensions dimensions,
125125
ProgressTracker progressTracker,
126126
int concurrency,
127-
IndexPropertyMappings.LoadablePropertyMappings propertyMappings,
128127
Map<NodeLabel, PropertyMappings> propertyMappingsByLabel,
128+
IndexPropertyMappings.LoadablePropertyMappings propertyMappings,
129129
@Nullable NativeNodePropertyImporter nodePropertyImporter,
130130
IdMapBuilder idMapBuilder,
131131
LabelInformation.Builder labelInformationBuilder

core/src/test/java/org/neo4j/gds/api/DefaultValueTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,36 @@ void createUserDefinedValueTypes(Object valueToSet, Function<DefaultValue, ?> fn
180180
assertThat(fn.apply(defaultValue)).isEqualTo(expectedValue);
181181
}
182182

183+
@Test
184+
void initFromValueType() {
185+
assertThat(DefaultValue.of(ValueType.DOUBLE)).isEqualTo(DefaultValue.forDouble());
186+
assertThat(DefaultValue.of(ValueType.LONG)).isEqualTo(DefaultValue.forLong());
187+
assertThat(DefaultValue.of(ValueType.LONG_ARRAY)).isEqualTo(DefaultValue.forLongArray());
188+
assertThat(DefaultValue.of(ValueType.FLOAT_ARRAY)).isEqualTo(DefaultValue.forFloatArray());
189+
assertThat(DefaultValue.of(ValueType.DOUBLE_ARRAY)).isEqualTo(DefaultValue.forDoubleArray());
190+
assertThat(DefaultValue.of(ValueType.STRING)).isEqualTo(DefaultValue.of(DEFAULT));
191+
}
192+
183193
private static Stream<Arguments> values() {
184194
return Stream.of(
185195
Arguments.of(42, (Function<DefaultValue, ?>) DefaultValue::longValue, 42L),
186196
Arguments.of(42, (Function<DefaultValue, ?>) DefaultValue::doubleValue, 42D),
187197
Arguments.of(13.37, (Function<DefaultValue, ?>) DefaultValue::doubleValue, 13.37D),
188-
Arguments.of(List.of(13.37, 42), (Function<DefaultValue, ?>) DefaultValue::doubleArrayValue, new double[] {13.37D, 42D}),
189-
Arguments.of(List.of(1337L, 42L), (Function<DefaultValue, ?>) DefaultValue::longArrayValue, new long[] {1337L, 42L}),
190-
Arguments.of(List.of(1337, 42), (Function<DefaultValue, ?>) DefaultValue::longArrayValue, new long[] {1337L, 42L})
198+
Arguments.of(
199+
List.of(13.37, 42),
200+
(Function<DefaultValue, ?>) DefaultValue::doubleArrayValue,
201+
new double[]{13.37D, 42D}
202+
),
203+
Arguments.of(
204+
List.of(1337L, 42L),
205+
(Function<DefaultValue, ?>) DefaultValue::longArrayValue,
206+
new long[]{1337L, 42L}
207+
),
208+
Arguments.of(
209+
List.of(1337, 42),
210+
(Function<DefaultValue, ?>) DefaultValue::longArrayValue,
211+
new long[]{1337L, 42L}
212+
)
191213
);
192214
}
193215

graph-projection-api/src/main/java/org/neo4j/gds/api/DefaultValue.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ public static DefaultValue of(@Nullable Object defaultValue, ValueType type, boo
8484
}
8585
}
8686

87+
public static DefaultValue of(ValueType type) {
88+
switch (type) {
89+
case LONG:
90+
return DefaultValue.forLong();
91+
case DOUBLE:
92+
return DefaultValue.forDouble();
93+
case DOUBLE_ARRAY:
94+
return DefaultValue.forDoubleArray();
95+
case FLOAT_ARRAY:
96+
return DefaultValue.forFloatArray();
97+
case LONG_ARRAY:
98+
return DefaultValue.forLongArray();
99+
default:
100+
return DefaultValue.of(type.fallbackValue());
101+
}
102+
}
103+
87104
private static DefaultValue ofFallBackValue(@Nullable Object defaultValue) {
88105
return of(defaultValue, false);
89106
}

0 commit comments

Comments
 (0)