use of io.prestosql.spi.type.SqlDecimal in project boostkit-bigdata by kunpengcompute.
the class AbstractTestParquetReader method testDecimalBackedByINT32.
@Test
public void testDecimalBackedByINT32() throws Exception {
for (int precision = 1; precision <= MAX_PRECISION_INT32; precision++) {
int scale = ThreadLocalRandom.current().nextInt(precision);
MessageType parquetSchema = parseMessageType(format("message hive_decimal { optional INT32 test (DECIMAL(%d, %d)); }", precision, scale));
ContiguousSet<Integer> intValues = intsBetween(1, 1_000);
ImmutableList.Builder<SqlDecimal> expectedValues = new ImmutableList.Builder<>();
for (Integer value : intValues) {
expectedValues.add(SqlDecimal.of(value, precision, scale));
}
tester.testRoundTrip(javaIntObjectInspector, intValues, expectedValues.build(), createDecimalType(precision, scale), Optional.of(parquetSchema));
}
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class AbstractTestParquetReader method testDecimalBackedByFixedLenByteArray.
@Test
public void testDecimalBackedByFixedLenByteArray() throws Exception {
for (int precision = MAX_PRECISION_INT64 + 1; precision < MAX_PRECISION; precision++) {
int scale = ThreadLocalRandom.current().nextInt(precision);
ContiguousSet<BigInteger> values = bigIntegersBetween(BigDecimal.valueOf(Math.pow(10, precision - 1)).toBigInteger(), BigDecimal.valueOf(Math.pow(10, precision)).toBigInteger());
ImmutableList.Builder<SqlDecimal> expectedValues = new ImmutableList.Builder<>();
ImmutableList.Builder<HiveDecimal> writeValues = new ImmutableList.Builder<>();
for (BigInteger value : limit(values, 1_000)) {
writeValues.add(HiveDecimal.create(value, scale));
expectedValues.add(new SqlDecimal(value, precision, scale));
}
tester.testRoundTrip(new JavaHiveDecimalObjectInspector(new DecimalTypeInfo(precision, scale)), writeValues.build(), expectedValues.build(), createDecimalType(precision, scale));
}
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class AbstractTestDecimalAverageAggregation method getExpectedValue.
@Override
protected SqlDecimal getExpectedValue(int start, int length) {
if (length == 0) {
return null;
}
BigDecimal avg = BigDecimal.ZERO;
for (int i = start; i < start + length; i++) {
avg = avg.add(getBigDecimalForCounter(i));
}
avg = avg.divide(BigDecimal.valueOf(length), ROUND_HALF_UP);
return new SqlDecimal(avg.unscaledValue(), avg.precision(), avg.scale());
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class AbstractTestRcFileReader method decimalSequence.
private static List<SqlDecimal> decimalSequence(String start, String step, int items, int precision, int scale) {
BigInteger decimalStep = new BigInteger(step);
List<SqlDecimal> values = new ArrayList<>();
BigInteger nextValue = new BigInteger(start);
for (int i = 0; i < items; i++) {
values.add(new SqlDecimal(nextValue, precision, scale));
nextValue = nextValue.add(decimalStep);
}
return values;
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
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((Long) expectedValue);
assertEquals(actualValue, expectedTimestamp, "Wrong value for column " + testColumn.getName());
} else if (testColumn.getObjectInspector().getTypeName().startsWith("char")) {
assertEquals(actualValue, padEnd((String) expectedValue, ((CharType) type).getLength(), ' '), "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();
}
}
Aggregations