Search in sources :

Example 76 with FieldValueProvider

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);
}
Also used : HashMap(java.util.HashMap) FieldValueProvider(io.trino.decoder.FieldValueProvider) JsonNode(com.fasterxml.jackson.databind.JsonNode) DecoderColumnHandle(io.trino.decoder.DecoderColumnHandle) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with FieldValueProvider

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;
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) DecoderColumnHandle(io.trino.decoder.DecoderColumnHandle) HashMap(java.util.HashMap) FieldValueProvider(io.trino.decoder.FieldValueProvider) DecoderColumnHandle(io.trino.decoder.DecoderColumnHandle) HashMap(java.util.HashMap) Map(java.util.Map)

Example 78 with FieldValueProvider

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);
        }
    };
}
Also used : Type(io.trino.spi.type.Type) MapType(io.trino.spi.type.MapType) Header(org.apache.kafka.common.header.Header) FieldValueProvider(io.trino.decoder.FieldValueProvider) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

FieldValueProvider (io.trino.decoder.FieldValueProvider)78 DecoderColumnHandle (io.trino.decoder.DecoderColumnHandle)63 DecoderTestColumnHandle (io.trino.decoder.DecoderTestColumnHandle)60 Test (org.testng.annotations.Test)58 Schema (org.apache.avro.Schema)22 ArrayType (io.trino.spi.type.ArrayType)15 RowDecoder (io.trino.decoder.RowDecoder)14 ImmutableList (com.google.common.collect.ImmutableList)13 ArrayList (java.util.ArrayList)13 List (java.util.List)12 GenericArray (org.apache.avro.generic.GenericArray)11 HashMap (java.util.HashMap)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 ByteBuffer (java.nio.ByteBuffer)4 GenericData (org.apache.avro.generic.GenericData)4 Type (io.trino.spi.type.Type)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)3 Slice (io.airlift.slice.Slice)2