Search in sources :

Example 1 with SqlTime

use of com.facebook.presto.common.type.SqlTime in project presto by prestodb.

the class TestShardWriter method testWriter.

@Test(dataProvider = "useOptimizedOrcWriter")
public void testWriter(boolean useOptimizedOrcWriter) throws Exception {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    List<Long> columnIds = ImmutableList.of(1L, 2L, 4L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L);
    ArrayType arrayType = new ArrayType(BIGINT);
    ArrayType arrayOfArrayType = new ArrayType(arrayType);
    Type mapType = functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(createVarcharType(10).getTypeSignature()), TypeSignatureParameter.of(BOOLEAN.getTypeSignature())));
    List<Type> columnTypes = ImmutableList.of(BIGINT, createVarcharType(10), VARBINARY, DOUBLE, BOOLEAN, arrayType, mapType, arrayOfArrayType, TIMESTAMP, TIME, DATE);
    File file = new File(directory, System.nanoTime() + ".orc");
    byte[] bytes1 = octets(0x00, 0xFE, 0xFF);
    byte[] bytes3 = octets(0x01, 0x02, 0x19, 0x80);
    long timestampValue = sqlTimestampOf(2002, 4, 6, 7, 8, 9, 0, UTC, UTC_KEY, SESSION).getMillisUtc();
    long timeValue = new SqlTime(NANOSECONDS.toMillis(new DateTime(2004, 11, 29, 0, 0, 0, 0, UTC).toLocalTime().getMillisOfDay())).getMillis();
    DateTime date = new DateTime(2001, 11, 22, 0, 0, 0, 0, UTC);
    int dateValue = new SqlDate(Days.daysBetween(new DateTime(0, ISOChronology.getInstanceUTC()), date).getDays()).getDays();
    RowPagesBuilder rowPagesBuilder = RowPagesBuilder.rowPagesBuilder(columnTypes).row(123L, "hello", wrappedBuffer(bytes1), 123.456, true, arrayBlockOf(BIGINT, 1, 2), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5)), timestampValue, timeValue, dateValue).row(null, "world", null, Double.POSITIVE_INFINITY, null, arrayBlockOf(BIGINT, 3, null), mapBlockOf(createVarcharType(5), BOOLEAN, "k2", null), arrayBlockOf(arrayType, null, arrayBlockOf(BIGINT, 6, 7)), timestampValue, timeValue, dateValue).row(456L, "bye \u2603", wrappedBuffer(bytes3), Double.NaN, false, arrayBlockOf(BIGINT), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", false), arrayBlockOf(arrayType, arrayBlockOf(BIGINT)), timestampValue, timeValue, dateValue);
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(new EmptyClassLoader());
        FileWriter writer = createFileWriter(columnIds, columnTypes, file)) {
        writer.appendPages(rowPagesBuilder.build());
    }
    try (OrcDataSource dataSource = fileOrcDataSource(file)) {
        OrcBatchRecordReader reader = createReader(dataSource, columnIds, columnTypes);
        assertEquals(reader.getReaderRowCount(), 3);
        assertEquals(reader.getReaderPosition(), 0);
        assertEquals(reader.getFileRowCount(), reader.getReaderRowCount());
        assertEquals(reader.getFilePosition(), reader.getFilePosition());
        assertEquals(reader.nextBatch(), 3);
        assertEquals(reader.getReaderPosition(), 0);
        assertEquals(reader.getFilePosition(), reader.getFilePosition());
        Block column0 = reader.readBlock(0);
        assertEquals(column0.isNull(0), false);
        assertEquals(column0.isNull(1), true);
        assertEquals(column0.isNull(2), false);
        assertEquals(BIGINT.getLong(column0, 0), 123L);
        assertEquals(BIGINT.getLong(column0, 2), 456L);
        Block column1 = reader.readBlock(1);
        assertEquals(createVarcharType(10).getSlice(column1, 0), utf8Slice("hello"));
        assertEquals(createVarcharType(10).getSlice(column1, 1), utf8Slice("world"));
        assertEquals(createVarcharType(10).getSlice(column1, 2), utf8Slice("bye \u2603"));
        Block column2 = reader.readBlock(2);
        assertEquals(VARBINARY.getSlice(column2, 0), wrappedBuffer(bytes1));
        assertEquals(column2.isNull(1), true);
        assertEquals(VARBINARY.getSlice(column2, 2), wrappedBuffer(bytes3));
        Block column3 = reader.readBlock(3);
        assertEquals(column3.isNull(0), false);
        assertEquals(column3.isNull(1), false);
        assertEquals(column3.isNull(2), false);
        assertEquals(DOUBLE.getDouble(column3, 0), 123.456);
        assertEquals(DOUBLE.getDouble(column3, 1), Double.POSITIVE_INFINITY);
        assertEquals(DOUBLE.getDouble(column3, 2), Double.NaN);
        Block column4 = reader.readBlock(4);
        assertEquals(column4.isNull(0), false);
        assertEquals(column4.isNull(1), true);
        assertEquals(column4.isNull(2), false);
        assertEquals(BOOLEAN.getBoolean(column4, 0), true);
        assertEquals(BOOLEAN.getBoolean(column4, 2), false);
        Block column5 = reader.readBlock(5);
        assertEquals(column5.getPositionCount(), 3);
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column5, 0), arrayBlockOf(BIGINT, 1, 2)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column5, 1), arrayBlockOf(BIGINT, 3, null)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column5, 2), arrayBlockOf(BIGINT)));
        Block column6 = reader.readBlock(6);
        assertEquals(column6.getPositionCount(), 3);
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column6, 0), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true)));
        Block object = arrayType.getObject(column6, 1);
        Block k2 = mapBlockOf(createVarcharType(5), BOOLEAN, "k2", null);
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, object, k2));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column6, 2), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", false)));
        Block column7 = reader.readBlock(7);
        assertEquals(column7.getPositionCount(), 3);
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column7, 0), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column7, 1), arrayBlockOf(arrayType, null, arrayBlockOf(BIGINT, 6, 7))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column7, 2), arrayBlockOf(arrayType, arrayBlockOf(BIGINT))));
        Block column8 = reader.readBlock(8);
        assertEquals(TIMESTAMP.getLong(column8, 0), timestampValue);
        assertEquals(TIMESTAMP.getLong(column8, 1), timestampValue);
        assertEquals(TIMESTAMP.getLong(column8, 2), timestampValue);
        Block column9 = reader.readBlock(9);
        assertEquals(TIME.getLong(column9, 0), timeValue);
        assertEquals(TIME.getLong(column9, 1), timeValue);
        assertEquals(TIME.getLong(column9, 2), timeValue);
        Block column10 = reader.readBlock(10);
        assertEquals(DATE.getLong(column10, 0), dateValue);
        assertEquals(DATE.getLong(column10, 1), dateValue);
        assertEquals(DATE.getLong(column10, 2), dateValue);
        assertEquals(reader.nextBatch(), -1);
        assertEquals(reader.getReaderPosition(), 3);
        assertEquals(reader.getFilePosition(), reader.getFilePosition());
        OrcFileMetadata orcFileMetadata = METADATA_CODEC.fromJson(reader.getUserMetadata().get(OrcFileMetadata.KEY).getBytes());
        assertEquals(orcFileMetadata, new OrcFileMetadata(ImmutableMap.<Long, TypeSignature>builder().put(1L, BIGINT.getTypeSignature()).put(2L, createVarcharType(10).getTypeSignature()).put(4L, VARBINARY.getTypeSignature()).put(6L, DOUBLE.getTypeSignature()).put(7L, BOOLEAN.getTypeSignature()).put(8L, arrayType.getTypeSignature()).put(9L, mapType.getTypeSignature()).put(10L, arrayOfArrayType.getTypeSignature()).put(11L, TIMESTAMP.getTypeSignature()).put(12L, TIME.getTypeSignature()).put(13L, DATE.getTypeSignature()).build()));
    }
    File crcFile = new File(file.getParentFile(), "." + file.getName() + ".crc");
    assertFalse(crcFile.exists());
    // Test unsupported types
    for (Type type : ImmutableList.of(TIMESTAMP_WITH_TIME_ZONE, RowType.anonymous(ImmutableList.of(BIGINT, DOUBLE)))) {
        try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(new EmptyClassLoader())) {
            createFileWriter(ImmutableList.of(1L), ImmutableList.of(type), file);
            fail();
        } catch (PrestoException e) {
            assertTrue(e.getMessage().toLowerCase(ENGLISH).contains("type"));
        }
    }
}
Also used : OrcDataSource(com.facebook.presto.orc.OrcDataSource) OrcTestingUtil.fileOrcDataSource(com.facebook.presto.raptor.storage.OrcTestingUtil.fileOrcDataSource) RowPagesBuilder(com.facebook.presto.RowPagesBuilder) OrcBatchRecordReader(com.facebook.presto.orc.OrcBatchRecordReader) OrcTestingUtil.createFileWriter(com.facebook.presto.raptor.storage.OrcTestingUtil.createFileWriter) SqlTime(com.facebook.presto.common.type.SqlTime) PrestoException(com.facebook.presto.spi.PrestoException) DateTime(org.joda.time.DateTime) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) TypeSignature(com.facebook.presto.common.type.TypeSignature) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) SqlDate(com.facebook.presto.common.type.SqlDate) Block(com.facebook.presto.common.block.Block) File(java.io.File) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader) Test(org.testng.annotations.Test)

