Search in sources :

Example 31 with Type

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

the class ElementsSelector method newElementFactory.

/**
 * Creates a {@code Selector.Factory} for the selection of an element of a collection.
 *
 * @param name a string representing the selection the factory is for. Something like "c[x]".
 * @param factory the {@code Selector.Factory} corresponding to the collection on which an element
 * is selected.
 * @param type the type of the collection.
 * @param key the element within the value represented by {@code factory} that is selected.
 * @return the created factory.
 */
public static Factory newElementFactory(String name, Selector.Factory factory, CollectionType<?> type, final Term key) {
    return new AbstractFactory(name, factory, type) {

        protected AbstractType<?> getReturnType() {
            return valueType(type);
        }

        public Selector newInstance(QueryOptions options) throws InvalidRequestException {
            ByteBuffer keyValue = key.bindAndGet(options);
            if (keyValue == null)
                throw new InvalidRequestException("Invalid null value for element selection on " + factory.getColumnName());
            if (keyValue == ByteBufferUtil.UNSET_BYTE_BUFFER)
                throw new InvalidRequestException("Invalid unset value for element selection on " + factory.getColumnName());
            return new ElementSelector(factory.newInstance(options), keyValue);
        }

        public boolean areAllFetchedColumnsKnown() {
            // 3) the element selected is terminal.
            return factory.areAllFetchedColumnsKnown() && (!type.isMultiCell() || !factory.isSimpleSelectorFactory() || key.isTerminal());
        }

        public void addFetchedColumns(ColumnFilter.Builder builder) {
            if (!type.isMultiCell() || !factory.isSimpleSelectorFactory()) {
                factory.addFetchedColumns(builder);
                return;
            }
            ColumnMetadata column = ((SimpleSelectorFactory) factory).getColumn();
            builder.select(column, CellPath.create(((Term.Terminal) key).get(ProtocolVersion.V3)));
        }
    };
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SimpleSelectorFactory(org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ByteBuffer(java.nio.ByteBuffer)

Example 32 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type 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 33 with Type

use of org.apache.cassandra.cql3.statements.schema.IndexTarget.Type 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 34 with Type

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

the class UFTest method testUserTypeDrop.

@Test
public void testUserTypeDrop() 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 fName = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "CALLED ON NULL INPUT " + "RETURNS int " + "LANGUAGE java " + "AS $$return " + "     Integer.valueOf(udt.getInt(\"i\"));$$;");
    FunctionName fNameName = parseFunctionName(fName);
    Assert.assertEquals(1, Schema.instance.getFunctions(fNameName).size());
    ResultMessage.Prepared prepared = QueryProcessor.instance.prepare(String.format("SELECT key, %s(udt) FROM %s.%s", fName, KEYSPACE, currentTable()), ClientState.forInternalCalls());
    Assert.assertNotNull(QueryProcessor.instance.getPrepared(prepared.statementId));
    // UT still referenced by table
    assertInvalidMessage("Cannot drop user type", "DROP TYPE " + type);
    execute("DROP TABLE %s");
    // UT still referenced by UDF
    assertInvalidMessage("as it is still used by function", "DROP TYPE " + type);
    Assert.assertNull(QueryProcessor.instance.getPrepared(prepared.statementId));
    // function stays
    Assert.assertEquals(1, Schema.instance.getFunctions(fNameName).size());
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) Test(org.junit.Test)

Example 35 with Type

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

the class UFTypesTest method testComplexNullValues.

