Search in sources :

Example 36 with DecimalType

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

the class OrcTester method testValue.

private static boolean testValue(Type type, Object value, TupleDomainFilter filter) {
    if (value == null) {
        return filter.testNull();
    }
    if (filter == IS_NULL) {
        return false;
    }
    if (filter == IS_NOT_NULL) {
        return true;
    }
    if (type == BOOLEAN) {
        return filter.testBoolean((Boolean) value);
    }
    if (type == TINYINT || type == BIGINT || type == INTEGER || type == SMALLINT) {
        return filter.testLong(((Number) value).longValue());
    }
    if (type == REAL) {
        return filter.testFloat(((Number) value).floatValue());
    }
    if (type == DOUBLE) {
        return filter.testDouble((double) value);
    }
    if (type == DATE) {
        return filter.testLong(((SqlDate) value).getDays());
    }
    if (type == TIMESTAMP) {
        return filter.testLong(((SqlTimestamp) value).getMillisUtc());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
        if (decimalType.isShort()) {
            return filter.testLong(bigDecimal.unscaledValue().longValue());
        } else {
            Slice encodedDecimal = Decimals.encodeScaledValue(bigDecimal);
            return filter.testDecimal(encodedDecimal.getLong(0), encodedDecimal.getLong(Long.BYTES));
        }
    }
    if (type == VARCHAR) {
        return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
    }
    if (type instanceof CharType) {
        String charString = String.valueOf(value);
        return filter.testBytes(charString.getBytes(), 0, charString.length());
    }
    if (type == VARBINARY) {
        byte[] binary = ((SqlVarbinary) value).getBytes();
        return filter.testBytes(binary, 0, binary.length);
    }
    fail("Unsupported type: " + type);
    return false;
}
Also used : Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) DecimalType(com.facebook.presto.common.type.DecimalType) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) CharType(com.facebook.presto.common.type.CharType) BigDecimal(java.math.BigDecimal)

Example 37 with DecimalType

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

the class TypeConverter method toPrestoType.

public static Type toPrestoType(org.apache.iceberg.types.Type type, TypeManager typeManager) {
    switch(type.typeId()) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case BINARY:
        case FIXED:
            return VarbinaryType.VARBINARY;
        case DATE:
            return DateType.DATE;
        case DECIMAL:
            Types.DecimalType decimalType = (Types.DecimalType) type;
            return DecimalType.createDecimalType(decimalType.precision(), decimalType.scale());
        case DOUBLE:
            return DoubleType.DOUBLE;
        case LONG:
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case INTEGER:
            return IntegerType.INTEGER;
        case TIME:
            return TimeType.TIME;
        case TIMESTAMP:
            return TimestampType.TIMESTAMP;
        case STRING:
            return VarcharType.createUnboundedVarcharType();
        case LIST:
            Types.ListType listType = (Types.ListType) type;
            return new ArrayType(toPrestoType(listType.elementType(), typeManager));
        case MAP:
            Types.MapType mapType = (Types.MapType) type;
            TypeSignature keyType = toPrestoType(mapType.keyType(), typeManager).getTypeSignature();
            TypeSignature valueType = toPrestoType(mapType.valueType(), typeManager).getTypeSignature();
            return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
        case STRUCT:
            List<Types.NestedField> fields = ((Types.StructType) type).fields();
            return RowType.from(fields.stream().map(field -> new RowType.Field(Optional.of(field.name()), toPrestoType(field.type(), typeManager))).collect(toImmutableList()));
        default:
            throw new UnsupportedOperationException(format("Cannot convert from Iceberg type '%s' (%s) to Presto type", type, type.typeId()));
    }
}
Also used : Types(org.apache.iceberg.types.Types) StandardTypes(com.facebook.presto.common.type.StandardTypes) MetastoreUtil.isRowType(com.facebook.presto.hive.metastore.MetastoreUtil.isRowType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) MetastoreUtil.isMapType(com.facebook.presto.hive.metastore.MetastoreUtil.isMapType) ArrayType(com.facebook.presto.common.type.ArrayType) MetastoreUtil.isArrayType(com.facebook.presto.hive.metastore.MetastoreUtil.isArrayType) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) DecimalType(com.facebook.presto.common.type.DecimalType)

Example 38 with DecimalType

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

the class TypeConverter method toHiveTypeInfo.

