Search in sources :

Example 46 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DataTypeClassNameParser method parse.

private DataType parse(String toParse, Map<CqlIdentifier, UserDefinedType> userTypes, AttachmentPoint attachmentPoint, String logPrefix) {
    boolean frozen = false;
    if (isReversed(toParse)) {
        // Just skip the ReversedType part, we don't care
        toParse = getNestedClassName(toParse);
    } else if (toParse.startsWith("org.apache.cassandra.db.marshal.FrozenType")) {
        frozen = true;
        toParse = getNestedClassName(toParse);
    }
    Parser parser = new Parser(toParse, 0);
    String next = parser.parseNextName();
    if (next.startsWith("org.apache.cassandra.db.marshal.ListType")) {
        DataType elementType = parse(parser.getTypeParameters().get(0), userTypes, attachmentPoint, logPrefix);
        return DataTypes.listOf(elementType, frozen);
    }
    if (next.startsWith("org.apache.cassandra.db.marshal.SetType")) {
        DataType elementType = parse(parser.getTypeParameters().get(0), userTypes, attachmentPoint, logPrefix);
        return DataTypes.setOf(elementType, frozen);
    }
    if (next.startsWith("org.apache.cassandra.db.marshal.MapType")) {
        List<String> parameters = parser.getTypeParameters();
        DataType keyType = parse(parameters.get(0), userTypes, attachmentPoint, logPrefix);
        DataType valueType = parse(parameters.get(1), userTypes, attachmentPoint, logPrefix);
        return DataTypes.mapOf(keyType, valueType, frozen);
    }
    if (frozen)
        LOG.warn("[{}] Got o.a.c.db.marshal.FrozenType for something else than a collection, " + "this driver version might be too old for your version of Cassandra", logPrefix);
    if (next.startsWith("org.apache.cassandra.db.marshal.UserType")) {
        // skipping '('
        ++parser.idx;
        CqlIdentifier keyspace = CqlIdentifier.fromInternal(parser.readOne());
        parser.skipBlankAndComma();
        String typeName = TypeCodecs.TEXT.decode(Bytes.fromHexString("0x" + parser.readOne()), attachmentPoint.getProtocolVersion());
        if (typeName == null) {
            throw new AssertionError("Type name cannot be null, this is a server bug");
        }
        CqlIdentifier typeId = CqlIdentifier.fromInternal(typeName);
        Map<String, String> nameAndTypeParameters = parser.getNameAndTypeParameters();
        // Avoid re-parsing if we already have the definition
        if (userTypes != null && userTypes.containsKey(typeId)) {
            // copy as frozen since C* 2.x UDTs are always frozen.
            return userTypes.get(typeId).copy(true);
        } else {
            UserDefinedTypeBuilder builder = new UserDefinedTypeBuilder(keyspace, typeId);
            parser.skipBlankAndComma();
            for (Map.Entry<String, String> entry : nameAndTypeParameters.entrySet()) {
                CqlIdentifier fieldName = CqlIdentifier.fromInternal(entry.getKey());
                DataType fieldType = parse(entry.getValue(), userTypes, attachmentPoint, logPrefix);
                builder.withField(fieldName, fieldType);
            }
            // Create a frozen UserType since C* 2.x UDTs are always frozen.
            return builder.frozen().withAttachmentPoint(attachmentPoint).build();
        }
    }
    if (next.startsWith("org.apache.cassandra.db.marshal.TupleType")) {
        List<String> rawTypes = parser.getTypeParameters();
        ImmutableList.Builder<DataType> componentTypesBuilder = ImmutableList.builder();
        for (String rawType : rawTypes) {
            componentTypesBuilder.add(parse(rawType, userTypes, attachmentPoint, logPrefix));
        }
        return new DefaultTupleType(componentTypesBuilder.build(), attachmentPoint);
    }
    DataType type = NATIVE_TYPES_BY_CLASS_NAME.get(next);
    return type == null ? DataTypes.custom(toParse) : type;
}
Also used : DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 47 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class FunctionParser method parseFunction.