@Test
public void testComplexNullValues() throws Throwable {
    String type = KEYSPACE + '.' + createType("CREATE TYPE %s (txt text, i int)");
    createTable("CREATE TABLE %s (key int primary key, lst list<double>, st set<text>, mp map<int, boolean>," + "tup frozen<tuple<double, text, int, boolean>>, udt frozen<" + type + ">)");
    String fList = createFunction(KEYSPACE, "list<double>", "CREATE FUNCTION %s( coll list<double> ) " + "CALLED ON NULL INPUT " + "RETURNS list<double> " + "LANGUAGE java\n" + "AS $$return coll;$$;");
    String fSet = createFunction(KEYSPACE, "set<text>", "CREATE FUNCTION %s( coll set<text> ) " + "CALLED ON NULL INPUT " + "RETURNS set<text> " + "LANGUAGE java\n" + "AS $$return coll;$$;");
    String fMap = createFunction(KEYSPACE, "map<int, boolean>", "CREATE FUNCTION %s( coll map<int, boolean> ) " + "CALLED ON NULL INPUT " + "RETURNS map<int, boolean> " + "LANGUAGE java\n" + "AS $$return coll;$$;");
    String fTup = createFunction(KEYSPACE, "tuple<double, text, int, boolean>", "CREATE FUNCTION %s( val tuple<double, text, int, boolean> ) " + "CALLED ON NULL INPUT " + "RETURNS tuple<double, text, int, boolean> " + "LANGUAGE java\n" + "AS $$return val;$$;");
    String fUdt = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( val " + type + " ) " + "CALLED ON NULL INPUT " + "RETURNS " + type + " " + "LANGUAGE java\n" + "AS $$return val;$$;");
    List<Double> list = Arrays.asList(1d, 2d, 3d);
    Set<String> set = new TreeSet<>(Arrays.asList("one", "three", "two"));
    Map<Integer, Boolean> map = new TreeMap<>();
    map.put(1, true);
    map.put(2, false);
    map.put(3, true);
    Object t = tuple(1d, "one", 42, false);
    execute("INSERT INTO %s (key, lst, st, mp, tup, udt) VALUES (1, ?, ?, ?, ?, {txt: 'one', i:1})", list, set, map, t);
    execute("INSERT INTO %s (key, lst, st, mp, tup, udt) VALUES (2, ?, ?, ?, ?, null)", null, null, null, null);
    execute("SELECT " + fList + "(lst), " + fSet + "(st), " + fMap + "(mp), " + fTup + "(tup), " + fUdt + "(udt) FROM %s WHERE key = 1");
    UntypedResultSet.Row row = execute("SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 1").one();
    Assert.assertNotNull(row.getBytes("l"));
    Assert.assertNotNull(row.getBytes("s"));
    Assert.assertNotNull(row.getBytes("m"));
    Assert.assertNotNull(row.getBytes("t"));
    Assert.assertNotNull(row.getBytes("u"));
    row = execute("SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 2").one();
    Assert.assertNull(row.getBytes("l"));
    Assert.assertNull(row.getBytes("s"));
    Assert.assertNull(row.getBytes("m"));
    Assert.assertNull(row.getBytes("t"));
    Assert.assertNull(row.getBytes("u"));
    for (ProtocolVersion version : PROTOCOL_VERSIONS) {
        Row r = executeNet(version, "SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 1").one();
        Assert.assertNotNull(r.getBytesUnsafe("l"));
        Assert.assertNotNull(r.getBytesUnsafe("s"));
        Assert.assertNotNull(r.getBytesUnsafe("m"));
        Assert.assertNotNull(r.getBytesUnsafe("t"));
        Assert.assertNotNull(r.getBytesUnsafe("u"));
        r = executeNet(version, "SELECT " + fList + "(lst) as l, " + fSet + "(st) as s, " + fMap + "(mp) as m, " + fTup + "(tup) as t, " + fUdt + "(udt) as u " + "FROM %s WHERE key = 2").one();
        Assert.assertNull(r.getBytesUnsafe("l"));
        Assert.assertNull(r.getBytesUnsafe("s"));
        Assert.assertNull(r.getBytesUnsafe("m"));
        Assert.assertNull(r.getBytesUnsafe("t"));
        Assert.assertNull(r.getBytesUnsafe("u"));
    }
}
Also used : TreeMap(java.util.TreeMap) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) TreeSet(java.util.TreeSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

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