Search in sources :

Example 16 with TupleType

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

the class CoreGraphDataTypeITBase method should_insert_and_retrieve_nested_UDTS_and_tuples.

@Test
public void should_insert_and_retrieve_nested_UDTS_and_tuples() {
    CqlSession session = session();
    // use CQL to create type for now because DSP-17567 is not in yet, so this is more stable
    session.execute(String.format("CREATE TYPE %s.udt1(a int, b text)", graphName()));
    session.execute(String.format("CREATE TYPE %s.udt2(" + "a int" + ", b text" + ", c frozen<udt1>" + ", mylist list<bigint>" + ", mytuple_withlist tuple<varchar, tuple<bigint, frozen<list<bigint>>>>" + ")", graphName()));
    session.execute(String.format("CREATE TYPE %s.udt3(" + "a list<int>" + ", b set<float>" + ", c map<text, bigint>" + ", d list<frozen<list<double>>>" + ", e set<frozen<set<float>>>" + ", f list<frozen<tuple<int, text>>>" + ")", graphName()));
    UserDefinedType udt1 = session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt1")).orElseThrow(IllegalStateException::new);
    UdtValue udtValue1 = udt1.newValue(1, "2");
    UserDefinedType udt2 = session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt2")).orElseThrow(IllegalStateException::new);
    TupleType secondNested = tupleOf(BIGINT, listOf(BIGINT));
    TupleType firstNested = tupleOf(TEXT, secondNested);
    UdtValue udtValue2 = udt2.newValue(1, "2", udt1.newValue(3, "4"), ImmutableList.of(5L), firstNested.newValue("6", secondNested.newValue(7L, ImmutableList.of(8L))));
    UserDefinedType udt3 = session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt3")).orElseThrow(IllegalStateException::new);
    UdtValue udtValue3 = udt3.newValue(ImmutableList.of(1), ImmutableSet.of(2.1f), ImmutableMap.of("3", 4L), ImmutableList.of(ImmutableList.of(5.1d, 6.1d), ImmutableList.of(7.1d)), ImmutableSet.of(ImmutableSet.of(8.1f), ImmutableSet.of(9.1f)), ImmutableList.of(tupleOf(INT, TEXT).newValue(10, "11")));
    Map<String, Object> properties = ImmutableMap.<String, Object>builder().put("frozen(typeOf('udt1'))", udtValue1).put("frozen(typeOf('udt2'))", udtValue2).put("frozen(typeOf('udt3'))", udtValue3).build();
    int vertexID = 1;
    String vertexLabel = "graphBinaryNestedTypes";
    runTest(properties, vertexLabel, vertexID);
}
Also used : Arrays(java.util.Arrays) INT(com.datastax.oss.driver.api.core.type.DataTypes.INT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LineString(com.datastax.dse.driver.api.core.data.geometry.LineString) ByteBuffer(java.nio.ByteBuffer) BIGINT(com.datastax.oss.driver.api.core.type.DataTypes.BIGINT) Point(com.datastax.dse.driver.api.core.data.geometry.Point) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Map(java.util.Map) LocalTime(java.time.LocalTime) BigInteger(java.math.BigInteger) DseDataTypes(com.datastax.dse.driver.api.core.type.DseDataTypes) Polygon(com.datastax.dse.driver.api.core.data.geometry.Polygon) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Geo(com.datastax.dse.driver.api.core.graph.predicates.Geo) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test) Instant(java.time.Instant) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) Sets(com.google.common.collect.Sets) ZoneId(java.time.ZoneId) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ChronoUnit(java.time.temporal.ChronoUnit) DataTypes.listOf(com.datastax.oss.driver.api.core.type.DataTypes.listOf) LocalDate(java.time.LocalDate) CqlDuration(com.datastax.oss.driver.api.core.data.CqlDuration) DataTypes.tupleOf(com.datastax.oss.driver.api.core.type.DataTypes.tupleOf) TEXT(com.datastax.oss.driver.api.core.type.DataTypes.TEXT) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) LineString(com.datastax.dse.driver.api.core.data.geometry.LineString) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Point(com.datastax.dse.driver.api.core.data.geometry.Point) Test(org.junit.Test)

Example 17 with TupleType

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

the class GeometryIT method should_insert_as_tuple.

/**
 * Validates that geometry values can be inserted as a tuple and verifies that the tuple is stored
 * correctly by retrieving it and ensuring it matches.
 */
