use of io.trino.decoder.FieldValueProvider in project trino by trinodb.
the class JsonRowDecoder method decodeRow.
@Override
public Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodeRow(byte[] data, Map<String, String> dataMap) {
JsonNode tree;
try {
tree = objectMapper.readTree(data);
} catch (Exception e) {
return Optional.empty();
}
Map<DecoderColumnHandle, FieldValueProvider> decodedRow = new HashMap<>();
for (Map.Entry<DecoderColumnHandle, JsonFieldDecoder> entry : fieldDecoders.entrySet()) {
DecoderColumnHandle columnHandle = entry.getKey();
JsonFieldDecoder decoder = entry.getValue();
JsonNode node = locateNode(tree, columnHandle);
decodedRow.put(columnHandle, decoder.decode(node));
}
return Optional.of(decodedRow);
}
use of io.trino.decoder.FieldValueProvider in project trino by trinodb.
the class RedisRecordCursor method nextRow.
private boolean nextRow(String keyString) {
fetchData(keyString);
byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);
byte[] valueData = EMPTY_BYTE_ARRAY;
if (valueString != null) {
valueData = valueString.getBytes(StandardCharsets.UTF_8);
}
totalBytes += valueData.length;
totalValues++;
Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodedKey = keyDecoder.decodeRow(keyData);
Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodedValue = valueDecoder.decodeRow(valueData, valueMap);
Map<ColumnHandle, FieldValueProvider> currentRowValuesMap = new HashMap<>();
for (DecoderColumnHandle columnHandle : columnHandles) {
if (columnHandle.isInternal()) {
RedisInternalFieldDescription fieldDescription = RedisInternalFieldDescription.forColumnName(columnHandle.getName());
switch(fieldDescription) {
case KEY_FIELD:
currentRowValuesMap.put(columnHandle, bytesValueProvider(keyData));
break;
case VALUE_FIELD:
currentRowValuesMap.put(columnHandle, bytesValueProvider(valueData));
break;
case KEY_LENGTH_FIELD:
currentRowValuesMap.put(columnHandle, longValueProvider(keyData.length));
break;
case VALUE_LENGTH_FIELD:
currentRowValuesMap.put(columnHandle, longValueProvider(valueData.length));
break;
case KEY_CORRUPT_FIELD:
currentRowValuesMap.put(columnHandle, booleanValueProvider(decodedKey.isEmpty()));
break;
case VALUE_CORRUPT_FIELD:
currentRowValuesMap.put(columnHandle, booleanValueProvider(decodedValue.isEmpty()));
break;
default:
throw new IllegalArgumentException("unknown internal field " + fieldDescription);
}
}
}
decodedKey.ifPresent(currentRowValuesMap::putAll);
decodedValue.ifPresent(currentRowValuesMap::putAll);
for (int i = 0; i < columnHandles.size(); i++) {
ColumnHandle columnHandle = columnHandles.get(i);
currentRowValues[i] = currentRowValuesMap.get(columnHandle);
}
return true;
}
use of io.trino.decoder.FieldValueProvider in project trino by trinodb.
the class KafkaRecordSet method headerMapValueProvider.
public static FieldValueProvider headerMapValueProvider(MapType varcharMapType, Headers headers) {
Type keyType = varcharMapType.getTypeParameters().get(0);
Type valueArrayType = varcharMapType.getTypeParameters().get(1);
Type valueType = valueArrayType.getTypeParameters().get(0);
BlockBuilder mapBlockBuilder = varcharMapType.createBlockBuilder(null, 1);
BlockBuilder builder = mapBlockBuilder.beginBlockEntry();
// Group by keys and collect values as array.
Multimap<String, byte[]> headerMap = ArrayListMultimap.create();
for (Header header : headers) {
headerMap.put(header.key(), header.value());
}
for (String headerKey : headerMap.keySet()) {
writeNativeValue(keyType, builder, headerKey);
BlockBuilder arrayBuilder = builder.beginBlockEntry();
for (byte[] value : headerMap.get(headerKey)) {
writeNativeValue(valueType, arrayBuilder, value);
}
builder.closeEntry();
}
mapBlockBuilder.closeEntry();
return new FieldValueProvider() {
@Override
public boolean isNull() {
return false;
}
@Override
public Block getBlock() {
return varcharMapType.getObject(mapBlockBuilder, 0);
}
};
}
Aggregations