Skip to content

Commit cf4d1cc

Browse files
authored
Merge pull request #448 from weaviate/v6-data-types
v6: Extend supported data types - boolean / boolean[] - number / number[] - date / date[] - uuid / uuid[] - text[] - integer[]
2 parents e4d1f90 + 8a45ee3 commit cf4d1cc

22 files changed

Lines changed: 952 additions & 164 deletions

src/it/java/io/weaviate/integration/AggregationITest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import io.weaviate.client6.v1.api.collections.Property;
1717
import io.weaviate.client6.v1.api.collections.Vectorizers;
1818
import io.weaviate.client6.v1.api.collections.Vectors;
19+
import io.weaviate.client6.v1.api.collections.aggregate.Aggregate;
1920
import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponseGroup;
2021
import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponseGrouped;
21-
import io.weaviate.client6.v1.api.collections.aggregate.Aggregation;
2222
import io.weaviate.client6.v1.api.collections.aggregate.GroupBy;
2323
import io.weaviate.client6.v1.api.collections.aggregate.GroupedBy;
2424
import io.weaviate.client6.v1.api.collections.aggregate.IntegerAggregation;
@@ -57,7 +57,7 @@ public void testOverAll() {
5757
var result = things.aggregate.overAll(
5858
with -> with
5959
.metrics(
60-
Aggregation.integer("price",
60+
Aggregate.integer("price",
6161
calculate -> calculate.median().max().count()))
6262
.includeTotalCount(true));
6363

@@ -77,7 +77,7 @@ public void testOverAll_groupBy_category() {
7777
var result = things.aggregate.overAll(
7878
with -> with
7979
.metrics(
80-
Aggregation.integer("price",
80+
Aggregate.integer("price",
8181
calculate -> calculate.min().max().count()))
8282
.includeTotalCount(true),
8383
GroupBy.property("category"));
@@ -115,7 +115,7 @@ public void testNearVector() {
115115
near -> near.limit(5),
116116
with -> with
117117
.metrics(
118-
Aggregation.integer("price",
118+
Aggregate.integer("price",
119119
calculate -> calculate.min().max().count()))
120120
.objectLimit(4)
121121
.includeTotalCount(true));
@@ -135,7 +135,7 @@ public void testNearVector_groupBy_category() {
135135
near -> near.distance(2f),
136136
with -> with
137137
.metrics(
138-
Aggregation.integer("price",
138+
Aggregate.integer("price",
139139
calculate -> calculate.min().max().median()))
140140
.objectLimit(9)
141141
.includeTotalCount(true),

src/it/java/io/weaviate/integration/DataITest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.weaviate.integration;
22

33
import java.io.IOException;
4+
import java.time.OffsetDateTime;
5+
import java.util.List;
46
import java.util.Map;
7+
import java.util.UUID;
58

69
import org.assertj.core.api.Assertions;
710
import org.assertj.core.api.InstanceOfAssertFactories;
@@ -407,4 +410,61 @@ public void testDuplicateUuid() throws IOException {
407410
// Act
408411
things.data.insert(Map.of(), thing -> thing.uuid(thing_1.uuid()));
409412
}
413+
414+
@Test
415+
public void testDataTypes() throws IOException {
416+
// Arrange
417+
var nsDataTypes = ns("DataTypes");
418+
419+
// BLOB type is omitted because a base64-encoded image
420+
// isn't doing the failure message any favours.
421+
// It's tested in other test cases above.
422+
client.collections.create(
423+
nsDataTypes, c -> c
424+
.properties(
425+
Property.text("prop_text"),
426+
Property.integer("prop_integer"),
427+
Property.number("prop_number"),
428+
Property.bool("prop_bool"),
429+
Property.date("prop_date"),
430+
Property.uuid("prop_uuid"),
431+
Property.integerArray("prop_integer_array"),
432+
Property.numberArray("prop_number_array"),
433+
Property.boolArray("prop_bool_array"),
434+
Property.dateArray("prop_date_array"),
435+
Property.uuidArray("prop_uuid_array"),
436+
Property.textArray("prop_text_array")));
437+
438+
var types = client.collections.use(nsDataTypes);
439+
440+
var now = OffsetDateTime.now();
441+
var uuid = UUID.randomUUID();
442+
443+
Map<String, Object> want = Map.ofEntries(
444+
Map.entry("prop_text", "Hello, World!"),
445+
Map.entry("prop_integer", 1L),
446+
Map.entry("prop_number", 1D),
447+
Map.entry("prop_bool", true),
448+
Map.entry("prop_date", now),
449+
Map.entry("prop_uuid", uuid),
450+
Map.entry("prop_integer_array", List.of(1L, 2L, 3L)),
451+
Map.entry("prop_number_array", List.of(1D, 2D, 3D)),
452+
Map.entry("prop_bool_array", List.of(true, false)),
453+
Map.entry("prop_date_array", List.of(now, now)),
454+
Map.entry("prop_uuid_array", List.of(uuid, uuid)),
455+
Map.entry("prop_text_array", List.of("a", "b", "c")));
456+
var returnProperties = want.keySet().toArray(String[]::new);
457+
458+
// Act
459+
var object = types.data.insert(want);
460+
var got = types.query.byId(object.uuid(),
461+
q -> q.returnProperties(returnProperties));
462+
463+
// Assert
464+
Assertions.assertThat(got).get()
465+
.extracting(WeaviateObject::properties)
466+
.asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class))
467+
.containsAllEntriesOf(want);
468+
469+
}
410470
}

src/main/java/io/weaviate/client6/v1/api/collections/DataType.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66

77
public interface DataType {
88
public static final String TEXT = "text";
9+
public static final String TEXT_ARRAY = "text[]";
910
public static final String INT = "int";
11+
public static final String INT_ARRAY = "int[]";
12+
public static final String NUMBER = "number";
13+
public static final String NUMBER_ARRAY = "number[]";
14+
public static final String BOOL = "boolean";
15+
public static final String BOOL_ARRAY = "boolean[]";
1016
public static final String BLOB = "blob";
17+
public static final String DATE = "date";
18+
public static final String DATE_ARRAY = "date[]";
19+
public static final String UUID = "uuid";
20+
public static final String UUID_ARRAY = "uuid[]";
1121

12-
public static final Set<String> KNOWN_TYPES = ImmutableSet.of(TEXT, INT, BLOB);
22+
public static final Set<String> KNOWN_TYPES = ImmutableSet.of(
23+
TEXT, INT, BLOB, BOOL, DATE, UUID, NUMBER,
24+
TEXT_ARRAY, INT_ARRAY, NUMBER_ARRAY, BOOL_ARRAY, DATE_ARRAY, UUID_ARRAY);
1325
}

src/main/java/io/weaviate/client6/v1/api/collections/Property.java

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,107 @@ public static Property text(String name) {
2424
}
2525

2626
public static Property text(String name, Function<Builder, ObjectBuilder<Property>> fn) {
27-
return fn.apply(new Builder(name, DataType.TEXT)).build();
27+
return newProperty(name, DataType.TEXT, fn);
28+
}
29+
30+
public static Property textArray(String name) {
31+
return textArray(name, ObjectBuilder.identity());
32+
}
33+
34+
public static Property textArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
35+
return newProperty(name, DataType.TEXT_ARRAY, fn);
2836
}
2937

3038
public static Property integer(String name) {
3139
return integer(name, ObjectBuilder.identity());
3240
}
3341

3442
public static Property integer(String name, Function<Builder, ObjectBuilder<Property>> fn) {
35-
return fn.apply(new Builder(name, DataType.INT)).build();
43+
return newProperty(name, DataType.INT, fn);
44+
}
45+
46+
public static Property integerArray(String name) {
47+
return integerArray(name, ObjectBuilder.identity());
48+
}
49+
50+
public static Property integerArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
51+
return newProperty(name, DataType.INT_ARRAY, fn);
3652
}
3753

3854
public static Property blob(String name) {
3955
return blob(name, ObjectBuilder.identity());
4056
}
4157

4258
public static Property blob(String name, Function<Builder, ObjectBuilder<Property>> fn) {
43-
return fn.apply(new Builder(name, DataType.BLOB)).build();
59+
return newProperty(name, DataType.BLOB, fn);
60+
}
61+
62+
public static Property bool(String name) {
63+
return bool(name, ObjectBuilder.identity());
64+
}
65+
66+
public static Property bool(String name, Function<Builder, ObjectBuilder<Property>> fn) {
67+
return newProperty(name, DataType.BOOL, fn);
68+
}
69+
70+
public static Property boolArray(String name) {
71+
return boolArray(name, ObjectBuilder.identity());
72+
}
73+
74+
public static Property boolArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
75+
return newProperty(name, DataType.BOOL_ARRAY, fn);
76+
}
77+
78+
public static Property date(String name) {
79+
return date(name, ObjectBuilder.identity());
80+
}
81+
82+
public static Property date(String name, Function<Builder, ObjectBuilder<Property>> fn) {
83+
return newProperty(name, DataType.DATE, fn);
84+
}
85+
86+
public static Property dateArray(String name) {
87+
return dateArray(name, ObjectBuilder.identity());
88+
}
89+
90+
public static Property dateArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
91+
return newProperty(name, DataType.DATE_ARRAY, fn);
92+
}
93+
94+
public static Property uuid(String name) {
95+
return uuid(name, ObjectBuilder.identity());
96+
}
97+
98+
public static Property uuid(String name, Function<Builder, ObjectBuilder<Property>> fn) {
99+
return newProperty(name, DataType.UUID, fn);
100+
}
101+
102+
public static Property uuidArray(String name) {
103+
return uuidArray(name, ObjectBuilder.identity());
104+
}
105+
106+
public static Property uuidArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
107+
return newProperty(name, DataType.UUID_ARRAY, fn);
108+
}
109+
110+
public static Property number(String name) {
111+
return number(name, ObjectBuilder.identity());
112+
}
113+
114+
public static Property number(String name, Function<Builder, ObjectBuilder<Property>> fn) {
115+
return newProperty(name, DataType.NUMBER, fn);
116+
}
117+
118+
public static Property numberArray(String name) {
119+
return numberArray(name, ObjectBuilder.identity());
120+
}
121+
122+
public static Property numberArray(String name, Function<Builder, ObjectBuilder<Property>> fn) {
123+
return newProperty(name, DataType.NUMBER_ARRAY, fn);
124+
}
125+
126+
private static Property newProperty(String name, String dataType, Function<Builder, ObjectBuilder<Property>> fn) {
127+
return fn.apply(new Builder(name, dataType)).build();
44128
}
45129

