use of io.trino.spi.type.Int128 in project trino by trinodb.
the class DecimalEncoding method parseSlice.
private Int128 parseSlice(Slice slice, int offset) {
// first vint is scale
int scale = toIntExact(readVInt(slice, offset));
offset += decodeVIntSize(slice, offset);
// second vint is length
int length = toIntExact(readVInt(slice, offset));
offset += decodeVIntSize(slice, offset);
checkState(length <= BYTES_IN_LONG_DECIMAL);
// reset the results buffer
if (slice.getByte(offset) >= 0) {
resultSlice.setLong(0, 0L);
resultSlice.setLong(8, 0L);
} else {
resultSlice.setLong(0, 0xFFFF_FFFF_FFFF_FFFFL);
resultSlice.setLong(8, 0xFFFF_FFFF_FFFF_FFFFL);
}
resultSlice.setBytes(BYTES_IN_LONG_DECIMAL - length, slice, offset, length);
Int128 result = Int128.fromBigEndian(resultBytes);
return Int128Math.rescale(result, type.getScale() - scale);
}
use of io.trino.spi.type.Int128 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.Int128 in project trino by trinodb.
the class TupleDomainOrcPredicate method getDomain.
@VisibleForTesting
public static Domain getDomain(Type type, long rowCount, ColumnStatistics columnStatistics) {
if (rowCount == 0) {
return Domain.none(type);
}
if (columnStatistics == null) {
return Domain.all(type);
}
if (columnStatistics.hasNumberOfValues() && columnStatistics.getNumberOfValues() == 0) {
return Domain.onlyNull(type);
}
boolean hasNullValue = columnStatistics.getNumberOfValues() != rowCount;
if (type instanceof TimeType && columnStatistics.getIntegerStatistics() != null) {
// This is the representation of TIME used by Iceberg
return createDomain(type, hasNullValue, columnStatistics.getIntegerStatistics(), value -> ((long) value) * Timestamps.PICOSECONDS_PER_MICROSECOND);
}
if (type.getJavaType() == boolean.class && columnStatistics.getBooleanStatistics() != null) {
BooleanStatistics booleanStatistics = columnStatistics.getBooleanStatistics();
boolean hasTrueValues = (booleanStatistics.getTrueValueCount() != 0);
boolean hasFalseValues = (columnStatistics.getNumberOfValues() != booleanStatistics.getTrueValueCount());
if (hasTrueValues && hasFalseValues) {
return Domain.all(BOOLEAN);
}
if (hasTrueValues) {
return Domain.create(ValueSet.of(BOOLEAN, true), hasNullValue);
}
if (hasFalseValues) {
return Domain.create(ValueSet.of(BOOLEAN, false), hasNullValue);
}
} else if (isShortDecimal(type) && columnStatistics.getDecimalStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> rescale(value, (DecimalType) type).unscaledValue().longValue());
} else if (isLongDecimal(type) && columnStatistics.getDecimalStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> Int128.valueOf(rescale(value, (DecimalType) type).unscaledValue()));
} else if (type instanceof CharType && columnStatistics.getStringStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getStringStatistics(), value -> truncateToLengthAndTrimSpaces(value, type));
} else if (type instanceof VarcharType && columnStatistics.getStringStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getStringStatistics());
} else if (type instanceof DateType && columnStatistics.getDateStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDateStatistics(), value -> (long) value);
} else if ((type.equals(TIMESTAMP_MILLIS) || type.equals(TIMESTAMP_MICROS)) && columnStatistics.getTimestampStatistics() != null) {
// upper bound of the domain we create must be adjusted accordingly, to includes the rounded timestamp.
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> min * MICROSECONDS_PER_MILLISECOND, max -> (max + 1) * MICROSECONDS_PER_MILLISECOND);
} else if (type.equals(TIMESTAMP_NANOS) && columnStatistics.getTimestampStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> new LongTimestamp(min * MICROSECONDS_PER_MILLISECOND, 0), max -> new LongTimestamp((max + 1) * MICROSECONDS_PER_MILLISECOND, 0));
} else if (type.equals(TIMESTAMP_TZ_MILLIS) && columnStatistics.getTimestampStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), value -> packDateTimeWithZone(value, UTC_KEY));
} else if (type.equals(TIMESTAMP_TZ_MICROS) && (columnStatistics.getTimestampStatistics() != null)) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(min, 0, UTC_KEY), max -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(max, 999_000_000, UTC_KEY));
} else if (type.equals(TIMESTAMP_TZ_NANOS) && columnStatistics.getTimestampStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getTimestampStatistics(), min -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(min, 0, UTC_KEY), max -> LongTimestampWithTimeZone.fromEpochMillisAndFraction(max, 999_999_000, UTC_KEY));
} else if (type.getJavaType() == long.class && columnStatistics.getIntegerStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getIntegerStatistics());
} else if (type.getJavaType() == double.class && columnStatistics.getDoubleStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDoubleStatistics());
} else if (REAL.equals(type) && columnStatistics.getDoubleStatistics() != null) {
return createDomain(type, hasNullValue, columnStatistics.getDoubleStatistics(), value -> (long) floatToRawIntBits(value.floatValue()));
}
return Domain.create(ValueSet.all(type), hasNullValue);
}
use of io.trino.spi.type.Int128 in project trino by trinodb.
the class LongDecimalStatisticsBuilder method addBlock.
@Override
public void addBlock(Type type, Block block) {
int scale = ((DecimalType) type).getScale();
for (int position = 0; position < block.getPositionCount(); position++) {
if (!block.isNull(position)) {
Int128 value = (Int128) type.getObject(block, position);
addValue(new BigDecimal(value.toBigInteger(), scale));
}
}
}
use of io.trino.spi.type.Int128 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);
}
Aggregations