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);
}
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);
}
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);
}
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);
}
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");
}
Aggregations