Search in sources :

Example 46 with UserDefinedType

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);
        });
    }
}
Also used : CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) TestRule(org.junit.rules.TestRule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SessionUtils(com.datastax.oss.driver.api.testinfra.session.SessionUtils) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) RuleChain(org.junit.rules.RuleChain) Version(com.datastax.oss.driver.api.core.Version) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Duration(java.time.Duration) ClassRule(org.junit.ClassRule) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlSession(com.datastax.oss.driver.api.core.CqlSession) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Test(org.junit.Test)

Example 47 with UserDefinedType

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);
    }
}
Also used : DefaultUserDefinedType(com.datastax.oss.driver.internal.core.type.DefaultUserDefinedType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) AdminRow(com.datastax.oss.driver.internal.core.adminrequest.AdminRow) LinkedHashMap(java.util.LinkedHashMap)

Example 48 with UserDefinedType

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();
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Test(org.junit.Test)

Example 49 with UserDefinedType

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)) } };
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) Inet4Address(java.net.Inet4Address) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ByteBuffer(java.nio.ByteBuffer) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) BigInteger(java.math.BigInteger) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) InetAddress(java.net.InetAddress) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Example 50 with UserDefinedType

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)) } };
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DefaultUdtValue(com.datastax.oss.driver.internal.core.data.DefaultUdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4