use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class AbstractTestDecimalSumAggregation method getExpectedValue.
@Override
protected SqlDecimal getExpectedValue(int start, int length) {
if (length == 0) {
return null;
}
BigDecimal sum = BigDecimal.ZERO;
for (int i = start; i < start + length; i++) {
sum = sum.add(getBigDecimalForCounter(i));
}
DecimalType expectedType = getExpectedType();
return new SqlDecimal(sum.unscaledValue(), expectedType.getPrecision(), expectedType.getScale());
}
use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class OrcTester method preprocessWriteValueHive.
private static Object preprocessWriteValueHive(Type type, Object value) {
if (value == null) {
return null;
}
if (type.equals(BOOLEAN)) {
return value;
}
if (type.equals(TINYINT)) {
return ((Number) value).byteValue();
}
if (type.equals(SMALLINT)) {
return ((Number) value).shortValue();
}
if (type.equals(INTEGER)) {
return ((Number) value).intValue();
}
if (type.equals(BIGINT)) {
return ((Number) value).longValue();
}
if (type.equals(REAL)) {
return ((Number) value).floatValue();
}
if (type.equals(DOUBLE)) {
return ((Number) value).doubleValue();
}
if (type instanceof VarcharType) {
return value;
}
if (type instanceof CharType) {
return new HiveChar((String) value, ((CharType) type).getLength());
}
if (type.equals(VARBINARY)) {
return ((SqlVarbinary) value).getBytes();
}
if (type.equals(DATE)) {
return Date.ofEpochDay(((SqlDate) value).getDays());
}
if (type.equals(TIMESTAMP_MILLIS) || type.equals(TIMESTAMP_MICROS) || type.equals(TIMESTAMP_NANOS)) {
LocalDateTime dateTime = ((SqlTimestamp) value).toLocalDateTime();
return Timestamp.ofEpochSecond(dateTime.toEpochSecond(ZoneOffset.UTC), dateTime.getNano());
}
if (type.equals(TIMESTAMP_TZ_MILLIS) || type.equals(TIMESTAMP_TZ_MICROS) || type.equals(TIMESTAMP_TZ_NANOS)) {
SqlTimestampWithTimeZone timestamp = (SqlTimestampWithTimeZone) value;
int nanosOfMilli = roundDiv(timestamp.getPicosOfMilli(), PICOSECONDS_PER_NANOSECOND);
return Timestamp.ofEpochMilli(timestamp.getEpochMillis(), nanosOfMilli);
}
if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
}
if (type instanceof ArrayType) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueHive(elementType, element)).collect(toList());
}
if (type instanceof MapType) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Map<Object, Object> newMap = new HashMap<>();
for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
newMap.put(preprocessWriteValueHive(keyType, entry.getKey()), preprocessWriteValueHive(valueType, entry.getValue()));
}
return newMap;
}
if (type instanceof RowType) {
List<?> fieldValues = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
List<Object> newStruct = new ArrayList<>();
for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
newStruct.add(preprocessWriteValueHive(fieldTypes.get(fieldId), fieldValues.get(fieldId)));
}
return newStruct;
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class AbstractTestOrcReader method testDecimalSequence.
@Test
public void testDecimalSequence() throws Exception {
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_2, decimalSequence("-30", "1", 60, 2, 1));
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_4, decimalSequence("-3000", "1", 60_00, 4, 2));
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_8, decimalSequence("-3000000", "100", 60_000, 8, 4));
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_17, decimalSequence("-30000000000", "1000000", 60_000, 17, 8));
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_18, decimalSequence("-30000000000", "1000000", 60_000, 18, 8));
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_38, decimalSequence("-3000000000000000000", "100000000000000", 60_000, 38, 16));
Random random = new Random(0);
List<SqlDecimal> values = new ArrayList<>();
values.add(new SqlDecimal(new BigInteger("9".repeat(18)), DECIMAL_TYPE_PRECISION_18.getPrecision(), DECIMAL_TYPE_PRECISION_18.getScale()));
values.add(new SqlDecimal(new BigInteger("-" + "9".repeat(18)), DECIMAL_TYPE_PRECISION_18.getPrecision(), DECIMAL_TYPE_PRECISION_18.getScale()));
BigInteger nextValue = BigInteger.ONE;
for (int i = 0; i < 59; i++) {
values.add(new SqlDecimal(nextValue, DECIMAL_TYPE_PRECISION_18.getPrecision(), DECIMAL_TYPE_PRECISION_18.getScale()));
values.add(new SqlDecimal(nextValue.negate(), DECIMAL_TYPE_PRECISION_18.getPrecision(), DECIMAL_TYPE_PRECISION_18.getScale()));
nextValue = nextValue.multiply(BigInteger.valueOf(2));
}
for (int i = 0; i < 100_000; ++i) {
BigInteger value = new BigInteger(59, random);
if (random.nextBoolean()) {
value = value.negate();
}
values.add(new SqlDecimal(value, DECIMAL_TYPE_PRECISION_18.getPrecision(), DECIMAL_TYPE_PRECISION_18.getScale()));
}
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_18, values);
random = new Random(0);
values = new ArrayList<>();
values.add(new SqlDecimal(new BigInteger("9".repeat(38)), DECIMAL_TYPE_PRECISION_38.getPrecision(), DECIMAL_TYPE_PRECISION_38.getScale()));
values.add(new SqlDecimal(new BigInteger("-" + "9".repeat(38)), DECIMAL_TYPE_PRECISION_38.getPrecision(), DECIMAL_TYPE_PRECISION_38.getScale()));
nextValue = BigInteger.ONE;
for (int i = 0; i < 127; i++) {
values.add(new SqlDecimal(nextValue, 38, 16));
values.add(new SqlDecimal(nextValue.negate(), 38, 16));
nextValue = nextValue.multiply(BigInteger.valueOf(2));
}
for (int i = 0; i < 100_000; ++i) {
BigInteger value = new BigInteger(126, random);
if (random.nextBoolean()) {
value = value.negate();
}
values.add(new SqlDecimal(value, DECIMAL_TYPE_PRECISION_38.getPrecision(), DECIMAL_TYPE_PRECISION_38.getScale()));
}
tester.testRoundTrip(DECIMAL_TYPE_PRECISION_38, values);
}
use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class TestExpressionCompiler method smokedTest.
@Test
public void smokedTest() throws Exception {
assertExecute("cast(true as boolean)", BOOLEAN, true);
assertExecute("true", BOOLEAN, true);
assertExecute("false", BOOLEAN, false);
assertExecute("42", INTEGER, 42);
assertExecute("'foo'", createVarcharType(3), "foo");
assertExecute("4.2E0", DOUBLE, 4.2);
assertExecute("10000000000 + 1", BIGINT, 10000000001L);
assertExecute("4.2", createDecimalType(2, 1), new SqlDecimal(BigInteger.valueOf(42), 2, 1));
assertExecute("DECIMAL '4.2'", createDecimalType(2, 1), new SqlDecimal(BigInteger.valueOf(42), 2, 1));
assertExecute("X' 1 f'", VARBINARY, sqlVarbinary(0x1F));
assertExecute("X' '", VARBINARY, sqlVarbinary());
assertExecute("bound_integer", INTEGER, 1234);
assertExecute("bound_long", BIGINT, 1234L);
assertExecute("bound_string", VARCHAR, "hello");
assertExecute("bound_double", DOUBLE, 12.34);
assertExecute("bound_boolean", BOOLEAN, true);
assertExecute("bound_timestamp", BIGINT, new DateTime(2001, 8, 22, 3, 4, 5, 321, UTC).getMillis());
assertExecute("bound_pattern", VARCHAR, "%el%");
assertExecute("bound_null_string", VARCHAR, null);
assertExecute("bound_timestamp_with_timezone", TIMESTAMP_WITH_TIME_ZONE, SqlTimestampWithTimeZone.newInstance(3, new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), 0, TimeZoneKey.getTimeZoneKey("Z")));
assertExecute("bound_binary_literal", VARBINARY, sqlVarbinary(0xAB));
// todo enable when null output type is supported
// assertExecute("null", null);
Futures.allAsList(futures).get();
}
use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class TestExpressionCompiler method assertExecute.
private void assertExecute(List<String> expressions, BigDecimal decimal) {
Type type = getDecimalType(decimal);
SqlDecimal value = decimal == null ? null : new SqlDecimal(decimal.unscaledValue(), decimal.precision(), decimal.scale());
for (String expression : expressions) {
assertExecute(expression, type, value);
}
}
Aggregations