Search in sources :

Example 71 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class AbstractTestAccumuloRowSerializer method testLong.

@Test
public void testLong() throws Exception {
    AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance();
    Type type = BIGINT;
    Long expected = 123456L;
    byte[] data = serializer.encode(type, expected);
    Long actual = serializer.decode(type, data);
    assertEquals(actual, expected);
    deserializeData(serializer, data);
    actual = serializer.getLong(COLUMN_NAME);
    assertEquals(actual, expected);
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) Test(org.testng.annotations.Test)

Example 72 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class AbstractTestAccumuloRowSerializer method testDouble.

@Test
public void testDouble() throws Exception {
    AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance();
    Type type = DOUBLE;
    Double expected = 123.45678;
    byte[] data = serializer.encode(type, expected);
    Double actual = serializer.decode(type, data);
    assertEquals(actual, expected);
    deserializeData(serializer, data);
    actual = serializer.getDouble(COLUMN_NAME);
    assertEquals(actual, expected);
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) Test(org.testng.annotations.Test)

Example 73 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class Row method fromString.

/**
     * Creates a new {@link Row} from the given delimited string based on the given {@link RowSchema}
     *
     * @param schema Row's schema
     * @param str String to parse
     * @param delimiter Delimiter of the string
     * @return A new Row
     * @throws PrestoException If the length of the split string is not equal to the length of the schema
     * @throws PrestoException If the schema contains an unsupported type
     */
public static Row fromString(RowSchema schema, String str, char delimiter) {
    Row row = new Row();
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    List<String> fields = builder.addAll(Splitter.on(delimiter).split(str)).build();
    if (fields.size() != schema.getLength()) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Number of split tokens is not equal to schema length. Expected %s received %s. Schema: %s, fields {%s}, delimiter %s", schema.getLength(), fields.size(), schema, StringUtils.join(fields, ","), delimiter));
    }
    for (int i = 0; i < fields.size(); ++i) {
        Type type = schema.getColumn(i).getType();
        row.addField(valueFromString(fields.get(i), type), type);
    }
    return row;
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) PrestoException(com.facebook.presto.spi.PrestoException)

Example 74 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class AccumuloRowSerializer method getBlockFromMap.

/**
     * Encodes the given map into a Block.
     *
     * @param mapType Presto type of the map
     * @param map Map of key/value pairs to encode
     * @return Presto Block
     */
static Block getBlockFromMap(Type mapType, Map<?, ?> map) {
    Type keyType = mapType.getTypeParameters().get(0);
    Type valueType = mapType.getTypeParameters().get(1);
    BlockBuilder builder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), map.size() * 2);
    for (Entry<?, ?> entry : map.entrySet()) {
        writeObject(builder, keyType, entry.getKey());
        writeObject(builder, valueType, entry.getValue());
    }
    return builder.build();
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 75 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class HiveCoercionRecordCursor method createCoercer.

private static Coercer createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType) {
    Type fromType = typeManager.getType(fromHiveType.getTypeSignature());
    Type toType = typeManager.getType(toHiveType.getTypeSignature());
    if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
        return new IntegerNumberToVarcharCoercer();
    } else if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
        return new VarcharToIntegerNumberCoercer(toHiveType);
    } else if (fromHiveType.equals(HIVE_BYTE) && toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    } else if (fromHiveType.equals(HIVE_SHORT) && toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    } else if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
        return new IntegerNumberUpscaleCoercer();
    } else if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
        return new FloatToDoubleCoercer();
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) PrestoException(com.facebook.presto.spi.PrestoException)

Aggregations

Type (com.facebook.presto.spi.type.Type)392 Test (org.testng.annotations.Test)103 Block (com.facebook.presto.spi.block.Block)83 ImmutableList (com.google.common.collect.ImmutableList)79 ArrayType (com.facebook.presto.type.ArrayType)78 MapType (com.facebook.presto.type.MapType)72 Page (com.facebook.presto.spi.Page)68 PrestoException (com.facebook.presto.spi.PrestoException)51 List (java.util.List)50 VarcharType (com.facebook.presto.spi.type.VarcharType)48 DecimalType (com.facebook.presto.spi.type.DecimalType)44 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)40 MaterializedResult (com.facebook.presto.testing.MaterializedResult)40 ArrayList (java.util.ArrayList)40 MethodHandle (java.lang.invoke.MethodHandle)39 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)37 ImmutableMap (com.google.common.collect.ImmutableMap)36 Map (java.util.Map)36 Slice (io.airlift.slice.Slice)35 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)30