use of io.trino.spi.type.Int128 in project trino by trinodb.
the class TestLongDecimalStream method test.
@Test
public void test() throws IOException {
Random random = new Random(0);
List<List<Int128>> groups = new ArrayList<>();
for (int groupIndex = 0; groupIndex < 3; groupIndex++) {
List<Int128> group = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
BigInteger value = new BigInteger(120, random);
group.add(Int128.valueOf(value));
}
groups.add(group);
}
testWriteValue(groups);
}
use of io.trino.spi.type.Int128 in project trino by trinodb.
the class FunctionAssertions method interpret.
private Object interpret(Expression expression, Type expectedType, Session session) {
Map<NodeRef<Expression>, Type> expressionTypes = getTypes(session, getPlannerContext(), INPUT_TYPES, expression);
ExpressionInterpreter evaluator = new ExpressionInterpreter(expression, runner.getPlannerContext(), session, expressionTypes);
Object result = evaluator.evaluate(symbol -> {
int position = 0;
int channel = INPUT_MAPPING.get(symbol);
Type type = INPUT_TYPES.get(symbol);
Block block = SOURCE_PAGE.getBlock(channel);
if (block.isNull(position)) {
return null;
}
Class<?> javaType = type.getJavaType();
if (javaType == boolean.class) {
return type.getBoolean(block, position);
} else if (javaType == long.class) {
return type.getLong(block, position);
} else if (javaType == double.class) {
return type.getDouble(block, position);
} else if (javaType == Slice.class) {
return type.getSlice(block, position);
} else if (javaType == Block.class || javaType == Int128.class) {
return type.getObject(block, position);
} else {
throw new UnsupportedOperationException("not yet implemented");
}
});
// convert result from stack type to Type ObjectValue
Block block = Utils.nativeValueToBlock(expectedType, result);
return expectedType.getObjectValue(session.toConnectorSession(), block, 0);
}
use of io.trino.spi.type.Int128 in project trino by trinodb.
the class TestTupleDomainParquetPredicate method testLongDecimalWithNoScale.
@Test
public void testLongDecimalWithNoScale() throws Exception {
ColumnDescriptor columnDescriptor = createColumnDescriptor(FIXED_LEN_BYTE_ARRAY, "LongDecimalColumnWithNoScale");
DecimalType type = createDecimalType(20, 0);
Int128 zero = Int128.ZERO;
Int128 hundred = Int128.valueOf(100L);
assertEquals(getDomain(columnDescriptor, type, 0, null, ID, UTC), all(type));
assertEquals(getDomain(columnDescriptor, type, 10, binaryColumnStats(100L, 100L), ID, UTC), singleValue(type, hundred));
assertEquals(getDomain(columnDescriptor, type, 10, binaryColumnStats(0L, 100L), ID, UTC), create(ValueSet.ofRanges(range(type, zero, true, hundred, true)), false));
// fail on corrupted statistics
assertThatExceptionOfType(ParquetCorruptionException.class).isThrownBy(() -> getDomain(columnDescriptor, type, 10, binaryColumnStats(100L, 10L), ID, UTC)).withMessage("Corrupted statistics for column \"[] required fixed_len_byte_array(0) LongDecimalColumnWithNoScale\" in Parquet file \"testFile\": [min: 0x64, max: 0x0A, num_nulls: 0]");
}
use of io.trino.spi.type.Int128 in project trino by trinodb.
the class DecimalAverageAggregation method average.
@VisibleForTesting
public static Int128 average(LongDecimalWithOverflowAndLongState state, DecimalType type) {
long[] decimal = state.getDecimalArray();
int offset = state.getDecimalArrayOffset();
long overflow = state.getOverflow();
if (overflow != 0) {
BigDecimal sum = new BigDecimal(Int128.valueOf(decimal[offset], decimal[offset + 1]).toBigInteger(), type.getScale());
sum = sum.add(new BigDecimal(OVERFLOW_MULTIPLIER.multiply(BigInteger.valueOf(overflow))));
BigDecimal count = BigDecimal.valueOf(state.getLong());
return Decimals.encodeScaledValue(sum.divide(count, type.getScale(), ROUND_HALF_UP), type.getScale());
}
Int128 result = divideRoundUp(decimal[offset], decimal[offset + 1], 0, 0, state.getLong(), 0);
if (overflows(result)) {
throw new ArithmeticException("Decimal overflow");
}
return result;
}
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