Search in sources :

Example 16 with SqlDate

use of io.trino.spi.type.SqlDate 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

SqlDate (io.trino.spi.type.SqlDate)16 DecimalType (io.trino.spi.type.DecimalType)7 ImmutableList (com.google.common.collect.ImmutableList)6 SqlDecimal (io.trino.spi.type.SqlDecimal)6 SqlVarbinary (io.trino.spi.type.SqlVarbinary)6 Type (io.trino.spi.type.Type)6 VarcharType (io.trino.spi.type.VarcharType)6 List (java.util.List)6 Slice (io.airlift.slice.Slice)5 SqlTimestamp (io.trino.spi.type.SqlTimestamp)5 ArrayList (java.util.ArrayList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Block (io.trino.spi.block.Block)4 Map (java.util.Map)4 Collectors.toList (java.util.stream.Collectors.toList)4 DateTime (org.joda.time.DateTime)4 Test (org.testng.annotations.Test)4 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 BlockBuilder (io.trino.spi.block.BlockBuilder)3