Search in sources :

Example 21 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class CompositeTypeTest method testValidate.

@Test
public void testValidate() {
    ByteBuffer key = createCompositeKey("test1", uuids[1], 42, false);
    comparator.validate(key);
    key = createCompositeKey("test1", null, -1, false);
    comparator.validate(key);
    key = createCompositeKey("test1", uuids[2], -1, true);
    comparator.validate(key);
    // make sure we're not aligned anymore
    key.get();
    try {
        comparator.validate(key);
        fail("Should not validate");
    } catch (MarshalException e) {
    }
    key = ByteBuffer.allocate(3 + "test1".length() + 3 + 14);
    key.putShort((short) "test1".length());
    key.put(ByteBufferUtil.bytes("test1"));
    key.put((byte) 0);
    key.putShort((short) 14);
    key.rewind();
    try {
        comparator.validate(key);
        fail("Should not validate");
    } catch (MarshalException e) {
        assert e.toString().contains("should be 16 or 0 bytes");
    }
    key = createCompositeKey("test1", UUID.randomUUID(), 42, false);
    try {
        comparator.validate(key);
        fail("Should not validate");
    } catch (MarshalException e) {
        assert e.toString().contains("Invalid version for TimeUUID type");
    }
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 22 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class FromJsonFct method execute.

public ByteBuffer execute(ProtocolVersion protocolVersion, List<ByteBuffer> parameters) {
    assert parameters.size() == 1 : "Unexpectedly got " + parameters.size() + " arguments for fromJson()";
    ByteBuffer argument = parameters.get(0);
    if (argument == null)
        return null;
    String jsonArg = UTF8Type.instance.getSerializer().deserialize(argument);
    try {
        Object object = Json.JSON_OBJECT_MAPPER.readValue(jsonArg, Object.class);
        if (object == null)
            return null;
        return returnType.fromJSONObject(object).bindAndGet(QueryOptions.forProtocolVersion(protocolVersion));
    } catch (IOException exc) {
        throw FunctionExecutionException.create(NAME, Collections.singletonList("text"), String.format("Could not decode JSON string '%s': %s", jsonArg, exc.toString()));
    } catch (MarshalException exc) {
        throw FunctionExecutionException.create(this, exc);
    }
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 23 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.

the class CreateAggregateStatement method apply.

public Keyspaces apply(Keyspaces schema) {
    if (ifNotExists && orReplace)
        throw ire("Cannot use both 'OR REPLACE' and 'IF NOT EXISTS' directives");
    rawArgumentTypes.stream().filter(raw -> !raw.isTuple() && raw.isFrozen()).findFirst().ifPresent(t -> {
        throw ire("Argument '%s' cannot be frozen; remove frozen<> modifier from '%s'", t, t);
    });
    if (!rawStateType.isTuple() && rawStateType.isFrozen())
        throw ire("State type '%s' cannot be frozen; remove frozen<> modifier from '%s'", rawStateType, rawStateType);
    KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
    if (null == keyspace)
        throw ire("Keyspace '%s' doesn't exist", keyspaceName);
    /*
         * Resolve the state function
         */
    List<AbstractType<?>> argumentTypes = rawArgumentTypes.stream().map(t -> t.prepare(keyspaceName, keyspace.types).getType()).collect(toList());
    AbstractType<?> stateType = rawStateType.prepare(keyspaceName, keyspace.types).getType();
    List<AbstractType<?>> stateFunctionArguments = Lists.newArrayList(concat(singleton(stateType), argumentTypes));
    Function stateFunction = keyspace.functions.find(stateFunctionName, stateFunctionArguments).orElseThrow(() -> ire("State function %s doesn't exist", stateFunctionString()));
    if (stateFunction.isAggregate())
        throw ire("State function %s isn't a scalar function", stateFunctionString());
    if (!stateFunction.returnType().equals(stateType)) {
        throw ire("State function %s return type must be the same as the first argument type - check STYPE, argument and return types", stateFunctionString());
    }
    /*
         * Resolve the final function and return type
         */
    Function finalFunction = null;
    AbstractType<?> returnType = stateFunction.returnType();
    if (null != finalFunctionName) {
        finalFunction = keyspace.functions.find(finalFunctionName, singletonList(stateType)).orElse(null);
        if (null == finalFunction)
            throw ire("Final function %s doesn't exist", finalFunctionString());
        if (finalFunction.isAggregate())
            throw ire("Final function %s isn't a scalar function", finalFunctionString());
        // override return type with that of the final function
        returnType = finalFunction.returnType();
    }
    /*
         * Validate initial condition
         */
    ByteBuffer initialValue = null;
    if (null != rawInitialValue) {
        initialValue = Terms.asBytes(keyspaceName, rawInitialValue.toString(), stateType);
        if (null != initialValue) {
            try {
                stateType.validate(initialValue);
            } catch (MarshalException e) {
                throw ire("Invalid value for INITCOND of type %s", stateType.asCQL3Type());
            }
        }
        // Converts initcond to a CQL literal and parse it back to avoid another CASSANDRA-11064
        String initialValueString = stateType.asCQL3Type().toCQLLiteral(initialValue, ProtocolVersion.CURRENT);
        assert Objects.equal(initialValue, Terms.asBytes(keyspaceName, initialValueString, stateType));
        if (Constants.NULL_LITERAL != rawInitialValue && UDHelper.isNullOrEmpty(stateType, initialValue))
            throw ire("INITCOND must not be empty for all types except TEXT, ASCII, BLOB");
    }
    if (!((UDFunction) stateFunction).isCalledOnNullInput() && null == initialValue) {
        throw ire("Cannot create aggregate '%s' without INITCOND because state function %s does not accept 'null' arguments", aggregateName, stateFunctionName);
    }
    /*
         * Create or replace
         */
    UDAggregate aggregate = new UDAggregate(new FunctionName(keyspaceName, aggregateName), argumentTypes, returnType, (ScalarFunction) stateFunction, (ScalarFunction) finalFunction, initialValue);
    Function existingAggregate = keyspace.functions.find(aggregate.name(), argumentTypes).orElse(null);
    if (null != existingAggregate) {
        if (!existingAggregate.isAggregate())
            throw ire("Aggregate '%s' cannot replace a function", aggregateName);
        if (ifNotExists)
            return schema;
        if (!orReplace)
            throw ire("Aggregate '%s' already exists", aggregateName);
        if (!returnType.isCompatibleWith(existingAggregate.returnType())) {
            throw ire("Cannot replace aggregate '%s', the new return type %s isn't compatible with the return type %s of existing function", aggregateName, returnType.asCQL3Type(), existingAggregate.returnType().asCQL3Type());
        }
    }
    return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.functions.withAddedOrUpdated(aggregate)));
}
Also used : AuditLogContext(org.apache.cassandra.audit.AuditLogContext) Change(org.apache.cassandra.transport.Event.SchemaChange.Change) FunctionResource(org.apache.cassandra.auth.FunctionResource) Iterables.transform(com.google.common.collect.Iterables.transform) Permission(org.apache.cassandra.auth.Permission) IResource(org.apache.cassandra.auth.IResource) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ByteBuffer(java.nio.ByteBuffer) org.apache.cassandra.cql3(org.apache.cassandra.cql3) Schema(org.apache.cassandra.schema.Schema) Collections.singletonList(java.util.Collections.singletonList) Lists(com.google.common.collect.Lists) String.join(java.lang.String.join) Collections.singleton(java.util.Collections.singleton) Iterables.concat(com.google.common.collect.Iterables.concat) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) org.apache.cassandra.cql3.functions(org.apache.cassandra.cql3.functions) FunctionsDiff(org.apache.cassandra.schema.Functions.FunctionsDiff) Objects(com.google.common.base.Objects) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) ImmutableSet(com.google.common.collect.ImmutableSet) Keyspaces(org.apache.cassandra.schema.Keyspaces) SchemaChange(org.apache.cassandra.transport.Event.SchemaChange) ClientState(org.apache.cassandra.service.ClientState) Set(java.util.Set) String.format(java.lang.String.format) AuditLogEntryType(org.apache.cassandra.audit.AuditLogEntryType) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Target(org.apache.cassandra.transport.Event.SchemaChange.Target) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) MarshalException(org.apache.cassandra.serializers.MarshalException) MarshalException(org.apache.cassandra.serializers.MarshalException) AbstractType(org.apache.cassandra.db.marshal.AbstractType) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) ByteBuffer(java.nio.ByteBuffer)