@Test
@Ignore
public void should_insert_as_tuple() {
    UUID key = Uuids.random();
    PreparedStatement prepared = sessionRule.session().prepare("INSERT INTO tbl (k, t) values (?, ?)");
    TupleType tupleType = (TupleType) prepared.getVariableDefinitions().get(1).getType();
    TupleValue tuple = tupleType.newValue();
    tuple = tuple.set(0, sampleData.get(0), genericType);
    tuple = tuple.set(1, sampleData.get(1), genericType);
    tuple = tuple.set(2, sampleData.get(2), genericType);
    BoundStatement bs = prepared.boundStatementBuilder().setUuid(0, key).setTupleValue(1, tuple).build();
    sessionRule.session().execute(bs);
    ResultSet rs = sessionRule.session().execute(SimpleStatement.builder("SELECT k,t FROM tbl where k=?").addPositionalValues(key).build());
    Row row = rs.iterator().next();
    assertThat(row.getUuid("k")).isEqualTo(key);
    assertThat(row.getTupleValue("t")).isEqualTo(tuple);
    assertThat(row.getTupleValue(1)).isEqualTo(tuple);
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Row(com.datastax.oss.driver.api.core.cql.Row) UUID(java.util.UUID) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with TupleType

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

the class DefaultTupleValueTest method should_equate_instances_with_same_values_but_different_binary_representations.

@Test
public void should_equate_instances_with_same_values_but_different_binary_representations() {
    TupleType tupleType = DataTypes.tupleOf(DataTypes.VARINT);
    TupleValue tuple1 = tupleType.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    TupleValue tuple2 = tupleType.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x0001"));
    assertThat(tuple1).isEqualTo(tuple2);
    assertThat(tuple1.hashCode()).isEqualTo(tuple2.hashCode());
}
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 19 with TupleType

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

the class DefaultTupleValueTest method should_equate_instances_with_different_protocol_versions.

@Test
public void should_equate_instances_with_different_protocol_versions() {
    TupleType tupleType1 = DataTypes.tupleOf(DataTypes.TEXT);
    tupleType1.attach(attachmentPoint);
    // use the V3 attachmentPoint for type2
    TupleType tupleType2 = DataTypes.tupleOf(DataTypes.TEXT);
    tupleType2.attach(v3AttachmentPoint);
    TupleValue tuple1 = tupleType1.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    TupleValue tuple2 = tupleType2.newValue().setBytesUnsafe(0, Bytes.fromHexString("0x01"));
    assertThat(tuple1).isEqualTo(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 20 with TupleType

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

the class CoreGraphDataTypeITBase method should_create_and_retrieve_correct_data_with_types.

@Test
public void should_create_and_retrieve_correct_data_with_types() {
    CqlSession session = session();
    // use CQL to create type for now because DSP-17567 is not in yet, so this is more stable
    session.execute(String.format("CREATE TYPE %s.udt_graphbinary(simple text, complex tuple<int, text>, missing text)", graphName()));
    session.execute(String.format("CREATE TYPE %s.udt_graphbinarygeo(point 'PointType', line 'LineStringType', poly 'PolygonType')", graphName()));
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.<String, Object>builder().put("Ascii", "test").put("Bigint", 5L).put("Boolean", true).put("Date", LocalDate.of(2007, 7, 7)).put("Decimal", BigDecimal.valueOf(2.3)).put("Double", 4.5d).put("Float", 4.8f).put("Int", 45).put("Smallint", (short) 1).put("Text", "test").put("Time", LocalTime.now(ZoneId.systemDefault())).put("Timeuuid", Uuids.timeBased()).put("Timestamp", Instant.now().truncatedTo(ChronoUnit.MILLIS)).put("Uuid", java.util.UUID.randomUUID()).put("Varint", BigInteger.valueOf(3234)).put("Blob", ByteBuffer.wrap(new byte[] { 1, 2, 3 })).put("Tinyint", (byte) 38).put("listOf(Int)", Arrays.asList(2, 3, 4)).put("setOf(Int)", Sets.newHashSet(2, 3, 4)).put("mapOf(Int, Text)", ImmutableMap.of(2, "two", 4, "four")).put("Duration", CqlDuration.newInstance(1, 2, 3)).put("LineString", Geo.lineString(1, 2, 3, 4, 5, 6)).put("Point", Geo.point(3, 4)).put("Polygon", Geo.polygon(Geo.point(3, 4), Geo.point(5, 4), Geo.point(6, 6))).put("tupleOf(Int, Text)", tupleOf(INT, TEXT).newValue(5, "Bar")).put("typeOf('udt_graphbinary')", session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt_graphbinary")).orElseThrow(IllegalStateException::new).newValue("some text", tupleOf(INT, TEXT).newValue(5, "Bar"), "some missing text")).put("typeOf('udt_graphbinarygeo')", session.getMetadata().getKeyspace(graphName()).flatMap(keyspaceMetadata -> keyspaceMetadata.getUserDefinedType("udt_graphbinarygeo")).orElseThrow(IllegalStateException::new).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))));
    TupleType tuple = tupleOf(DseDataTypes.POINT, DseDataTypes.LINE_STRING, DseDataTypes.POLYGON);
    tuple.attach(session.getContext());
    properties.put("tupleOf(Point, LineString, Polygon)", 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))));
    int vertexID = 1;
    String vertexLabel = "graphBinaryAllTypes";
    runTest(properties.build(), vertexLabel, vertexID);
}
Also used : Arrays(java.util.Arrays) INT(com.datastax.oss.driver.api.core.type.DataTypes.INT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LineString(com.datastax.dse.driver.api.core.data.geometry.LineString) ByteBuffer(java.nio.ByteBuffer) BIGINT(com.datastax.oss.driver.api.core.type.DataTypes.BIGINT) Point(com.datastax.dse.driver.api.core.data.geometry.Point) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Map(java.util.Map) LocalTime(java.time.LocalTime) BigInteger(java.math.BigInteger) DseDataTypes(com.datastax.dse.driver.api.core.type.DseDataTypes) Polygon(com.datastax.dse.driver.api.core.data.geometry.Polygon) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Geo(com.datastax.dse.driver.api.core.graph.predicates.Geo) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test) Instant(java.time.Instant) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) Sets(com.google.common.collect.Sets) ZoneId(java.time.ZoneId) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ChronoUnit(java.time.temporal.ChronoUnit) DataTypes.listOf(com.datastax.oss.driver.api.core.type.DataTypes.listOf) LocalDate(java.time.LocalDate) CqlDuration(com.datastax.oss.driver.api.core.data.CqlDuration) DataTypes.tupleOf(com.datastax.oss.driver.api.core.type.DataTypes.tupleOf) TEXT(com.datastax.oss.driver.api.core.type.DataTypes.TEXT) TupleType(com.datastax.oss.driver.api.core.type.TupleType) LineString(com.datastax.dse.driver.api.core.data.geometry.LineString) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ImmutableMap(com.google.common.collect.ImmutableMap) Point(com.datastax.dse.driver.api.core.data.geometry.Point) 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