46130
public static ReferenceProperty reference(String name, String... collections) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.weaviate.client6.v1.api.collections.aggregate;
2+
3+
import java.util.function.Function;
4+
5+
import io.weaviate.client6.v1.internal.ObjectBuilder;
6+
7+
public final class Aggregate {
8+
/** Prevent public initialization. */
9+
private Aggregate() {
10+
}
11+
12+
public static final PropertyAggregation text(String property,
13+
Function<TextAggregation.Builder, ObjectBuilder<TextAggregation>> fn) {
14+
return TextAggregation.of(property, fn);
15+
}
16+
17+
public static final PropertyAggregation integer(String property,
18+
Function<IntegerAggregation.Builder, ObjectBuilder<IntegerAggregation>> fn) {
19+
return IntegerAggregation.of(property, fn);
20+
}
21+
22+
public static final PropertyAggregation bool(String property,
23+
Function<BooleanAggregation.Builder, ObjectBuilder<BooleanAggregation>> fn) {
24+
return BooleanAggregation.of(property, fn);
25+
}
26+
27+
public static final PropertyAggregation date(String property,
28+
Function<DateAggregation.Builder, ObjectBuilder<DateAggregation>> fn) {
29+
return DateAggregation.of(property, fn);
30+
}
31+
32+
public static final PropertyAggregation number(String property,
33+
Function<NumberAggregation.Builder, ObjectBuilder<NumberAggregation>> fn) {
34+
return NumberAggregation.of(property, fn);
35+
}
36+
}

