Skip to content

Commit 61bdf29

Browse files
committed
nested vector passed
1 parent 522fab8 commit 61bdf29

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/type/DataTypes.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,20 @@ public static DataType custom(@NonNull String className) {
6969

7070
/* Vector support is currently implemented as a custom type but is also parameterized */
7171
if (className.startsWith(DefaultVectorType.VECTOR_CLASS_NAME)) {
72-
List<String> params =
73-
paramSplitter.splitToList(
74-
className.substring(
75-
DefaultVectorType.VECTOR_CLASS_NAME.length() + 1, className.length() - 1));
76-
DataType subType = classNameParser.parse(params.get(0), AttachmentPoint.NONE);
77-
int dimensions = Integer.parseInt(params.get(1));
72+
String paramsString = className.substring(
73+
DefaultVectorType.VECTOR_CLASS_NAME.length() + 1, className.length() - 1);
74+
int lastCommaIndex = paramsString.lastIndexOf(',');
75+
if (lastCommaIndex == -1) {
76+
throw new IllegalArgumentException(
77+
String.format(
78+
"Invalid vector type %s, expected format is %s<subtype, dimensions>",
79+
className, DefaultVectorType.VECTOR_CLASS_NAME));
80+
}
81+
String subTypeString = paramsString.substring(0, lastCommaIndex).trim();
82+
String dimensionsString = paramsString.substring(lastCommaIndex + 1).trim();
83+
84+
DataType subType = classNameParser.parse(subTypeString, AttachmentPoint.NONE);
85+
int dimensions = Integer.parseInt(dimensionsString);
7886
if (dimensions <= 0) {
7987
throw new IllegalArgumentException(
8088
String.format(

core/src/main/java/com/datastax/oss/driver/internal/core/type/codec/VectorCodec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
2626
import com.datastax.oss.driver.internal.core.type.DefaultVectorType;
2727
import com.datastax.oss.driver.internal.core.type.util.VIntCoding;
28+
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
2829
import com.datastax.oss.driver.shaded.guava.common.collect.Iterables;
2930
import edu.umd.cs.findbugs.annotations.NonNull;
3031
import edu.umd.cs.findbugs.annotations.Nullable;
@@ -69,6 +70,11 @@ public GenericType<CqlVector<SubtypeT>> getJavaType() {
6970
return this.javaType;
7071
}
7172

73+
@NonNull
74+
public Optional<Integer> serializedSize(){
75+
return subtypeCodec.serializedSize().isPresent() ? Optional.of(subtypeCodec.serializedSize().get() * cqlType.getDimensions()) : Optional.absent();
76+
}
77+
7278
@NonNull
7379
@Override
7480
public DataType getCqlType() {

core/src/test/java/com/datastax/oss/driver/internal/core/type/codec/registry/CachingCodecRegistryTestDataProviders.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ public static Object[][] collectionsWithCqlAndJavaTypes()
337337
GenericType.vectorOf(BigInteger.class),
338338
CqlVector.newInstance(BigInteger.ONE)
339339
},
340+
// // vector with arbitrary types
341+
// {
342+
// DataTypes.vectorOf(DataTypes.listOf(DataTypes.INT), 1),
343+
//
344+
// }
340345
};
341346
}
342347

integration-tests/src/test/java/com/datastax/oss/driver/core/data/DataTypeIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ public static Object[][] typeSamples() {
271271
samples.add(new Object[] {DataTypes.vectorOf(dataType, 1), vector});
272272
}
273273

274+
if (o[1].equals("ascii")){
275+
// once
276+
CqlVector<?> vector = CqlVector.newInstance(CqlVector.newInstance(1, 2));
277+
samples.add(new Object[] {DataTypes.vectorOf(DataTypes.vectorOf(DataTypes.INT, 2), 1), vector});
278+
}
274279
return samples.stream();
275280
})
276281
.toArray(Object[][]::new);

0 commit comments

Comments
 (0)