Search in sources :

Example 86 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class PrometheusRecordCursor method writeObject.

private static void writeObject(BlockBuilder builder, Type type, Object obj) {
    if (type instanceof ArrayType) {
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBuilder = builder.beginBlockEntry();
        for (Object item : (List<?>) obj) {
            writeObject(arrayBuilder, elementType, item);
        }
        builder.closeEntry();
    } else if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        BlockBuilder mapBlockBuilder = builder.beginBlockEntry();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
            writeObject(mapBlockBuilder, mapType.getKeyType(), entry.getKey());
            writeObject(mapBlockBuilder, mapType.getValueType(), entry.getValue());
        }
        builder.closeEntry();
    } else {
        if (BOOLEAN.equals(type) || TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type) || type instanceof VarcharType) {
            TypeUtils.writeNativeValue(type, builder, obj);
        }
    }
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 87 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class PrometheusRecordCursor method getBlockFromMap.

static Block getBlockFromMap(Type mapType, Map<?, ?> map) {
    // on functions like COUNT() the Type won't be a MapType
    if (!(mapType instanceof MapType)) {
        return null;
    }
    Type keyType = mapType.getTypeParameters().get(0);
    Type valueType = mapType.getTypeParameters().get(1);
    BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder builder = mapBlockBuilder.beginBlockEntry();
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        writeObject(builder, keyType, entry.getKey());
        writeObject(builder, valueType, entry.getValue());
    }
    mapBlockBuilder.closeEntry();
    return (Block) mapType.getObject(mapBlockBuilder, 0);
}
Also used : MapType(com.facebook.presto.common.type.MapType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) Block(com.facebook.presto.common.block.Block) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 88 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class PrometheusRecordCursor method getMapFromBlock.

static Map<Object, Object> getMapFromBlock(Type type, Block block) {
    MapType mapType = (MapType) type;
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    Map<Object, Object> map = new HashMap<>(block.getPositionCount() / 2);
    for (int i = 0; i < block.getPositionCount(); i += 2) {
        map.put(readObject(keyType, block, i), readObject(valueType, block, i + 1));
    }
    return map;
}
Also used : MapType(com.facebook.presto.common.type.MapType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) HashMap(java.util.HashMap) MapType(com.facebook.presto.common.type.MapType)

Example 89 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class QueryRewriter method getColumnTypeRewrite.

private Optional<Type> getColumnTypeRewrite(Type type) {
    if (type.equals(DATE) || type.equals(TIME)) {
        return Optional.of(TIMESTAMP);
    }
    if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
        return Optional.of(VARCHAR);
    }
    if (type.equals(UNKNOWN)) {
        return Optional.of(BIGINT);
    }
    if (type instanceof DecimalType) {
        return Optional.of(DOUBLE);
    }
    if (type instanceof ArrayType) {
        return getColumnTypeRewrite(((ArrayType) type).getElementType()).map(ArrayType::new);
    }
    if (type instanceof MapType) {
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        Optional<Type> keyTypeRewrite = getColumnTypeRewrite(keyType);
        Optional<Type> valueTypeRewrite = getColumnTypeRewrite(valueType);
        if (keyTypeRewrite.isPresent() || valueTypeRewrite.isPresent()) {
            return Optional.of(typeManager.getType(new TypeSignature(MAP, TypeSignatureParameter.of(keyTypeRewrite.orElse(keyType).getTypeSignature()), TypeSignatureParameter.of(valueTypeRewrite.orElse(valueType).getTypeSignature()))));
        }
        return Optional.empty();
    }
    if (type instanceof RowType) {
        List<Field> fields = ((RowType) type).getFields();
        List<Field> fieldsRewrite = new ArrayList<>();
        boolean rewrite = false;
        for (Field field : fields) {
            Optional<Type> fieldTypeRewrite = getColumnTypeRewrite(field.getType());
            rewrite = rewrite || fieldTypeRewrite.isPresent();
            fieldsRewrite.add(new Field(field.getName(), fieldTypeRewrite.orElse(field.getType())));
        }
        return rewrite ? Optional.of(RowType.from(fieldsRewrite)) : Optional.empty();
    }
    return Optional.empty();
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Field(com.facebook.presto.common.type.RowType.Field) ClusterType(com.facebook.presto.verifier.framework.ClusterType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) TypeSignature(com.facebook.presto.common.type.TypeSignature) ArrayList(java.util.ArrayList) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType)

Example 90 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class StructuredColumnMismatchResolver method resolveResultMismatch.

@Override
public Optional<String> resolveResultMismatch(DataMatchResult matchResult, QueryBundle control) {
    checkArgument(!matchResult.isMatched(), "Expect not matched");
    if (matchResult.getMatchType() != COLUMN_MISMATCH) {
        return Optional.empty();
    }
    for (ColumnMatchResult<?> mismatchedColumn : matchResult.getMismatchedColumns()) {
        Type columnType = mismatchedColumn.getColumn().getType();
        if (columnType instanceof ArrayType) {
            ArrayColumnChecksum controlChecksum = (ArrayColumnChecksum) mismatchedColumn.getControlChecksum();
            ArrayColumnChecksum testChecksum = (ArrayColumnChecksum) mismatchedColumn.getTestChecksum();
            if (!containsFloatingPointType(((ArrayType) columnType).getElementType()) || !isCardinalityMatched(controlChecksum, testChecksum)) {
                return Optional.empty();
            }
        } else if (columnType instanceof MapType) {
            MapColumnChecksum controlChecksum = (MapColumnChecksum) mismatchedColumn.getControlChecksum();
            MapColumnChecksum testChecksum = (MapColumnChecksum) mismatchedColumn.getTestChecksum();
            if (!isCardinalityMatched(controlChecksum, testChecksum)) {
                return Optional.empty();
            }
            boolean keyContainsFloatingPoint = containsFloatingPointType(((MapType) columnType).getKeyType());
            boolean valueContainsFloatingPoint = containsFloatingPointType(((MapType) columnType).getValueType());
            if (!keyContainsFloatingPoint && !valueContainsFloatingPoint) {
                return Optional.empty();
            }
            if (!keyContainsFloatingPoint && !Objects.equals(controlChecksum.getKeysChecksum(), testChecksum.getKeysChecksum())) {
                return Optional.empty();
            }
            if (!valueContainsFloatingPoint && !Objects.equals(controlChecksum.getValuesChecksum(), testChecksum.getValuesChecksum())) {
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    }
    return Optional.of("Structured columns auto-resolved");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) RealType(com.facebook.presto.common.type.RealType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) DoubleType(com.facebook.presto.common.type.DoubleType) RowType(com.facebook.presto.common.type.RowType) MapColumnChecksum(com.facebook.presto.verifier.checksum.MapColumnChecksum) ArrayColumnChecksum(com.facebook.presto.verifier.checksum.ArrayColumnChecksum) MapType(com.facebook.presto.common.type.MapType)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5