use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class RcFileTester method decodeRecordReaderValue.
private static Object decodeRecordReaderValue(Format format, Type type, Object inputActualValue) {
Object actualValue = inputActualValue;
if (actualValue instanceof LazyPrimitive) {
actualValue = ((LazyPrimitive<?, ?>) actualValue).getWritableObject();
}
if (actualValue instanceof BooleanWritable) {
actualValue = ((BooleanWritable) actualValue).get();
} else if (actualValue instanceof ByteWritable) {
actualValue = ((ByteWritable) actualValue).get();
} else if (actualValue instanceof BytesWritable) {
actualValue = new SqlVarbinary(((BytesWritable) actualValue).copyBytes());
} else if (actualValue instanceof DateWritableV2) {
actualValue = new SqlDate(((DateWritableV2) actualValue).getDays());
} else if (actualValue instanceof DoubleWritable) {
actualValue = ((DoubleWritable) actualValue).get();
} else if (actualValue instanceof FloatWritable) {
actualValue = ((FloatWritable) actualValue).get();
} else if (actualValue instanceof IntWritable) {
actualValue = ((IntWritable) actualValue).get();
} else if (actualValue instanceof LongWritable) {
actualValue = ((LongWritable) actualValue).get();
} else if (actualValue instanceof ShortWritable) {
actualValue = ((ShortWritable) actualValue).get();
} else if (actualValue instanceof HiveDecimalWritable) {
DecimalType decimalType = (DecimalType) type;
HiveDecimalWritable writable = (HiveDecimalWritable) actualValue;
// writable messes with the scale so rescale the values to the Presto type
BigInteger rescaledValue = rescale(writable.getHiveDecimal().unscaledValue(), writable.getScale(), decimalType.getScale());
actualValue = new SqlDecimal(rescaledValue, decimalType.getPrecision(), decimalType.getScale());
} else if (actualValue instanceof Text) {
actualValue = actualValue.toString();
} else if (actualValue instanceof TimestampWritableV2) {
long millis = ((TimestampWritableV2) actualValue).getTimestamp().toEpochMilli();
if (format == Format.BINARY) {
millis = HIVE_STORAGE_TIME_ZONE.convertUTCToLocal(millis);
}
actualValue = sqlTimestampOf(millis);
} else if (actualValue instanceof StructObject) {
StructObject structObject = (StructObject) actualValue;
actualValue = decodeRecordReaderStruct(format, type, structObject.getFieldsAsList());
} else if (actualValue instanceof LazyBinaryArray) {
actualValue = decodeRecordReaderList(format, type, ((LazyBinaryArray) actualValue).getList());
} else if (actualValue instanceof LazyBinaryMap) {
actualValue = decodeRecordReaderMap(format, type, ((LazyBinaryMap) actualValue).getMap());
} else if (actualValue instanceof LazyArray) {
actualValue = decodeRecordReaderList(format, type, ((LazyArray) actualValue).getList());
} else if (actualValue instanceof LazyMap) {
actualValue = decodeRecordReaderMap(format, type, ((LazyMap) actualValue).getMap());
} else if (actualValue instanceof List) {
actualValue = decodeRecordReaderList(format, type, ((List<?>) actualValue));
}
return actualValue;
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class MaterializedResult method convertToTestTypes.
private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow) {
List<Object> convertedValues = new ArrayList<>();
for (int field = 0; field < prestoRow.getFieldCount(); field++) {
Object prestoValue = prestoRow.getField(field);
Object convertedValue;
if (prestoValue instanceof SqlDate) {
convertedValue = LocalDate.ofEpochDay(((SqlDate) prestoValue).getDays());
} else if (prestoValue instanceof SqlTime) {
convertedValue = DateTimeFormatter.ISO_LOCAL_TIME.parse(prestoValue.toString(), LocalTime::from);
} else if (prestoValue instanceof SqlTimeWithTimeZone) {
// Political timezone cannot be represented in OffsetTime and there isn't any better representation.
long millisUtc = ((SqlTimeWithTimeZone) prestoValue).getMillisUtc();
ZoneOffset zone = toZoneOffset(((SqlTimeWithTimeZone) prestoValue).getTimeZoneKey());
convertedValue = OffsetTime.of(LocalTime.ofNanoOfDay(MILLISECONDS.toNanos(millisUtc) + SECONDS.toNanos(zone.getTotalSeconds())), zone);
} else if (prestoValue instanceof SqlTimestamp) {
convertedValue = SqlTimestamp.JSON_FORMATTER.parse(prestoValue.toString(), LocalDateTime::from);
} else if (prestoValue instanceof SqlTimestampWithTimeZone) {
convertedValue = Instant.ofEpochMilli(((SqlTimestampWithTimeZone) prestoValue).getMillisUtc()).atZone(ZoneId.of(((SqlTimestampWithTimeZone) prestoValue).getTimeZoneKey().getId()));
} else if (prestoValue instanceof SqlDecimal) {
convertedValue = ((SqlDecimal) prestoValue).toBigDecimal();
} else {
convertedValue = prestoValue;
}
convertedValues.add(convertedValue);
}
return new MaterializedRow(prestoRow.getPrecision(), convertedValues);
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class AbstractTestFunctions method decimal.
protected static SqlDecimal decimal(String decimalString) {
DecimalParseResult parseResult = Decimals.parseIncludeLeadingZerosInPrecision(decimalString);
BigInteger unscaledValue;
if (parseResult.getType().isShort()) {
unscaledValue = BigInteger.valueOf((Long) parseResult.getObject());
} else {
unscaledValue = Decimals.decodeUnscaledValue((Slice) parseResult.getObject());
}
return new SqlDecimal(unscaledValue, parseResult.getType().getPrecision(), parseResult.getType().getScale());
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class AbstractTestParquetReader method testDecimalBackedByINT64.
@Test
public void testDecimalBackedByINT64() throws Exception {
for (int precision = MAX_PRECISION_INT32 + 1; precision <= MAX_PRECISION_INT64; precision++) {
int scale = ThreadLocalRandom.current().nextInt(precision);
MessageType parquetSchema = parseMessageType(format("message hive_decimal { optional INT64 test (DECIMAL(%d, %d)); }", precision, scale));
ContiguousSet<Long> longValues = longsBetween(1, 1_000);
ImmutableList.Builder<SqlDecimal> expectedValues = new ImmutableList.Builder<>();
for (Long value : longValues) {
expectedValues.add(SqlDecimal.of(value, precision, scale));
}
tester.testRoundTrip(javaLongObjectInspector, longValues, 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 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));
}
}
Aggregations