21
21
package com .apple .foundationdb .record .query .plan .cascades ;
22
22
23
23
import com .apple .foundationdb .record .EvaluationContext ;
24
+ import com .apple .foundationdb .record .TupleFieldsProto ;
24
25
import com .apple .foundationdb .record .query .plan .cascades .typing .Type ;
25
26
import com .apple .foundationdb .record .query .plan .cascades .typing .TypeRepository ;
26
27
import com .apple .foundationdb .record .query .plan .cascades .typing .Typed ;
40
41
import java .util .Map ;
41
42
import java .util .Optional ;
42
43
import java .util .Random ;
44
+ import java .util .UUID ;
43
45
import java .util .stream .Collectors ;
44
46
45
47
/**
@@ -64,6 +66,7 @@ class TypeRepositoryTest {
64
66
private static final LiteralValue <Integer > INT_NOT_NULLABLE_2 = new LiteralValue <>(Type .primitiveType (Type .TypeCode .INT , false ), 2 );
65
67
private static final LiteralValue <Float > FLOAT_1 = new LiteralValue <>(Type .primitiveType (Type .TypeCode .FLOAT ), 1.0F );
66
68
private static final LiteralValue <String > STRING_1 = new LiteralValue <>(Type .primitiveType (Type .TypeCode .STRING ), "a" );
69
+ private static final LiteralValue <UUID > UUID_1 = new LiteralValue <>(Type .uuidType (false ), UUID .fromString ("eebee473-690b-48c1-beb0-07c3aca77768" ));
67
70
68
71
private static Type generateRandomType () {
69
72
return generateRandomTypeInternal (0 );
@@ -82,6 +85,9 @@ private static int countTypes(@Nonnull final Type type) {
82
85
if (type .isPrimitive ()) {
83
86
return 0 ;
84
87
}
88
+ if (type .isUuid ()) {
89
+ return 1 ;
90
+ }
85
91
if (type instanceof Type .Array ) {
86
92
if (type .isNullable ()) {
87
93
return 1 + countTypes (((Type .Array )type ).getElementType ());
@@ -104,7 +110,7 @@ private static Type.TypeCode generateRandomTypeCode(int depth) {
104
110
List <Type .TypeCode > choices = new ArrayList <>();
105
111
for (Type .TypeCode typeCode : Type .TypeCode .values ()) {
106
112
if (typeCode != Type .TypeCode .UNKNOWN && typeCode != Type .TypeCode .ANY && typeCode != Type .TypeCode .NULL ) {
107
- if (typeCode .isPrimitive ()) {
113
+ if (typeCode .isPrimitive () || typeCode . equals ( Type . TypeCode . UUID ) ) {
108
114
choices .add (typeCode );
109
115
} else if (depth < MAX_ALLOWED_DEPTH && (typeCode == Type .TypeCode .RECORD || typeCode == Type .TypeCode .ARRAY )) {
110
116
choices .add (typeCode );
@@ -135,6 +141,8 @@ private static Type generateType(int depth, Type.TypeCode requestedTypeCode) {
135
141
fields .add (Type .Record .Field .of (generateRandomTypeInternal (depth + 1 ), Optional .of ("random" + ++counter ), Optional .empty ()));
136
142
}
137
143
return Type .Record .fromFields (fields );
144
+ case UUID :
145
+ return Type .uuidType (random .nextBoolean ());
138
146
case RELATION : // fallthrough
139
147
case UNKNOWN : // fallthrough
140
148
case ANY : // fallthrough
@@ -251,18 +259,19 @@ void createLightArrayConstructorValueWorks() {
251
259
252
260
@ Test
253
261
void createRecordTypeConstructorWorks () {
254
- final Typed value = new RecordConstructorValue .RecordFn ().encapsulate (List .of (STRING_1 , INT_2 , FLOAT_1 ));
262
+ final Typed value = new RecordConstructorValue .RecordFn ().encapsulate (List .of (STRING_1 , INT_2 , FLOAT_1 , UUID_1 ));
255
263
Assertions .assertTrue (value instanceof RecordConstructorValue );
256
264
final RecordConstructorValue recordConstructorValue = (RecordConstructorValue )value ;
257
265
final Type resultType = recordConstructorValue .getResultType ();
258
266
Assertions .assertEquals (Type .Record .fromFields (false , List .of (Type .Record .Field .of (STRING_1 .getResultType (), Optional .empty ()),
259
267
Type .Record .Field .of (INT_2 .getResultType (), Optional .empty ()),
260
- Type .Record .Field .of (FLOAT_1 .getResultType (), Optional .empty ())
268
+ Type .Record .Field .of (FLOAT_1 .getResultType (), Optional .empty ()),
269
+ Type .Record .Field .of (UUID_1 .getResultType (), Optional .empty ())
261
270
)), resultType );
262
271
final Object result = recordConstructorValue .evalWithoutStore (EvaluationContext .forTypeRepository (TypeRepository .newBuilder ().addTypeIfNeeded (recordConstructorValue .getResultType ()).build ()));
263
272
Assertions .assertTrue (result instanceof DynamicMessage );
264
273
final DynamicMessage resultMessage = (DynamicMessage )result ;
265
- Assertions .assertEquals (3 , resultMessage .getAllFields ().size ());
274
+ Assertions .assertEquals (4 , resultMessage .getAllFields ().size ());
266
275
List <Object > fieldSorted = resultMessage .getAllFields ().entrySet ().stream ()
267
276
.map (kv -> Pair .of (kv .getKey ().getIndex (), kv .getValue ()))
268
277
.sorted (Map .Entry .comparingByKey ())
@@ -271,5 +280,10 @@ void createRecordTypeConstructorWorks() {
271
280
Assertions .assertEquals ("a" , fieldSorted .get (0 ));
272
281
Assertions .assertEquals (2 , fieldSorted .get (1 ));
273
282
Assertions .assertEquals (1.0F , fieldSorted .get (2 ));
283
+ final var expectedUuid = UUID .fromString ("eebee473-690b-48c1-beb0-07c3aca77768" );
284
+ Assertions .assertEquals (TupleFieldsProto .UUID .newBuilder ()
285
+ .setMostSignificantBits (expectedUuid .getMostSignificantBits ())
286
+ .setLeastSignificantBits (expectedUuid .getLeastSignificantBits ())
287
+ .build (), fieldSorted .get (3 ));
274
288
}
275
289
}
0 commit comments