use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
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);
}
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class StructuralTestUtil method appendToBlockBuilder.
public static void appendToBlockBuilder(Type type, Object element, BlockBuilder blockBuilder) {
Class<?> javaType = type.getJavaType();
if (element == null) {
blockBuilder.appendNull();
} else if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY) && element instanceof Iterable<?>) {
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Object subElement : (Iterable<?>) element) {
appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
}
blockBuilder.closeEntry();
} else if (type.getTypeSignature().getBase().equals(StandardTypes.ROW) && element instanceof Iterable<?>) {
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
int field = 0;
for (Object subElement : (Iterable<?>) element) {
appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
field++;
}
blockBuilder.closeEntry();
} else if (type.getTypeSignature().getBase().equals(StandardTypes.MAP) && element instanceof Map<?, ?>) {
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) element).entrySet()) {
appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
}
blockBuilder.closeEntry();
} else if (javaType == boolean.class) {
type.writeBoolean(blockBuilder, (Boolean) element);
} else if (javaType == long.class) {
if (element instanceof SqlDecimal) {
type.writeLong(blockBuilder, ((SqlDecimal) element).getUnscaledValue().longValue());
} else if (REAL.equals(type)) {
type.writeLong(blockBuilder, floatToRawIntBits(((Number) element).floatValue()));
} else {
type.writeLong(blockBuilder, ((Number) element).longValue());
}
} else if (javaType == double.class) {
type.writeDouble(blockBuilder, ((Number) element).doubleValue());
} else if (javaType == Slice.class) {
if (element instanceof String) {
type.writeSlice(blockBuilder, Slices.utf8Slice(element.toString()));
} else if (element instanceof byte[]) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) element));
} else if (element instanceof SqlDecimal) {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) element).getUnscaledValue()));
} else {
type.writeSlice(blockBuilder, (Slice) element);
}
} else {
type.writeObject(blockBuilder, element);
}
}
use of io.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
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)) {
return Timestamp.ofEpochMilli(((SqlTimestamp) value).getMillis());
}
if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
}
if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY)) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueHive(elementType, element)).collect(toList());
}
if (type.getTypeSignature().getBase().equals(StandardTypes.MAP)) {
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.getTypeSignature().getBase().equals(StandardTypes.ROW)) {
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.prestosql.spi.type.SqlDecimal in project hetu-core by openlookeng.
the class AbstractTestOrcReader 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 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(Strings.repeat("9", 18)), DECIMAL_TYPE_PRECISION_18.getPrecision(), DECIMAL_TYPE_PRECISION_18.getScale()));
values.add(new SqlDecimal(new BigInteger("-" + Strings.repeat("9", 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(Strings.repeat("9", 38)), DECIMAL_TYPE_PRECISION_38.getPrecision(), DECIMAL_TYPE_PRECISION_38.getScale()));
values.add(new SqlDecimal(new BigInteger("-" + Strings.repeat("9", 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);
}
Aggregations