use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class AbstractTestHiveFileFormats method checkPageSource.
protected void checkPageSource(ConnectorPageSource pageSource, List<TestColumn> testColumns, List<Type> types, int rowCount) throws IOException {
try {
MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, types);
assertEquals(result.getMaterializedRows().size(), rowCount);
for (MaterializedRow row : result) {
for (int i = 0, testColumnsSize = testColumns.size(); i < testColumnsSize; i++) {
TestColumn testColumn = testColumns.get(i);
Type type = types.get(i);
Object actualValue = row.getField(i);
Object expectedValue = testColumn.getExpectedValue();
if (expectedValue instanceof Slice) {
expectedValue = ((Slice) expectedValue).toStringUtf8();
}
if (actualValue == null || expectedValue == null) {
assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getTypeName().equals("float")) {
assertEquals((float) actualValue, (float) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getTypeName().equals("double")) {
assertEquals((double) actualValue, (double) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getTypeName().equals("date")) {
SqlDate expectedDate = new SqlDate(((Long) expectedValue).intValue());
assertEquals(actualValue, expectedDate, "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getTypeName().equals("int") || testColumn.getObjectInspector().getTypeName().equals("smallint") || testColumn.getObjectInspector().getTypeName().equals("tinyint")) {
assertEquals(actualValue, expectedValue);
} else if (testColumn.getObjectInspector().getTypeName().equals("timestamp")) {
SqlTimestamp expectedTimestamp = sqlTimestampOf(floorDiv((Long) expectedValue, MICROSECONDS_PER_MILLISECOND));
assertEquals(actualValue, expectedTimestamp, "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getTypeName().startsWith("char")) {
assertEquals(actualValue, padSpaces((String) expectedValue, (CharType) type), "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getCategory() == Category.PRIMITIVE) {
if (expectedValue instanceof Slice) {
expectedValue = ((Slice) expectedValue).toStringUtf8();
}
if (actualValue instanceof Slice) {
actualValue = ((Slice) actualValue).toStringUtf8();
}
if (actualValue instanceof SqlVarbinary) {
actualValue = new String(((SqlVarbinary) actualValue).getBytes(), UTF_8);
}
if (actualValue instanceof SqlDecimal) {
actualValue = new BigDecimal(actualValue.toString());
}
assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
} else {
BlockBuilder builder = type.createBlockBuilder(null, 1);
type.writeObject(builder, expectedValue);
expectedValue = type.getObjectValue(SESSION, builder.build(), 0);
assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
}
}
}
} finally {
pageSource.close();
}
}
use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class AbstractTestParquetReader method testDecimalBackedByFixedLenByteArray.
@Test
public void testDecimalBackedByFixedLenByteArray() throws Exception {
for (int precision = 1; precision < MAX_PRECISION; precision++) {
int scale = ThreadLocalRandom.current().nextInt(precision);
ImmutableList.Builder<SqlDecimal> expectedValues = ImmutableList.builder();
ImmutableList.Builder<SqlDecimal> expectedValuesMaxPrecision = ImmutableList.builder();
ImmutableList.Builder<HiveDecimal> writeValues = ImmutableList.builder();
BigInteger start = BigDecimal.valueOf(Math.pow(10, precision - 1)).negate().toBigInteger();
BigInteger end = BigDecimal.valueOf(Math.pow(10, precision)).toBigInteger();
BigInteger step = BigInteger.valueOf(1).max(end.subtract(start).divide(BigInteger.valueOf(1_000)));
for (BigInteger value = start; value.compareTo(end) < 0; value = value.add(step)) {
writeValues.add(HiveDecimal.create(value, scale));
expectedValues.add(new SqlDecimal(value, precision, scale));
expectedValuesMaxPrecision.add(new SqlDecimal(value, MAX_PRECISION, scale));
}
tester.testRoundTrip(new JavaHiveDecimalObjectInspector(new DecimalTypeInfo(precision, scale)), writeValues.build(), expectedValues.build(), createDecimalType(precision, scale));
tester.testRoundTrip(new JavaHiveDecimalObjectInspector(new DecimalTypeInfo(precision, scale)), writeValues.build(), expectedValuesMaxPrecision.build(), createDecimalType(MAX_PRECISION, scale));
}
}
use of io.trino.spi.type.SqlDecimal in project trino by trinodb.
the class KuduPageSink method appendColumn.
private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel) {
Block block = page.getBlock(channel);
Type type = columnTypes.get(destChannel);
if (block.isNull(position)) {
row.setNull(destChannel);
} else if (TIMESTAMP_MILLIS.equals(type)) {
row.addLong(destChannel, truncateEpochMicrosToMillis(type.getLong(block, position)));
} else if (REAL.equals(type)) {
row.addFloat(destChannel, intBitsToFloat(toIntExact(type.getLong(block, position))));
} else if (BIGINT.equals(type)) {
row.addLong(destChannel, type.getLong(block, position));
} else if (INTEGER.equals(type)) {
row.addInt(destChannel, toIntExact(type.getLong(block, position)));
} else if (SMALLINT.equals(type)) {
row.addShort(destChannel, Shorts.checkedCast(type.getLong(block, position)));
} else if (TINYINT.equals(type)) {
row.addByte(destChannel, SignedBytes.checkedCast(type.getLong(block, position)));
} else if (BOOLEAN.equals(type)) {
row.addBoolean(destChannel, type.getBoolean(block, position));
} else if (DOUBLE.equals(type)) {
row.addDouble(destChannel, type.getDouble(block, position));
} else if (type instanceof VarcharType) {
Type originalType = originalColumnTypes.get(destChannel);
if (DATE.equals(originalType)) {
SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession, block, position);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
row.addStringUtf8(destChannel, bytes);
} else {
row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
}
} else if (VARBINARY.equals(type)) {
row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
} else if (type instanceof DecimalType) {
SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession, block, position);
row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
} else {
throw new UnsupportedOperationException("Type is not supported: " + type);
}
}
Aggregations