Search in sources :

Example 1 with TupleType

use of com.datastax.oss.driver.api.core.type.TupleType 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 TupleType

use of com.datastax.oss.driver.api.core.type.TupleType 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 TupleType

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

the class CachingCodecRegistryTestDataProviders method emptyCollectionsWithCqlAndJavaTypes.

@DataProvider
public static Object[][] emptyCollectionsWithCqlAndJavaTypes() {
    TupleType tupleType = DataTypes.tupleOf(DataTypes.INT, DataTypes.listOf(DataTypes.TEXT));
    UserDefinedType userType = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.listOf(DataTypes.TEXT)).build();
    return new Object[][] { // lists
    { DataTypes.listOf(DataTypes.INT), GenericType.listOf(Integer.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.TEXT), GenericType.listOf(String.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.BLOB), GenericType.listOf(ByteBuffer.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.INET), GenericType.listOf(InetAddress.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(tupleType), GenericType.listOf(TupleValue.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(userType), GenericType.listOf(UdtValue.class), DataTypes.listOf(DataTypes.BOOLEAN), GenericType.listOf(Boolean.class), Collections.emptyList() }, { DataTypes.listOf(DataTypes.listOf(DataTypes.INT)), GenericType.listOf(GenericType.listOf(Integer.class)), DataTypes.listOf(DataTypes.listOf(DataTypes.BOOLEAN)), GenericType.listOf(GenericType.listOf(Boolean.class)), ImmutableList.of(Collections.emptyList()) }, { DataTypes.listOf(DataTypes.listOf(tupleType)), GenericType.listOf(GenericType.listOf(TupleValue.class)), DataTypes.listOf(DataTypes.listOf(DataTypes.BOOLEAN)), GenericType.listOf(GenericType.listOf(Boolean.class)), ImmutableList.of(Collections.emptyList()) }, { DataTypes.listOf(DataTypes.listOf(userType)), GenericType.listOf(GenericType.listOf(UdtValue.class)), DataTypes.listOf(DataTypes.listOf(DataTypes.BOOLEAN)), GenericType.listOf(GenericType.listOf(Boolean.class)), ImmutableList.of(Collections.emptyList()) }, // sets
    { DataTypes.setOf(DataTypes.INT), GenericType.setOf(Integer.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.TEXT), GenericType.setOf(String.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.BLOB), GenericType.setOf(ByteBuffer.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.INET), GenericType.setOf(InetAddress.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(tupleType), GenericType.setOf(TupleValue.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(userType), GenericType.setOf(UdtValue.class), DataTypes.setOf(DataTypes.BOOLEAN), GenericType.setOf(Boolean.class), Collections.emptySet() }, { DataTypes.setOf(DataTypes.setOf(DataTypes.INT)), GenericType.setOf(GenericType.setOf(Integer.class)), DataTypes.setOf(DataTypes.setOf(DataTypes.BOOLEAN)), GenericType.setOf(GenericType.setOf(Boolean.class)), ImmutableSet.of(Collections.emptySet()) }, { DataTypes.setOf(DataTypes.setOf(tupleType)), GenericType.setOf(GenericType.setOf(TupleValue.class)), DataTypes.setOf(DataTypes.setOf(DataTypes.BOOLEAN)), GenericType.setOf(GenericType.setOf(Boolean.class)), ImmutableSet.of(Collections.emptySet()) }, { DataTypes.setOf(DataTypes.setOf(userType)), GenericType.setOf(GenericType.setOf(UdtValue.class)), DataTypes.setOf(DataTypes.setOf(DataTypes.BOOLEAN)), GenericType.setOf(GenericType.setOf(Boolean.class)), ImmutableSet.of(Collections.emptySet()) }, // maps
    { DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT), GenericType.mapOf(Integer.class, String.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(DataTypes.BLOB, DataTypes.INET), GenericType.mapOf(ByteBuffer.class, InetAddress.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(tupleType, tupleType), GenericType.mapOf(TupleValue.class, TupleValue.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(userType, userType), GenericType.mapOf(UdtValue.class, UdtValue.class), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), GenericType.mapOf(Boolean.class, Boolean.class), Collections.emptyMap() }, { DataTypes.mapOf(DataTypes.UUID, DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Integer.class, String.class)), DataTypes.mapOf(DataTypes.UUID, DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Boolean.class, Boolean.class)), ImmutableMap.of(UUID.randomUUID(), Collections.emptyMap()) }, { DataTypes.mapOf(DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT), DataTypes.UUID), GenericType.mapOf(GenericType.mapOf(Integer.class, String.class), GenericType.UUID), DataTypes.mapOf(DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), DataTypes.UUID), GenericType.mapOf(GenericType.mapOf(Boolean.class, Boolean.class), GenericType.UUID), ImmutableMap.of(Collections.emptyMap(), UUID.randomUUID()) }, { DataTypes.mapOf(DataTypes.mapOf(userType, userType), DataTypes.mapOf(tupleType, tupleType)), GenericType.mapOf(GenericType.mapOf(UdtValue.class, UdtValue.class), GenericType.mapOf(TupleValue.class, TupleValue.class)), DataTypes.mapOf(DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN), DataTypes.mapOf(DataTypes.BOOLEAN, DataTypes.BOOLEAN)), GenericType.mapOf(GenericType.mapOf(Boolean.class, Boolean.class), GenericType.mapOf(Boolean.class, Boolean.class)), ImmutableMap.of(Collections.emptyMap(), Collections.emptyMap()) } };
}
Also used : BigInteger(java.math.BigInteger) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Example 4 with TupleType

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

