Search in sources :

Example 11 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type in project cassandra by apache.

the class UFJavaTest method testJavaFunction.

@Test
public void testJavaFunction() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, val double)");
    String functionBody = '\n' + "  // parameter val is of type java.lang.Double\n" + "  /* return type is of type java.lang.Double */\n" + "  if (val == null) {\n" + "    return null;\n" + "  }\n" + "  return Math.sin(val);\n";
    String fName = createFunction(KEYSPACE, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "CALLED ON NULL INPUT " + "RETURNS double " + "LANGUAGE JAVA " + "AS '" + functionBody + "';");
    FunctionName fNameName = parseFunctionName(fName);
    assertRows(execute("SELECT language, body FROM system_schema.functions WHERE keyspace_name=? AND function_name=?", fNameName.keyspace, fNameName.name), row("java", functionBody));
    execute("INSERT INTO %s (key, val) VALUES (?, ?)", 1, 1d);
    execute("INSERT INTO %s (key, val) VALUES (?, ?)", 2, 2d);
    execute("INSERT INTO %s (key, val) VALUES (?, ?)", 3, 3d);
    assertRows(execute("SELECT key, val, " + fName + "(val) FROM %s"), row(1, 1d, Math.sin(1d)), row(2, 2d, Math.sin(2d)), row(3, 3d, Math.sin(3d)));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Example 12 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type in project cassandra by apache.

the class SecondaryIndexTest method assertColumnValue.

private static void assertColumnValue(int expected, String name, Row row, TableMetadata cfm) {
    ColumnMetadata col = cfm.getColumn(new ColumnIdentifier(name, true));
    AbstractType<?> type = col.type;
    assertEquals(expected, type.compose(row.getCell(col).buffer()));
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 13 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type in project cassandra by apache.

the class FullQueryLoggerTest method assertRoundTripQuery.

private void assertRoundTripQuery(@Nullable String keyspace) {
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptTailer tailer = queue.createTailer();
        assertTrue(tailer.readDocument(wire -> {
            assertEquals(0, wire.read(VERSION).int16());
            assertEquals(SINGLE_QUERY, wire.read(TYPE).text());
            assertEquals(1L, wire.read(QUERY_START_TIME).int64());
            ProtocolVersion protocolVersion = ProtocolVersion.decode(wire.read(PROTOCOL_VERSION).int32(), true);
            assertEquals(ProtocolVersion.CURRENT, protocolVersion);
            QueryOptions queryOptions = QueryOptions.codec.decode(Unpooled.wrappedBuffer(wire.read(QUERY_OPTIONS).bytes()), protocolVersion);
            compareQueryOptions(QueryOptions.DEFAULT, queryOptions);
            String wireKeyspace = wire.read(FullQueryLogger.KEYSPACE).text();
            assertEquals(keyspace, wireKeyspace);
            assertEquals("foo", wire.read(QUERY).text());
        }));
    }
}
Also used : Arrays(java.util.Arrays) BATCH_TYPE(org.apache.cassandra.fql.FullQueryLogger.BATCH_TYPE) File(org.apache.cassandra.io.util.File) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) GENERATED_NOW_IN_SECONDS(org.apache.cassandra.fql.FullQueryLogger.GENERATED_NOW_IN_SECONDS) Unpooled(io.netty.buffer.Unpooled) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SINGLE_QUERY(org.apache.cassandra.fql.FullQueryLogger.SINGLE_QUERY) After(org.junit.After) VERSION(org.apache.cassandra.fql.FullQueryLogger.VERSION) Path(java.nio.file.Path) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) QueryState(org.apache.cassandra.service.QueryState) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Util(org.apache.cassandra.Util) QUERY(org.apache.cassandra.fql.FullQueryLogger.QUERY) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) FileUtils(org.apache.cassandra.io.util.FileUtils) Assert.assertFalse(org.junit.Assert.assertFalse) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) VALUES(org.apache.cassandra.fql.FullQueryLogger.VALUES) ObjectSizes(org.apache.cassandra.utils.ObjectSizes) BeforeClass(org.junit.BeforeClass) CQLTester(org.apache.cassandra.cql3.CQLTester) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) ArrayList(java.util.ArrayList) BATCH(org.apache.cassandra.fql.FullQueryLogger.BATCH) PROTOCOL_VERSION(org.apache.cassandra.fql.FullQueryLogger.PROTOCOL_VERSION) Query(org.apache.cassandra.fql.FullQueryLogger.Query) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) QUERY_START_TIME(org.apache.cassandra.fql.FullQueryLogger.QUERY_START_TIME) Semaphore(java.util.concurrent.Semaphore) ClientState(org.apache.cassandra.service.ClientState) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TYPE(org.apache.cassandra.fql.FullQueryLogger.TYPE) Batch(org.apache.cassandra.fql.FullQueryLogger.Batch) GENERATED_TIMESTAMP(org.apache.cassandra.fql.FullQueryLogger.GENERATED_TIMESTAMP) WireOut(net.openhft.chronicle.wire.WireOut) ValueIn(net.openhft.chronicle.wire.ValueIn) RollCycles(net.openhft.chronicle.queue.RollCycles) Type(org.apache.cassandra.cql3.statements.BatchStatement.Type) QUERY_OPTIONS(org.apache.cassandra.fql.FullQueryLogger.QUERY_OPTIONS) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) Assert.assertEquals(org.junit.Assert.assertEquals) QUERIES(org.apache.cassandra.fql.FullQueryLogger.QUERIES) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 14 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type in project cassandra by apache.

