Search in sources :

Example 6 with TimestampWithTimeZoneType

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

the class DeltaLakeJsonFileStatistics method deserializeStatisticsValue.

private Optional<Object> deserializeStatisticsValue(DeltaLakeColumnHandle columnHandle, String statValue) {
    Object columnValue = deserializeColumnValue(columnHandle, statValue, DeltaLakeJsonFileStatistics::readStatisticsTimestamp);
    Type columnType = columnHandle.getType();
    if (columnType.equals(DATE)) {
        long epochDate = (long) columnValue;
        if (LocalDate.ofEpochDay(epochDate).isBefore(START_OF_MODERN_ERA)) {
            return Optional.empty();
        }
    }
    if (columnType instanceof TimestampWithTimeZoneType) {
        long packedTimestamp = (long) columnValue;
        ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(unpackMillisUtc(packedTimestamp)), UTC);
        if (dateTime.toLocalDate().isBefore(START_OF_MODERN_ERA)) {
            return Optional.empty();
        }
    }
    return Optional.of(columnValue);
}
Also used : Type(io.trino.spi.type.Type) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) ZonedDateTime(java.time.ZonedDateTime) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType)

Example 7 with TimestampWithTimeZoneType

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

the class StatsUtil method toStatsRepresentation.

public static OptionalDouble toStatsRepresentation(Type type, Object value) {
    requireNonNull(type, "type is null");
    requireNonNull(value, "value is null");
    if (type == BOOLEAN) {
        return OptionalDouble.of((boolean) value ? 1 : 0);
    }
    if (type == TINYINT || type == SMALLINT || type == INTEGER || type == BIGINT) {
        return OptionalDouble.of((long) value);
    }
    if (type == REAL) {
        return OptionalDouble.of(intBitsToFloat(toIntExact((Long) value)));
    }
    if (type == DOUBLE) {
        return OptionalDouble.of((double) value);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            return OptionalDouble.of(shortDecimalToDouble((long) value, longTenToNth(decimalType.getScale())));
        }
        return OptionalDouble.of(longDecimalToDouble((Int128) value, decimalType.getScale()));
    }
    if (type == DATE) {
        return OptionalDouble.of((long) value);
    }
    if (type instanceof TimestampType) {
        if (((TimestampType) type).isShort()) {
            return OptionalDouble.of((long) value);
        }
        return OptionalDouble.of(((LongTimestamp) value).getEpochMicros());
    }
    if (type instanceof TimestampWithTimeZoneType) {
        if (((TimestampWithTimeZoneType) type).isShort()) {
            return OptionalDouble.of(unpackMillisUtc((long) value));
        }
        return OptionalDouble.of(((LongTimestampWithTimeZone) value).getEpochMillis());
    }
    return OptionalDouble.empty();
}
Also used : TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) Int128(io.trino.spi.type.Int128)

Example 8 with TimestampWithTimeZoneType

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

the class TimestampColumnReader method readValue.

// TODO: refactor to provide type at construction time (https://github.com/trinodb/trino/issues/5198)
@Override
protected void readValue(BlockBuilder blockBuilder, Type type) {
    if (type instanceof TimestampWithTimeZoneType) {
        DecodedTimestamp decodedTimestamp = decodeInt96Timestamp(valuesReader.readBytes());
        long utcMillis = decodedTimestamp.getEpochSeconds() * MILLISECONDS_PER_SECOND + decodedTimestamp.getNanosOfSecond() / NANOSECONDS_PER_MILLISECOND;
        type.writeLong(blockBuilder, packDateTimeWithZone(utcMillis, UTC_KEY));
    } else {
        TrinoTimestampEncoder<?> trinoTimestampEncoder = createTimestampEncoder((TimestampType) type, timeZone);
        trinoTimestampEncoder.write(decodeInt96Timestamp(valuesReader.readBytes()), blockBuilder);
    }
}
Also used : TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp)

Example 9 with TimestampWithTimeZoneType

use of io.trino.spi.type.TimestampWithTimeZoneType 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));
}
Also used : VarcharType(io.trino.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) CharType(io.trino.spi.type.CharType)

Example 10 with TimestampWithTimeZoneType

use of io.trino.spi.type.TimestampWithTimeZoneType 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)

Aggregations

TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)17 VarcharType (io.trino.spi.type.VarcharType)12 DecimalType (io.trino.spi.type.DecimalType)10 TimestampType (io.trino.spi.type.TimestampType)10 Type (io.trino.spi.type.Type)9 CharType (io.trino.spi.type.CharType)7 TrinoException (io.trino.spi.TrinoException)6 ArrayType (io.trino.spi.type.ArrayType)6 ImmutableList (com.google.common.collect.ImmutableList)5 TimeType (io.trino.spi.type.TimeType)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Slice (io.airlift.slice.Slice)4 MapType (io.trino.spi.type.MapType)4 BigDecimal (java.math.BigDecimal)4 DecodedTimestamp (io.trino.plugin.base.type.DecodedTimestamp)3 Block (io.trino.spi.block.Block)3 Int128 (io.trino.spi.type.Int128)3 RowType (io.trino.spi.type.RowType)3 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)2 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)2