Search in sources :

Example 6 with DecimalType

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

the class ColumnarBinaryHiveRecordCursor method parseDecimalColumn.

private void parseDecimalColumn(int column, byte[] bytes, int start, int length) {
    if (length == 0) {
        nulls[column] = true;
    } else {
        nulls[column] = false;
        decimalWritable.setFromBytes(bytes, start, length);
        DecimalType columnType = (DecimalType) types[column];
        if (columnType.isShort()) {
            longs[column] = getShortDecimalValue(decimalWritable, columnType.getScale());
        } else {
            slices[column] = getLongDecimalValue(decimalWritable, columnType.getScale());
        }
    }
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType)

Example 7 with DecimalType

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

the class ColumnarTextHiveRecordCursor method parseDecimalColumn.

private void parseDecimalColumn(int column, byte[] bytes, int start, int length) {
    boolean wasNull;
    if (length == 0 || (length == "\\N".length() && bytes[start] == '\\' && bytes[start + 1] == 'N')) {
        wasNull = true;
    } else {
        DecimalType columnType = (DecimalType) types[column];
        BigDecimal decimal = parseHiveDecimal(bytes, start, length, columnType);
        if (columnType.isShort()) {
            longs[column] = decimal.unscaledValue().longValue();
        } else {
            slices[column] = Decimals.encodeUnscaledValue(decimal.unscaledValue());
        }
        wasNull = false;
    }
    nulls[column] = wasNull;
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) BigDecimal(java.math.BigDecimal)

Example 8 with DecimalType

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

the class TypeRegistry method isTypeOnlyCoercion.

@Override
public boolean isTypeOnlyCoercion(Type source, Type result) {
    if (source.equals(result)) {
        return true;
    }
    if (!canCoerce(source, result)) {
        return false;
    }
    if (source instanceof VarcharType && result instanceof VarcharType) {
        return true;
    }
    if (source instanceof DecimalType && result instanceof DecimalType) {
        DecimalType sourceDecimal = (DecimalType) source;
        DecimalType resultDecimal = (DecimalType) result;
        boolean sameDecimalSubtype = (sourceDecimal.isShort() && resultDecimal.isShort()) || (!sourceDecimal.isShort() && !resultDecimal.isShort());
        boolean sameScale = sourceDecimal.getScale() == resultDecimal.getScale();
        boolean sourcePrecisionIsLessOrEqualToResultPrecision = sourceDecimal.getPrecision() <= resultDecimal.getPrecision();
        return sameDecimalSubtype && sameScale && sourcePrecisionIsLessOrEqualToResultPrecision;
    }
    String sourceTypeBase = source.getTypeSignature().getBase();
    String resultTypeBase = result.getTypeSignature().getBase();
    if (sourceTypeBase.equals(resultTypeBase) && isCovariantParametrizedType(source)) {
        List<Type> sourceTypeParameters = source.getTypeParameters();
        List<Type> resultTypeParameters = result.getTypeParameters();
        checkState(sourceTypeParameters.size() == resultTypeParameters.size());
        for (int i = 0; i < sourceTypeParameters.size(); i++) {
            if (!isTypeOnlyCoercion(sourceTypeParameters.get(i), resultTypeParameters.get(i))) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : CharType.createCharType(com.facebook.presto.spi.type.CharType.createCharType) DecimalType(com.facebook.presto.spi.type.DecimalType) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) DecimalType.createDecimalType(com.facebook.presto.spi.type.DecimalType.createDecimalType) Type(com.facebook.presto.spi.type.Type) ParametricType(com.facebook.presto.spi.type.ParametricType) CharType(com.facebook.presto.spi.type.CharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) DecimalType(com.facebook.presto.spi.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.spi.type.DecimalType.createDecimalType)

Example 9 with DecimalType

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

the class DecimalOperators method divideRescaleFactor.

private static List<Object> divideRescaleFactor(SqlScalarFunctionBuilder.SpecializeContext context) {
    DecimalType returnType = (DecimalType) context.getReturnType();
    int dividendScale = toIntExact(requireNonNull(context.getLiteral("a_scale"), "a_scale is null"));
    int divisorScale = toIntExact(requireNonNull(context.getLiteral("b_scale"), "b_scale is null"));
    int resultScale = returnType.getScale();
    int rescaleFactor = resultScale - dividendScale + divisorScale;
    return ImmutableList.of(rescaleFactor);
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType)

Example 10 with DecimalType

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

the class HiveWriteUtils 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.getObject(position, Block.class);
        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.getObject(position, Block.class);
        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.getObject(position, Block.class);
        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 : VarcharType(com.facebook.presto.spi.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) IntegerType(com.facebook.presto.spi.type.IntegerType) DoubleType(com.facebook.presto.spi.type.DoubleType) VarbinaryType(com.facebook.presto.spi.type.VarbinaryType) HiveUtil.isMapType(com.facebook.presto.hive.HiveUtil.isMapType) HiveUtil.isRowType(com.facebook.presto.hive.HiveUtil.isRowType) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) BooleanType(com.facebook.presto.spi.type.BooleanType) RealType(com.facebook.presto.spi.type.RealType) VarcharType(com.facebook.presto.spi.type.VarcharType) DateType(com.facebook.presto.spi.type.DateType) TinyintType(com.facebook.presto.spi.type.TinyintType) DecimalType(com.facebook.presto.spi.type.DecimalType) TimestampType(com.facebook.presto.spi.type.TimestampType) SmallintType(com.facebook.presto.spi.type.SmallintType) HiveUtil.isArrayType(com.facebook.presto.hive.HiveUtil.isArrayType) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) DecimalType(com.facebook.presto.spi.type.DecimalType) Block(com.facebook.presto.spi.block.Block) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType)

Aggregations

DecimalType (com.facebook.presto.spi.type.DecimalType)17 Slice (io.airlift.slice.Slice)7 BigDecimal (java.math.BigDecimal)7 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)5 Type (com.facebook.presto.spi.type.Type)4 BigInteger (java.math.BigInteger)4 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 CharType (com.facebook.presto.spi.type.CharType)3 TimestampType (com.facebook.presto.spi.type.TimestampType)3 VarcharType (com.facebook.presto.spi.type.VarcharType)3 PrestoException (com.facebook.presto.spi.PrestoException)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 DataType (org.apache.carbondata.core.metadata.datatype.DataType)2 HiveUtil.isArrayType (com.facebook.presto.hive.HiveUtil.isArrayType)1 HiveUtil.isMapType (com.facebook.presto.hive.HiveUtil.isMapType)1 HiveUtil.isRowType (com.facebook.presto.hive.HiveUtil.isRowType)1 OrcCorruptionException (com.facebook.presto.orc.OrcCorruptionException)1 Block (com.facebook.presto.spi.block.Block)1