Search in sources :

Example 11 with Function

use of org.apache.cassandra.cql3.functions.Function in project cassandra by apache.

the class UFJavaTest method testJavaUserType.

@Test
public void testJavaUserType() throws Throwable {
    String type = KEYSPACE + '.' + createType("CREATE TYPE %s (txt text, i int)");
    createTable("CREATE TABLE %s (key int primary key, udt frozen<" + type + ">)");
    String fUdt0 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS " + type + " " + "LANGUAGE java " + "AS $$return " + "     udt;$$;");
    String fUdt1 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + ") " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE java " + "AS $$return " + "     udt.getString(\"txt\");$$;");
    String fUdt2 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + ") " + "CALLED ON NULL INPUT " + "RETURNS int " + "LANGUAGE java " + "AS $$return " + "     Integer.valueOf(udt.getInt(\"i\"));$$;");
    execute("INSERT INTO %s (key, udt) VALUES (1, {txt: 'one', i:1})");
    UntypedResultSet rows = execute("SELECT " + fUdt0 + "(udt) FROM %s WHERE key = 1");
    Assert.assertEquals(1, rows.size());
    assertRows(execute("SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1"), row("one"));
    assertRows(execute("SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row(1));
    for (ProtocolVersion version : PROTOCOL_VERSIONS) {
        List<Row> rowsNet = executeNet(version, "SELECT " + fUdt0 + "(udt) FROM %s WHERE key = 1").all();
        Assert.assertEquals(1, rowsNet.size());
        UDTValue udtVal = rowsNet.get(0).getUDTValue(0);
        Assert.assertEquals("one", udtVal.getString("txt"));
        Assert.assertEquals(1, udtVal.getInt("i"));
        assertRowsNet(version, executeNet(version, "SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1"), row("one"));
        assertRowsNet(version, executeNet(version, "SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row(1));
    }
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) UDTValue(com.datastax.driver.core.UDTValue) Row(com.datastax.driver.core.Row) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Test(org.junit.Test)

Example 12 with Function

use of org.apache.cassandra.cql3.functions.Function in project cassandra by apache.

the class UFJavaTest method testUDFToCqlString.

@Test
public void testUDFToCqlString() {
    UDFunction function = UDFunction.create(new FunctionName("my_ks", "my_function"), Arrays.asList(ColumnIdentifier.getInterned("column", false)), Arrays.asList(UTF8Type.instance), Int32Type.instance, false, "java", "return 0;");
    Assert.assertTrue(function.toCqlString(true, true).contains("CREATE FUNCTION IF NOT EXISTS"));
    Assert.assertFalse(function.toCqlString(true, false).contains("CREATE FUNCTION IF NOT EXISTS"));
    Assert.assertEquals(function.toCqlString(true, true), function.toCqlString(false, true));
    Assert.assertEquals(function.toCqlString(true, false), function.toCqlString(false, false));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) UDFunction(org.apache.cassandra.cql3.functions.UDFunction) Test(org.junit.Test)

Example 13 with Function

use of org.apache.cassandra.cql3.functions.Function 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 14 with Function

use of org.apache.cassandra.cql3.functions.Function in project cassandra by apache.

the class UFJavaTest method testJavaRuntimeException.

@Test
public void testJavaRuntimeException() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, val double)");
    String functionBody = '\n' + "  throw new RuntimeException(\"oh no!\");\n";
    String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "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));
    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);
    // function throws a RuntimeException which is wrapped by FunctionExecutionException
    assertInvalidThrowMessage("java.lang.RuntimeException: oh no", FunctionExecutionException.class, "SELECT key, val, " + fName + "(val) FROM %s");
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Example 15 with Function

use of org.apache.cassandra.cql3.functions.Function in project cassandra by apache.

the class UFScriptTest method testJavascriptFunction.

@Test
public void testJavascriptFunction() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, val double)");
    String functionBody = '\n' + "  Math.sin(val);\n";
    String fName = createFunction(KEYSPACE, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE javascript\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("javascript", 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)

Aggregations

Test (org.junit.Test)34 Function (io.reactivex.rxjava3.functions.Function)20 FunctionName (org.apache.cassandra.cql3.functions.FunctionName)13 ByteBuffer (java.nio.ByteBuffer)7 Function (org.apache.cassandra.cql3.functions.Function)7 AbstractType (org.apache.cassandra.db.marshal.AbstractType)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)6 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)6 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)5 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)5 InOrder (org.mockito.InOrder)5 List (java.util.List)4 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)4 UDFunction (org.apache.cassandra.cql3.functions.UDFunction)4 ViewMetadata (org.apache.cassandra.schema.ViewMetadata)4 TestException (io.reactivex.rxjava3.exceptions.TestException)3 java.util (java.util)3 ArrayList (java.util.ArrayList)3