Search in sources :

Example 6 with SqlDecimal

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;
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LazyBinaryArray(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryArray) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) LazyPrimitive(org.apache.hadoop.hive.serde2.lazy.LazyPrimitive) StructObject(org.apache.hadoop.hive.serde2.StructObject) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) LazyMap(org.apache.hadoop.hive.serde2.lazy.LazyMap) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) BytesWritable(org.apache.hadoop.io.BytesWritable) SqlDecimal(io.prestosql.spi.type.SqlDecimal) Text(org.apache.hadoop.io.Text) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) FloatWritable(org.apache.hadoop.io.FloatWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) BigInteger(java.math.BigInteger) StructObject(org.apache.hadoop.hive.serde2.StructObject) LazyArray(org.apache.hadoop.hive.serde2.lazy.LazyArray) LazyBinaryMap(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryMap)

Example 7 with SqlDecimal

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

Example 8 with SqlDecimal

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());
}
Also used : Slice(io.airlift.slice.Slice) DecimalParseResult(io.prestosql.spi.type.DecimalParseResult) BigInteger(java.math.BigInteger) SqlDecimal(io.prestosql.spi.type.SqlDecimal)

Example 9 with SqlDecimal

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));
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SqlDecimal(io.prestosql.spi.type.SqlDecimal) MessageType(org.apache.parquet.schema.MessageType) MessageTypeParser.parseMessageType(org.apache.parquet.schema.MessageTypeParser.parseMessageType) Test(org.testng.annotations.Test)

Example 10 with SqlDecimal

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

Aggregations

SqlDecimal (io.prestosql.spi.type.SqlDecimal)24 BigInteger (java.math.BigInteger)13 ImmutableList (com.google.common.collect.ImmutableList)12 ArrayList (java.util.ArrayList)11 SqlVarbinary (io.prestosql.spi.type.SqlVarbinary)9 DecimalType (io.prestosql.spi.type.DecimalType)8 Test (org.testng.annotations.Test)8 Slice (io.airlift.slice.Slice)7 BlockBuilder (io.prestosql.spi.block.BlockBuilder)7 SqlDate (io.prestosql.spi.type.SqlDate)7 SqlTimestamp (io.prestosql.spi.type.SqlTimestamp)7 Type (io.prestosql.spi.type.Type)7 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)6 Map (java.util.Map)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 ArrayType (io.prestosql.spi.type.ArrayType)5 VarcharType (io.prestosql.spi.type.VarcharType)5 List (java.util.List)5 Collectors.toList (java.util.stream.Collectors.toList)5 BigDecimal (java.math.BigDecimal)4