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