private static TypeInfo toHiveTypeInfo(Type type) {
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return HIVE_STRING.getTypeInfo();
        }
        if (varcharType.getLengthSafe() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharType.getLengthSafe());
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = toHiveTypeInfo(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = toHiveTypeInfo(type.getTypeParameters().get(0));
        TypeInfo valueType = toHiveTypeInfo(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            if (!namedTypeSignature.getName().isPresent()) {
                throw new PrestoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
            }
            fieldNames.add(namedTypeSignature.getName().get());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(TypeConverter::toHiveTypeInfo).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 39 with DecimalType

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

the class ExpressionConverter method getIcebergLiteralValue.

private static Object getIcebergLiteralValue(Type type, Marker marker) {
    if (type instanceof IntegerType) {
        return toIntExact((long) marker.getValue());
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact((long) marker.getValue()));
    }
    // TODO: Remove this conversion once we move to next iceberg version
    if (type instanceof DateType) {
        return toIntExact(((Long) marker.getValue()));
    }
    if (type instanceof TimestampType || type instanceof TimeType) {
        return MILLISECONDS.toMicros((Long) marker.getValue());
    }
    if (type instanceof VarcharType) {
        return ((Slice) marker.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return ByteBuffer.wrap(((Slice) marker.getValue()).getBytes());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        Object value = requireNonNull(marker.getValue(), "The value of the marker must be non-null");
        if (Decimals.isShortDecimal(decimalType)) {
            checkArgument(value instanceof Long, "A short decimal should be represented by a Long value but was %s", value.getClass().getName());
            return BigDecimal.valueOf((long) value).movePointLeft(decimalType.getScale());
        }
        checkArgument(value instanceof Slice, "A long decimal should be represented by a Slice value but was %s", value.getClass().getName());
        return new BigDecimal(Decimals.decodeUnscaledValue((Slice) value), decimalType.getScale());
    }
    return marker.getValue();
}
Also used : IntegerType(com.facebook.presto.common.type.IntegerType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) Slice(io.airlift.slice.Slice) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) RealType(com.facebook.presto.common.type.RealType) DateType(com.facebook.presto.common.type.DateType) BigDecimal(java.math.BigDecimal) TimeType(com.facebook.presto.common.type.TimeType)

Example 40 with DecimalType

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

the class MetastoreUtil method getField.

public static Object getField(Type type, Block block, int position) {
    if (block.isNull(position)) {
        return null;
    }
    if (BooleanType.BOOLEAN.equals(type)) {
        return type.getBoolean(block, position);
    }
    if (BigintType.BIGINT.equals(type)) {
        return type.getLong(block, position);
    }
    if (IntegerType.INTEGER.equals(type)) {
        return (int) type.getLong(block, position);
    }
    if (SmallintType.SMALLINT.equals(type)) {
        return (short) type.getLong(block, position);
    }
    if (TinyintType.TINYINT.equals(type)) {
        return (byte) type.getLong(block, position);
    }
    if (RealType.REAL.equals(type)) {
        return intBitsToFloat((int) type.getLong(block, position));
    }
    if (DoubleType.DOUBLE.equals(type)) {
        return type.getDouble(block, position);
    }
    if (type instanceof VarcharType) {
        return new Text(type.getSlice(block, position).getBytes());
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        return new Text(padEnd(type.getSlice(block, position).toStringUtf8(), charType.getLength(), ' '));
    }
    if (VarbinaryType.VARBINARY.equals(type)) {
        return type.getSlice(block, position).getBytes();
    }
    if (DateType.DATE.equals(type)) {
        long days = type.getLong(block, position);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), TimeUnit.DAYS.toMillis(days)));
    }
    if (TimestampType.TIMESTAMP.equals(type)) {
        long millisUtc = type.getLong(block, position);
        return new Timestamp(millisUtc);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getHiveDecimal(decimalType, block, position);
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        Block arrayBlock = block.getBlock(position);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            Object element = getField(elementType, arrayBlock, i);
            list.add(element);
        }
        return Collections.unmodifiableList(list);
    }
    if (isMapType(type)) {
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Block mapBlock = block.getBlock(position);
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Object key = getField(keyType, mapBlock, i);
            Object value = getField(valueType, mapBlock, i + 1);
            map.put(key, value);
        }
        return Collections.unmodifiableMap(map);
    }
    if (isRowType(type)) {
        Block rowBlock = block.getBlock(position);
        List<Type> fieldTypes = type.getTypeParameters();
        checkCondition(fieldTypes.size() == rowBlock.getPositionCount(), StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
        List<Object> row = new ArrayList<>(rowBlock.getPositionCount());
        for (int i = 0; i < rowBlock.getPositionCount(); i++) {
            Object element = getField(fieldTypes.get(i), rowBlock, i);
            row.add(element);
        }
        return Collections.unmodifiableList(row);
    }
    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) PrestoException(com.facebook.presto.spi.PrestoException) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) TypeUtils.isNumericType(com.facebook.presto.common.type.TypeUtils.isNumericType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) EnumType(com.facebook.presto.common.type.EnumType) ColumnStatisticType(com.facebook.presto.spi.statistics.ColumnStatisticType) DoubleType(com.facebook.presto.common.type.DoubleType) RowType(com.facebook.presto.common.type.RowType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) ColumnType.typeToThriftType(org.apache.hadoop.hive.metastore.ColumnType.typeToThriftType) TimestampType(com.facebook.presto.common.type.TimestampType) MapType(com.facebook.presto.common.type.MapType) BooleanType(com.facebook.presto.common.type.BooleanType) HiveType(com.facebook.presto.hive.HiveType) IntegerType(com.facebook.presto.common.type.IntegerType) Type(com.facebook.presto.common.type.Type) DateType(com.facebook.presto.common.type.DateType) DecimalType(com.facebook.presto.common.type.DecimalType) Block(com.facebook.presto.common.block.Block) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType)

Aggregations

DecimalType (com.facebook.presto.common.type.DecimalType)55 VarcharType (com.facebook.presto.common.type.VarcharType)28 Type (com.facebook.presto.common.type.Type)23 CharType (com.facebook.presto.common.type.CharType)20 Block (com.facebook.presto.common.block.Block)15 ArrayType (com.facebook.presto.common.type.ArrayType)15 RowType (com.facebook.presto.common.type.RowType)14 Slice (io.airlift.slice.Slice)14 ArrayList (java.util.ArrayList)13 PrestoException (com.facebook.presto.spi.PrestoException)12 TimestampType (com.facebook.presto.common.type.TimestampType)11 BigDecimal (java.math.BigDecimal)11 MapType (com.facebook.presto.common.type.MapType)10 Chars.isCharType (com.facebook.presto.common.type.Chars.isCharType)9 DateType (com.facebook.presto.common.type.DateType)9 IntegerType (com.facebook.presto.common.type.IntegerType)9 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)9 ImmutableList (com.google.common.collect.ImmutableList)9 BigintType (com.facebook.presto.common.type.BigintType)8 BooleanType (com.facebook.presto.common.type.BooleanType)8