use of com.facebook.presto.spi.type.DecimalType in project presto by prestodb.
the class ParquetShortDecimalColumnReader method readValue.
@Override
protected void readValue(BlockBuilder blockBuilder, Type type) {
if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) {
long decimalValue;
if (columnDescriptor.getType().equals(INT32)) {
HiveDecimalWritable hiveDecimalWritable = new HiveDecimalWritable(HiveDecimal.create(valuesReader.readInteger()));
decimalValue = getShortDecimalValue(hiveDecimalWritable, ((DecimalType) type).getScale());
} else if (columnDescriptor.getType().equals(INT64)) {
HiveDecimalWritable hiveDecimalWritable = new HiveDecimalWritable(HiveDecimal.create(valuesReader.readLong()));
decimalValue = getShortDecimalValue(hiveDecimalWritable, ((DecimalType) type).getScale());
} else {
decimalValue = getShortDecimalValue(valuesReader.readBytes().getBytes());
}
type.writeLong(blockBuilder, decimalValue);
} else {
blockBuilder.appendNull();
}
}
use of com.facebook.presto.spi.type.DecimalType in project presto by prestodb.
the class HiveTypeTranslator method translate.
@Override
public TypeInfo translate(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;
int varcharLength = varcharType.getLength();
if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
return getVarcharTypeInfo(varcharLength);
} else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
return HIVE_STRING.getTypeInfo();
} else {
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 = translate(type.getTypeParameters().get(0));
return getListTypeInfo(elementType);
}
if (isMapType(type)) {
TypeInfo keyType = translate(type.getTypeParameters().get(0));
TypeInfo valueType = translate(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();
fieldNames.add(namedTypeSignature.getName());
}
return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class PrestoFilterUtil method ConvertDataByType.
private static Object ConvertDataByType(Object rawdata, Type type) {
if (type.equals(IntegerType.INTEGER) || type.equals(SmallintType.SMALLINT))
return Integer.valueOf(rawdata.toString());
else // new Integer((rawdata.toString()));
if (type.equals(BigintType.BIGINT))
return rawdata;
else if (type.equals(VarcharType.VARCHAR)) {
if (rawdata instanceof Slice) {
return ((Slice) rawdata).toStringUtf8();
} else {
return rawdata;
}
} else if (type.equals(BooleanType.BOOLEAN))
return rawdata;
else if (type.equals(DateType.DATE)) {
Calendar c = Calendar.getInstance();
c.setTime(new Date(0));
c.add(Calendar.DAY_OF_YEAR, ((Long) rawdata).intValue());
Date date = c.getTime();
return date.getTime() * 1000;
} else if (type instanceof DecimalType) {
if (rawdata instanceof Double) {
return new BigDecimal((Double) rawdata);
} else if (rawdata instanceof Long) {
return new BigDecimal(new BigInteger(String.valueOf(rawdata)), ((DecimalType) type).getScale());
} else if (rawdata instanceof Slice) {
return new BigDecimal(Decimals.decodeUnscaledValue((Slice) rawdata), ((DecimalType) type).getScale());
}
} else if (type.equals(TimestampType.TIMESTAMP)) {
return (Long) rawdata * 1000;
}
return rawdata;
}
use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class DecimalSliceStreamReader method populateShortDecimalVector.
private void populateShortDecimalVector(Type type, int numberOfRows, BlockBuilder builder) {
DecimalType decimalType = (DecimalType) type;
if (isDictionary) {
for (int i = 0; i < numberOfRows; i++) {
int value = (int) columnVector.getData(i);
Object data = DataTypeUtil.getDataBasedOnDataType(dictionary.getDictionaryValueForKey(value), DataTypes.createDecimalType(decimalType.getPrecision(), decimalType.getScale()));
if (Objects.isNull(data)) {
builder.appendNull();
} else {
BigDecimal decimalValue = (BigDecimal) data;
long rescaledDecimal = Decimals.rescale(decimalValue.unscaledValue().longValue(), decimalValue.scale(), decimalType.getScale());
type.writeLong(builder, rescaledDecimal);
}
}
} else {
for (int i = 0; i < numberOfRows; i++) {
BigDecimal decimalValue = (BigDecimal) columnVector.getData(i);
long rescaledDecimal = Decimals.rescale(decimalValue.unscaledValue().longValue(), decimalValue.scale(), decimalType.getScale());
type.writeLong(builder, rescaledDecimal);
}
}
}
use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class DecimalSliceStreamReader method populateLongDecimalVector.
private void populateLongDecimalVector(Type type, int numberOfRows, BlockBuilder builder) {
if (isDictionary) {
for (int i = 0; i < numberOfRows; i++) {
int value = (int) columnVector.getData(i);
DecimalType decimalType = (DecimalType) type;
Object data = DataTypeUtil.getDataBasedOnDataType(dictionary.getDictionaryValueForKey(value), DataTypes.createDecimalType(decimalType.getPrecision(), decimalType.getScale()));
if (Objects.isNull(data)) {
builder.appendNull();
} else {
BigDecimal decimalValue = (BigDecimal) data;
Slice slice = getSlice(decimalValue, type);
type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
}
}
} else {
for (int i = 0; i < numberOfRows; i++) {
Slice slice = getSlice((columnVector.getData(i)), type);
type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
}
}
}
Aggregations