Search in sources :

Example 11 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class TestSelectiveOrcReader 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;
}
Also used : Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) SqlDecimal(com.facebook.presto.common.type.SqlDecimal)

Example 12 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

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);
    }
}
Also used : VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) Type(com.facebook.presto.common.type.Type) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) SqlDecimal(com.facebook.presto.common.type.SqlDecimal)

Example 13 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) ArrayList(java.util.ArrayList) SqlTime(com.facebook.presto.common.type.SqlTime) SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) ZoneOffset(java.time.ZoneOffset) SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) SqlDate(com.facebook.presto.common.type.SqlDate)

Example 14 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillisUtc();
            type.writeLong(blockBuilder, millis);
        } else {
            String baseType = type.getTypeSignature().getBase();
            if (StandardTypes.ARRAY.equals(baseType)) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.MAP.equals(baseType)) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.ROW.equals(baseType)) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) RowType(com.facebook.presto.common.type.RowType) VarcharType(com.facebook.presto.common.type.VarcharType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) Entry(java.util.Map.Entry) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) OrcLazyObject(com.facebook.hive.orc.lazy.OrcLazyObject) CharType(com.facebook.presto.common.type.CharType) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 15 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

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));
    }
}
Also used : BigInteger(java.math.BigInteger) ImmutableList(com.google.common.collect.ImmutableList) CacheBuilder(com.google.common.cache.CacheBuilder) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) MessageType(org.apache.parquet.schema.MessageType) MessageTypeParser.parseMessageType(org.apache.parquet.schema.MessageTypeParser.parseMessageType) Test(org.testng.annotations.Test)

Aggregations

SqlDecimal (com.facebook.presto.common.type.SqlDecimal)21 ArrayList (java.util.ArrayList)10 DecimalType (com.facebook.presto.common.type.DecimalType)9 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)9 ImmutableList (com.google.common.collect.ImmutableList)9 BigInteger (java.math.BigInteger)9 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)7 Type (com.facebook.presto.common.type.Type)7 Slice (io.airlift.slice.Slice)7 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)6 ArrayType (com.facebook.presto.common.type.ArrayType)6 SqlDate (com.facebook.presto.common.type.SqlDate)6 List (java.util.List)6 Map (java.util.Map)6 RowType (com.facebook.presto.common.type.RowType)5 VarcharType (com.facebook.presto.common.type.VarcharType)5 HashMap (java.util.HashMap)5 CharType (com.facebook.presto.common.type.CharType)4 MapType (com.facebook.presto.common.type.MapType)4 ImmutableMap (com.google.common.collect.ImmutableMap)4