the class DefaultTupleValueTest method should_not_equate_instances_with_same_binary_representation_but_different_types.

@Test
public void should_not_equate_instances_with_same_binary_representation_but_different_types() {
    TupleType tupleType1 = DataTypes.tupleOf(DataTypes.INT);
    TupleType tupleType2 = DataTypes.tupleOf(DataTypes.VARINT);
    TupleValue tuple1 = tupleType1.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x00000001"));
    TupleValue tuple2 = tupleType2.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x00000001"));
    assertThat(tuple1).isNotEqualTo(tuple2);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) DefaultTupleType(com.datastax.oss.driver.internal.core.type.DefaultTupleType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 5 with TupleType

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

the class GraphDataTypesTest method complexTypesAndGeoTests.

@Test
public void complexTypesAndGeoTests() throws IOException {
    TupleType tuple = tupleOf(DseDataTypes.POINT, DseDataTypes.LINE_STRING, DseDataTypes.POLYGON);
    tuple.attach(context);
    verifySerDeBinary(tuple.newValue(Point.fromCoordinates(3.3, 4.4), LineString.fromPoints(Point.fromCoordinates(1, 1), Point.fromCoordinates(2, 2), Point.fromCoordinates(3, 3)), Polygon.fromPoints(Point.fromCoordinates(3, 4), Point.fromCoordinates(5, 4), Point.fromCoordinates(6, 6))));
    UserDefinedType udt = new UserDefinedTypeBuilder("ks", "udt1").withField("a", DseDataTypes.POINT).withField("b", DseDataTypes.LINE_STRING).withField("c", DseDataTypes.POLYGON).build();
    udt.attach(context);
    verifySerDeBinary(udt.newValue(Point.fromCoordinates(3.3, 4.4), LineString.fromPoints(Point.fromCoordinates(1, 1), Point.fromCoordinates(2, 2), Point.fromCoordinates(3, 3)), Polygon.fromPoints(Point.fromCoordinates(3, 4), Point.fromCoordinates(5, 4), Point.fromCoordinates(6, 6))));
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Aggregations

TupleType (com.datastax.oss.driver.api.core.type.TupleType)32 Test (org.junit.Test)22 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)13 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)12 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)5 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)5 ByteBuffer (java.nio.ByteBuffer)5 DataType (com.datastax.oss.driver.api.core.type.DataType)4 BigInteger (java.math.BigInteger)4 CustomType (com.datastax.oss.driver.api.core.type.CustomType)3 DefaultTupleValue (com.datastax.oss.driver.internal.core.data.DefaultTupleValue)3 LineString (com.datastax.dse.driver.api.core.data.geometry.LineString)2 Point (com.datastax.dse.driver.api.core.data.geometry.Point)2 Polygon (com.datastax.dse.driver.api.core.data.geometry.Polygon)2 Geo (com.datastax.dse.driver.api.core.graph.predicates.Geo)2 DseDataTypes (com.datastax.dse.driver.api.core.type.DseDataTypes)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2