Search in sources :

Example 1 with ListType

use of com.datastax.oss.driver.api.core.type.ListType in project calcite by apache.

the class CassandraSchema method getRelDataType.

RelProtoDataType getRelDataType(String columnFamily, boolean view) {
    Map<CqlIdentifier, ColumnMetadata> columns;
    CqlIdentifier tableName = CqlIdentifier.fromInternal(columnFamily);
    if (view) {
        Optional<ViewMetadata> optionalViewMetadata = getKeyspace().getView(tableName);
        if (optionalViewMetadata.isPresent()) {
            columns = optionalViewMetadata.get().getColumns();
        } else {
            throw new IllegalStateException("Unknown view " + tableName + " in keyspace " + keyspace);
        }
    } else {
        Optional<TableMetadata> optionalTableMetadata = getKeyspace().getTable(tableName);
        if (optionalTableMetadata.isPresent()) {
            columns = optionalTableMetadata.get().getColumns();
        } else {
            throw new IllegalStateException("Unknown table " + tableName + " in keyspace " + keyspace);
        }
    }
    // Temporary type factory, just for the duration of this method. Allowable
    // because we're creating a proto-type, not a type; before being used, the
    // proto-type will be copied into a real type factory.
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    for (ColumnMetadata column : columns.values()) {
        final DataType dataType = column.getType();
        final String columnName = column.getName().asInternal();
        if (dataType instanceof ListType) {
            SqlTypeName arrayInnerType = CQL_TO_SQL_TYPE.lookup(((ListType) dataType).getElementType());
            fieldInfo.add(columnName, typeFactory.createArrayType(typeFactory.createSqlType(arrayInnerType), -1)).nullable(true);
        } else if (dataType instanceof SetType) {
            SqlTypeName multiSetInnerType = CQL_TO_SQL_TYPE.lookup(((SetType) dataType).getElementType());
            fieldInfo.add(columnName, typeFactory.createMultisetType(typeFactory.createSqlType(multiSetInnerType), -1)).nullable(true);
        } else if (dataType instanceof MapType) {
            MapType columnType = (MapType) dataType;
            SqlTypeName keyType = CQL_TO_SQL_TYPE.lookup(columnType.getKeyType());
            SqlTypeName valueType = CQL_TO_SQL_TYPE.lookup(columnType.getValueType());
            fieldInfo.add(columnName, typeFactory.createMapType(typeFactory.createSqlType(keyType), typeFactory.createSqlType(valueType))).nullable(true);
        } else if (dataType instanceof TupleType) {
            List<DataType> typeArgs = ((TupleType) dataType).getComponentTypes();
            List<Map.Entry<String, RelDataType>> typesList = IntStream.range(0, typeArgs.size()).mapToObj(i -> new Pair<>(// 1 indexed (as ARRAY)
            Integer.toString(i + 1), typeFactory.createSqlType(CQL_TO_SQL_TYPE.lookup(typeArgs.get(i))))).collect(Collectors.toList());
            fieldInfo.add(columnName, typeFactory.createStructType(typesList)).nullable(true);
        } else {
            SqlTypeName typeName = CQL_TO_SQL_TYPE.lookup(dataType);
            fieldInfo.add(columnName, typeName).nullable(true);
        }
    }
    return RelDataTypeImpl.proto(fieldInfo.build());
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MapType(com.datastax.oss.driver.api.core.type.MapType) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TupleType(com.datastax.oss.driver.api.core.type.TupleType) RelDataType(org.apache.calcite.rel.type.RelDataType) DataType(com.datastax.oss.driver.api.core.type.DataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ViewMetadata(com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)

Example 2 with ListType

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

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache.
// Variant where the Java type is unknown.
@NonNull
protected TypeCodec<?> createCodec(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.listOf(elementCodec);
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        TypeCodec<Object> elementCodec = codecFor(elementType);
        return TypeCodecs.setOf(elementCodec);
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        TypeCodec<Object> keyCodec = codecFor(keyType);
        TypeCodec<Object> valueCodec = codecFor(valueType);
        return TypeCodecs.mapOf(keyCodec, valueCodec);
    } else if (cqlType instanceof TupleType) {
        return TypeCodecs.tupleOf((TupleType) cqlType);
    } else if (cqlType instanceof UserDefinedType) {
        return TypeCodecs.udtOf((UserDefinedType) cqlType);
    } else if (cqlType instanceof CustomType) {
        return TypeCodecs.custom(cqlType);
    }
    throw new CodecNotFoundException(cqlType, null);
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with ListType

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

the class CachingCodecRegistry method inferJavaTypeFromCqlType.

@NonNull
protected GenericType<?> inferJavaTypeFromCqlType(@NonNull DataType cqlType) {
    if (cqlType instanceof ListType) {
        DataType elementType = ((ListType) cqlType).getElementType();
        return GenericType.listOf(inferJavaTypeFromCqlType(elementType));
    } else if (cqlType instanceof SetType) {
        DataType elementType = ((SetType) cqlType).getElementType();
        return GenericType.setOf(inferJavaTypeFromCqlType(elementType));
    } else if (cqlType instanceof MapType) {
        DataType keyType = ((MapType) cqlType).getKeyType();
        DataType valueType = ((MapType) cqlType).getValueType();
        return GenericType.mapOf(inferJavaTypeFromCqlType(keyType), inferJavaTypeFromCqlType(valueType));
    }
    switch(cqlType.getProtocolCode()) {
        case ProtocolConstants.DataType.CUSTOM:
        case ProtocolConstants.DataType.BLOB:
            return GenericType.BYTE_BUFFER;
        case ProtocolConstants.DataType.ASCII:
        case ProtocolConstants.DataType.VARCHAR:
            return GenericType.STRING;
        case ProtocolConstants.DataType.BIGINT:
        case ProtocolConstants.DataType.COUNTER:
            return GenericType.LONG;
        case ProtocolConstants.DataType.BOOLEAN:
            return GenericType.BOOLEAN;
        case ProtocolConstants.DataType.DECIMAL:
            return GenericType.BIG_DECIMAL;
        case ProtocolConstants.DataType.DOUBLE:
            return GenericType.DOUBLE;
        case ProtocolConstants.DataType.FLOAT:
            return GenericType.FLOAT;
        case ProtocolConstants.DataType.INT:
            return GenericType.INTEGER;
        case ProtocolConstants.DataType.TIMESTAMP:
            return GenericType.INSTANT;
        case ProtocolConstants.DataType.UUID:
        case ProtocolConstants.DataType.TIMEUUID:
            return GenericType.UUID;
        case ProtocolConstants.DataType.VARINT:
            return GenericType.BIG_INTEGER;
        case ProtocolConstants.DataType.INET:
            return GenericType.INET_ADDRESS;
        case ProtocolConstants.DataType.DATE:
            return GenericType.LOCAL_DATE;
        case ProtocolConstants.DataType.TIME:
            return GenericType.LOCAL_TIME;
        case ProtocolConstants.DataType.SMALLINT:
            return GenericType.SHORT;
        case ProtocolConstants.DataType.TINYINT:
            return GenericType.BYTE;
        case ProtocolConstants.DataType.DURATION:
            return GenericType.CQL_DURATION;
        case ProtocolConstants.DataType.UDT:
            return GenericType.UDT_VALUE;
        case ProtocolConstants.DataType.TUPLE:
            return GenericType.TUPLE_VALUE;
        default:
            throw new CodecNotFoundException(cqlType, null);
    }
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) DataType(com.datastax.oss.driver.api.core.type.DataType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) MapType(com.datastax.oss.driver.api.core.type.MapType) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 4 with ListType

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

the class UserDefinedTypeListParserTest method should_resolve_list_dependency.

@Test
public void should_resolve_list_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("bs"), ImmutableList.of("frozen<list<frozen<b>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(2);
    UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    assertThat(((ListType) aType.getFieldTypes().get(0)).getElementType()).isEqualTo(bType);
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 5 with ListType

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

the class DseTableParser method buildLegacyIndexTarget.

private static String buildLegacyIndexTarget(ColumnMetadata column, Map<String, String> options) {
    String columnName = column.getName().asCql(true);
    DataType columnType = column.getType();
    if (options.containsKey("index_keys")) {
        return String.format("keys(%s)", columnName);
    }
    if (options.containsKey("index_keys_and_values")) {
        return String.format("entries(%s)", columnName);
    }
    if ((columnType instanceof ListType && ((ListType) columnType).isFrozen()) || (columnType instanceof SetType && ((SetType) columnType).isFrozen()) || (columnType instanceof MapType && ((MapType) columnType).isFrozen())) {
        return String.format("full(%s)", columnName);
    }
    // Note: the keyword 'values' is not accepted as a valid index target function until 3.0
    return columnName;
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) DataType(com.datastax.oss.driver.api.core.type.DataType) MapType(com.datastax.oss.driver.api.core.type.MapType)

Aggregations

ListType (com.datastax.oss.driver.api.core.type.ListType)13 MapType (com.datastax.oss.driver.api.core.type.MapType)7 SetType (com.datastax.oss.driver.api.core.type.SetType)7 TupleType (com.datastax.oss.driver.api.core.type.TupleType)7 DataType (com.datastax.oss.driver.api.core.type.DataType)6 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)6 Test (org.junit.Test)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 CustomType (com.datastax.oss.driver.api.core.type.CustomType)3 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)2 List (java.util.List)2 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)1 ViewMetadata (com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)1 GenericType (com.datastax.oss.driver.api.core.type.reflect.GenericType)1 ShallowUserDefinedType (com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1