Search in sources :

Example 36 with DataType

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

the class ViewParser method parseView.

public ViewMetadata 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);
        ColumnMetadata column = new DefaultColumnMetadata(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 DefaultViewMetadata(keyspaceId, viewId, baseTableId, includesAllColumns, whereClause, uuid, partitionKeyBuilder.build(), clusteringColumnsBuilder.build(), allColumnsBuilder.build(), options);
}
Also used : DefaultColumnMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DefaultColumnMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultColumnMetadata) 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) DefaultViewMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultViewMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) UUID(java.util.UUID) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)

Example 37 with DataType

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

the class AggregateParser method parseAggregate.

public AggregateMetadata parseAggregate(AdminRow row, CqlIdentifier keyspaceId, Map<CqlIdentifier, UserDefinedType> userDefinedTypes) {
    // Cassandra < 3.0:
    // CREATE TABLE system.schema_aggregates (
    // keyspace_name text,
    // aggregate_name text,
    // signature frozen<list<text>>,
    // argument_types list<text>,
    // final_func text,
    // initcond blob,
    // return_type text,
    // state_func text,
    // state_type text,
    // PRIMARY KEY (keyspace_name, aggregate_name, signature)
    // ) WITH CLUSTERING ORDER BY (aggregate_name ASC, signature ASC)
    // 
    // Cassandra >= 3.0:
    // CREATE TABLE system.schema_aggregates (
    // keyspace_name text,
    // aggregate_name text,
    // argument_types frozen<list<text>>,
    // final_func text,
    // initcond text,
    // return_type text,
    // state_func text,
    // state_type text,
    // PRIMARY KEY (keyspace_name, aggregate_name, argument_types)
    // ) WITH CLUSTERING ORDER BY (aggregate_name ASC, argument_types ASC)
    String simpleName = row.getString("aggregate_name");
    List<String> argumentTypes = row.getListOfString("argument_types");
    FunctionSignature signature = new FunctionSignature(CqlIdentifier.fromInternal(simpleName), dataTypeParser.parse(keyspaceId, argumentTypes, userDefinedTypes, context));
    DataType stateType = dataTypeParser.parse(keyspaceId, row.getString("state_type"), userDefinedTypes, context);
    TypeCodec<Object> stateTypeCodec = context.getCodecRegistry().codecFor(stateType);
    String stateFuncSimpleName = row.getString("state_func");
    FunctionSignature stateFuncSignature = new FunctionSignature(CqlIdentifier.fromInternal(stateFuncSimpleName), ImmutableList.<DataType>builder().add(stateType).addAll(signature.getParameterTypes()).build());
    String finalFuncSimpleName = row.getString("final_func");
    FunctionSignature finalFuncSignature = (finalFuncSimpleName == null) ? null : new FunctionSignature(CqlIdentifier.fromInternal(finalFuncSimpleName), stateType);
    DataType returnType = dataTypeParser.parse(keyspaceId, row.getString("return_type"), userDefinedTypes, context);
    Object initCond;
    if (row.isString("initcond")) {
        // Cassandra 3
        String initCondString = row.getString("initcond");
        initCond = (initCondString == null) ? null : stateTypeCodec.parse(initCondString);
    } else {
        // Cassandra 2.2
        ByteBuffer initCondBlob = row.getByteBuffer("initcond");
        initCond = (initCondBlob == null) ? null : stateTypeCodec.decode(initCondBlob, context.getProtocolVersion());
    }
    return new DefaultAggregateMetadata(keyspaceId, signature, finalFuncSignature, initCond, returnType, stateFuncSignature, stateType, stateTypeCodec);
}
Also used : DefaultAggregateMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultAggregateMetadata) DataType(com.datastax.oss.driver.api.core.type.DataType) FunctionSignature(com.datastax.oss.driver.api.core.metadata.schema.FunctionSignature) ByteBuffer(java.nio.ByteBuffer)

Example 38 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project jnosql-diana-driver by eclipse.

the class QueryUtils method getUdtValue.

private static Object getUdtValue(UserDefinedType userType, Iterable elements, DataType type) {
    Collection<Object> udtValues = getCollectionUdt(type);
    UdtValue udtValue = userType.newValue();
    final List<String> udtNames = userType.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(Collectors.toList());
    for (Object object : elements) {
        if (Column.class.isInstance(object)) {
            Column column = Column.class.cast(object);
            Object convert = ValueUtil.convert(column.getValue());
            final int index = udtNames.indexOf(column.getName());
            if (index < 0) {
                throw new CommunicationException("This field has not been found: " + column.getName() + " the fields available are " + udtNames + " in the UDT type " + userType.getName().asCql(true) + " at the keyspace " + userType.getKeyspace());
            }
            DataType fieldType = userType.getFieldTypes().get(index);
            TypeCodec<Object> objectTypeCodec = CodecRegistry.DEFAULT.codecFor(fieldType);
            if (fieldType instanceof SetType) {
                udtValue.set(getName(column), new HashSet<Object>((Collection<?>) convert), objectTypeCodec);
            } else {
                udtValue.set(getName(column), convert, objectTypeCodec);
            }
        } else if (Iterable.class.isInstance(object)) {
            udtValues.add(getUdtValue(userType, Iterable.class.cast(Iterable.class.cast(object)), type));
        }
    }
    if (udtValues.isEmpty()) {
        return udtValue;
    }
    return udtValues;
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) CommunicationException(jakarta.nosql.CommunicationException) Column(jakarta.nosql.column.Column) SetType(com.datastax.oss.driver.api.core.type.SetType) DataType(com.datastax.oss.driver.api.core.type.DataType) Collection(java.util.Collection)

Example 39 with DataType

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

the class AbstractBufferedRateExecutor method toStringWithValues.

private static String toStringWithValues(BoundStatement boundStatement, ProtocolVersion protocolVersion) {
    CodecRegistry codecRegistry = boundStatement.codecRegistry();
    PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
    String query = preparedStatement.getQuery();
    ColumnDefinitions defs = preparedStatement.getVariableDefinitions();
    int index = 0;
    for (ColumnDefinition def : defs) {
        DataType type = def.getType();
        TypeCodec<Object> codec = codecRegistry.codecFor(type);
        if (boundStatement.getBytesUnsafe(index) != null) {
            Object value = codec.decode(boundStatement.getBytesUnsafe(index), protocolVersion);
            String replacement = Matcher.quoteReplacement(codec.format(value));
            query = query.replaceFirst("\\?", replacement);
        }
        index++;
    }
    return query;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) DataType(com.datastax.oss.driver.api.core.type.DataType) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition)

Example 40 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project jnosql-diana-driver by eclipse.

the class CassandraConverter method get.

static Object get(ColumnDefinition definition, Row row) {
    String name = definition.getName().asInternal();
    final DataType type = definition.getType();
    if (type instanceof UserDefinedType) {
        return getUDT(definition, row.getUdtValue(name));
    }
    final TypeCodec<Object> codec = row.codecRegistry().codecFor(type);
    return row.get(name, codec);
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType)

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