public FunctionMetadata parseFunction(AdminRow row, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userDefinedTypes) {
    // Cassandra < 3.0:
    // CREATE TABLE system.schema_functions (
    // keyspace_name text,
    // function_name text,
    // signature frozen<list<text>>,
    // argument_names list<text>,
    // argument_types list<text>,
    // body text,
    // called_on_null_input boolean,
    // language text,
    // return_type text,
    // PRIMARY KEY (keyspace_name, function_name, signature)
    // ) WITH CLUSTERING ORDER BY (function_name ASC, signature ASC)
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system_schema.functions (
    // keyspace_name text,
    // function_name text,
    // argument_names frozen<list<text>>,
    // argument_types frozen<list<text>>,
    // body text,
    // called_on_null_input boolean,
    // language text,
    // return_type text,
    // PRIMARY KEY (keyspace_name, function_name, argument_types)
    // ) WITH CLUSTERING ORDER BY (function_name ASC, argument_types ASC)
    String simpleName = row.getString("function_name");
    List<CqlIdentifier> argumentNames = ImmutableList.copyOf(Lists.transform(row.getListOfString("argument_names"), CqlIdentifier::fromInternal));
    List<String> argumentTypes = row.getListOfString("argument_types");
    if (argumentNames.size() != argumentTypes.size()) {
        LOG.warn("[{}] Error parsing system row for function {}.{}, " + "number of argument names and types don't match (got {} and {}).", logPrefix, keyspaceId.asInternal(), simpleName, argumentNames.size(), argumentTypes.size());
        return null;
    }
    FunctionSignature signature = new FunctionSignature(CqlIdentifier.fromInternal(simpleName), dataTypeParser.parse(keyspaceId, argumentTypes, userDefinedTypes, context));
    String body = row.getString("body");
    Boolean calledOnNullInput = row.getBoolean("called_on_null_input");
    String language = row.getString("language");
    DataType returnType = dataTypeParser.parse(keyspaceId, row.getString("return_type"), userDefinedTypes, context);
    return new DefaultFunctionMetadata(keyspaceId, signature, argumentNames, body, calledOnNullInput, language, returnType);
}
Also used : DefaultFunctionMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultFunctionMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature)

Example 48 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DefaultUdtValue method hashCode.

@Override
public int hashCode() {
    int result = type.hashCode();
    for (int i = 0; i < values.length; i++) {
        DataType innerThisType = type.getFieldTypes().get(i);
        Object thisValue = this.codecRegistry().codecFor(innerThisType).decode(this.values[i], this.protocolVersion());
        if (thisValue != null) {
            result = 31 * result + thisValue.hashCode();
        }
    }
    return result;
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType)

Example 49 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DseViewParser method parseView.

