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);
}
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();
}
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);
}
}
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));
}
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++;
}
Aggregations