Search in sources :

Example 61 with Row

use of org.apache.cassandra.cql3.UntypedResultSet.Row in project cassandra by apache.

the class KeyspaceTest method assertRowsInResult.

private static void assertRowsInResult(ColumnFamilyStore cfs, SinglePartitionReadCommand command, int... columnValues) {
    try (ReadExecutionController executionController = command.executionController();
        PartitionIterator iterator = command.executeInternal(executionController)) {
        if (columnValues.length == 0) {
            if (iterator.hasNext())
                fail("Didn't expect any results, but got rows starting with: " + iterator.next().next().toString(cfs.metadata()));
            return;
        }
        try (RowIterator rowIterator = iterator.next()) {
            for (int expected : columnValues) {
                Row row = rowIterator.next();
                Cell cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
                assertEquals(String.format("Expected %s, but got %s", ByteBufferUtil.bytesToHex(ByteBufferUtil.bytes(expected)), ByteBufferUtil.bytesToHex(cell.value())), ByteBufferUtil.bytes(expected), cell.value());
            }
            assertFalse(rowIterator.hasNext());
        }
    }
}
Also used : PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row) Cell(org.apache.cassandra.db.rows.Cell)

Example 62 with Row

use of org.apache.cassandra.cql3.UntypedResultSet.Row in project cassandra by apache.

the class UFJavaTest method testJavaDollarQuotedFunction.

@Test
public void testJavaDollarQuotedFunction() throws Throwable {
    String functionBody = '\n' + "  // parameter val is of type java.lang.Double\n" + "  /* return type is of type java.lang.Double */\n" + "  if (input == null) {\n" + "    return null;\n" + "  }\n" + "  return \"'\"+Math.sin(input)+'\\\'';\n";
    String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE FUNCTION %s( input double ) " + "CALLED ON NULL INPUT " + "RETURNS text " + "LANGUAGE java\n" + "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));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Example 63 with Row

use of org.apache.cassandra.cql3.UntypedResultSet.Row in project cassandra by apache.

the class UFScriptTest method testJavascriptUserType.

