Search in sources :

Example 6 with VarcharType

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

the class PinotBrokerPageSourceBase method setValue.

protected void setValue(Type type, BlockBuilder blockBuilder, String value) {
    if (blockBuilder == null) {
        return;
    }
    if (value == null) {
        blockBuilder.appendNull();
        return;
    }
    if (!(type instanceof FixedWidthType) && !(type instanceof VarcharType) && !(type instanceof JsonType)) {
        throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
    }
    if (type instanceof FixedWidthType) {
        completedBytes += ((FixedWidthType) type).getFixedSize();
        if (type instanceof BigintType) {
            type.writeLong(blockBuilder, parseDouble(value).longValue());
        } else if (type instanceof IntegerType) {
            blockBuilder.writeInt(parseDouble(value).intValue());
        } else if (type instanceof TinyintType) {
            blockBuilder.writeByte(parseDouble(value).byteValue());
        } else if (type instanceof SmallintType) {
            blockBuilder.writeShort(parseDouble(value).shortValue());
        } else if (type instanceof BooleanType) {
            type.writeBoolean(blockBuilder, parseBoolean(value));
        } else if (type instanceof DecimalType || type instanceof DoubleType) {
            type.writeDouble(blockBuilder, parseDouble(value));
        } else if (type instanceof TimestampType) {
            type.writeLong(blockBuilder, parseTimestamp(value));
        } else if (type instanceof DateType) {
            type.writeLong(blockBuilder, parseLong(value));
        } else {
            throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
        }
    } else {
        Slice slice = Slices.utf8Slice(value);
        blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
        completedBytes += slice.length();
    }
}
Also used : JsonType(com.facebook.presto.common.type.JsonType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) FixedWidthType(com.facebook.presto.common.type.FixedWidthType)

Example 7 with VarcharType

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

the class Row method nativeContainerToOrcValue.

private static Object nativeContainerToOrcValue(Type type, Object nativeValue) {
    if (nativeValue == null) {
        return null;
    }
    if (type instanceof DecimalType) {
        BigInteger unscaledValue;
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            unscaledValue = BigInteger.valueOf((long) nativeValue);
        } else {
            unscaledValue = Decimals.decodeUnscaledValue((Slice) nativeValue);
        }
        return HiveDecimal.create(unscaledValue, decimalType.getScale());
    }
    if (type.getJavaType() == boolean.class) {
        return nativeValue;
    }
    if (type.getJavaType() == long.class) {
        return nativeValue;
    }
    if (type.getJavaType() == double.class) {
        return nativeValue;
    }
    if (type.getJavaType() == Slice.class) {
        Slice slice = (Slice) nativeValue;
        return type instanceof VarcharType ? slice.toStringUtf8() : slice.getBytes();
    }
    if (isArrayType(type)) {
        Block arrayBlock = (Block) nativeValue;
        Type elementType = type.getTypeParameters().get(0);
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            list.add(nativeContainerToOrcValue(elementType, getNativeContainerValue(elementType, arrayBlock, i)));
        }
        return list;
    }
    if (isMapType(type)) {
        Block mapBlock = (Block) nativeValue;
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Map<Object, Object> map = new HashMap<>();
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Object key = nativeContainerToOrcValue(keyType, getNativeContainerValue(keyType, mapBlock, i));
            Object value = nativeContainerToOrcValue(valueType, getNativeContainerValue(valueType, mapBlock, i + 1));
            map.put(key, value);
        }
        return map;
    }
    throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unimplemented type: " + type);
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType(com.facebook.presto.common.type.VarcharType) Types.isArrayType(com.facebook.presto.raptor.util.Types.isArrayType) Types.isMapType(com.facebook.presto.raptor.util.Types.isMapType) Type(com.facebook.presto.common.type.Type) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) BigInteger(java.math.BigInteger) Block(com.facebook.presto.common.block.Block)

Example 8 with VarcharType

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

the class ShardStats method doComputeColumnStats.