the class FullQueryLoggerTest method assertRoundTripBatch.

private void assertRoundTripBatch(@Nullable String keyspace) {
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptTailer tailer = queue.createTailer();
        assertTrue(tailer.readDocument(wire -> {
            assertEquals(0, wire.read(VERSION).int16());
            assertEquals(BATCH, wire.read(TYPE).text());
            assertEquals(1L, wire.read(QUERY_START_TIME).int64());
            ProtocolVersion protocolVersion = ProtocolVersion.decode(wire.read(PROTOCOL_VERSION).int32(), true);
            assertEquals(ProtocolVersion.CURRENT, protocolVersion);
            QueryOptions queryOptions = QueryOptions.codec.decode(Unpooled.wrappedBuffer(wire.read(QUERY_OPTIONS).bytes()), protocolVersion);
            compareQueryOptions(QueryOptions.DEFAULT, queryOptions);
            assertEquals(Long.MIN_VALUE, wire.read(GENERATED_TIMESTAMP).int64());
            assertEquals(Integer.MIN_VALUE, wire.read(GENERATED_NOW_IN_SECONDS).int32());
            assertEquals(keyspace, wire.read(FullQueryLogger.KEYSPACE).text());
            assertEquals("UNLOGGED", wire.read(BATCH_TYPE).text());
            ValueIn in = wire.read(QUERIES);
            assertEquals(2, in.int32());
            assertEquals("foo1", in.text());
            assertEquals("foo2", in.text());
            in = wire.read(VALUES);
            assertEquals(2, in.int32());
            assertEquals(2, in.int32());
            assertTrue(Arrays.equals(new byte[1], in.bytes()));
            assertTrue(Arrays.equals(new byte[2], in.bytes()));
            assertEquals(0, in.int32());
        }));
    }
}
Also used : Arrays(java.util.Arrays) BATCH_TYPE(org.apache.cassandra.fql.FullQueryLogger.BATCH_TYPE) File(org.apache.cassandra.io.util.File) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) GENERATED_NOW_IN_SECONDS(org.apache.cassandra.fql.FullQueryLogger.GENERATED_NOW_IN_SECONDS) Unpooled(io.netty.buffer.Unpooled) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SINGLE_QUERY(org.apache.cassandra.fql.FullQueryLogger.SINGLE_QUERY) After(org.junit.After) VERSION(org.apache.cassandra.fql.FullQueryLogger.VERSION) Path(java.nio.file.Path) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) QueryState(org.apache.cassandra.service.QueryState) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Util(org.apache.cassandra.Util) QUERY(org.apache.cassandra.fql.FullQueryLogger.QUERY) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) FileUtils(org.apache.cassandra.io.util.FileUtils) Assert.assertFalse(org.junit.Assert.assertFalse) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) VALUES(org.apache.cassandra.fql.FullQueryLogger.VALUES) ObjectSizes(org.apache.cassandra.utils.ObjectSizes) BeforeClass(org.junit.BeforeClass) CQLTester(org.apache.cassandra.cql3.CQLTester) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) ArrayList(java.util.ArrayList) BATCH(org.apache.cassandra.fql.FullQueryLogger.BATCH) PROTOCOL_VERSION(org.apache.cassandra.fql.FullQueryLogger.PROTOCOL_VERSION) Query(org.apache.cassandra.fql.FullQueryLogger.Query) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) QUERY_START_TIME(org.apache.cassandra.fql.FullQueryLogger.QUERY_START_TIME) Semaphore(java.util.concurrent.Semaphore) ClientState(org.apache.cassandra.service.ClientState) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TYPE(org.apache.cassandra.fql.FullQueryLogger.TYPE) Batch(org.apache.cassandra.fql.FullQueryLogger.Batch) GENERATED_TIMESTAMP(org.apache.cassandra.fql.FullQueryLogger.GENERATED_TIMESTAMP) WireOut(net.openhft.chronicle.wire.WireOut) ValueIn(net.openhft.chronicle.wire.ValueIn) RollCycles(net.openhft.chronicle.queue.RollCycles) Type(org.apache.cassandra.cql3.statements.BatchStatement.Type) QUERY_OPTIONS(org.apache.cassandra.fql.FullQueryLogger.QUERY_OPTIONS) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) Assert.assertEquals(org.junit.Assert.assertEquals) QUERIES(org.apache.cassandra.fql.FullQueryLogger.QUERIES) ValueIn(net.openhft.chronicle.wire.ValueIn) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 15 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type in project cassandra by apache.

