Search in sources :

Example 26 with VarcharType

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);
}
Also used : Matcher(java.util.regex.Matcher) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint)

Example 27 with VarcharType

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++;
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) SqlTimestamp(io.trino.spi.type.SqlTimestamp) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) VarbinaryType(io.trino.spi.type.VarbinaryType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) VarbinaryType(io.trino.spi.type.VarbinaryType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 28 with VarcharType

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()));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) BigDecimal(java.math.BigDecimal) Date(java.sql.Date)

Example 29 with VarcharType

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);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(io.trino.spi.type.DecimalType) BigInteger(java.math.BigInteger) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) Int128(io.trino.spi.type.Int128) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date)

Example 30 with VarcharType

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()));
}
Also used : ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) BigDecimal(java.math.BigDecimal) Date(java.sql.Date)

Aggregations

VarcharType (io.trino.spi.type.VarcharType)83 DecimalType (io.trino.spi.type.DecimalType)50 CharType (io.trino.spi.type.CharType)47 Type (io.trino.spi.type.Type)37 TrinoException (io.trino.spi.TrinoException)35 ArrayType (io.trino.spi.type.ArrayType)29 TimestampType (io.trino.spi.type.TimestampType)23 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)23 MapType (io.trino.spi.type.MapType)21 Slice (io.airlift.slice.Slice)20 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)18 RowType (io.trino.spi.type.RowType)18 ImmutableList (com.google.common.collect.ImmutableList)17 TimeType (io.trino.spi.type.TimeType)15 VarbinaryType (io.trino.spi.type.VarbinaryType)15 Block (io.trino.spi.block.Block)14 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)14 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)12