Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,22 @@ public void defineDefaultValue(Object value) {

public Object defaultValue() {
// TODO add a field default_value
return this.userdata().get(Userdata.DEFAULT_VALUE);
Object value = this.userdata().get(Userdata.DEFAULT_VALUE);
if (value == null) {
return null;
}

// Userdata is reloaded from JSON as a raw Map, so a typed default
// value (e.g. Date) comes back as a String. Normalize it to the
// runtime type expected by this property key's data type. Idempotent
// for values already of the expected type.
Object raw = value;
if (this.cardinality == Cardinality.SET && value instanceof Collection &&
!(value instanceof Set)) {
raw = new LinkedHashSet<>((Collection<?>) value);
}

return this.validValueOrThrow(raw);
}

public boolean hasSameContent(PropertyKey other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.apache.hugegraph.type.define.WriteType;
import org.apache.hugegraph.util.Blob;
import org.apache.hugegraph.util.CollectionUtil;
import org.apache.hugegraph.util.DateUtil;
import org.apache.hugegraph.util.LongEncoding;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
Expand Down Expand Up @@ -826,6 +828,63 @@ public void testAddVertexWithDefaultPropertyValue() {
Assert.assertFalse(vertex.values("age").hasNext());
}

@Test
public void testAddVertexWithDateDefaultValue() {
SchemaManager schema = graph().schema();

Date joined = DateUtil.parse("2026-05-14 10:11:12.345");
schema.propertyKey("joinDate").asDate()
.userdata(Userdata.DEFAULT_VALUE, joined).create();
schema.vertexLabel("person")
.properties("joinDate")
.nullableKeys("joinDate").append();

// No 'joinDate' supplied
Vertex vertex = graph().addVertex(T.label, "person",
"name", "Baby", "city", "Shanghai");

this.commitTx();

// Reload from backend then query: the typed default must survive the
// JSON serialize/reload round-trip as a Date, not a String (#3028).
vertex = graph().vertex(vertex.id());
Object value = vertex.value("joinDate");
Assert.assertTrue("default 'joinDate' should be a Date, was " +
(value == null ? "null" : value.getClass()),
value instanceof Date);
Assert.assertEquals(joined, value);
}

@Test
public void testAddVertexWithDateSetDefaultValue() {
SchemaManager schema = graph().schema();

String dateStr = "2026-05-14 10:11:12.345";
Date expected = DateUtil.parse(dateStr);

// Simulate JSON-deserialized default: ArrayList of Strings with duplicates
schema.propertyKey("joinDates").asDate().valueSet()
.userdata(Userdata.DEFAULT_VALUE, Arrays.asList(dateStr, dateStr))
.create();
schema.vertexLabel("person")
.properties("joinDates")
.nullableKeys("joinDates").append();

Vertex vertex = graph().addVertex(T.label, "person",
"name", "Baby", "city", "Shanghai");
this.commitTx();

vertex = graph().vertex(vertex.id());
Object raw = vertex.value("joinDates");

Assert.assertTrue("joinDates should be a Set, was " +
(raw == null ? "null" : raw.getClass()),
raw instanceof Set);
Set<?> values = (Set<?>) raw;
Assert.assertEquals("duplicates must be collapsed", 1, values.size());
Assert.assertTrue(values.contains(expected));
}

@Test
public void testAddVertexWithNotExistsVertexPropKey() {
HugeGraph graph = graph();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package org.apache.hugegraph.unit.core;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hugegraph.backend.id.IdGenerator;
Expand All @@ -27,6 +30,8 @@
import org.apache.hugegraph.schema.Userdata;
import org.apache.hugegraph.schema.VertexLabel;
import org.apache.hugegraph.testutil.Assert;
import org.apache.hugegraph.type.define.Cardinality;
import org.apache.hugegraph.type.define.DataType;
import org.apache.hugegraph.unit.FakeObjects;
import org.apache.hugegraph.util.DateUtil;
import org.junit.Test;
Expand Down Expand Up @@ -199,6 +204,62 @@ public void testVertexLabelFromMapNormalizesCreateTimeString() {
createTime);
}

@Test
public void testPropertyKeyFromMapNormalizesDateDefaultValue() {
String formatted = "2026-05-14 10:11:12.345";
Map<String, Object> userdata = new HashMap<>();
userdata.put(Userdata.DEFAULT_VALUE, formatted);

Map<String, Object> map = new HashMap<>();
map.put(PropertyKey.P.ID, 1);
map.put(PropertyKey.P.NAME, "birth");
map.put(PropertyKey.P.DATA_TYPE, DataType.DATE.string());
map.put(PropertyKey.P.CARDINALITY, Cardinality.SINGLE.string());
map.put(PropertyKey.P.USERDATA, userdata);

PropertyKey propertyKey = PropertyKey.fromMap(map,
new FakeObjects().graph());

Object value = propertyKey.defaultValue();
Assert.assertTrue("DEFAULT_VALUE should be a Date, was " +
(value == null ? "null" : value.getClass()),
value instanceof Date);
Assert.assertEquals(DateUtil.parse(formatted), value);
}

@Test
public void testPropertyKeyFromMapNormalizesDateSetDefaultValue() {
String first = "2026-05-14 10:11:12.345";
String second = "2026-05-15 11:12:13.456";
Map<String, Object> userdata = new HashMap<>();
userdata.put(Userdata.DEFAULT_VALUE, Arrays.asList(first, second));

Map<String, Object> map = new HashMap<>();
map.put(PropertyKey.P.ID, 1);
map.put(PropertyKey.P.NAME, "tags");
map.put(PropertyKey.P.DATA_TYPE, DataType.DATE.string());
map.put(PropertyKey.P.CARDINALITY, Cardinality.SET.string());
map.put(PropertyKey.P.USERDATA, userdata);

PropertyKey propertyKey = PropertyKey.fromMap(map,
new FakeObjects().graph());

Object value = propertyKey.defaultValue();
Assert.assertTrue("DEFAULT_VALUE should be a Collection, was " +
(value == null ? "null" : value.getClass()),
value instanceof Collection);
Collection<?> values = (Collection<?>) value;
Assert.assertEquals(2, values.size());
for (Object element : values) {
Assert.assertTrue("each element should be a Date, was " +
(element == null ? "null" : element.getClass()),
element instanceof Date);
}
List<Date> expected = Arrays.asList(DateUtil.parse(first),
DateUtil.parse(second));
Assert.assertTrue(values.containsAll(expected));
}

@Test
public void testBulkSetterRejectsNullUserdata() {
SchemaElement schema = newSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.hugegraph.unit.serializer;

import java.util.Arrays;
import java.util.Date;
import java.util.Set;

import org.apache.hugegraph.backend.id.IdGenerator;
import org.apache.hugegraph.backend.serializer.BinarySerializer;
Expand All @@ -29,6 +31,8 @@
import org.apache.hugegraph.structure.HugeVertex;
import org.apache.hugegraph.testutil.Assert;
import org.apache.hugegraph.testutil.Whitebox;
import org.apache.hugegraph.type.define.Cardinality;
import org.apache.hugegraph.type.define.DataType;
import org.apache.hugegraph.unit.BaseUnitTest;
import org.apache.hugegraph.unit.FakeObjects;
import org.apache.hugegraph.util.DateUtil;
Expand Down Expand Up @@ -132,6 +136,54 @@ public void testPropertyKeyUserdataCreateTimeRoundTripsAsDate() {
Assert.assertEquals(created, value);
}

@Test
public void testPropertyKeyDefaultValueRoundTripsAsDate() {
HugeConfig config = FakeObjects.newConfig();
BinarySerializer ser = new BinarySerializer(config);

FakeObjects objects = new FakeObjects();
PropertyKey original = objects.newPropertyKey(IdGenerator.of(1L),
"name", DataType.DATE);
Date defaultValue = DateUtil.parse("2026-05-14 10:11:12.345");
original.userdata(Userdata.DEFAULT_VALUE, defaultValue);

BackendEntry entry = ser.writePropertyKey(original);
PropertyKey reloaded = ser.readPropertyKey(objects.graph(), entry);

Object value = reloaded.defaultValue();
Assert.assertTrue("DEFAULT_VALUE should be a Date after round-trip, " +
"was " + (value == null ? "null" : value.getClass()),
value instanceof Date);
Assert.assertEquals(defaultValue, value);
}

@Test
public void testPropertyKeySetDefaultValueRoundTripsAsDate() {
HugeConfig config = FakeObjects.newConfig();
BinarySerializer ser = new BinarySerializer(config);

FakeObjects objects = new FakeObjects();
PropertyKey original = objects.newPropertyKey(IdGenerator.of(2L),
"tags", DataType.DATE);
original.cardinality(Cardinality.SET);

String dateStr = "2026-05-14 10:11:12.345";
Date expected = DateUtil.parse(dateStr);
// ArrayList<String> with duplicates — what JSON deserialization produces
original.userdata(Userdata.DEFAULT_VALUE, Arrays.asList(dateStr, dateStr));

BackendEntry entry = ser.writePropertyKey(original);
PropertyKey reloaded = ser.readPropertyKey(objects.graph(), entry);

Object value = reloaded.defaultValue();
Assert.assertTrue("DEFAULT_VALUE should be a Set after round-trip, was " +
(value == null ? "null" : value.getClass()),
value instanceof Set);
Set<?> values = (Set<?>) value;
Assert.assertEquals("duplicates must be collapsed", 1, values.size());
Assert.assertTrue(values.contains(expected));
}

@Test
public void testEdgeForPartition() {
BinarySerializer ser = new BinarySerializer(true, true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.hugegraph.unit.serializer;

import java.util.Arrays;
import java.util.Date;
import java.util.Set;

import org.apache.hugegraph.backend.id.IdGenerator;
import org.apache.hugegraph.backend.serializer.TextSerializer;
Expand All @@ -26,6 +28,8 @@
import org.apache.hugegraph.schema.PropertyKey;
import org.apache.hugegraph.schema.Userdata;
import org.apache.hugegraph.testutil.Assert;
import org.apache.hugegraph.type.define.Cardinality;
import org.apache.hugegraph.type.define.DataType;
import org.apache.hugegraph.unit.BaseUnitTest;
import org.apache.hugegraph.unit.FakeObjects;
import org.apache.hugegraph.util.DateUtil;
Expand Down Expand Up @@ -53,4 +57,51 @@ public void testPropertyKeyUserdataCreateTimeRoundTripsAsDate() {
value instanceof Date);
Assert.assertEquals(created, value);
}

@Test
public void testPropertyKeyDefaultValueRoundTripsAsDate() {
HugeConfig config = FakeObjects.newConfig();
TextSerializer ser = new TextSerializer(config);

FakeObjects objects = new FakeObjects();
PropertyKey original = objects.newPropertyKey(IdGenerator.of(1L),
"name", DataType.DATE);
Date defaultValue = DateUtil.parse("2026-05-14 10:11:12.345");
original.userdata(Userdata.DEFAULT_VALUE, defaultValue);

BackendEntry entry = ser.writePropertyKey(original);
PropertyKey reloaded = ser.readPropertyKey(objects.graph(), entry);

Object value = reloaded.defaultValue();
Assert.assertTrue("DEFAULT_VALUE should be a Date after round-trip, " +
"was " + (value == null ? "null" : value.getClass()),
value instanceof Date);
Assert.assertEquals(defaultValue, value);
}

@Test
public void testPropertyKeySetDefaultValueRoundTripsAsDate() {
HugeConfig config = FakeObjects.newConfig();
TextSerializer ser = new TextSerializer(config);

FakeObjects objects = new FakeObjects();
PropertyKey original = objects.newPropertyKey(IdGenerator.of(2L),
"tags", DataType.DATE);
original.cardinality(Cardinality.SET);

String dateStr = "2026-05-14 10:11:12.345";
Date expected = DateUtil.parse(dateStr);
original.userdata(Userdata.DEFAULT_VALUE, Arrays.asList(dateStr, dateStr));

BackendEntry entry = ser.writePropertyKey(original);
PropertyKey reloaded = ser.readPropertyKey(objects.graph(), entry);

Object value = reloaded.defaultValue();
Assert.assertTrue("DEFAULT_VALUE should be a Set after round-trip, was " +
(value == null ? "null" : value.getClass()),
value instanceof Set);
Set<?> values = (Set<?>) value;
Assert.assertEquals("duplicates must be collapsed", 1, values.size());
Assert.assertTrue(values.contains(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,22 @@ public void defineDefaultValue(Object value) {

public Object defaultValue() {
// TODO add a field default_value
return this.userdata().get(Userdata.DEFAULT_VALUE);
Object value = this.userdata().get(Userdata.DEFAULT_VALUE);
if (value == null) {
return null;
}

// Userdata is reloaded from JSON as a raw Map, so a typed default
// value (e.g. Date) comes back as a String. Normalize it to the
// runtime type expected by this property key's data type. Idempotent
// for values already of the expected type.
Object raw = value;
if (this.cardinality == Cardinality.SET && value instanceof Collection &&
!(value instanceof Set)) {
raw = new LinkedHashSet<>((Collection<?>) value);
}

return this.validValueOrThrow(raw);
}

public boolean hasSameContent(PropertyKey other) {
Expand Down
Loading
Loading