Skip to content

Commit

Permalink
Fix ByteBufferAccessor cast exceptions are thrown when trying to quer…
Browse files Browse the repository at this point in the history
…y a virtual table

Patch by Alex Petrov and Caleb Rackliffe; reviewed by David Capwell and Chris Lohfink for CASSANDRA-16155

Co-authored-by: Caleb Rackliffe <calebrackliffe@gmail.com>
  • Loading branch information
ifesdjeen and maedhroz committed Oct 12, 2020
1 parent 1728da3 commit 896baf6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 18 deletions.
23 changes: 17 additions & 6 deletions src/java/org/apache/cassandra/db/virtual/SimpleDataSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@
package org.apache.cassandra.db.virtual;

import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

import com.google.common.collect.Iterables;

import org.apache.cassandra.db.*;
import org.apache.cassandra.db.Clustering;
import org.apache.cassandra.db.DecoratedKey;
import org.apache.cassandra.db.DeletionTime;
import org.apache.cassandra.db.RegularAndStaticColumns;
import org.apache.cassandra.db.filter.ClusteringIndexFilter;
import org.apache.cassandra.db.filter.ColumnFilter;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.ByteBufferAccessor;
import org.apache.cassandra.db.marshal.CompositeType;
import org.apache.cassandra.db.rows.*;
import org.apache.cassandra.db.rows.AbstractUnfilteredRowIterator;
import org.apache.cassandra.db.rows.BTreeRow;
import org.apache.cassandra.db.rows.BufferCell;
import org.apache.cassandra.db.rows.EncodingStats;
import org.apache.cassandra.db.rows.Rows;
import org.apache.cassandra.db.rows.Unfiltered;
import org.apache.cassandra.db.rows.UnfilteredRowIterator;
import org.apache.cassandra.schema.ColumnMetadata;
import org.apache.cassandra.schema.TableMetadata;
import org.apache.cassandra.utils.ByteBufferUtil;
Expand Down Expand Up @@ -84,7 +95,7 @@ private DecoratedKey makeDecoratedKey(Object... partitionKeyValues)
{
ByteBuffer partitionKey = partitionKeyValues.length == 1
? decompose(metadata.partitionKeyType, partitionKeyValues[0])
: ((CompositeType) metadata.partitionKeyType).decompose(ByteBufferAccessor.instance, partitionKeyValues);
: ((CompositeType) metadata.partitionKeyType).decompose(partitionKeyValues);
return metadata.partitioner.decorateKey(partitionKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class VirtualTableTest extends CQLTester
private static final String KS_NAME = "test_virtual_ks";
private static final String VT1_NAME = "vt1";
private static final String VT2_NAME = "vt2";
private static final String VT3_NAME = "vt3";

private static class WritableVirtualTable extends AbstractVirtualTable
{
Expand Down Expand Up @@ -80,24 +81,24 @@ public void apply(PartitionUpdate update)
{
String key = (String) metadata().partitionKeyType.compose(update.partitionKey().getKey());
update.forEach(row ->
{
Integer value = Int32Type.instance.compose(row.getCell(valueColumn).buffer());
backingMap.put(key, value);
});
{
Integer value = Int32Type.instance.compose(row.getCell(valueColumn).buffer());
backingMap.put(key, value);
});
}
}

@BeforeClass
public static void setUpClass()
{
TableMetadata vt1Metadata =
TableMetadata.builder(KS_NAME, VT1_NAME)
.kind(TableMetadata.Kind.VIRTUAL)
.addPartitionKeyColumn("pk", UTF8Type.instance)
.addClusteringColumn("c", UTF8Type.instance)
.addRegularColumn("v1", Int32Type.instance)
.addRegularColumn("v2", LongType.instance)
.build();
TableMetadata.builder(KS_NAME, VT1_NAME)
.kind(TableMetadata.Kind.VIRTUAL)
.addPartitionKeyColumn("pk", UTF8Type.instance)
.addClusteringColumn("c", UTF8Type.instance)
.addRegularColumn("v1", Int32Type.instance)
.addRegularColumn("v2", LongType.instance)
.build();

SimpleDataSet vt1data = new SimpleDataSet(vt1Metadata);

Expand All @@ -117,7 +118,30 @@ public DataSet data()
};
VirtualTable vt2 = new WritableVirtualTable(KS_NAME, VT2_NAME);

VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(vt1, vt2)));
TableMetadata vt3Metadata =
TableMetadata.builder(KS_NAME, VT3_NAME)
.kind(TableMetadata.Kind.VIRTUAL)
.addPartitionKeyColumn("pk1", UTF8Type.instance)
.addPartitionKeyColumn("pk2", UTF8Type.instance)
.addClusteringColumn("ck1", UTF8Type.instance)
.addClusteringColumn("ck2", UTF8Type.instance)
.addRegularColumn("v1", Int32Type.instance)
.addRegularColumn("v2", LongType.instance)
.build();

SimpleDataSet vt3data = new SimpleDataSet(vt3Metadata);

vt3data.row("pk11", "pk11", "ck11", "ck11").column("v1", 1111).column("v2", 1111L)
.row("pk11", "pk11", "ck22", "ck22").column("v1", 1122).column("v2", 1122L);

VirtualTable vt3 = new AbstractVirtualTable(vt3Metadata)
{
public DataSet data()
{
return vt3data;
}
};
VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(vt1, vt2, vt3)));

CQLTester.setUpClass();
}
Expand Down Expand Up @@ -207,6 +231,23 @@ public void testQueries() throws Throwable
}
}

@Test
public void testQueriesOnTableWithMultiplePks() throws Throwable
{
assertRowsNet(executeNet("SELECT * FROM test_virtual_ks.vt3 WHERE pk1 = 'UNKNOWN' AND pk2 = 'UNKNOWN'"));

assertRowsNet(executeNet("SELECT * FROM test_virtual_ks.vt3 WHERE pk1 = 'pk11' AND pk2 = 'pk22' AND ck1 = 'UNKNOWN'"));

// Test DISTINCT query
assertRowsNet(executeNet("SELECT DISTINCT pk1, pk2 FROM test_virtual_ks.vt3"),
row("pk11", "pk11"));

// Test single partition queries
assertRowsNet(executeNet("SELECT v1, v2 FROM test_virtual_ks.vt3 WHERE pk1 = 'pk11' AND pk2 = 'pk11'"),
row(1111, 1111L),
row(1122, 1122L));
}

@Test
public void testModifications() throws Throwable
{
Expand Down

0 comments on commit 896baf6

Please sign in to comment.