Search in sources :

Example 11 with SqlDecimal

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));
    }
}
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)

Example 12 with SqlDecimal

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));
    }
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) JavaHiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaHiveDecimalObjectInspector) ImmutableList(com.google.common.collect.ImmutableList) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) BigInteger(java.math.BigInteger) SqlDecimal(io.prestosql.spi.type.SqlDecimal) Test(org.testng.annotations.Test)

Example 13 with SqlDecimal

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());
}
Also used : SqlDecimal(io.prestosql.spi.type.SqlDecimal) BigDecimal(java.math.BigDecimal)

Example 14 with SqlDecimal

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

Example 15 with SqlDecimal

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();
    }
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) SqlDecimal(io.prestosql.spi.type.SqlDecimal) BigDecimal(java.math.BigDecimal) RowType(io.prestosql.spi.type.RowType) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) TimestampType(io.prestosql.spi.type.TimestampType) CharType(io.prestosql.spi.type.CharType) HiveTestUtils.mapType(io.prestosql.plugin.hive.HiveTestUtils.mapType) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) HiveUtil.isStructuralType(io.prestosql.plugin.hive.HiveUtil.isStructuralType) DateType(io.prestosql.spi.type.DateType) Slice(io.airlift.slice.Slice) SqlDate(io.prestosql.spi.type.SqlDate) SerDeUtils.serializeObject(io.prestosql.plugin.hive.util.SerDeUtils.serializeObject) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) MaterializedResult(io.prestosql.testing.MaterializedResult) MaterializedRow(io.prestosql.testing.MaterializedRow) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

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