Skip to content

JAVA-3143: Extend driver vector support to arbitrary subtypes and fix handling of variable length types (OSS C* 5.0) #1952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2025
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
297 changes: 297 additions & 0 deletions core/revapi.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
package com.datastax.oss.driver.api.core.data;

import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.internal.core.type.codec.ParseUtils;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import com.datastax.oss.driver.shaded.guava.common.base.Predicates;
import com.datastax.oss.driver.shaded.guava.common.base.Splitter;
import com.datastax.oss.driver.shaded.guava.common.collect.Iterables;
import com.datastax.oss.driver.shaded.guava.common.collect.Streams;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.io.InvalidObjectException;
Expand All @@ -35,7 +34,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand All @@ -52,15 +50,15 @@
* where possible we've tried to make the API of this class similar to the equivalent methods on
* {@link List}.
*/
public class CqlVector<T extends Number> implements Iterable<T>, Serializable {
public class CqlVector<T> implements Iterable<T>, Serializable {

/**
* Create a new CqlVector containing the specified values.
*
* @param vals the collection of values to wrap.
* @return a CqlVector wrapping those values
*/
public static <V extends Number> CqlVector<V> newInstance(V... vals) {
public static <V> CqlVector<V> newInstance(V... vals) {

// Note that Array.asList() guarantees the return of an array which implements RandomAccess
return new CqlVector(Arrays.asList(vals));
Expand All @@ -73,29 +71,64 @@ public static <V extends Number> CqlVector<V> newInstance(V... vals) {
* @param list the collection of values to wrap.
* @return a CqlVector wrapping those values
*/
public static <V extends Number> CqlVector<V> newInstance(List<V> list) {
public static <V> CqlVector<V> newInstance(List<V> list) {
Preconditions.checkArgument(list != null, "Input list should not be null");
return new CqlVector(list);
}

/**
* Create a new CqlVector instance from the specified string representation. Note that this method
* is intended to mirror {@link #toString()}; passing this method the output from a <code>toString
* </code> call on some CqlVector should return a CqlVector that is equal to the origin instance.
* Create a new CqlVector instance from the specified string representation.
*
* @param str a String representation of a CqlVector
* @param subtypeCodec
* @return a new CqlVector built from the String representation
*/
public static <V extends Number> CqlVector<V> from(
@NonNull String str, @NonNull TypeCodec<V> subtypeCodec) {
public static <V> CqlVector<V> from(@NonNull String str, @NonNull TypeCodec<V> subtypeCodec) {
Preconditions.checkArgument(str != null, "Cannot create CqlVector from null string");
Preconditions.checkArgument(!str.isEmpty(), "Cannot create CqlVector from empty string");
ArrayList<V> vals =
Streams.stream(Splitter.on(", ").split(str.substring(1, str.length() - 1)))
.map(subtypeCodec::parse)
.collect(Collectors.toCollection(ArrayList::new));
return new CqlVector(vals);
if (str.equalsIgnoreCase("NULL")) return null;

int idx = ParseUtils.skipSpaces(str, 0);
if (str.charAt(idx++) != '[')
throw new IllegalArgumentException(
String.format(
"Cannot parse vector value from \"%s\", at character %d expecting '[' but got '%c'",
str, idx, str.charAt(idx)));

idx = ParseUtils.skipSpaces(str, idx);

if (str.charAt(idx) == ']') {
return new CqlVector<>(new ArrayList<>());
}

List<V> list = new ArrayList<>();
while (idx < str.length()) {
int n;
try {
n = ParseUtils.skipCQLValue(str, idx);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
String.format(
"Cannot parse vector value from \"%s\", invalid CQL value at character %d",
str, idx),
e);
}

list.add(subtypeCodec.parse(str.substring(idx, n)));
idx = n;

idx = ParseUtils.skipSpaces(str, idx);
if (str.charAt(idx) == ']') return new CqlVector<>(list);
if (str.charAt(idx++) != ',')
throw new IllegalArgumentException(
String.format(
"Cannot parse vector value from \"%s\", at character %d expecting ',' but got '%c'",
str, idx, str.charAt(idx)));

idx = ParseUtils.skipSpaces(str, idx);
}
throw new IllegalArgumentException(
String.format("Malformed vector value \"%s\", missing closing ']'", str));
}

private final List<T> list;
Expand Down Expand Up @@ -194,6 +227,11 @@ public int hashCode() {
return Objects.hash(list);
}

/**
* The string representation of the vector. Elements, like strings, may not be properly quoted.
*
* @return the string representation
*/
@Override
public String toString() {
return Iterables.toString(this.list);
Expand All @@ -205,7 +243,7 @@ public String toString() {
*
* @param <T> inner type of CqlVector, assume Number is always Serializable.
*/
private static class SerializationProxy<T extends Number> implements Serializable {
private static class SerializationProxy<T> implements Serializable {

private static final long serialVersionUID = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ default CqlDuration getCqlDuration(@NonNull CqlIdentifier id) {
* @throws IllegalArgumentException if the id is invalid.
*/
@Nullable
default <ElementT extends Number> CqlVector<ElementT> getVector(
default <ElementT> CqlVector<ElementT> getVector(
@NonNull CqlIdentifier id, @NonNull Class<ElementT> elementsClass) {
return getVector(firstIndexOf(id), elementsClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ default CqlDuration getCqlDuration(int i) {
* @throws IndexOutOfBoundsException if the index is invalid.
*/
@Nullable
default <ElementT extends Number> CqlVector<ElementT> getVector(
int i, @NonNull Class<ElementT> elementsClass) {
default <ElementT> CqlVector<ElementT> getVector(int i, @NonNull Class<ElementT> elementsClass) {
return get(i, GenericType.vectorOf(elementsClass));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ default CqlDuration getCqlDuration(@NonNull String name) {
* @throws IllegalArgumentException if the name is invalid.
*/
@Nullable
default <ElementT extends Number> CqlVector<ElementT> getVector(
default <ElementT> CqlVector<ElementT> getVector(
@NonNull String name, @NonNull Class<ElementT> elementsClass) {
return getVector(firstIndexOf(name), elementsClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ default SelfT setCqlDuration(@NonNull CqlIdentifier id, @Nullable CqlDuration v)
*/
@NonNull
@CheckReturnValue
default <ElementT extends Number> SelfT setVector(
default <ElementT> SelfT setVector(
@NonNull CqlIdentifier id,
@Nullable CqlVector<ElementT> v,
@NonNull Class<ElementT> elementsClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ default SelfT setCqlDuration(int i, @Nullable CqlDuration v) {
*/
@NonNull
@CheckReturnValue
default <ElementT extends Number> SelfT setVector(
default <ElementT> SelfT setVector(
int i, @Nullable CqlVector<ElementT> v, @NonNull Class<ElementT> elementsClass) {
return set(i, v, GenericType.vectorOf(elementsClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ default SelfT setCqlDuration(@NonNull String name, @Nullable CqlDuration v) {
*/
@NonNull
@CheckReturnValue
default <ElementT extends Number> SelfT setVector(
default <ElementT> SelfT setVector(
@NonNull String name,
@Nullable CqlVector<ElementT> v,
@NonNull Class<ElementT> elementsClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
import com.datastax.oss.driver.internal.core.type.DefaultTupleType;
import com.datastax.oss.driver.internal.core.type.DefaultVectorType;
import com.datastax.oss.driver.internal.core.type.PrimitiveType;
import com.datastax.oss.driver.shaded.guava.common.base.Splitter;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Arrays;
import java.util.List;

/** Constants and factory methods to obtain data type instances. */
public class DataTypes {
Expand All @@ -59,7 +57,6 @@ public class DataTypes {
public static final DataType DURATION = new PrimitiveType(ProtocolConstants.DataType.DURATION);

private static final DataTypeClassNameParser classNameParser = new DataTypeClassNameParser();
private static final Splitter paramSplitter = Splitter.on(',').trimResults();

@NonNull
public static DataType custom(@NonNull String className) {
Expand All @@ -68,20 +65,8 @@ public static DataType custom(@NonNull String className) {
if (className.equals("org.apache.cassandra.db.marshal.DurationType")) return DURATION;

/* Vector support is currently implemented as a custom type but is also parameterized */
if (className.startsWith(DefaultVectorType.VECTOR_CLASS_NAME)) {
List<String> params =
paramSplitter.splitToList(
className.substring(
DefaultVectorType.VECTOR_CLASS_NAME.length() + 1, className.length() - 1));
DataType subType = classNameParser.parse(params.get(0), AttachmentPoint.NONE);
int dimensions = Integer.parseInt(params.get(1));
if (dimensions <= 0) {
throw new IllegalArgumentException(
String.format(
"Request to create vector of size %d, size must be positive", dimensions));
}
return new DefaultVectorType(subType, dimensions);
}
if (className.startsWith(DefaultVectorType.VECTOR_CLASS_NAME))
return classNameParser.parse(className, AttachmentPoint.NONE);
return new DefaultCustomType(className);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.Optional;

/**
* Manages the two-way conversion between a CQL type and a Java type.
Expand Down Expand Up @@ -234,4 +235,9 @@ default boolean accepts(@NonNull DataType cqlType) {
*/
@Nullable
JavaTypeT parse(@Nullable String value);

@NonNull
default Optional<Integer> serializedSize() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ public static TypeCodec<TupleValue> tupleOf(@NonNull TupleType cqlType) {
return new TupleCodec(cqlType);
}

public static <SubtypeT extends Number> TypeCodec<CqlVector<SubtypeT>> vectorOf(
public static <SubtypeT> TypeCodec<CqlVector<SubtypeT>> vectorOf(
@NonNull VectorType type, @NonNull TypeCodec<SubtypeT> subtypeCodec) {
return new VectorCodec(
DataTypes.vectorOf(subtypeCodec.getCqlType(), type.getDimensions()), subtypeCodec);
}

public static <SubtypeT extends Number> TypeCodec<CqlVector<SubtypeT>> vectorOf(
public static <SubtypeT> TypeCodec<CqlVector<SubtypeT>> vectorOf(
int dimensions, @NonNull TypeCodec<SubtypeT> subtypeCodec) {
return new VectorCodec(DataTypes.vectorOf(subtypeCodec.getCqlType(), dimensions), subtypeCodec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,15 @@ public static <T> GenericType<Set<T>> setOf(@NonNull GenericType<T> elementType)
}

@NonNull
public static <T extends Number> GenericType<CqlVector<T>> vectorOf(
@NonNull Class<T> elementType) {
public static <T> GenericType<CqlVector<T>> vectorOf(@NonNull Class<T> elementType) {
TypeToken<CqlVector<T>> token =
new TypeToken<CqlVector<T>>() {}.where(
new TypeParameter<T>() {}, TypeToken.of(elementType));
return new GenericType<>(token);
}

@NonNull
public static <T extends Number> GenericType<CqlVector<T>> vectorOf(
@NonNull GenericType<T> elementType) {
public static <T> GenericType<CqlVector<T>> vectorOf(@NonNull GenericType<T> elementType) {
TypeToken<CqlVector<T>> token =
new TypeToken<CqlVector<T>>() {}.where(new TypeParameter<T>() {}, elementType.token);
return new GenericType<>(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.datastax.oss.protocol.internal.util.Bytes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -164,6 +165,13 @@ private DataType parse(
return new DefaultTupleType(componentTypesBuilder.build(), attachmentPoint);
}

if (next.startsWith("org.apache.cassandra.db.marshal.VectorType")) {
Iterator<String> rawTypes = parser.getTypeParameters().iterator();
DataType subtype = parse(rawTypes.next(), userTypes, attachmentPoint, logPrefix);
int dimensions = Integer.parseInt(rawTypes.next());
return DataTypes.vectorOf(subtype, dimensions);
}

DataType type = NATIVE_TYPES_BY_CLASS_NAME.get(next);
return type == null ? DataTypes.custom(toParse) : type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), subtype, dimensions);
return Objects.hash(DefaultVectorType.class, subtype, dimensions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.Optional;
import net.jcip.annotations.ThreadSafe;

@ThreadSafe
Expand Down Expand Up @@ -90,4 +91,10 @@ public Long parse(@Nullable String value) {
String.format("Cannot parse 64-bits long value from \"%s\"", value));
}
}

@NonNull
@Override
public Optional<Integer> serializedSize() {
return Optional.of(8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.Optional;
import net.jcip.annotations.ThreadSafe;

@ThreadSafe
Expand Down Expand Up @@ -98,4 +99,10 @@ public Boolean parse(@Nullable String value) {
String.format("Cannot parse boolean value from \"%s\"", value));
}
}

@NonNull
@Override
public Optional<Integer> serializedSize() {
return Optional.of(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.Optional;
import net.jcip.annotations.ThreadSafe;

@ThreadSafe
Expand Down Expand Up @@ -90,4 +91,10 @@ public Double parse(@Nullable String value) {
String.format("Cannot parse 64-bits double value from \"%s\"", value));
}
}

@NonNull
@Override
public Optional<Integer> serializedSize() {
return Optional.of(8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.Optional;
import net.jcip.annotations.ThreadSafe;

@ThreadSafe
Expand Down Expand Up @@ -90,4 +91,10 @@ public Float parse(@Nullable String value) {
String.format("Cannot parse 32-bits float value from \"%s\"", value));
}
}

@NonNull
@Override
public Optional<Integer> serializedSize() {
return Optional.of(4);
}
}
Loading