Example 2 with SqlTime

use of com.facebook.presto.common.type.SqlTime in project presto by prestodb.

the class LiteralInterpreter method evaluate.

public static Object evaluate(ConnectorSession session, ConstantExpression node) {
    Type type = node.getType();
    SqlFunctionProperties properties = session.getSqlFunctionProperties();
    if (node.getValue() == null) {
        return null;
    }
    if (type instanceof BooleanType) {
        return node.getValue();
    }
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        return node.getValue();
    }
    if (type instanceof DoubleType) {
        return node.getValue();
    }
    if (type instanceof RealType) {
        Long number = (Long) node.getValue();
        return intBitsToFloat(number.intValue());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(node.getValue() instanceof Long);
            return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
        }
        checkState(node.getValue() instanceof Slice);
        Slice value = (Slice) node.getValue();
        return decodeDecimal(decodeUnscaledValue(value), decimalType);
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return ((Slice) node.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return new SqlVarbinary(((Slice) node.getValue()).getBytes());
    }
    if (type instanceof DateType) {
        return new SqlDate(((Long) node.getValue()).intValue());
    }
    if (type instanceof TimeType) {
        if (properties.isLegacyTimestamp()) {
            return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
        }
        return new SqlTime((long) node.getValue());
    }
    if (type instanceof TimestampType) {
        try {
            if (properties.isLegacyTimestamp()) {
                return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
            }
            return new SqlTimestamp((long) node.getValue());
        } catch (RuntimeException e) {
            throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
        }
    }
    if (type instanceof IntervalDayTimeType) {
        return new SqlIntervalDayTime((long) node.getValue());
    }
    if (type instanceof IntervalYearMonthType) {
        return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
    }
    if (type.getJavaType().equals(Slice.class)) {
        // DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
        return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
    }
    // We should not fail at the moment; just return the raw value (block, regex, etc) to the user
    return node.getValue();
}
Also used : IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTime(com.facebook.presto.common.type.SqlTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TimeType(com.facebook.presto.common.type.TimeType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DoubleType(com.facebook.presto.common.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 3 with SqlTime

use of com.facebook.presto.common.type.SqlTime in project presto by prestodb.

the class TestDateTimeOperators method testTimeRepresentation.

@Test
public void testTimeRepresentation() {
    // PST -> PDT date
    testTimeRepresentationOnDate(new DateTime(2017, 3, 12, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
    testTimeRepresentationOnDate(new DateTime(2017, 3, 12, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
    // PDT -> PST date
    testTimeRepresentationOnDate(new DateTime(2017, 10, 4, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
    testTimeRepresentationOnDate(new DateTime(2017, 10, 4, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
    // PDT date
    testTimeRepresentationOnDate(new DateTime(2017, 6, 6, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
    testTimeRepresentationOnDate(new DateTime(2017, 6, 6, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
    // PST date
    testTimeRepresentationOnDate(new DateTime(2017, 11, 1, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
    testTimeRepresentationOnDate(new DateTime(2017, 11, 1, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
}
Also used : SqlTime(com.facebook.presto.common.type.SqlTime) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 4 with SqlTime

use of com.facebook.presto.common.type.SqlTime in project presto by prestodb.

the class AbstractRowEncoder method appendColumnValue.

@Override
public void appendColumnValue(Block block, int position) {
    checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
    Type type = columnHandles.get(currentColumnIndex).getType();
    if (block.isNull(position)) {
        appendNullValue();
    } else if (type == BOOLEAN) {
        appendBoolean(type.getBoolean(block, position));
    } else if (type == BIGINT) {
        appendLong(type.getLong(block, position));
    } else if (type == INTEGER) {
        appendInt(toIntExact(type.getLong(block, position)));
    } else if (type == SMALLINT) {
        appendShort(Shorts.checkedCast(type.getLong(block, position)));
    } else if (type == TINYINT) {
        appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
    } else if (type == DOUBLE) {
        appendDouble(type.getDouble(block, position));
    } else if (type == REAL) {
        appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
    } else if (isVarcharType(type)) {
        appendString(type.getSlice(block, position).toStringUtf8());
    } else if (isVarbinaryType(type)) {
        appendByteBuffer(type.getSlice(block, position).toByteBuffer());
    } else if (type == DATE) {
        appendSqlDate((SqlDate) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type == TIME) {
        appendSqlTime((SqlTime) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type == TIME_WITH_TIME_ZONE) {
        appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type instanceof TimestampType) {
        appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else if (type instanceof TimestampWithTimeZoneType) {
        appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
    } else {
        throw new UnsupportedOperationException(format("Column '%s' does not support 'null' value", columnHandles.get(currentColumnIndex).getName()));
    }
    currentColumnIndex++;
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarbinaryType.isVarbinaryType(com.facebook.presto.common.type.VarbinaryType.isVarbinaryType) Type(com.facebook.presto.common.type.Type) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) TimestampType(com.facebook.presto.common.type.TimestampType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) SqlTime(com.facebook.presto.common.type.SqlTime) TimestampType(com.facebook.presto.common.type.TimestampType) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp)

Example 5 with SqlTime

use of com.facebook.presto.common.type.SqlTime in project presto by prestodb.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillisUtc();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) SqlTime(com.facebook.presto.common.type.SqlTime) RowType(com.facebook.presto.common.type.RowType) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) Entry(java.util.Map.Entry) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(com.facebook.presto.common.type.CharType) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

SqlTime (com.facebook.presto.common.type.SqlTime)7 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)4 Type (com.facebook.presto.common.type.Type)4 SqlDate (com.facebook.presto.common.type.SqlDate)3 DateTime (org.joda.time.DateTime)3 Test (org.testng.annotations.Test)3 ArrayType (com.facebook.presto.common.type.ArrayType)2 CharType (com.facebook.presto.common.type.CharType)2 RowType (com.facebook.presto.common.type.RowType)2 TimestampType (com.facebook.presto.common.type.TimestampType)2 VarcharType (com.facebook.presto.common.type.VarcharType)2 PrestoException (com.facebook.presto.spi.PrestoException)2 ArrayList (java.util.ArrayList)2 RowPagesBuilder (com.facebook.presto.RowPagesBuilder)1 Block (com.facebook.presto.common.block.Block)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)1 BigintType (com.facebook.presto.common.type.BigintType)1 BooleanType (com.facebook.presto.common.type.BooleanType)1 DateType (com.facebook.presto.common.type.DateType)1