Search in sources :

Example 1 with TimeWithTimeZoneType

use of io.trino.spi.type.TimeWithTimeZoneType 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 2 with TimeWithTimeZoneType

use of io.trino.spi.type.TimeWithTimeZoneType in project TiBigData by tidb-incubator.

the class TypeHelpers method toSqlString.

public static String toSqlString(Type type) {
    if (type instanceof TimeWithTimeZoneType || type instanceof TimestampWithTimeZoneType) {
        throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
    }
    if (type instanceof TimestampType) {
        return format("timestamp(%s)", ((TimestampType) type).getPrecision());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return "longtext";
        }
        Integer length = varcharType.getLength().orElseThrow(IllegalStateException::new);
        if (length <= 255) {
            return "tinytext";
        }
        if (length <= 65535) {
            return "text";
        }
        if (length <= 16777215) {
            return "mediumtext";
        }
        return "longtext";
    }
    if (type instanceof CharType) {
        int length = ((CharType) type).getLength();
        if (length <= 255) {
            return "char(" + length + ")";
        }
        return "text";
    }
    if (type instanceof DecimalType) {
        return format("decimal(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale());
    }
    if (type instanceof TimeType) {
        return format("time(%s)", ((TimeType) type).getPrecision());
    }
    String sqlType = SQL_TYPES.get(type);
    if (sqlType != null) {
        return sqlType;
    }
    return type.getDisplayName();
}
Also used : VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) CharType(io.trino.spi.type.CharType) TimeType(io.trino.spi.type.TimeType)

Example 3 with TimeWithTimeZoneType

use of io.trino.spi.type.TimeWithTimeZoneType in project trino by trinodb.

the class TypeCoercion method compatibility.

private TypeCompatibility compatibility(Type fromType, Type toType) {
    if (fromType.equals(toType)) {
        return TypeCompatibility.compatible(toType, true);
    }
    if (fromType.equals(UnknownType.UNKNOWN)) {
        return TypeCompatibility.compatible(toType, true);
    }
    if (toType.equals(UnknownType.UNKNOWN)) {
        return TypeCompatibility.compatible(fromType, false);
    }
    String fromTypeBaseName = fromType.getBaseName();
    String toTypeBaseName = toType.getBaseName();
    if (fromTypeBaseName.equals(toTypeBaseName)) {
        if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
            Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
            Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.CHAR)) {
            Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.ROW)) {
            return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
        }
        if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) {
            Type commonSuperType = createTimestampType(Math.max(((TimestampType) fromType).getPrecision(), ((TimestampType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
            Type commonSuperType = createTimestampWithTimeZoneType(Math.max(((TimestampWithTimeZoneType) fromType).getPrecision(), ((TimestampWithTimeZoneType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.TIME)) {
            Type commonSuperType = createTimeType(Math.max(((TimeType) fromType).getPrecision(), ((TimeType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.TIME_WITH_TIME_ZONE)) {
            Type commonSuperType = createTimeWithTimeZoneType(Math.max(((TimeWithTimeZoneType) fromType).getPrecision(), ((TimeWithTimeZoneType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (isCovariantParametrizedType(fromType)) {
            return typeCompatibilityForCovariantParametrizedType(fromType, toType);
        }
        return TypeCompatibility.incompatible();
    }
    Optional<Type> coercedType = coerceTypeBase(fromType, toType.getBaseName());
    if (coercedType.isPresent()) {
        return compatibility(coercedType.get(), toType);
    }
    coercedType = coerceTypeBase(toType, fromType.getBaseName());
    if (coercedType.isPresent()) {
        TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
    }
    return TypeCompatibility.incompatible();
}
Also used : TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) TimeWithTimeZoneType.createTimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) TimestampType(io.trino.spi.type.TimestampType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) SetDigestType(io.trino.type.setdigest.SetDigestType) RowType(io.trino.spi.type.RowType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) DecimalType(io.trino.spi.type.DecimalType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) TimeWithTimeZoneType.createTimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 4 with TimeWithTimeZoneType

use of io.trino.spi.type.TimeWithTimeZoneType in project trino by trinodb.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, (Long) value);
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, (Integer) value);
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, (Short) value);
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, (Byte) value);
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, (Double) value);
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (type instanceof TimeType) {
        SqlTime time = (SqlTime) value;
        type.writeLong(blockBuilder, time.getPicos());
    } else if (type instanceof TimeWithTimeZoneType) {
        long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
        int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
        type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
    } else if (type instanceof TimestampType) {
        long micros = ((SqlTimestamp) value).getEpochMicros();
        if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
            type.writeLong(blockBuilder, micros);
        } else {
            type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
        }
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (type instanceof ArrayType) {
        List<?> list = (List<?>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType) {
        Map<?, ?> map = (Map<?, ?>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<?> row = (List<?>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
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) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TimeZoneKey(io.trino.spi.type.TimeZoneKey) BlockBuilder(io.trino.spi.block.BlockBuilder) LongTimestamp(io.trino.spi.type.LongTimestamp) SqlTimeWithTimeZone(io.trino.spi.type.SqlTimeWithTimeZone) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) SqlDate(io.trino.spi.type.SqlDate) OptionalLong(java.util.OptionalLong) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

TimeType (io.trino.spi.type.TimeType)4 TimeWithTimeZoneType (io.trino.spi.type.TimeWithTimeZoneType)4 TimestampType (io.trino.spi.type.TimestampType)4 VarcharType (io.trino.spi.type.VarcharType)4 ArrayType (io.trino.spi.type.ArrayType)3 CharType (io.trino.spi.type.CharType)3 MapType (io.trino.spi.type.MapType)3 RowType (io.trino.spi.type.RowType)3 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)3 Type (io.trino.spi.type.Type)3 ImmutableList (com.google.common.collect.ImmutableList)2 DecimalType (io.trino.spi.type.DecimalType)2 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)2 SqlTime (io.trino.spi.type.SqlTime)2 List (java.util.List)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 TrinoException (io.trino.spi.TrinoException)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 CharType.createCharType (io.trino.spi.type.CharType.createCharType)1