use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class CaseSensitiveUdtIT method should_expose_metadata_with_correct_case.
@Test
public void should_expose_metadata_with_correct_case() {
boolean supportsFunctions = CCM_RULE.getCassandraVersion().compareTo(Version.V2_2_0) >= 0;
CqlSession session = SESSION_RULE.session();
session.execute("CREATE TYPE \"Address\"(street text)");
session.execute("CREATE TABLE user(id uuid PRIMARY KEY, address frozen<\"Address\">)");
session.execute("CREATE TYPE t(a frozen<\"Address\">)");
if (supportsFunctions) {
session.execute("CREATE FUNCTION eq(a \"Address\") " + "CALLED ON NULL INPUT " + "RETURNS \"Address\" " + "LANGUAGE java " + "AS $$return a;$$");
session.execute("CREATE FUNCTION left(l \"Address\", r \"Address\") " + "CALLED ON NULL INPUT " + "RETURNS \"Address\" " + "LANGUAGE java " + "AS $$return l;$$");
session.execute("CREATE AGGREGATE ag(\"Address\") " + "SFUNC left " + "STYPE \"Address\" " + "INITCOND {street: 'foo'};");
}
KeyspaceMetadata keyspace = session.getMetadata().getKeyspace(SESSION_RULE.keyspace()).orElseThrow(() -> new AssertionError("Couldn't find rule's keyspace"));
UserDefinedType addressType = keyspace.getUserDefinedType(CqlIdentifier.fromInternal("Address")).orElseThrow(() -> new AssertionError("Couldn't find UDT definition"));
assertThat(keyspace.getTable("user")).hasValueSatisfying(table -> assertThat(table.getColumn("address")).hasValueSatisfying(column -> assertThat(column.getType()).isEqualTo(addressType)));
assertThat(keyspace.getUserDefinedType("t")).hasValueSatisfying(type -> assertThat(type.getFieldTypes()).containsExactly(addressType));
if (supportsFunctions) {
assertThat(keyspace.getFunction("eq", addressType)).hasValueSatisfying(function -> {
assertThat(function.getSignature().getParameterTypes()).containsExactly(addressType);
assertThat(function.getReturnType()).isEqualTo(addressType);
});
assertThat(keyspace.getAggregate("ag", addressType)).hasValueSatisfying(aggregate -> {
assertThat(aggregate.getSignature().getParameterTypes()).containsExactly(addressType);
assertThat(aggregate.getStateType()).isEqualTo(addressType);
assertThat(aggregate.getReturnType()).isEqualTo(addressType);
});
}
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class UserDefinedTypeParser method parse.
/**
* Contrary to other element parsers, this one processes all the types of a keyspace in one go.
* UDTs can depend on each other, but the system table returns them in alphabetical order. In
* order to properly build the definitions, we need to do a topological sort of the rows first, so
* that each type is parsed after its dependencies.
*/
public Map<CqlIdentifier, UserDefinedType> parse(Collection<AdminRow> typeRows, CqlIdentifier keyspaceId) {
if (typeRows.isEmpty()) {
return Collections.emptyMap();
} else {
Map<CqlIdentifier, UserDefinedType> types = new LinkedHashMap<>();
for (AdminRow row : topologicalSort(typeRows, keyspaceId)) {
UserDefinedType type = parseType(row, keyspaceId, types);
types.put(type.getName(), type);
}
return ImmutableMap.copyOf(types);
}
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class DataTypeDetachableTest method manually_created_udt_should_be_detached.
@Test
public void manually_created_udt_should_be_detached() {
UserDefinedType udt = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
assertThat(udt.isDetached()).isTrue();
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class CachingCodecRegistryTestDataProviders method collectionsWithCqlAndJavaTypes.
@DataProvider
public static Object[][] collectionsWithCqlAndJavaTypes() throws UnknownHostException, ClassNotFoundException {
TupleType tupleType = DataTypes.tupleOf(DataTypes.INT, DataTypes.listOf(DataTypes.TEXT));
TupleValue tupleValue = tupleType.newValue();
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();
UdtValue udtValue = userType.newValue();
return new Object[][] { // lists
{ DataTypes.listOf(DataTypes.INT), GenericType.listOf(Integer.class), GenericType.listOf(Integer.class), ImmutableList.of(1) }, { DataTypes.listOf(DataTypes.TEXT), GenericType.listOf(String.class), GenericType.listOf(String.class), ImmutableList.of("foo") }, { DataTypes.listOf(DataTypes.BLOB), GenericType.listOf(ByteBuffer.class), GenericType.listOf(Class.forName("java.nio.HeapByteBuffer")), ImmutableList.of(ByteBuffer.wrap(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.listOf(DataTypes.INET), GenericType.listOf(InetAddress.class), GenericType.listOf(Inet4Address.class), ImmutableList.of(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.listOf(tupleType), GenericType.listOf(TupleValue.class), GenericType.listOf(DefaultTupleValue.class), ImmutableList.of(tupleValue) }, { DataTypes.listOf(userType), GenericType.listOf(UdtValue.class), GenericType.listOf(DefaultUdtValue.class), ImmutableList.of(udtValue) }, { DataTypes.listOf(DataTypes.listOf(DataTypes.INT)), GenericType.listOf(GenericType.listOf(Integer.class)), GenericType.listOf(GenericType.listOf(Integer.class)), ImmutableList.of(ImmutableList.of(1)) }, { DataTypes.listOf(DataTypes.listOf(tupleType)), GenericType.listOf(GenericType.listOf(TupleValue.class)), GenericType.listOf(GenericType.listOf(DefaultTupleValue.class)), ImmutableList.of(ImmutableList.of(tupleValue)) }, { DataTypes.listOf(DataTypes.listOf(userType)), GenericType.listOf(GenericType.listOf(UdtValue.class)), GenericType.listOf(GenericType.listOf(DefaultUdtValue.class)), ImmutableList.of(ImmutableList.of(udtValue)) }, // sets
{ DataTypes.setOf(DataTypes.INT), GenericType.setOf(Integer.class), GenericType.setOf(Integer.class), ImmutableSet.of(1) }, { DataTypes.setOf(DataTypes.TEXT), GenericType.setOf(String.class), GenericType.setOf(String.class), ImmutableSet.of("foo") }, { DataTypes.setOf(DataTypes.BLOB), GenericType.setOf(ByteBuffer.class), GenericType.setOf(Class.forName("java.nio.HeapByteBuffer")), ImmutableSet.of(ByteBuffer.wrap(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.setOf(DataTypes.INET), GenericType.setOf(InetAddress.class), GenericType.setOf(Inet4Address.class), ImmutableSet.of(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.setOf(tupleType), GenericType.setOf(TupleValue.class), GenericType.setOf(DefaultTupleValue.class), ImmutableSet.of(tupleValue) }, { DataTypes.setOf(userType), GenericType.setOf(UdtValue.class), GenericType.setOf(DefaultUdtValue.class), ImmutableSet.of(udtValue) }, { DataTypes.setOf(DataTypes.setOf(DataTypes.INT)), GenericType.setOf(GenericType.setOf(Integer.class)), GenericType.setOf(GenericType.setOf(Integer.class)), ImmutableSet.of(ImmutableSet.of(1)) }, { DataTypes.setOf(DataTypes.setOf(tupleType)), GenericType.setOf(GenericType.setOf(TupleValue.class)), GenericType.setOf(GenericType.setOf(DefaultTupleValue.class)), ImmutableSet.of(ImmutableSet.of(tupleValue)) }, { DataTypes.setOf(DataTypes.setOf(userType)), GenericType.setOf(GenericType.setOf(UdtValue.class)), GenericType.setOf(GenericType.setOf(DefaultUdtValue.class)), ImmutableSet.of(ImmutableSet.of(udtValue)) }, // maps
{ DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT), GenericType.mapOf(Integer.class, String.class), GenericType.mapOf(Integer.class, String.class), ImmutableMap.of(1, "foo") }, { DataTypes.mapOf(DataTypes.BLOB, DataTypes.INET), GenericType.mapOf(ByteBuffer.class, InetAddress.class), GenericType.mapOf(Class.forName("java.nio.HeapByteBuffer"), Inet4Address.class), ImmutableMap.of(ByteBuffer.wrap(new byte[] { 127, 0, 0, 1 }), InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })) }, { DataTypes.mapOf(tupleType, tupleType), GenericType.mapOf(TupleValue.class, TupleValue.class), GenericType.mapOf(DefaultTupleValue.class, DefaultTupleValue.class), ImmutableMap.of(tupleValue, tupleValue) }, { DataTypes.mapOf(userType, userType), GenericType.mapOf(UdtValue.class, UdtValue.class), GenericType.mapOf(DefaultUdtValue.class, DefaultUdtValue.class), ImmutableMap.of(udtValue, udtValue) }, { DataTypes.mapOf(DataTypes.UUID, DataTypes.mapOf(DataTypes.INT, DataTypes.TEXT)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Integer.class, String.class)), GenericType.mapOf(GenericType.UUID, GenericType.mapOf(Integer.class, String.class)), ImmutableMap.of(UUID.randomUUID(), ImmutableMap.of(1, "foo")) }, { DataTypes.mapOf(DataTypes.mapOf(userType, userType), DataTypes.mapOf(tupleType, tupleType)), GenericType.mapOf(GenericType.mapOf(UdtValue.class, UdtValue.class), GenericType.mapOf(TupleValue.class, TupleValue.class)), GenericType.mapOf(GenericType.mapOf(DefaultUdtValue.class, DefaultUdtValue.class), GenericType.mapOf(DefaultTupleValue.class, DefaultTupleValue.class)), ImmutableMap.of(ImmutableMap.of(udtValue, udtValue), ImmutableMap.of(tupleValue, tupleValue)) } };
}
use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.
the class CachingCodecRegistryTestDataProviders method udtsWithCqlTypes.
@DataProvider
public static Object[][] udtsWithCqlTypes() {
UserDefinedType userType1 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
UserDefinedType userType2 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.setOf(DataTypes.BIGINT)).withField(CqlIdentifier.fromInternal("field2"), DataTypes.listOf(DataTypes.TEXT)).build();
UserDefinedType userType3 = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.mapOf(userType1, userType2)).build();
UdtValue userValue1 = userType1.newValue(42, "foo");
UdtValue userValue2 = userType2.newValue(ImmutableSet.of(24L, 43L), ImmutableList.of("foo", "bar"));
return new Object[][] { { userType1, userType1.newValue() }, { userType1, userValue1 }, { userType2, userType2.newValue() }, { userType2, userValue2 }, { userType3, userType3.newValue() }, { userType3, userType3.newValue(ImmutableMap.of(userValue1, userValue2)) } };
}
Aggregations