src/main/java/io/weaviate/client6/v1/api/collections/aggregate/AggregateRequest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashMap;
55
import java.util.Map;
66

7+
import io.weaviate.client6.v1.internal.DateUtil;
78
import io.weaviate.client6.v1.api.collections.CollectionHandleDefaults;
89
import io.weaviate.client6.v1.internal.grpc.Rpc;
910
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateBlockingStub;
@@ -62,9 +63,21 @@ static <T> Rpc<AggregateRequest, WeaviateProtoAggregate.AggregateRequest, Aggreg
6263
var property = groupBy.getPathList().get(0);
6364

6465
if (groupBy.hasInt()) {
65-
groupedBy = new GroupedBy<Long>(property, groupBy.getInt());
66+
groupedBy = new GroupedBy<>(property, groupBy.getInt());
6667
} else if (groupBy.hasText()) {
67-
groupedBy = new GroupedBy<String>(property, groupBy.getText());
68+
groupedBy = new GroupedBy<>(property, groupBy.getText());
69+
} else if (groupBy.hasBoolean()) {
70+
groupedBy = new GroupedBy<>(property, groupBy.getBoolean());
71+
} else if (groupBy.hasNumber()) {
72+
groupedBy = new GroupedBy<>(property, groupBy.getNumber());
73+
} else if (groupBy.hasTexts()) {
74+
groupedBy = new GroupedBy<>(property, groupBy.getTexts().getValuesList().toArray(String[]::new));
75+
} else if (groupBy.hasInts()) {
76+
groupedBy = new GroupedBy<>(property, groupBy.getInts().getValuesList().toArray(Long[]::new));
77+
} else if (groupBy.hasNumbers()) {
78+
groupedBy = new GroupedBy<>(property, groupBy.getNumbers().getValuesList().toArray(Double[]::new));
79+
} else if (groupBy.hasBooleans()) {
80+
groupedBy = new GroupedBy<>(property, groupBy.getBooleans().getValuesList().toArray(Boolean[]::new));
6881
} else {
6982
assert false : "(aggregate) branch not covered";
7083
}
@@ -77,6 +90,7 @@ static <T> Rpc<AggregateRequest, WeaviateProtoAggregate.AggregateRequest, Aggreg
7790
}
7891
return new AggregateResponseGrouped(groups);
7992
}, () -> rpc.method(), () -> rpc.methodAsync());
93+
8094
}
8195

