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;
}
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()));
}
}
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));
}
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();
}
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);
}
Aggregations