public DseViewMetadata parseView(AdminRow viewRow, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userTypes) {
    // Cassandra 3.0 (no views in earlier versions):
    // CREATE TABLE system_schema.views (
    // keyspace_name text,
    // view_name text,
    // base_table_id uuid,
    // base_table_name text,
    // bloom_filter_fp_chance double,
    // caching frozen<map<text, text>>,
    // cdc boolean,
    // comment text,
    // compaction frozen<map<text, text>>,
    // compression frozen<map<text, text>>,
    // crc_check_chance double,
    // dclocal_read_repair_chance double,
    // default_time_to_live int,
    // extensions frozen<map<text, blob>>,
    // gc_grace_seconds int,
    // id uuid,
    // include_all_columns boolean,
    // max_index_interval int,
    // memtable_flush_period_in_ms int,
    // min_index_interval int,
    // read_repair_chance double,
    // speculative_retry text,
    // where_clause text,
    // PRIMARY KEY (keyspace_name, view_name)
    // ) WITH CLUSTERING ORDER BY (view_name ASC)
    CqlIdentifier viewId = CqlIdentifier.fromInternal(viewRow.getString("view_name"));
    UUID uuid = viewRow.getUuid("id");
    CqlIdentifier baseTableId = CqlIdentifier.fromInternal(viewRow.getString("base_table_name"));
    boolean includesAllColumns = MoreObjects.firstNonNull(viewRow.getBoolean("include_all_columns"), false);
    String whereClause = viewRow.getString("where_clause");
    List<RawColumn> rawColumns = RawColumn.toRawColumns(rows.columns().getOrDefault(keyspaceId, ImmutableMultimap.of()).get(viewId));
    if (rawColumns.isEmpty()) {
        LOG.warn("[{}] Processing VIEW refresh for {}.{} but found no matching rows, skipping", logPrefix, keyspaceId, viewId);
        return null;
    }
    Collections.sort(rawColumns);
    ImmutableMap.Builder<CqlIdentifier, ColumnMetadata> allColumnsBuilder = ImmutableMap.builder();
    ImmutableList.Builder<ColumnMetadata> partitionKeyBuilder = ImmutableList.builder();
    ImmutableMap.Builder<ColumnMetadata, ClusteringOrder> clusteringColumnsBuilder = ImmutableMap.builder();
    for (RawColumn raw : rawColumns) {
        DataType dataType = rows.dataTypeParser().parse(keyspaceId, raw.dataType, userTypes, context);
        DseColumnMetadata column = new DefaultDseColumnMetadata(keyspaceId, viewId, raw.name, dataType, raw.kind.equals(RawColumn.KIND_STATIC));
        switch(raw.kind) {
            case RawColumn.KIND_PARTITION_KEY:
                partitionKeyBuilder.add(column);
                break;
            case RawColumn.KIND_CLUSTERING_COLUMN:
                clusteringColumnsBuilder.put(column, raw.reversed ? ClusteringOrder.DESC : ClusteringOrder.ASC);
                break;
            default:
        }
        allColumnsBuilder.put(column.getName(), column);
    }
    Map<CqlIdentifier, Object> options;
    try {
        options = parseOptions(viewRow);
    } catch (Exception e) {
        // Options change the most often, so be especially lenient if anything goes wrong.
        Loggers.warnWithException(LOG, "[{}] Error while parsing options for {}.{}, getOptions() will be empty", logPrefix, keyspaceId, viewId, e);
        options = Collections.emptyMap();
    }
    return new DefaultDseViewMetadata(keyspaceId, viewId, baseTableId, includesAllColumns, whereClause, uuid, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options);
}
Also used : DefaultDseColumnMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DseColumnMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseColumnMetadata) DefaultDseColumnMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata) DseColumnMetadata(com.datastax.dse.driver.api.core.metadata.schema.DseColumnMetadata) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) DefaultDseColumnMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseColumnMetadata) DefaultDseViewMetadata(com.datastax.dse.driver.internal.core.metadata.schema.DefaultDseViewMetadata) RawColumn(com.datastax.oss.driver.internal.core.metadata.schema.parsing.RawColumn) DataType(com.datastax.oss.driver.api.core.type.DataType) UUID(java.util.UUID) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)

Example 50 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class SettableByIndex method setByte.

/**
 * Sets the {@code i}th value to the provided Java primitive byte.
 *
 * <p>By default, this works with CQL type {@code tinyint}.
 *
 * <p>To set the value to CQL {@code NULL}, use {@link #setToNull(int)}, or {@code set(i, v,
 * Boolean.class)}.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
@NonNull
@CheckReturnValue
default SelfT setByte(int i, byte v) {
    DataType cqlType = getType(i);
    TypeCodec<Byte> codec = codecRegistry().codecFor(cqlType, Byte.class);
    return (codec instanceof PrimitiveByteCodec) ? setBytesUnsafe(i, ((PrimitiveByteCodec) codec).encodePrimitive(v, protocolVersion())) : set(i, v, codec);
}
Also used : PrimitiveByteCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveByteCodec) DataType(com.datastax.oss.driver.api.core.type.DataType) CheckReturnValue(edu.umd.cs.findbugs.annotations.CheckReturnValue) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)73 NonNull (edu.umd.cs.findbugs.annotations.NonNull)21 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)19 SetType (com.datastax.oss.driver.api.core.type.SetType)10 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)10 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)10 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)9 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)9 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)8 Map (java.util.Map)8 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)7 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)7 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)6 Nullable (edu.umd.cs.findbugs.annotations.Nullable)6 UUID (java.util.UUID)6 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)5 List (java.util.List)5