private static ColumnStats doComputeColumnStats(OrcReader orcReader, long columnId, Type type, TypeManager typeManager) throws IOException {
    StorageTypeConverter storageTypeConverter = new StorageTypeConverter(typeManager);
    int columnIndex = columnIndex(orcReader.getColumnNames(), columnId);
    OrcBatchRecordReader reader = orcReader.createBatchRecordReader(storageTypeConverter.toStorageTypes(ImmutableMap.of(columnIndex, toOrcFileType(type, typeManager))), OrcPredicate.TRUE, UTC, new RaptorOrcAggregatedMemoryContext(), INITIAL_BATCH_SIZE);
    if (type.equals(BOOLEAN)) {
        return indexBoolean(reader, columnIndex, columnId);
    }
    if (type.equals(BigintType.BIGINT) || type.equals(DateType.DATE) || type.equals(TimeType.TIME) || type.equals(TimestampType.TIMESTAMP)) {
        return indexLong(type, reader, columnIndex, columnId);
    }
    if (type.equals(DOUBLE)) {
        return indexDouble(reader, columnIndex, columnId);
    }
    if (type instanceof VarcharType) {
        return indexString(type, reader, columnIndex, columnId);
    }
    return null;
}
Also used : OrcBatchRecordReader(com.facebook.presto.orc.OrcBatchRecordReader) VarcharType(com.facebook.presto.common.type.VarcharType) RaptorOrcAggregatedMemoryContext(com.facebook.presto.raptor.RaptorOrcAggregatedMemoryContext)

Example 9 with VarcharType

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

the class ParquetSchemaConverter method getPrimitiveType.

private org.apache.parquet.schema.Type getPrimitiveType(Type type, String name, List<String> parent) {
    List<String> fullName = ImmutableList.<String>builder().addAll(parent).add(name).build();
    primitiveTypes.put(fullName, type);
    if (BOOLEAN.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BOOLEAN, OPTIONAL).named(name);
    }
    if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.INT32, OPTIONAL).named(name);
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.getPrecision() <= 9) {
            return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        } else if (decimalType.isShort()) {
            return Types.optional(PrimitiveType.PrimitiveTypeName.INT64).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        } else {
            return Types.optional(PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY).length(16).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
        }
    }
    if (DATE.equals(type)) {
        return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DATE).named(name);
    }
    if (BIGINT.equals(type) || TIMESTAMP.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).named(name);
    }
    if (DOUBLE.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.DOUBLE, OPTIONAL).named(name);
    }
    if (RealType.REAL.equals(type)) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.FLOAT, OPTIONAL).named(name);
    }
    if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType) {
        return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).named(name);
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported primitive type: %s", type));
}
Also used : VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) PrestoException(com.facebook.presto.spi.PrestoException) CharType(com.facebook.presto.common.type.CharType)

Example 10 with VarcharType

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

the class TestExpressionCompiler method testFunctionCall.

@Test
public void testFunctionCall() throws Exception {
    for (Integer left : intLefts) {
        for (Integer right : intRights) {
            assertExecute(generateExpression("bitwise_and(%s, %s)", left, right), BIGINT, left == null || right == null ? null : BitwiseFunctions.bitwiseAnd(left, right));
        }
    }
    for (Integer left : intLefts) {
        for (Double right : doubleRights) {
            assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right));
        }
    }
    for (Double left : doubleLefts) {
        for (Integer right : intRights) {
            assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right));
        }
    }
    for (Double left : doubleLefts) {
        for (Double right : doubleRights) {
            assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right));
        }
    }
    for (Double left : doubleLefts) {
        for (BigDecimal right : decimalRights) {
            assertExecute(generateExpression("mod(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.mod(left, right.doubleValue()));
        }
    }
    for (BigDecimal left : decimalLefts) {
        for (Long right : longRights) {
            assertExecute(generateExpression("power(%s, %s)", left, right), DOUBLE, left == null || right == null ? null : MathFunctions.power(left.doubleValue(), right));
        }
    }
    for (String value : stringLefts) {
        for (Integer start : intLefts) {
            for (Integer length : intRights) {
                String expected;
                if (value == null || start == null || length == null) {
                    expected = null;
                } else {
                    expected = StringFunctions.substr(utf8Slice(value), start, length).toStringUtf8();
                }
                VarcharType expectedType = value != null ? createVarcharType(value.length()) : VARCHAR;
                assertExecute(generateExpression("substr(%s, %s, %s)", value, start, length), expectedType, expected);
            }
        }
    }
    Futures.allAsList(futures).get();
}
Also used : BigInteger(java.math.BigInteger) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) BigDecimal(java.math.BigDecimal) 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