Skip to content

Commit 25c7c71

Browse files
Merge branch 'cassandra-5.0' into trunk
* cassandra-5.0: Fix CQLSSTableWriter serialization of vector of date and time patch by Lukasz Antoniak; reviewed by Andres de la Pena, Yifan Cai for CASSANDRA-20979
2 parents d2c48fa + 0136fc9 commit 25c7c71

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
* Add the ability to disable bulk loading of SSTables (CASSANDRA-18781)
272272
* Clean up obsolete functions and simplify cql_version handling in cqlsh (CASSANDRA-18787)
273273
Merged from 5.0:
274+
* Fix CQLSSTableWriter serialization of vector of date and time (CASSANDRA-20979)
274275
* Correctly calculate default for FailureDetector max interval (CASSANDRA-21025)
275276
* Adding missing configs in system_views.settings to be backward compatible (CASSANDRA-20863)
276277
* Heap dump should not be generated on handled exceptions (CASSANDRA-20974)

src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,12 +1787,6 @@ private DateCodec()
17871787
super(DataType.date(), LocalDate.class);
17881788
}
17891789

1790-
@Override
1791-
public int serializedSize()
1792-
{
1793-
return 8;
1794-
}
1795-
17961790
@Override
17971791
public LocalDate parse(String value)
17981792
{
@@ -1876,6 +1870,13 @@ private TimeCodec()
18761870
super(DataType.time());
18771871
}
18781872

1873+
@Override
1874+
public int serializedSize()
1875+
{
1876+
// matching behavior of TimeType, which is not declared as fixed length
1877+
return VARIABLE_LENGTH;
1878+
}
1879+
18791880
@Override
18801881
public Long parse(String value)
18811882
{

test/unit/org/apache/cassandra/io/sstable/CQLSSTableWriterTest.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import java.util.concurrent.ExecutionException;
3636
import java.util.concurrent.TimeUnit;
3737
import java.util.concurrent.atomic.AtomicInteger;
38+
import java.util.function.BiConsumer;
3839
import java.util.function.BiPredicate;
40+
import java.util.function.Function;
3941
import java.util.stream.Collectors;
4042
import java.util.stream.Stream;
4143
import java.util.stream.StreamSupport;
@@ -50,6 +52,7 @@
5052

5153
import com.datastax.driver.core.utils.UUIDs;
5254
import org.apache.cassandra.Util;
55+
import org.apache.cassandra.cql3.CQL3Type;
5356
import org.apache.cassandra.cql3.QueryProcessor;
5457
import org.apache.cassandra.cql3.UntypedResultSet;
5558
import org.apache.cassandra.cql3.constraints.ConstraintViolationException;
@@ -61,7 +64,10 @@
6164
import org.apache.cassandra.db.compression.CompressionDictionary;
6265
import org.apache.cassandra.db.compression.CompressionDictionary.DictId;
6366
import org.apache.cassandra.db.compression.ZstdCompressionDictionary;
67+
import org.apache.cassandra.db.marshal.AbstractType;
6468
import org.apache.cassandra.db.marshal.FloatType;
69+
import org.apache.cassandra.db.marshal.SimpleDateType;
70+
import org.apache.cassandra.db.marshal.TimeType;
6571
import org.apache.cassandra.db.marshal.UTF8Type;
6672
import org.apache.cassandra.dht.ByteOrderedPartitioner;
6773
import org.apache.cassandra.dht.Murmur3Partitioner;
@@ -1621,9 +1627,36 @@ public void testSkipBuildingIndexesWithSAI() throws Exception
16211627
@Test
16221628
public void testWritingVectorData() throws Exception
16231629
{
1630+
testWritingVectorData(CQL3Type.Native.FLOAT, FloatType.instance, (i) -> (float) i, (i, vector) -> {
1631+
assertThat(vector).allMatch(val -> val instanceof Float);
1632+
assertThat(vector).allMatch(val -> (float) val == (float) i);
1633+
});
1634+
1635+
perTestSetup();
1636+
1637+
testWritingVectorData(CQL3Type.Native.DATE, SimpleDateType.instance, LocalDate::fromDaysSinceEpoch, (i, vector) -> {
1638+
assertThat(vector).allMatch(val -> val instanceof Integer);
1639+
assertThat(vector).allMatch(val -> {
1640+
int days = (int) val - Integer.MIN_VALUE; // signed to unsigned conversion
1641+
return days == i;
1642+
});
1643+
});
1644+
1645+
perTestSetup();
1646+
1647+
testWritingVectorData(CQL3Type.Native.TIME, TimeType.instance, (i) -> (long) i, (i, vector) -> {
1648+
assertThat(vector).allMatch(val -> val instanceof Long);
1649+
assertThat(vector).allMatch(val -> (long) val == (long) i);
1650+
});
1651+
}
1652+
1653+
private void testWritingVectorData(CQL3Type.Native cqlType, AbstractType<?> subType, Function<Integer, ?> valueFactory,
1654+
BiConsumer<Integer, List<?>> checkFunction) throws Exception
1655+
{
1656+
final int dimensions = 5;
16241657
final String schema = "CREATE TABLE " + qualifiedTable + " ("
16251658
+ " k int,"
1626-
+ " v1 VECTOR<FLOAT, 5>,"
1659+
+ " v1 VECTOR<" + cqlType.name() + ", " + dimensions + ">,"
16271660
+ " PRIMARY KEY (k)"
16281661
+ ")";
16291662

@@ -1635,7 +1668,12 @@ public void testWritingVectorData() throws Exception
16351668

16361669
for (int i = 0; i < 100; i++)
16371670
{
1638-
writer.addRow(i, List.of( (float)i, (float)i, (float)i, (float)i, (float)i));
1671+
List<Object> vector = new ArrayList<>(dimensions);
1672+
for (int j = 0; j < dimensions; j++)
1673+
{
1674+
vector.add(valueFactory.apply(i));
1675+
}
1676+
writer.addRow(i, vector);
16391677
}
16401678

16411679
writer.close();
@@ -1650,10 +1688,9 @@ public void testWritingVectorData() throws Exception
16501688
for (UntypedResultSet.Row row : resultSet)
16511689
{
16521690
assertEquals(cnt, row.getInt("k"));
1653-
List<Float> vector = row.getVector("v1", FloatType.instance, 5);
1654-
assertThat(vector).hasSize(5);
1655-
final float floatCount = (float)cnt;
1656-
assertThat(vector).allMatch(val -> val == floatCount);
1691+
List<?> vector = row.getVector("v1", subType, dimensions);
1692+
assertThat(vector).hasSize(dimensions);
1693+
checkFunction.accept(cnt, vector);
16571694
cnt++;
16581695
}
16591696
}

0 commit comments

Comments
 (0)