Search in sources :

Example 6 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class TypeUtils method getActualValue.

public static Object getActualValue(Type type, Object value) {
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        return value;
    } else if (type instanceof BooleanType) {
        return value;
    } else if (type instanceof DoubleType) {
        return value;
    } else if (type instanceof DateType) {
        // keep the `long` representation of date
        return value;
    } else if (type instanceof RealType) {
        Long number = (Long) value;
        return intBitsToFloat(number.intValue());
    } else if (type instanceof VarcharType || type instanceof CharType) {
        if (value instanceof Slice) {
            return ((Slice) value).toStringUtf8();
        }
        return value;
    } else if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(value instanceof Long);
            return new BigDecimal(BigInteger.valueOf((Long) value), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        Slice slice;
        if (value instanceof String) {
            slice = Slices.utf8Slice((String) value);
        } else {
            checkState(value instanceof Slice);
            slice = (Slice) value;
        }
        return new BigDecimal(decodeUnscaledValue(slice), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    } else if (type instanceof TimestampType) {
        Long time = (Long) value;
        return new Timestamp(time);
    }
    throw new UnsupportedOperationException("Not Implemented Exception: " + value + "->" + type);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) RealType(io.prestosql.spi.type.RealType) Timestamp(java.sql.Timestamp) BigintType(io.prestosql.spi.type.BigintType) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) IntegerType(io.prestosql.spi.type.IntegerType) DoubleType(io.prestosql.spi.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) CharType(io.prestosql.spi.type.CharType) DateType(io.prestosql.spi.type.DateType)

Example 7 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class OrcTester method decodeRecordReaderValue.

private static Object decodeRecordReaderValue(Type type, Object inputActualValue) {
    Object actualValue = inputActualValue;
    if (actualValue instanceof BooleanWritable) {
        actualValue = ((BooleanWritable) actualValue).get();
    } else if (actualValue instanceof ByteWritable) {
        actualValue = ((ByteWritable) actualValue).get();
    } else if (actualValue instanceof BytesWritable) {
        actualValue = new SqlVarbinary(((BytesWritable) actualValue).copyBytes());
    } else if (actualValue instanceof DateWritableV2) {
        actualValue = new SqlDate(((DateWritableV2) actualValue).getDays());
    } else if (actualValue instanceof DoubleWritable) {
        actualValue = ((DoubleWritable) actualValue).get();
    } else if (actualValue instanceof FloatWritable) {
        actualValue = ((FloatWritable) actualValue).get();
    } else if (actualValue instanceof IntWritable) {
        actualValue = ((IntWritable) actualValue).get();
    } else if (actualValue instanceof HiveCharWritable) {
        actualValue = ((HiveCharWritable) actualValue).getPaddedValue().toString();
    } else if (actualValue instanceof LongWritable) {
        actualValue = ((LongWritable) actualValue).get();
    } else if (actualValue instanceof ShortWritable) {
        actualValue = ((ShortWritable) actualValue).get();
    } else if (actualValue instanceof HiveDecimalWritable) {
        DecimalType decimalType = (DecimalType) type;
        HiveDecimalWritable writable = (HiveDecimalWritable) actualValue;
        // writable messes with the scale so rescale the values to the Presto type
        BigInteger rescaledValue = rescale(writable.getHiveDecimal().unscaledValue(), writable.getScale(), decimalType.getScale());
        actualValue = new SqlDecimal(rescaledValue, decimalType.getPrecision(), decimalType.getScale());
    } else if (actualValue instanceof Text) {
        actualValue = actualValue.toString();
    } else if (actualValue instanceof TimestampWritableV2) {
        actualValue = sqlTimestampOf(((TimestampWritableV2) actualValue).getTimestamp().toEpochMilli());
    } else if (actualValue instanceof OrcStruct) {
        List<Object> fields = new ArrayList<>();
        OrcStruct structObject = (OrcStruct) actualValue;
        for (int fieldId = 0; fieldId < structObject.getNumFields(); fieldId++) {
            fields.add(OrcUtil.getFieldValue(structObject, fieldId));
        }
        actualValue = decodeRecordReaderStruct(type, fields);
    } else if (actualValue instanceof List) {
        actualValue = decodeRecordReaderList(type, ((List<?>) actualValue));
    } else if (actualValue instanceof Map) {
        actualValue = decodeRecordReaderMap(type, (Map<?, ?>) actualValue);
    }
    return actualValue;
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) SqlDecimal(io.prestosql.spi.type.SqlDecimal) Text(org.apache.hadoop.io.Text) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) FloatWritable(org.apache.hadoop.io.FloatWritable) OrcStruct(org.apache.hadoop.hive.ql.io.orc.OrcStruct) BooleanWritable(org.apache.hadoop.io.BooleanWritable) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) BigInteger(java.math.BigInteger) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) IntWritable(org.apache.hadoop.io.IntWritable)

