use of io.trino.spi.type.VarcharType 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.VarcharType in project trino by trinodb.
the class AbstractRowEncoder method appendColumnValue.
@Override
public void appendColumnValue(Block block, int position) {
checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
Type type = columnHandles.get(currentColumnIndex).getType();
if (block.isNull(position)) {
appendNullValue();
} else if (type == BOOLEAN) {
appendBoolean(type.getBoolean(block, position));
} else if (type == BIGINT) {
appendLong(type.getLong(block, position));
} else if (type == INTEGER) {
appendInt(toIntExact(type.getLong(block, position)));
} else if (type == SMALLINT) {
appendShort(Shorts.checkedCast(type.getLong(block, position)));
} else if (type == TINYINT) {
appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
} else if (type == DOUBLE) {
appendDouble(type.getDouble(block, position));
} else if (type == REAL) {
appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
} else if (type instanceof VarcharType) {
appendString(type.getSlice(block, position).toStringUtf8());
} else if (type instanceof VarbinaryType) {
appendByteBuffer(type.getSlice(block, position).toByteBuffer());
} else if (type == DATE) {
appendSqlDate((SqlDate) type.getObjectValue(session, block, position));
} else if (type instanceof TimeType) {
appendSqlTime((SqlTime) type.getObjectValue(session, block, position));
} else if (type instanceof TimeWithTimeZoneType) {
appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session, block, position));
} else if (type instanceof TimestampType) {
appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session, block, position));
} else if (type instanceof TimestampWithTimeZoneType) {
appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session, block, position));
} else if (type instanceof ArrayType) {
appendArray((List<Object>) type.getObjectValue(session, block, position));
} else if (type instanceof MapType) {
appendMap((Map<Object, Object>) type.getObjectValue(session, block, position));
} else if (type instanceof RowType) {
appendRow((List<Object>) type.getObjectValue(session, block, position));
} else {
throw new UnsupportedOperationException(format("Unsupported type '%s' for column '%s'", type, columnHandles.get(currentColumnIndex).getName()));
}
currentColumnIndex++;
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class TypeUtils method jdbcObjectToTrinoNative.
private static Object jdbcObjectToTrinoNative(ConnectorSession session, Object jdbcObject, Type type) {
if (jdbcObject == null) {
return null;
}
if (BOOLEAN.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type)) {
return jdbcObject;
}
if (TINYINT.equals(type)) {
return (long) (byte) jdbcObject;
}
if (SMALLINT.equals(type)) {
return (long) (short) jdbcObject;
}
if (INTEGER.equals(type)) {
return (long) (int) jdbcObject;
}
if (type instanceof ArrayType) {
return jdbcObjectArrayToBlock(session, ((ArrayType) type).getElementType(), (Object[]) jdbcObject);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal value = (BigDecimal) jdbcObject;
if (decimalType.isShort()) {
return encodeShortScaledValue(value, decimalType.getScale());
}
return encodeScaledValue(value, decimalType.getScale());
}
if (REAL.equals(type)) {
return (long) floatToRawIntBits((float) jdbcObject);
}
if (DATE.equals(type)) {
long localMillis = ((Date) jdbcObject).getTime();
// Convert it to a ~midnight in UTC.
long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
// convert to days
return MILLISECONDS.toDays(utcMillis);
}
if (type instanceof VarcharType) {
return utf8Slice((String) jdbcObject);
}
if (type instanceof CharType) {
return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", type, jdbcObject.getClass()));
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class TypeUtils method trinoNativeToJdbcObject.
private static Object trinoNativeToJdbcObject(ConnectorSession session, Type type, Object object) {
if (object == null) {
return null;
}
if (DOUBLE.equals(type) || BOOLEAN.equals(type) || BIGINT.equals(type)) {
return object;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
BigInteger unscaledValue = BigInteger.valueOf((long) object);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
BigInteger unscaledValue = ((Int128) object).toBigInteger();
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
if (REAL.equals(type)) {
return intBitsToFloat(toIntExact((long) object));
}
if (TINYINT.equals(type)) {
return SignedBytes.checkedCast((long) object);
}
if (SMALLINT.equals(type)) {
return Shorts.checkedCast((long) object);
}
if (INTEGER.equals(type)) {
return toIntExact((long) object);
}
if (DATE.equals(type)) {
// convert to midnight in default time zone
long millis = DAYS.toMillis((long) object);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
}
if (type instanceof VarcharType || type instanceof CharType) {
return ((Slice) object).toStringUtf8();
}
if (type instanceof ArrayType) {
// process subarray of multi-dimensional array
return getJdbcObjectArray(session, ((ArrayType) type).getElementType(), (Block) object);
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + type);
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class TypeUtils method jdbcObjectToTrinoNative.
private static Object jdbcObjectToTrinoNative(ConnectorSession session, Object jdbcObject, Type type) {
if (jdbcObject == null) {
return null;
}
if (BOOLEAN.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type)) {
return jdbcObject;
}
if (TINYINT.equals(type)) {
return (long) (byte) jdbcObject;
}
if (SMALLINT.equals(type)) {
return (long) (short) jdbcObject;
}
if (INTEGER.equals(type)) {
return (long) (int) jdbcObject;
}
if (type instanceof ArrayType) {
return jdbcObjectArrayToBlock(session, ((ArrayType) type).getElementType(), (Object[]) jdbcObject);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal value = (BigDecimal) jdbcObject;
if (decimalType.isShort()) {
return encodeShortScaledValue(value, decimalType.getScale());
}
return encodeScaledValue(value, decimalType.getScale());
}
if (REAL.equals(type)) {
return (long) floatToRawIntBits((float) jdbcObject);
}
if (DATE.equals(type)) {
long localMillis = ((Date) jdbcObject).getTime();
// Convert it to a ~midnight in UTC.
long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
// convert to days
return MILLISECONDS.toDays(utcMillis);
}
if (type instanceof VarcharType) {
return utf8Slice((String) jdbcObject);
}
if (type instanceof CharType) {
return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", type, jdbcObject.getClass()));
}
Aggregations