Search in sources :

Example 16 with SqlDecimal

use of io.trino.spi.type.SqlDecimal in project trino by trinodb.

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(floorDiv((Long) expectedValue, MICROSECONDS_PER_MILLISECOND));
                    assertEquals(actualValue, expectedTimestamp, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().startsWith("char")) {
                    assertEquals(actualValue, padSpaces((String) expectedValue, (CharType) type), "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.trino.spi.type.SqlVarbinary) SqlTimestamp(io.trino.spi.type.SqlTimestamp) SqlDecimal(io.trino.spi.type.SqlDecimal) BigDecimal(java.math.BigDecimal) DateType(io.trino.spi.type.DateType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) TimestampType(io.trino.spi.type.TimestampType) HiveTestUtils.mapType(io.trino.plugin.hive.HiveTestUtils.mapType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) HiveUtil.isStructuralType(io.trino.plugin.hive.util.HiveUtil.isStructuralType) Slice(io.airlift.slice.Slice) SqlDate(io.trino.spi.type.SqlDate) SerDeUtils.serializeObject(io.trino.plugin.hive.util.SerDeUtils.serializeObject) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType) MaterializedResult(io.trino.testing.MaterializedResult) MaterializedRow(io.trino.testing.MaterializedRow) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 17 with SqlDecimal

use of io.trino.spi.type.SqlDecimal in project trino by trinodb.

the class AbstractTestParquetReader method testDecimalBackedByFixedLenByteArray.

@Test
public void testDecimalBackedByFixedLenByteArray() throws Exception {
    for (int precision = 1; precision < MAX_PRECISION; precision++) {
        int scale = ThreadLocalRandom.current().nextInt(precision);
        ImmutableList.Builder<SqlDecimal> expectedValues = ImmutableList.builder();
        ImmutableList.Builder<SqlDecimal> expectedValuesMaxPrecision = ImmutableList.builder();
        ImmutableList.Builder<HiveDecimal> writeValues = ImmutableList.builder();
        BigInteger start = BigDecimal.valueOf(Math.pow(10, precision - 1)).negate().toBigInteger();
        BigInteger end = BigDecimal.valueOf(Math.pow(10, precision)).toBigInteger();
        BigInteger step = BigInteger.valueOf(1).max(end.subtract(start).divide(BigInteger.valueOf(1_000)));
        for (BigInteger value = start; value.compareTo(end) < 0; value = value.add(step)) {
            writeValues.add(HiveDecimal.create(value, scale));
            expectedValues.add(new SqlDecimal(value, precision, scale));
            expectedValuesMaxPrecision.add(new SqlDecimal(value, MAX_PRECISION, scale));
        }
        tester.testRoundTrip(new JavaHiveDecimalObjectInspector(new DecimalTypeInfo(precision, scale)), writeValues.build(), expectedValues.build(), createDecimalType(precision, scale));
        tester.testRoundTrip(new JavaHiveDecimalObjectInspector(new DecimalTypeInfo(precision, scale)), writeValues.build(), expectedValuesMaxPrecision.build(), createDecimalType(MAX_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.trino.spi.type.SqlDecimal) Test(org.testng.annotations.Test)

Example 18 with SqlDecimal

use of io.trino.spi.type.SqlDecimal in project trino by trinodb.

the class KuduPageSink method appendColumn.

private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel) {
    Block block = page.getBlock(channel);
    Type type = columnTypes.get(destChannel);
    if (block.isNull(position)) {
        row.setNull(destChannel);
    } else if (TIMESTAMP_MILLIS.equals(type)) {
        row.addLong(destChannel, truncateEpochMicrosToMillis(type.getLong(block, position)));
    } else if (REAL.equals(type)) {
        row.addFloat(destChannel, intBitsToFloat(toIntExact(type.getLong(block, position))));
    } else if (BIGINT.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position));
    } else if (INTEGER.equals(type)) {
        row.addInt(destChannel, toIntExact(type.getLong(block, position)));
    } else if (SMALLINT.equals(type)) {
        row.addShort(destChannel, Shorts.checkedCast(type.getLong(block, position)));
    } else if (TINYINT.equals(type)) {
        row.addByte(destChannel, SignedBytes.checkedCast(type.getLong(block, position)));
    } else if (BOOLEAN.equals(type)) {
        row.addBoolean(destChannel, type.getBoolean(block, position));
    } else if (DOUBLE.equals(type)) {
        row.addDouble(destChannel, type.getDouble(block, position));
    } else if (type instanceof VarcharType) {
        Type originalType = originalColumnTypes.get(destChannel);
        if (DATE.equals(originalType)) {
            SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession, block, position);
            LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
            byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
            row.addStringUtf8(destChannel, bytes);
        } else {
            row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
        }
    } else if (VARBINARY.equals(type)) {
        row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
    } else if (type instanceof DecimalType) {
        SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession, block, position);
        row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
    } else {
        throw new UnsupportedOperationException("Type is not supported: " + type);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) VarcharType(io.trino.spi.type.VarcharType) SqlDate(io.trino.spi.type.SqlDate) Block(io.trino.spi.block.Block) DecimalType(io.trino.spi.type.DecimalType) SqlDecimal(io.trino.spi.type.SqlDecimal)

Aggregations

SqlDecimal (io.trino.spi.type.SqlDecimal)18 DecimalType (io.trino.spi.type.DecimalType)11 ArrayList (java.util.ArrayList)10 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayType (io.trino.spi.type.ArrayType)8 SqlVarbinary (io.trino.spi.type.SqlVarbinary)8 Type (io.trino.spi.type.Type)8 VarcharType (io.trino.spi.type.VarcharType)8 BigInteger (java.math.BigInteger)8 BlockBuilder (io.trino.spi.block.BlockBuilder)7 RowType (io.trino.spi.type.RowType)7 Slice (io.airlift.slice.Slice)6 SqlDate (io.trino.spi.type.SqlDate)6 SqlTimestamp (io.trino.spi.type.SqlTimestamp)6 List (java.util.List)6 Map (java.util.Map)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)5 MapType (io.trino.spi.type.MapType)5 HashMap (java.util.HashMap)5