Search in sources :

Example 21 with VarcharType

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

the class TestVariableWidthBlock method testGetSizeInBytes.

@Test
private void testGetSizeInBytes() {
    int numEntries = 1000;
    VarcharType unboundedVarcharType = createUnboundedVarcharType();
    VariableWidthBlockBuilder blockBuilder = new VariableWidthBlockBuilder(null, numEntries, 20 * numEntries);
    for (int i = 0; i < numEntries; i++) {
        unboundedVarcharType.writeString(blockBuilder, String.valueOf(ThreadLocalRandom.current().nextLong()));
    }
    Block block = blockBuilder.build();
    List<Block> splitQuarter = splitBlock(block, 4);
    long sizeInBytes = block.getSizeInBytes();
    long quarter1size = splitQuarter.get(0).getSizeInBytes();
    long quarter2size = splitQuarter.get(1).getSizeInBytes();
    long quarter3size = splitQuarter.get(2).getSizeInBytes();
    long quarter4size = splitQuarter.get(3).getSizeInBytes();
    double expectedQuarterSizeMin = sizeInBytes * 0.2;
    double expectedQuarterSizeMax = sizeInBytes * 0.3;
    assertTrue(quarter1size > expectedQuarterSizeMin && quarter1size < expectedQuarterSizeMax, format("quarter1size is %s, should be between %s and %s", quarter1size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter2size > expectedQuarterSizeMin && quarter2size < expectedQuarterSizeMax, format("quarter2size is %s, should be between %s and %s", quarter2size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter3size > expectedQuarterSizeMin && quarter3size < expectedQuarterSizeMax, format("quarter3size is %s, should be between %s and %s", quarter3size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter4size > expectedQuarterSizeMin && quarter4size < expectedQuarterSizeMax, format("quarter4size is %s, should be between %s and %s", quarter4size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertEquals(quarter1size + quarter2size + quarter3size + quarter4size, sizeInBytes);
}
Also used : VariableWidthBlockBuilder(com.facebook.presto.common.block.VariableWidthBlockBuilder) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) VariableWidthBlock(com.facebook.presto.common.block.VariableWidthBlock) Block(com.facebook.presto.common.block.Block) Test(org.testng.annotations.Test)

Example 22 with VarcharType

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

the class ParquetTestUtils method getHiveType.

private static String getHiveType(Type type) {
    if (type.equals(BOOLEAN) || type.equals(BIGINT) || type.equals(SmallintType.SMALLINT) || type.equals(TinyintType.TINYINT) || type.equals(DOUBLE)) {
        return type.getTypeSignature().toString();
    }
    if (type.equals(INTEGER)) {
        return "int";
    }
    if (type.equals(REAL)) {
        return "float";
    }
    if (type.equals(TIMESTAMP)) {
        return "timestamp";
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        return "varchar(" + varcharLength + ")";
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return format("decimal(%d,%d)", decimalType.getPrecision(), decimalType.getScale());
    }
    if (type instanceof ArrayType) {
        return "array<" + getHiveType(((ArrayType) type).getElementType()) + ">";
    }
    if (type instanceof RowType) {
        return "struct<" + Joiner.on(",").join(((RowType) type).getFields().stream().map(t -> t.getName().get() + ":" + getHiveType(t.getType())).collect(toList())) + ">";
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 23 with VarcharType

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

the class ParquetTestUtils method getObjectInspector.

private static ObjectInspector getObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return writableBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return writableLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return writableIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return writableShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return writableByteObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return writableDoubleObjectInspector;
    }
    if (type.equals(REAL)) {
        return writableFloatObjectInspector;
    }
    if (type.equals(TIMESTAMP)) {
        return writableTimestampObjectInspector;
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType || type instanceof RowType) {
        return getJavaObjectInspector(type);
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 24 with VarcharType

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

the class PartitionTransforms method getColumnTransform.

public static ColumnTransform getColumnTransform(PartitionField field, Type type) {
    String transform = field.transform().toString();
    if (transform.equals("identity")) {
        return new ColumnTransform(type, Function.identity());
    }
    Matcher matcher = BUCKET_PATTERN.matcher(transform);
    if (matcher.matches()) {
        int count = parseInt(matcher.group(1));
        if (type.equals(INTEGER)) {
            return new ColumnTransform(INTEGER, block -> bucketInteger(block, count));
        }
        if (type.equals(BIGINT)) {
            return new ColumnTransform(INTEGER, block -> bucketBigint(block, count));
        }
        if (isShortDecimal(type)) {
            DecimalType decimal = (DecimalType) type;
            return new ColumnTransform(INTEGER, block -> bucketShortDecimal(decimal, block, count));
        }
        if (isLongDecimal(type)) {
            DecimalType decimal = (DecimalType) type;
            return new ColumnTransform(INTEGER, block -> bucketLongDecimal(decimal, block, count));
        }
        if (type.equals(DATE)) {
            return new ColumnTransform(INTEGER, block -> bucketDate(block, count));
        }
        if (type instanceof VarcharType) {
            return new ColumnTransform(INTEGER, block -> bucketVarchar(block, count));
        }
        if (type.equals(VARBINARY)) {
            return new ColumnTransform(INTEGER, block -> bucketVarbinary(block, count));
        }
        throw new UnsupportedOperationException("Unsupported type for 'bucket': " + field);
    }
    matcher = TRUNCATE_PATTERN.matcher(transform);
    if (matcher.matches()) {
        int width = parseInt(matcher.group(1));
        if (type.equals(INTEGER)) {
            return new ColumnTransform(INTEGER, block -> truncateInteger(block, width));
        }
        if (type.equals(BIGINT)) {
            return new ColumnTransform(BIGINT, block -> truncateBigint(block, width));
        }
        if (isShortDecimal(type)) {
            DecimalType decimal = (DecimalType) type;
            return new ColumnTransform(type, block -> truncateShortDecimal(decimal, block, width));
        }
        if (isLongDecimal(type)) {
            DecimalType decimal = (DecimalType) type;
            return new ColumnTransform(type, block -> truncateLongDecimal(decimal, block, width));
        }
        if (type instanceof VarcharType) {
            return new ColumnTransform(VARCHAR, block -> truncateVarchar(block, width));
        }
        if (type.equals(VARBINARY)) {
            return new ColumnTransform(VARBINARY, block -> truncateVarbinary(block, width));
        }
        throw new UnsupportedOperationException("Unsupported type for 'truncate': " + field);
    }
    throw new UnsupportedOperationException("Unsupported partition transform: " + field);
}
Also used : Matcher(java.util.regex.Matcher) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint)

Example 25 with VarcharType

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

the class TestMathFunctions method testToBase.

@Test
public void testToBase() {
    VarcharType toBaseReturnType = VarcharType.createVarcharType(64);
    assertFunction("to_base(2147483648, 16)", toBaseReturnType, "80000000");
    assertFunction("to_base(255, 2)", toBaseReturnType, "11111111");
    assertFunction("to_base(-2147483647, 16)", toBaseReturnType, "-7fffffff");
    assertFunction("to_base(NULL, 16)", toBaseReturnType, null);
    assertFunction("to_base(-2147483647, NULL)", toBaseReturnType, null);
    assertFunction("to_base(NULL, NULL)", toBaseReturnType, null);
    assertInvalidFunction("to_base(255, 1)", "Radix must be between 2 and 36");
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) Test(org.testng.annotations.Test)

Aggregations

VarcharType (com.facebook.presto.common.type.VarcharType)48 DecimalType (com.facebook.presto.common.type.DecimalType)30 Type (com.facebook.presto.common.type.Type)26 CharType (com.facebook.presto.common.type.CharType)23 PrestoException (com.facebook.presto.spi.PrestoException)16 Slice (io.airlift.slice.Slice)16 ArrayType (com.facebook.presto.common.type.ArrayType)14 RowType (com.facebook.presto.common.type.RowType)13 MapType (com.facebook.presto.common.type.MapType)12 ArrayList (java.util.ArrayList)12 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 IntegerType (com.facebook.presto.common.type.IntegerType)10 TimestampType (com.facebook.presto.common.type.TimestampType)10 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)10 BigintType (com.facebook.presto.common.type.BigintType)9 BooleanType (com.facebook.presto.common.type.BooleanType)9 DoubleType (com.facebook.presto.common.type.DoubleType)9 RealType (com.facebook.presto.common.type.RealType)9 SmallintType (com.facebook.presto.common.type.SmallintType)9