the class CollectionTypesTest method testSerializationDeserialization.

static <CT extends CollectionType, T> void testSerializationDeserialization(TypeFactory<CT> typeFactory, ValueFactory<T> valueFactory, ValueGenerator keyType) {
    for (ValueGenerator valueType : ValueGenerator.GENERATORS) {
        CT type = typeFactory.createType(keyType != null ? keyType.getType() : null, valueType.getType());
        CQL3Type.Collection cql3Type = new CQL3Type.Collection(type);
        for (int i = 0; i < 500; i++) {
            Random random = new Random(i);
            int size = random.nextInt(1000);
            T expected = valueFactory.createValue(keyType, valueType, size, random);
            for (ValueAccessor<Object> srcAccessor : ACCESSORS) {
                ByteBuffer srcBuffer = type.decompose(expected);
                Object srcBytes = srcAccessor.convert(srcBuffer, ByteBufferAccessor.instance);
                String srcString = type.getString(srcBytes, srcAccessor);
                for (ValueAccessor<Object> dstAccessor : ACCESSORS) {
                    Object dstBytes = dstAccessor.convert(srcBytes, srcAccessor);
                    String dstString = type.getString(dstBytes, dstAccessor);
                    T composed = (T) type.compose(dstBytes, dstAccessor);
                    Assert.assertEquals(expected, composed);
                    ValueAccessors.assertDataEquals(srcBytes, srcAccessor, dstBytes, dstAccessor);
                    Assert.assertEquals(srcString, dstString);
                }
            }
        }
    }
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CQL3Type (org.apache.cassandra.cql3.CQL3Type)14 ByteBuffer (java.nio.ByteBuffer)12 Test (org.junit.Test)12 AbstractType (org.apache.cassandra.db.marshal.AbstractType)11 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 ClientState (org.apache.cassandra.service.ClientState)8 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)7 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)7 FunctionName (org.apache.cassandra.cql3.functions.FunctionName)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)6 java.util (java.util)4 Collections (java.util.Collections)4 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)4 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)4 RollCycles (net.openhft.chronicle.queue.RollCycles)4 QueryOptions (org.apache.cassandra.cql3.QueryOptions)4 TableMetadata (org.apache.cassandra.schema.TableMetadata)4 Set (java.util.Set)3