Skip to content
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

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

Open
wants to merge 25 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9e87033
INSERT and DELETE working
SiyaoIsHiding May 2, 2024
9ea8689
orderBy(annOf("c1", CqlVector.newInstance(0.1, 0.2, 0.3))))
SiyaoIsHiding May 6, 2024
b035763
fmt
SiyaoIsHiding May 6, 2024
943e3f7
DataTypes.custom("org.apache.cassandra.db.marshal.VectorType(org.apac…
SiyaoIsHiding May 7, 2024
d176f09
SchemaBuilder add tests
SiyaoIsHiding May 7, 2024
953fa47
Add blank line
SiyaoIsHiding May 7, 2024
52db4d5
customize ccm bridge
SiyaoIsHiding Aug 2, 2024
3f27134
Merge branch '4.x' into vector-support
SiyaoIsHiding Aug 2, 2024
1a3c314
works for DataTypes.vectorOf(DataTypes.FLOAT, 3)
SiyaoIsHiding Aug 12, 2024
3bbd519
Refactor CqlVector to generic type
SiyaoIsHiding Aug 14, 2024
1645b78
serialize size and migrated fixed&var length codecs, not tested
SiyaoIsHiding Aug 21, 2024
5d0087e
fix circle
SiyaoIsHiding Aug 21, 2024
3f75dd8
tinyint works
SiyaoIsHiding Aug 21, 2024
ae14fa4
adding integration tests of vector in all subtypes
SiyaoIsHiding Aug 21, 2024
522fab8
all tests passed
SiyaoIsHiding Aug 21, 2024
61bdf29
nested vector passed
SiyaoIsHiding Aug 22, 2024
d2ff145
add vectors of all types
SiyaoIsHiding Aug 22, 2024
6422932
add a ton of unit tests
SiyaoIsHiding Aug 29, 2024
14e908d
more tests and try to fix toString
SiyaoIsHiding Aug 30, 2024
5834c25
fmt
SiyaoIsHiding Sep 3, 2024
680a6a0
revert ccmbridge
SiyaoIsHiding Sep 3, 2024
86088d6
fmt
SiyaoIsHiding Sep 4, 2024
1c17c72
add revapi.json
SiyaoIsHiding Sep 5, 2024
aaf7fff
add revapi.json for query builder
SiyaoIsHiding Sep 5, 2024
7ef44b6
fix failed tests
SiyaoIsHiding Sep 6, 2024
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,11 @@
package com.datastax.oss.driver.api.core.data;

import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
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 Down Expand Up @@ -52,15 +52,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,7 +73,7 @@ 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);
}
Expand All @@ -87,15 +87,52 @@ public static <V extends Number> CqlVector<V> newInstance(List<V> list) {
* @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 == null || str.isEmpty() || 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 null;
}

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 @@ -196,7 +233,9 @@ public int hashCode() {

@Override
public String toString() {
return Iterables.toString(this.list);
if (this.list.isEmpty()) return "[]";
TypeCodec<T> subcodec = CodecRegistry.DEFAULT.codecFor(list.get(0));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we promised that CqlVector.from should mirror CqlVector.toString. CqlVector.from has access to a subtype codec and expects some values to be single-quoted, e.g. texts. Therefore, without this change to toString, a round-trip test like CqlVector.from(vector1.toString(), codec); will fail. However, fetching the default codec for the subtype looks like too much effort for something simple like toString.

return this.list.stream().map(subcodec::format).collect(Collectors.joining(", ", "[", "]"));
}

/**
Expand All @@ -205,7 +244,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 @@ -69,12 +66,21 @@ public static DataType custom(@NonNull String className) {

/* 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));
String paramsString =
className.substring(
DefaultVectorType.VECTOR_CLASS_NAME.length() + 1, className.length() - 1);
int lastCommaIndex = paramsString.lastIndexOf(',');
if (lastCommaIndex == -1) {
throw new IllegalArgumentException(
String.format(
"Invalid vector type %s, expected format is %s<subtype, dimensions>",
className, DefaultVectorType.VECTOR_CLASS_NAME));
}
String subTypeString = paramsString.substring(0, lastCommaIndex).trim();
String dimensionsString = paramsString.substring(lastCommaIndex + 1).trim();

DataType subType = classNameParser.parse(subTypeString, AttachmentPoint.NONE);
int dimensions = Integer.parseInt(dimensionsString);
if (dimensions <= 0) {
throw new IllegalArgumentException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.datastax.oss.driver.api.core.metadata.schema.AggregateMetadata;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
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.absent();
}
}
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 @@ -60,7 +60,8 @@ public String getClassName() {
@NonNull
@Override
public String asCql(boolean includeFrozen, boolean pretty) {
return String.format("'%s(%d)'", getClassName(), getDimensions());
return String.format(
"vector<%s, %d>", this.subtype.asCql(includeFrozen, pretty).toLowerCase(), getDimensions());
}

/* ============== General class implementation ============== */
Expand All @@ -78,7 +79,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 @@ -22,6 +22,7 @@
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.PrimitiveLongCodec;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
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 @@ -22,6 +22,7 @@
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
import com.datastax.oss.protocol.internal.util.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down Expand Up @@ -83,4 +84,10 @@ public ByteBuffer parse(@Nullable String value) {
? null
: Bytes.fromHexString(value);
}

@NonNull
@Override
public Optional<Integer> serializedSize() {
return Optional.absent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.PrimitiveBooleanCodec;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
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 @@ -19,6 +19,7 @@

import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
import edu.umd.cs.findbugs.annotations.NonNull;
import net.jcip.annotations.ThreadSafe;

Expand All @@ -29,4 +30,10 @@ public class CounterCodec extends BigIntCodec {
public DataType getCqlType() {
return DataTypes.COUNTER;
}

@NonNull
@Override
public Optional<Integer> serializedSize() {
return Optional.absent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
import com.datastax.oss.driver.internal.core.type.util.VIntCoding;
import com.datastax.oss.driver.shaded.guava.common.base.Optional;
import com.datastax.oss.driver.shaded.guava.common.io.ByteArrayDataOutput;
import com.datastax.oss.driver.shaded.guava.common.io.ByteStreams;
import com.datastax.oss.protocol.internal.util.Bytes;
Expand Down Expand Up @@ -115,4 +116,10 @@ public CqlDuration parse(@Nullable String value) {
? null
: CqlDuration.from(value);
}

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