@Test
public void testJavascriptUserType() throws Throwable {
    String type = createType("CREATE TYPE %s (txt text, i int)");
    createTable("CREATE TABLE %s (key int primary key, udt frozen<" + type + ">)");
    String fUdt1 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS " + type + ' ' + "LANGUAGE javascript\n" + "AS $$" + "     udt;$$;");
    String fUdt2 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE javascript\n" + "AS $$" + "     udt.getString(\"txt\");$$;");
    String fUdt3 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS int " + "LANGUAGE javascript\n" + "AS $$" + "     udt.getInt(\"i\");$$;");
    execute("INSERT INTO %s (key, udt) VALUES (1, {txt: 'one', i:1})");
    UntypedResultSet rows = execute("SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1");
    Assert.assertEquals(1, rows.size());
    assertRows(execute("SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row("one"));
    assertRows(execute("SELECT " + fUdt3 + "(udt) FROM %s WHERE key = 1"), row(1));
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) Test(org.junit.Test)

Example 64 with Row

use of org.apache.cassandra.cql3.UntypedResultSet.Row in project cassandra by apache.

the class UFTest method testFunctionDropOnKeyspaceDrop.

@Test
public void testFunctionDropOnKeyspaceDrop() throws Throwable {
    String fSin = createFunction(KEYSPACE_PER_TEST, "double", "CREATE FUNCTION %s ( input double ) " + "CALLED ON NULL INPUT " + "RETURNS double " + "LANGUAGE java " + "AS 'return Double.valueOf(Math.sin(input.doubleValue()));'");
    FunctionName fSinName = parseFunctionName(fSin);
    Assert.assertEquals(1, Schema.instance.getFunctions(parseFunctionName(fSin)).size());
    assertRows(execute("SELECT function_name, language FROM system_schema.functions WHERE keyspace_name=?", KEYSPACE_PER_TEST), row(fSinName.name, "java"));
    dropPerTestKeyspace();
    assertRows(execute("SELECT function_name, language FROM system_schema.functions WHERE keyspace_name=?", KEYSPACE_PER_TEST));
    Assert.assertEquals(0, Schema.instance.getFunctions(fSinName).size());
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Example 65 with Row

use of org.apache.cassandra.cql3.UntypedResultSet.Row in project cassandra by apache.

the class SecondaryIndexTest method testUpdatesToMemtableData.

@Test
public void testUpdatesToMemtableData() throws Throwable {
    // verify the contract specified by Index.Indexer::updateRow(oldRowData, newRowData),
    // when a row in the memtable is updated, the indexer should be informed of:
    // * new columns
    // * removed columns
    // * columns whose value, timestamp or ttl have been modified.
    // Any columns which are unchanged by the update are not passed to the Indexer
    // Note that for simplicity this test resets the index between each scenario
    createTable("CREATE TABLE %s (k int, c int, v1 int, v2 int, PRIMARY KEY (k,c))");
    createIndex(String.format("CREATE CUSTOM INDEX test_index ON %%s() USING '%s'", StubIndex.class.getName()));
    execute("INSERT INTO %s (k, c, v1, v2) VALUES (0, 0, 0, 0) USING TIMESTAMP 0");
    ColumnMetadata v1 = getCurrentColumnFamilyStore().metadata().getColumn(new ColumnIdentifier("v1", true));
    ColumnMetadata v2 = getCurrentColumnFamilyStore().metadata().getColumn(new ColumnIdentifier("v2", true));
    StubIndex index = (StubIndex) getCurrentColumnFamilyStore().indexManager.getIndexByName("test_index");
    assertEquals(1, index.rowsInserted.size());
    // Overwrite a single value, leaving the other untouched
    execute("UPDATE %s USING TIMESTAMP 1 SET v1=1 WHERE k=0 AND c=0");
    assertEquals(1, index.rowsUpdated.size());
    Row oldRow = index.rowsUpdated.get(0).left;
    assertEquals(1, oldRow.size());
    validateCell(oldRow.getCell(v1), v1, ByteBufferUtil.bytes(0), 0);
    Row newRow = index.rowsUpdated.get(0).right;
    assertEquals(1, newRow.size());
    validateCell(newRow.getCell(v1), v1, ByteBufferUtil.bytes(1), 1);
    index.reset();
    // Overwrite both values
    execute("UPDATE %s USING TIMESTAMP 2 SET v1=2, v2=2 WHERE k=0 AND c=0");
    assertEquals(1, index.rowsUpdated.size());
    oldRow = index.rowsUpdated.get(0).left;
    assertEquals(2, oldRow.size());
    validateCell(oldRow.getCell(v1), v1, ByteBufferUtil.bytes(1), 1);
    validateCell(oldRow.getCell(v2), v2, ByteBufferUtil.bytes(0), 0);
    newRow = index.rowsUpdated.get(0).right;
    assertEquals(2, newRow.size());
    validateCell(newRow.getCell(v1), v1, ByteBufferUtil.bytes(2), 2);
    validateCell(newRow.getCell(v2), v2, ByteBufferUtil.bytes(2), 2);
    index.reset();
    // Delete one value
    execute("DELETE v1 FROM %s USING TIMESTAMP 3 WHERE k=0 AND c=0");
    assertEquals(1, index.rowsUpdated.size());
    oldRow = index.rowsUpdated.get(0).left;
    assertEquals(1, oldRow.size());
    validateCell(oldRow.getCell(v1), v1, ByteBufferUtil.bytes(2), 2);
    newRow = index.rowsUpdated.get(0).right;
    assertEquals(1, newRow.size());
    Cell newCell = newRow.getCell(v1);
    assertTrue(newCell.isTombstone());
    assertEquals(3, newCell.timestamp());
    index.reset();
    // Modify the liveness of the primary key, the delta rows should contain
    // no cell data as only the pk was altered, but it should illustrate the
    // change to the liveness info
    execute("INSERT INTO %s(k, c) VALUES (0, 0) USING TIMESTAMP 4");
    assertEquals(1, index.rowsUpdated.size());
    oldRow = index.rowsUpdated.get(0).left;
    assertEquals(0, oldRow.size());
    assertEquals(0, oldRow.primaryKeyLivenessInfo().timestamp());
    newRow = index.rowsUpdated.get(0).right;
    assertEquals(0, newRow.size());
    assertEquals(4, newRow.primaryKeyLivenessInfo().timestamp());
}
Also used : StubIndex(org.apache.cassandra.index.StubIndex) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row) Cell(org.apache.cassandra.db.rows.Cell) Test(org.junit.Test)

Aggregations

UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)33 Test (org.junit.Test)29 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)18 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)15 ByteBuffer (java.nio.ByteBuffer)10 Row (org.apache.cassandra.db.rows.Row)8 FunctionName (org.apache.cassandra.cql3.functions.FunctionName)6 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)5 java.util (java.util)4 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)4 InetAddress (java.net.InetAddress)3 ColumnSpecification (org.apache.cassandra.cql3.ColumnSpecification)3 PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)3 Cell (org.apache.cassandra.db.rows.Cell)3 RowIterator (org.apache.cassandra.db.rows.RowIterator)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 Row (com.datastax.driver.core.Row)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables (com.google.common.collect.Iterables)2