8296
private static Map<String, Object> unmarshalAggregation(WeaviateProtoAggregate.AggregateReply.Aggregations result) {
@@ -107,7 +121,32 @@ private static Map<String, Object> unmarshalAggregation(WeaviateProtoAggregate.A
107121
value = new TextAggregation.Values(
108122
metric.hasCount() ? metric.getCount() : null,
109123
topOccurrences);
110-
124+
} else if (aggregation.hasBoolean()) {
125+
var metric = aggregation.getBoolean();
126+
value = new BooleanAggregation.Values(
127+
metric.hasCount() ? metric.getCount() : null,
128+
metric.hasPercentageFalse() ? Float.valueOf((float) metric.getPercentageFalse()) : null,
129+
metric.hasPercentageTrue() ? Float.valueOf((float) metric.getPercentageTrue()) : null,
130+
metric.hasTotalFalse() ? metric.getTotalFalse() : null,
131+
metric.hasTotalTrue() ? metric.getTotalTrue() : null);
132+
} else if (aggregation.hasDate()) {
133+
var metric = aggregation.getDate();
134+
value = new DateAggregation.Values(
135+
metric.hasCount() ? metric.getCount() : null,
136+
metric.hasMinimum() ? DateUtil.fromISO8601(metric.getMinimum()) : null,
137+
metric.hasMaximum() ? DateUtil.fromISO8601(metric.getMaximum()) : null,
138+
metric.hasMedian() ? DateUtil.fromISO8601(metric.getMedian()) : null,
139+
metric.hasMode() ? DateUtil.fromISO8601(metric.getMode()) : null);
140+
} else if (aggregation.hasNumber()) {
141+
var metric = aggregation.getNumber();
142+
value = new NumberAggregation.Values(
143+
metric.hasCount() ? metric.getCount() : null,
144+
metric.hasMinimum() ? metric.getMinimum() : null,
145+
metric.hasMaximum() ? metric.getMaximum() : null,
146+
metric.hasMean() ? metric.getMean() : null,
147+
metric.hasMedian() ? metric.getMedian() : null,
148+
metric.hasMode() ? metric.getMode() : null,
149+
metric.hasSum() ? metric.getSum() : null);
111150
} else {
112151
assert false : "branch not covered";
113152
}

0 commit comments

Comments
 (0)