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());
}
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);
}
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()) } };
}
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);
}
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))));
}
Aggregations