Search in sources :

Example 21 with Type

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

the class DropAggregateStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws RequestValidationException {
    Collection<Function> olds = Schema.instance.getFunctions(functionName);
    if (!argsPresent && olds != null && olds.size() > 1)
        throw new InvalidRequestException(String.format("'DROP AGGREGATE %s' matches multiple function definitions; " + "specify the argument types by issuing a statement like " + "'DROP AGGREGATE %s (type, type, ...)'. Hint: use cqlsh " + "'DESCRIBE AGGREGATE %s' command to find all overloads", functionName, functionName, functionName));
    Function old = null;
    if (argsPresent) {
        if (Schema.instance.getKeyspaceMetadata(functionName.keyspace) != null) {
            List<AbstractType<?>> argTypes = new ArrayList<>(argRawTypes.size());
            for (CQL3Type.Raw rawType : argRawTypes) argTypes.add(prepareType("arguments", rawType));
            old = Schema.instance.findFunction(functionName, argTypes).orElse(null);
        }
        if (old == null || !(old instanceof AggregateFunction)) {
            if (ifExists)
                return null;
            // just build a nicer error message
            StringBuilder sb = new StringBuilder();
            for (CQL3Type.Raw rawType : argRawTypes) {
                if (sb.length() > 0)
                    sb.append(", ");
                sb.append(rawType);
            }
            throw new InvalidRequestException(String.format("Cannot drop non existing aggregate '%s(%s)'", functionName, sb));
        }
    } else {
        if (olds == null || olds.isEmpty() || !(olds.iterator().next() instanceof AggregateFunction)) {
            if (ifExists)
                return null;
            throw new InvalidRequestException(String.format("Cannot drop non existing aggregate '%s'", functionName));
        }
        old = olds.iterator().next();
    }
    if (old.isNative())
        throw new InvalidRequestException(String.format("Cannot drop aggregate '%s' because it is a " + "native (built-in) function", functionName));
    MigrationManager.announceAggregateDrop((UDAggregate) old, isLocalOnly);
    return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.AGGREGATE, old.name().keyspace, old.name().name, AbstractType.asCQLTypeStringList(old.argTypes()));
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) ArrayList(java.util.ArrayList) AbstractType(org.apache.cassandra.db.marshal.AbstractType) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException)

Example 22 with Type

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

the class TargetParser method parse.

public static Pair<ColumnMetadata, IndexTarget.Type> parse(TableMetadata metadata, String target) {
    // if the regex matches then the target is in the form "keys(foo)", "entries(bar)" etc
    // if not, then it must be a simple column name and implictly its type is VALUES
    Matcher matcher = TARGET_REGEX.matcher(target);
    String columnName;
    IndexTarget.Type targetType;
    if (matcher.matches()) {
        targetType = IndexTarget.Type.fromString(matcher.group(1));
        columnName = matcher.group(2);
    } else {
        columnName = target;
        targetType = IndexTarget.Type.VALUES;
    }
    // need to un-escape any such quotes to get the actual column name
    if (columnName.startsWith(QUOTE)) {
        columnName = StringUtils.substring(StringUtils.substring(columnName, 1), 0, -1);
        columnName = TWO_QUOTES.matcher(columnName).replaceAll(QUOTE);
    }
    // if it's not a CQL table, we can't assume that the column name is utf8, so
    // in that case we have to do a linear scan of the cfm's columns to get the matching one.
    // After dropping compact storage (see CASSANDRA-10857), we can't distinguish between the
    // former compact/thrift table, so we have to fall back to linear scan in both cases.
    ColumnMetadata cd = metadata.getColumn(new ColumnIdentifier(columnName, true));
    if (cd != null)
        return Pair.create(cd, targetType);
    for (ColumnMetadata column : metadata.columns()) if (column.name.toString().equals(columnName))
        return Pair.create(column, targetType);
    return null;
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) Matcher(java.util.regex.Matcher) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 23 with Type

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

the class NativeSSTableLoaderClient method createDefinitionFromRow.

private static ColumnMetadata createDefinitionFromRow(Row row, String keyspace, String table, Types types) {
    ClusteringOrder order = ClusteringOrder.valueOf(row.getString("clustering_order").toUpperCase());
    AbstractType<?> type = CQLTypeParser.parse(keyspace, row.getString("type"), types);
    if (order == ClusteringOrder.DESC)
        type = ReversedType.getInstance(type);
    ColumnIdentifier name = new ColumnIdentifier(row.getBytes("column_name_bytes"), row.getString("column_name"));
    int position = row.getInt("position");
    org.apache.cassandra.schema.ColumnMetadata.Kind kind = ColumnMetadata.Kind.valueOf(row.getString("kind").toUpperCase());
    return new ColumnMetadata(keyspace, table, name, type, position, kind);
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) ClusteringOrder(org.apache.cassandra.schema.ColumnMetadata.ClusteringOrder)

Example 24 with Type

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

the class BinAuditLoggerTest method testSelectRoundTripQuery.

@Test
public void testSelectRoundTripQuery() throws Throwable {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 1, "Apache", "Cassandra");
    execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 2, "trace", "test");
    String cql = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + " WHERE id = ?";
    Session session = sessionNet();
    PreparedStatement pstmt = session.prepare(cql);
    ResultSet rs = session.execute(pstmt.bind(1));
    assertEquals(1, rs.all().size());
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptTailer tailer = queue.createTailer();
        assertTrue(tailer.readDocument(wire -> {
            assertEquals(0L, wire.read("version").int16());
            assertEquals("audit", wire.read("type").text());
            assertThat(wire.read("message").text(), containsString(AuditLogEntryType.PREPARE_STATEMENT.toString()));
        }));
        assertTrue(tailer.readDocument(wire -> {
            assertEquals(0L, wire.read("version").int16());
            assertEquals("audit", wire.read("type").text());
            assertThat(wire.read("message").text(), containsString(AuditLogEntryType.SELECT.toString()));
        }));
        assertFalse(tailer.readDocument(wire -> {
        }));
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) BeforeClass(org.junit.BeforeClass) CQLTester(org.apache.cassandra.cql3.CQLTester) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PreparedStatement(com.datastax.driver.core.PreparedStatement) Assert.assertThat(org.junit.Assert.assertThat) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ResultSet(com.datastax.driver.core.ResultSet) ParameterizedClass(org.apache.cassandra.config.ParameterizedClass) Assert.assertFalse(org.junit.Assert.assertFalse) Session(com.datastax.driver.core.Session) RollCycles(net.openhft.chronicle.queue.RollCycles) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Path(java.nio.file.Path) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Assert.assertEquals(org.junit.Assert.assertEquals) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Session(com.datastax.driver.core.Session) Test(org.junit.Test) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest)

Example 25 with Type

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

the class BytesConversionFcts method all.

public static Collection<Function> all() {
    Collection<Function> functions = new ArrayList<>();
    // for varchar, so we special case it below. We also skip blob for obvious reasons.
    for (CQL3Type type : CQL3Type.Native.values()) {
        if (type != CQL3Type.Native.VARCHAR && type != CQL3Type.Native.BLOB) {
            functions.add(makeToBlobFunction(type.getType()));
            functions.add(makeFromBlobFunction(type.getType()));
        }
    }
    functions.add(VarcharAsBlobFct);
    functions.add(BlobAsVarcharFct);
    return functions;
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) ArrayList(java.util.ArrayList)

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