diff --git a/CHANGES.txt b/CHANGES.txt index 8e748649ce..9c7ca4e5d9 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -11,6 +11,7 @@ * Add the ability to disable bulk loading of SSTables (CASSANDRA-18781) * Clean up obsolete functions and simplify cql_version handling in cqlsh (CASSANDRA-18787) Merged from 5.0: + * Fix data corruption in VectorCodec when using heap buffers (CASSANDRA-19167) * Avoid over-skipping of key iterators from static column indexes during mixed intersections (CASSANDRA-19278) * Make concurrent_index_builders configurable at runtime (CASSANDRA-19266) * Fix storage_compatibility_mode for streaming (CASSANDRA-19126) diff --git a/src/java/org/apache/cassandra/cql3/functions/types/VectorCodec.java b/src/java/org/apache/cassandra/cql3/functions/types/VectorCodec.java index c54b8da7b8..3507ae3778 100644 --- a/src/java/org/apache/cassandra/cql3/functions/types/VectorCodec.java +++ b/src/java/org/apache/cassandra/cql3/functions/types/VectorCodec.java @@ -127,18 +127,20 @@ public abstract class VectorCodec extends TypeCodec> : String.format("Expected elements of uniform size, observed %d elements with total bytes %d", type.getDimensions(), bytes.remaining()); + ByteBuffer bb = bytes.slice(); ImmutableList.Builder values = ImmutableList.builder(); for (int i = 0; i < type.getDimensions(); ++i) { - ByteBuffer slice = bytes.slice(); - slice.limit(elementSize); - values.add(subtypeCodec.deserialize(slice, protocolVersion)); - bytes.position(bytes.position() + elementSize); + int originalPosition = bb.position(); + // Set the limit for the current element + bb.limit(originalPosition + elementSize); + values.add(subtypeCodec.deserialize(bb, protocolVersion)); + // Move to the start of the next element + bb.position(originalPosition + elementSize); + // Reset the limit to the end of the buffer + bb.limit(bb.capacity()); } - // Restore the input ByteBuffer to its original state - bytes.rewind(); - return values.build(); } } diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/CQLVectorTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/CQLVectorTest.java index 093e70f969..2c6ce589da 100644 --- a/test/unit/org/apache/cassandra/cql3/validation/operations/CQLVectorTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/CQLVectorTest.java @@ -405,7 +405,7 @@ public class CQLVectorTest extends CQLTester.InMemory Vector vector = vector(1, 2); execute("INSERT INTO %s (pk, value) VALUES (0, ?)", vector); - // identitiy function + // identity function String f = createFunction(KEYSPACE, "", "CREATE FUNCTION %s (x vector) " +