Search in sources :

Example 1 with DecodedTimestamp

use of io.trino.plugin.base.type.DecodedTimestamp in project trino by trinodb.

the class SerDeUtils method serializePrimitive.

private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
    requireNonNull(builder, "builder is null");
    if (object == null) {
        builder.appendNull();
        return;
    }
    switch(inspector.getPrimitiveCategory()) {
        case BOOLEAN:
            type.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
            return;
        case BYTE:
            type.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
            return;
        case SHORT:
            type.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
            return;
        case INT:
            type.writeLong(builder, ((IntObjectInspector) inspector).get(object));
            return;
        case LONG:
            type.writeLong(builder, ((LongObjectInspector) inspector).get(object));
            return;
        case FLOAT:
            type.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
            return;
        case DOUBLE:
            type.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
            return;
        case STRING:
            type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case VARCHAR:
            type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
            return;
        case CHAR:
            HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
            type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), ((CharType) type).getLength()));
            return;
        case DATE:
            type.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
            return;
        case TIMESTAMP:
            TimestampType timestampType = (TimestampType) type;
            DecodedTimestamp timestamp = formatTimestamp(timestampType, object, (TimestampObjectInspector) inspector);
            createTimestampEncoder(timestampType, DateTimeZone.UTC).write(timestamp, builder);
            return;
        case BINARY:
            type.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case DECIMAL:
            DecimalType decimalType = (DecimalType) type;
            HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
            if (decimalType.isShort()) {
                type.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
            } else {
                type.writeObject(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
            }
            return;
        case VOID:
        case TIMESTAMPLOCALTZ:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_DAY_TIME:
        case UNKNOWN:
    }
    throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) CharType(io.trino.spi.type.CharType) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)

Example 2 with DecodedTimestamp

use of io.trino.plugin.base.type.DecodedTimestamp in project trino by trinodb.

the class TimestampEncoding method decodeValueInto.

@Override
public void decodeValueInto(int depth, BlockBuilder builder, Slice slice, int offset, int length) {
    DecodedTimestamp decodedTimestamp = parseTimestamp(slice, offset, length);
    trinoTimestampEncoder.write(decodedTimestamp, builder);
}
Also used : DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp)

Example 3 with DecodedTimestamp

use of io.trino.plugin.base.type.DecodedTimestamp in project trino by trinodb.

the class TimestampEncoding method decodeColumn.

@Override
public Block decodeColumn(ColumnData columnData) {
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);
    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int offset = columnData.getOffset(i);
        int length = columnData.getLength(i);
        if (length == 0 || nullSequence.equals(0, nullSequence.length(), slice, offset, length)) {
            builder.appendNull();
        } else {
            DecodedTimestamp decodedTimestamp = parseTimestamp(slice, offset, length);
            trinoTimestampEncoder.write(decodedTimestamp, builder);
        }
    }
    return builder.build();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(io.trino.spi.block.BlockBuilder) DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp)

Example 4 with DecodedTimestamp

use of io.trino.plugin.base.type.DecodedTimestamp in project trino by trinodb.

the class TimestampEncoding method parseTimestamp.

private static DecodedTimestamp parseTimestamp(Slice slice, int offset, int length) {
    String timestamp = new String(slice.getBytes(offset, length), US_ASCII);
    LocalDateTime localDateTime = LocalDateTime.parse(timestamp, HIVE_TIMESTAMP_PARSER);
    return new DecodedTimestamp(localDateTime.toEpochSecond(ZoneOffset.UTC), localDateTime.getNano());
}
Also used : LocalDateTime(java.time.LocalDateTime) DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp)

Example 5 with DecodedTimestamp

use of io.trino.plugin.base.type.DecodedTimestamp in project trino by trinodb.

the class TimestampEncoding method decodeColumn.

@Override
public Block decodeColumn(ColumnData columnData) {
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);
    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int length = columnData.getLength(i);
        if (length != 0) {
            int offset = columnData.getOffset(i);
            DecodedTimestamp decodedTimestamp = getTimestamp(slice, offset);
            trinoTimestampEncoder.write(decodedTimestamp, builder);
        } else {
            builder.appendNull();
        }
    }
    return builder.build();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(io.trino.spi.block.BlockBuilder) DecodedTimestamp(io.trino.plugin.base.type.DecodedTimestamp)

Aggregations

DecodedTimestamp (io.trino.plugin.base.type.DecodedTimestamp)11 DecimalType (io.trino.spi.type.DecimalType)3 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)3 Slice (io.airlift.slice.Slice)2 BlockBuilder (io.trino.spi.block.BlockBuilder)2 VarcharType (io.trino.spi.type.VarcharType)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Instant (java.time.Instant)2 LocalDate (java.time.LocalDate)2 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)2 BinaryStatistics (org.apache.parquet.column.statistics.BinaryStatistics)2 DoubleStatistics (org.apache.parquet.column.statistics.DoubleStatistics)2 FloatStatistics (org.apache.parquet.column.statistics.FloatStatistics)2 IntStatistics (org.apache.parquet.column.statistics.IntStatistics)2 LongStatistics (org.apache.parquet.column.statistics.LongStatistics)2 LogicalTypeAnnotation (org.apache.parquet.schema.LogicalTypeAnnotation)2 ParquetTimestampUtils.decodeInt96Timestamp (io.trino.parquet.ParquetTimestampUtils.decodeInt96Timestamp)1 CharType (io.trino.spi.type.CharType)1 TimestampType (io.trino.spi.type.TimestampType)1