use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class BigQueryResultPageSource method writeObject.
private static void writeObject(BlockBuilder output, Type type, Object value) {
if (type instanceof DecimalType) {
verify(isLongDecimal(type), "The type should be long decimal");
DecimalType decimalType = (DecimalType) type;
BigDecimal decimal = DECIMAL_CONVERTER.convert(decimalType.getPrecision(), decimalType.getScale(), value);
type.writeObject(output, Decimals.encodeScaledValue(decimal, decimalType.getScale()));
} else {
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Object: " + type.getTypeSignature());
}
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class DeltaHiveTypeTranslator method translate.
// Copy from HiveTypeTranslator with a custom mapping for TimestampWithTimeZone
public static TypeInfo translate(Type type) {
requireNonNull(type, "type is null");
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.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
return getVarcharTypeInfo(varcharType.getBoundedLength());
}
throw new TrinoException(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 TrinoException(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 (type instanceof TimestampWithTimeZoneType) {
verify(((TimestampWithTimeZoneType) type).getPrecision() == 3, "Unsupported type: %s", type);
return HIVE_TIMESTAMP.getTypeInfo();
}
if (type instanceof TimestampType) {
verify(((TimestampType) type).getPrecision() == 3, "Unsupported type: %s", 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();
if (namedTypeSignature.getName().isEmpty()) {
throw new TrinoException(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(DeltaHiveTypeTranslator::translate).collect(toImmutableList()));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported Delta Lake type: %s", type));
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class IcebergUtil method deserializePartitionValue.
public static Object deserializePartitionValue(Type type, String valueString, String name) {
if (valueString == null) {
return null;
}
try {
if (type.equals(BOOLEAN)) {
if (valueString.equalsIgnoreCase("true")) {
return true;
}
if (valueString.equalsIgnoreCase("false")) {
return false;
}
throw new IllegalArgumentException();
}
if (type.equals(INTEGER)) {
return parseLong(valueString);
}
if (type.equals(BIGINT)) {
return parseLong(valueString);
}
if (type.equals(REAL)) {
return (long) floatToRawIntBits(parseFloat(valueString));
}
if (type.equals(DOUBLE)) {
return parseDouble(valueString);
}
if (type.equals(DATE)) {
return parseLong(valueString);
}
if (type.equals(TIME_MICROS)) {
return parseLong(valueString) * PICOSECONDS_PER_MICROSECOND;
}
if (type.equals(TIMESTAMP_MICROS)) {
return parseLong(valueString);
}
if (type.equals(TIMESTAMP_TZ_MICROS)) {
return timestampTzFromMicros(parseLong(valueString));
}
if (type instanceof VarcharType) {
Slice value = utf8Slice(valueString);
VarcharType varcharType = (VarcharType) type;
if (!varcharType.isUnbounded() && SliceUtf8.countCodePoints(value) > varcharType.getBoundedLength()) {
throw new IllegalArgumentException();
}
return value;
}
if (type.equals(VarbinaryType.VARBINARY)) {
return Slices.wrappedBuffer(Base64.getDecoder().decode(valueString));
}
if (type.equals(UuidType.UUID)) {
return javaUuidToTrinoUuid(UUID.fromString(valueString));
}
if (isShortDecimal(type) || isLongDecimal(type)) {
DecimalType decimalType = (DecimalType) type;
BigDecimal decimal = new BigDecimal(valueString);
decimal = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY);
if (decimal.precision() > decimalType.getPrecision()) {
throw new IllegalArgumentException();
}
BigInteger unscaledValue = decimal.unscaledValue();
return isShortDecimal(type) ? unscaledValue.longValue() : Int128.valueOf(unscaledValue);
}
} catch (IllegalArgumentException e) {
throw new TrinoException(ICEBERG_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", valueString, type.getDisplayName(), name));
}
// Iceberg tables don't partition by non-primitive-type columns.
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Invalid partition type " + type.toString());
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class PartitionTransforms method getColumnTransform.
public static ColumnTransform getColumnTransform(PartitionField field, Type type) {
String transform = field.transform().toString();
switch(transform) {
case "identity":
return identity(type);
case "year":
if (type.equals(DATE)) {
return yearsFromDate();
}
if (type.equals(TIMESTAMP_MICROS)) {
return yearsFromTimestamp();
}
if (type.equals(TIMESTAMP_TZ_MICROS)) {
return yearsFromTimestampWithTimeZone();
}
throw new UnsupportedOperationException("Unsupported type for 'year': " + field);
case "month":
if (type.equals(DATE)) {
return monthsFromDate();
}
if (type.equals(TIMESTAMP_MICROS)) {
return monthsFromTimestamp();
}
if (type.equals(TIMESTAMP_TZ_MICROS)) {
return monthsFromTimestampWithTimeZone();
}
throw new UnsupportedOperationException("Unsupported type for 'month': " + field);
case "day":
if (type.equals(DATE)) {
return daysFromDate();
}
if (type.equals(TIMESTAMP_MICROS)) {
return daysFromTimestamp();
}
if (type.equals(TIMESTAMP_TZ_MICROS)) {
return daysFromTimestampWithTimeZone();
}
throw new UnsupportedOperationException("Unsupported type for 'day': " + field);
case "hour":
if (type.equals(TIMESTAMP_MICROS)) {
return hoursFromTimestamp();
}
if (type.equals(TIMESTAMP_TZ_MICROS)) {
return hoursFromTimestampWithTimeZone();
}
throw new UnsupportedOperationException("Unsupported type for 'hour': " + field);
case "void":
return voidTransform(type);
}
Matcher matcher = BUCKET_PATTERN.matcher(transform);
if (matcher.matches()) {
int count = parseInt(matcher.group(1));
return bucket(type, count);
}
matcher = TRUNCATE_PATTERN.matcher(transform);
if (matcher.matches()) {
int width = parseInt(matcher.group(1));
if (type.equals(INTEGER)) {
return truncateInteger(width);
}
if (type.equals(BIGINT)) {
return truncateBigint(width);
}
if (isShortDecimal(type)) {
DecimalType decimal = (DecimalType) type;
return truncateShortDecimal(type, width, decimal);
}
if (isLongDecimal(type)) {
DecimalType decimal = (DecimalType) type;
return truncateLongDecimal(type, width, decimal);
}
if (type instanceof VarcharType) {
return truncateVarchar(width);
}
if (type.equals(VARBINARY)) {
return truncateVarbinary(width);
}
throw new UnsupportedOperationException("Unsupported type for 'truncate': " + field);
}
throw new UnsupportedOperationException("Unsupported partition transform: " + field);
}
use of io.trino.spi.type.DecimalType in project trino by trinodb.
the class TypeConverter method toTrinoType.
public static Type toTrinoType(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 TIME_MICROS;
case TIMESTAMP:
return ((Types.TimestampType) type).shouldAdjustToUTC() ? TIMESTAMP_TZ_MICROS : TIMESTAMP_MICROS;
case STRING:
return VarcharType.createUnboundedVarcharType();
case UUID:
return UuidType.UUID;
case LIST:
Types.ListType listType = (Types.ListType) type;
return new ArrayType(toTrinoType(listType.elementType(), typeManager));
case MAP:
Types.MapType mapType = (Types.MapType) type;
TypeSignature keyType = toTrinoType(mapType.keyType(), typeManager).getTypeSignature();
TypeSignature valueType = toTrinoType(mapType.valueType(), typeManager).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(keyType), TypeSignatureParameter.typeParameter(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()), toTrinoType(field.type(), typeManager))).collect(toImmutableList()));
}
throw new UnsupportedOperationException(format("Cannot convert from Iceberg type '%s' (%s) to Trino type", type, type.typeId()));
}
Aggregations