Search in sources :

Example 1 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project calcite by apache.

the class CassandraEnumerator method convertToEnumeratorObject.

/**
 * Convert an object into the expected internal representation.
 *
 * @param obj Object to convert, if needed
 */
@Nullable
private Object convertToEnumeratorObject(@Nullable Object obj) {
    if (obj instanceof ByteBuffer) {
        ByteBuffer buf = (ByteBuffer) obj;
        byte[] bytes = new byte[buf.remaining()];
        buf.get(bytes, 0, bytes.length);
        return new ByteString(bytes);
    } else if (obj instanceof LocalDate) {
        // converts dates to the expected numeric format
        return ((LocalDate) obj).toEpochDay();
    } else if (obj instanceof Date) {
        @SuppressWarnings("JdkObsolete") long milli = ((Date) obj).toInstant().toEpochMilli();
        return milli;
    } else if (obj instanceof Instant) {
        return ((Instant) obj).toEpochMilli();
    } else if (obj instanceof LocalTime) {
        return ((LocalTime) obj).toNanoOfDay();
    } else if (obj instanceof LinkedHashSet) {
        // MULTISET is handled as an array
        return ((LinkedHashSet<?>) obj).toArray();
    } else if (obj instanceof TupleValue) {
        // STRUCT can be handled as an array
        final TupleValue tupleValue = (TupleValue) obj;
        int numComponents = tupleValue.getType().getComponentTypes().size();
        return IntStream.range(0, numComponents).mapToObj(i -> tupleValue.get(i, CodecRegistry.DEFAULT.codecFor(tupleValue.getType().getComponentTypes().get(i)))).map(this::convertToEnumeratorObject).map(// "null" cannot appear inside collections
        Objects::requireNonNull).toArray();
    }
    return obj;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IntStream(java.util.stream.IntStream) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Iterator(java.util.Iterator) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ByteString(org.apache.calcite.avatica.util.ByteString) Date(java.util.Date) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Instant(java.time.Instant) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) ByteBuffer(java.nio.ByteBuffer) Objects(java.util.Objects) List(java.util.List) Enumerator(org.apache.calcite.linq4j.Enumerator) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) LocalDate(java.time.LocalDate) LocalTime(java.time.LocalTime) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Row(com.datastax.oss.driver.api.core.cql.Row) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LinkedHashSet(java.util.LinkedHashSet) LocalTime(java.time.LocalTime) ByteString(org.apache.calcite.avatica.util.ByteString) Instant(java.time.Instant) ByteBuffer(java.nio.ByteBuffer) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class TupleCodec method decode.

@Nullable
@Override
public TupleValue decode(@Nullable ByteBuffer bytes, @NonNull ProtocolVersion protocolVersion) {
    if (bytes == null) {
        return null;
    }
    // empty byte buffers will result in empty values
    try {
        ByteBuffer input = bytes.duplicate();
        TupleValue value = cqlType.newValue();
        int i = 0;
        while (input.hasRemaining()) {
            if (i > cqlType.getComponentTypes().size()) {
                throw new IllegalArgumentException(String.format("Too many fields in encoded tuple, expected %d", cqlType.getComponentTypes().size()));
            }
            int elementSize = input.getInt();
            ByteBuffer element;
            if (elementSize < 0) {
                element = null;
            } else {
                element = input.slice();
                element.limit(elementSize);
                input.position(input.position() + elementSize);
            }
            value = value.setBytesUnsafe(i, element);
            i += 1;
        }
        return value;
    } catch (BufferUnderflowException e) {
        throw new IllegalArgumentException("Not enough bytes to deserialize a tuple", e);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) BufferUnderflowException(java.nio.BufferUnderflowException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 3 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class CachingCodecRegistryTest method should_create_tuple_codec_for_cql_and_java_types.

@Test
@UseDataProvider(value = "tuplesWithCqlTypes", location = CachingCodecRegistryTestDataProviders.class)
public void should_create_tuple_codec_for_cql_and_java_types(DataType cqlType, Object value) {
    TestCachingCodecRegistry registry = new TestCachingCodecRegistry(mockCache);
    InOrder inOrder = inOrder(mockCache);
    TypeCodec<TupleValue> codec = registry.codecFor(cqlType, GenericType.TUPLE_VALUE);
    assertThat(codec).isNotNull();
    assertThat(codec.accepts(cqlType)).isTrue();
    assertThat(codec.accepts(GenericType.TUPLE_VALUE)).isTrue();
    assertThat(codec.accepts(TupleValue.class)).isTrue();
    assertThat(codec.accepts(value)).isTrue();
    inOrder.verify(mockCache).lookup(cqlType, GenericType.TUPLE_VALUE, false);
// field codecs are only looked up when fields are accessed, so no cache hit for list<int> now
}
Also used : InOrder(org.mockito.InOrder) DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 4 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class TupleCodecTest method should_parse_tuple_with_extra_whitespace.

@Test
public void should_parse_tuple_with_extra_whitespace() {
    TupleValue tuple = parse("  (  1  ,  NULL  ,  'a'  )  ");
    assertThat(tuple.getInt(0)).isEqualTo(1);
    assertThat(tuple.isNull(1)).isTrue();
    assertThat(tuple.getString(2)).isEqualTo("a");
    verify(intCodec).parse("1");
    verify(doubleCodec).parse("NULL");
    verify(textCodec).parse("'a'");
}
Also used : DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Example 5 with TupleValue

use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.

the class TupleCodecTest method should_decode_negative_element_length_as_null_field.

/**
 * Test for JAVA-2557. Ensures that the codec can decode null fields with any negative length.
 */
@Test
public void should_decode_negative_element_length_as_null_field() {
    TupleValue tuple = decode("0x" + // field1 has length -1
    "ffffffff" + // field2 has length -2
    "fffffffe" + // field3 has length Integer.MIN_VALUE (-2147483648)
    "80000000");
    assertThat(tuple.isNull(0)).isTrue();
    assertThat(tuple.isNull(1)).isTrue();
    assertThat(tuple.isNull(2)).isTrue();
    verifyZeroInteractions(intCodec);
    verifyZeroInteractions(doubleCodec);
    verifyZeroInteractions(textCodec);
}
Also used : DefaultTupleValue(com.datastax.oss.driver.internal.core.data.DefaultTupleValue) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test)

Aggregations

TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)31 Test (org.junit.Test)18 DefaultTupleValue (com.datastax.oss.driver.internal.core.data.DefaultTupleValue)12 TupleType (com.datastax.oss.driver.api.core.type.TupleType)10 Row (com.datastax.oss.driver.api.core.cql.Row)6 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)5 DefaultTupleType (com.datastax.oss.driver.internal.core.type.DefaultTupleType)5 DataType (com.datastax.oss.driver.api.core.type.DataType)4 ByteBuffer (java.nio.ByteBuffer)4 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)3 Nullable (edu.umd.cs.findbugs.annotations.Nullable)3 InetAddress (java.net.InetAddress)3 List (java.util.List)3 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)2 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)2 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2