Example 8 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class OrcTester method testRow.

// later we can extend for multiple columns
private static boolean testRow(List<Type> types, List<?> values, int row, Map<Integer, TupleDomainFilter> columnFilters) {
    for (int column = 0; column < types.size(); column++) {
        TupleDomainFilter filter = columnFilters.get(column);
        if (filter == null) {
            continue;
        }
        Object value = values.get(row);
        if (value == null) {
            if (!filter.testNull()) {
                return false;
            }
        } else {
            Type type = types.get(column);
            if (type == BOOLEAN) {
                if (!filter.testBoolean((Boolean) value)) {
                    return false;
                }
            } else if (type == BIGINT || type == INTEGER || type == SMALLINT) {
                if (!filter.testLong(((Number) value).longValue())) {
                    return false;
                }
            } else if (type == DATE) {
                if (!filter.testLong(((SqlDate) value).getDays())) {
                    return false;
                }
            } else if (type == TIMESTAMP) {
                return filter.testLong(((SqlTimestamp) value).getMillis());
            } else if (type == VARCHAR) {
                return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
            } else if (type instanceof CharType) {
                String charString = String.valueOf(value);
                return filter.testBytes(charString.getBytes(StandardCharsets.UTF_8), 0, charString.length());
            } else if (type == VARBINARY) {
                byte[] binary = ((SqlVarbinary) value).getBytes();
                return filter.testBytes(binary, 0, binary.length);
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
                if (decimalType.isShort()) {
                    return filter.testLong(bigDecimal.unscaledValue().longValue());
                }
            } else if (type == DOUBLE) {
                if (!filter.testDouble((double) value)) {
                    return false;
                }
            } else {
                fail("Unsupported type: " + type);
            }
        }
    }
    return true;
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) BigDecimal(java.math.BigDecimal) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 9 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class OrcTester method getJavaObjectInspector.

private static ObjectInspector getJavaObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return javaStringObjectInspector;
    }
    if (type instanceof CharType) {
        int charLength = ((CharType) type).getLength();
        return new JavaHiveCharObjectInspector(getCharTypeInfo(charLength));
    }
    if (type instanceof VarbinaryType) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TIMESTAMP)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY)) {
        return getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (type.getTypeSignature().getBase().equals(StandardTypes.MAP)) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (type.getTypeSignature().getBase().equals(StandardTypes.ROW)) {
        return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(OrcTester::getJavaObjectInspector).collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector) PrimitiveObjectInspectorFactory.javaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector) PrimitiveObjectInspectorFactory.javaFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector) PrimitiveObjectInspectorFactory.javaDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) JavaHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveCharObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) ObjectInspectorFactory.getStandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) ObjectInspectorFactory.getStandardMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector) ObjectInspectorFactory.getStandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) VarbinaryType(io.prestosql.spi.type.VarbinaryType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 10 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class LongDecimalStatisticsBuilder method addBlock.

@Override
public void addBlock(Type type, Block block) {
    int scale = ((DecimalType) type).getScale();
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (!block.isNull(position)) {
            Slice value = type.getSlice(block, position);
            addValue(new BigDecimal(Decimals.decodeUnscaledValue(value), scale));
        }
    }
}
Also used : Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) BigDecimal(java.math.BigDecimal)

Aggregations

DecimalType (io.prestosql.spi.type.DecimalType)82 VarcharType (io.prestosql.spi.type.VarcharType)52 CharType (io.prestosql.spi.type.CharType)45 Type (io.prestosql.spi.type.Type)39 PrestoException (io.prestosql.spi.PrestoException)28 Slice (io.airlift.slice.Slice)20 BigDecimal (java.math.BigDecimal)19 Block (io.prestosql.spi.block.Block)15 BigInteger (java.math.BigInteger)14 ArrayList (java.util.ArrayList)14 ArrayType (io.prestosql.spi.type.ArrayType)13 TimestampType (io.prestosql.spi.type.TimestampType)13 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)11 Chars.isCharType (io.prestosql.spi.type.Chars.isCharType)11 DateType (io.prestosql.spi.type.DateType)11 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)11 VarbinaryType (io.prestosql.spi.type.VarbinaryType)11 ImmutableList (com.google.common.collect.ImmutableList)10 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)9 CharType.createCharType (io.prestosql.spi.type.CharType.createCharType)8