Example 24 with MarshalException

use of org.apache.cassandra.serializers.MarshalException in project stargate-core by tuplejump.

the class RowIndexSupport method addField.

protected void addField(Type type, ColumnDefinition columnDefinition, String name, FieldType fieldType, ByteBuffer value, List<Field> fields) {
    if (fieldType != null) {
        try {
            Field field = type.fieldCreator.field(name, columnDefinition.type, value, fieldType);
            fields.add(field);
        } catch (MarshalException e) {
            fields.add(new Field(name, "_null_", Fields.STRING_FIELD_TYPE));
        } catch (Exception e) {
            logger.warn("Could not index column {}{}", columnDefinition, name);
            logger.warn("Exception while indexing column", e);
        }
    }
}
Also used : Field(org.apache.lucene.document.Field) MarshalException(org.apache.cassandra.serializers.MarshalException) MarshalException(org.apache.cassandra.serializers.MarshalException)

Aggregations

MarshalException (org.apache.cassandra.serializers.MarshalException)24 ByteBuffer (java.nio.ByteBuffer)10 CharacterCodingException (java.nio.charset.CharacterCodingException)3 Term (org.apache.cassandra.cql3.Term)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 IOException (java.io.IOException)2 UUID (java.util.UUID)2 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)2 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)2 SyntaxException (org.apache.cassandra.exceptions.SyntaxException)2 IVerbHandler (org.apache.cassandra.net.IVerbHandler)2 MessagingService (org.apache.cassandra.net.MessagingService)2 StorageProxy (org.apache.cassandra.service.StorageProxy)2 StorageService (org.apache.cassandra.service.StorageService)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Objects (com.google.common.base.Objects)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.concat (com.google.common.collect.Iterables.concat)1 Iterables.transform (com.google.common.collect.Iterables.transform)1