Java Code Examples for io.prestosql.spi.type.Type#getBoolean()

The following examples show how to use io.prestosql.spi.type.Type#getBoolean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrayFilterFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType("array(T)")
public static Block filterBoolean(
        @TypeParameter("T") Type elementType,
        @SqlType("array(T)") Block arrayBlock,
        @SqlType("function(T, boolean)") BooleanToBooleanFunction function)
{
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        Boolean input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getBoolean(arrayBlock, position);
        }

        Boolean keep = function.apply(input);
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
 
Example 2
Source File: ArrayMinMaxUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
@UsedByGeneratedCode
public static Boolean booleanArrayMinMax(MethodHandle compareMethodHandle, Type elementType, Block block)
{
    try {
        if (block.getPositionCount() == 0) {
            return null;
        }

        boolean selectedValue = elementType.getBoolean(block, 0);
        for (int i = 0; i < block.getPositionCount(); i++) {
            if (block.isNull(i)) {
                return null;
            }
            boolean value = elementType.getBoolean(block, i);
            if ((boolean) compareMethodHandle.invokeExact(value, selectedValue)) {
                selectedValue = value;
            }
        }

        return selectedValue;
    }
    catch (Throwable t) {
        throw internalError(t);
    }
}
 
Example 3
Source File: Row.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Object getNativeContainerValue(Type type, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }
    else if (type.getJavaType() == boolean.class) {
        return type.getBoolean(block, position);
    }
    else if (type.getJavaType() == long.class) {
        return type.getLong(block, position);
    }
    else if (type.getJavaType() == double.class) {
        return type.getDouble(block, position);
    }
    else if (type.getJavaType() == Slice.class) {
        return type.getSlice(block, position);
    }
    else if (type.getJavaType() == Block.class) {
        return type.getObject(block, position);
    }
    else {
        throw new AssertionError("Unimplemented type: " + type);
    }
}
 
Example 4
Source File: PageRecordSet.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public boolean getBoolean(int field)
{
    checkState(position >= 0, "Not yet advanced");
    checkState(position < page.getPositionCount(), "Already finished");
    Type type = types.get(field);
    return type.getBoolean(page.getBlock(field), position);
}
 
Example 5
Source File: ArrayAllMatchFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType(StandardTypes.BOOLEAN)
@SqlNullable
public static Boolean allMatchBoolean(
        @TypeParameter("T") Type elementType,
        @SqlType("array(T)") Block arrayBlock,
        @SqlType("function(T, boolean)") BooleanToBooleanFunction function)
{
    boolean hasNullResult = false;
    int positionCount = arrayBlock.getPositionCount();
    for (int i = 0; i < positionCount; i++) {
        Boolean element = null;
        if (!arrayBlock.isNull(i)) {
            element = elementType.getBoolean(arrayBlock, i);
        }
        Boolean match = function.apply(element);
        if (FALSE.equals(match)) {
            return false;
        }
        if (match == null) {
            hasNullResult = true;
        }
    }
    if (hasNullResult) {
        return null;
    }
    return true;
}
 
Example 6
Source File: ArrayAnyMatchFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType(StandardTypes.BOOLEAN)
@SqlNullable
public static Boolean anyMatchBoolean(
        @TypeParameter("T") Type elementType,
        @SqlType("array(T)") Block arrayBlock,
        @SqlType("function(T, boolean)") BooleanToBooleanFunction function)
{
    boolean hasNullResult = false;
    int positionCount = arrayBlock.getPositionCount();
    for (int i = 0; i < positionCount; i++) {
        Boolean element = null;
        if (!arrayBlock.isNull(i)) {
            element = elementType.getBoolean(arrayBlock, i);
        }
        Boolean match = function.apply(element);
        if (TRUE.equals(match)) {
            return true;
        }
        if (match == null) {
            hasNullResult = true;
        }
    }
    if (hasNullResult) {
        return null;
    }
    return false;
}
 
Example 7
Source File: ArrayElementAtFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("E")
@SqlNullable
@SqlType("E")
public static Boolean booleanElementAt(@TypeParameter("E") Type elementType, @SqlType("array(E)") Block array, @SqlType("bigint") long index)
{
    int position = checkedIndexToBlockPosition(array, index);
    if (position == -1) {
        return null;
    }
    if (array.isNull(position)) {
        return null;
    }

    return elementType.getBoolean(array, position);
}
 
Example 8
Source File: ArraySubscriptOperator.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Boolean booleanSubscript(Type elementType, Block array, long index)
{
    checkIndex(array, index);
    int position = toIntExact(index - 1);
    if (array.isNull(position)) {
        return null;
    }

    return elementType.getBoolean(array, position);
}
 
Example 9
Source File: JsonUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
static ObjectKeyProvider createObjectKeyProvider(Type type)
{
    if (type instanceof UnknownType) {
        return (block, position) -> null;
    }
    if (type instanceof BooleanType) {
        return (block, position) -> type.getBoolean(block, position) ? "true" : "false";
    }
    if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) {
        return (block, position) -> String.valueOf(type.getLong(block, position));
    }
    if (type instanceof RealType) {
        return (block, position) -> String.valueOf(intBitsToFloat(toIntExact(type.getLong(block, position))));
    }
    if (type instanceof DoubleType) {
        return (block, position) -> String.valueOf(type.getDouble(block, position));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (isShortDecimal(decimalType)) {
            return (block, position) -> Decimals.toString(decimalType.getLong(block, position), decimalType.getScale());
        }
        return (block, position) -> Decimals.toString(
                decodeUnscaledValue(type.getSlice(block, position)),
                decimalType.getScale());
    }
    if (type instanceof VarcharType) {
        return (block, position) -> type.getSlice(block, position).toStringUtf8();
    }

    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Unsupported type: %s", type));
}
 
Example 10
Source File: TestBlockAndPositionNullConvention.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("E")
@SqlNullable
@SqlType("E")
public static Boolean speciailizedBoolean(@TypeParameter("E") Type type, @BlockPosition @SqlType(value = "E", nativeContainerType = boolean.class) Block block, @BlockIndex int position)
{
    hitBlockPositionBoolean.set(true);
    return type.getBoolean(block, position);
}
 
Example 11
Source File: IcebergPageSink.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Object getIcebergValue(Block block, int position, Type type)
{
    if (block.isNull(position)) {
        return null;
    }
    if (type instanceof BigintType) {
        return type.getLong(block, position);
    }
    if (type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType || type instanceof DateType) {
        return toIntExact(type.getLong(block, position));
    }
    if (type instanceof BooleanType) {
        return type.getBoolean(block, position);
    }
    if (type instanceof DecimalType) {
        return readBigDecimal((DecimalType) type, block, position);
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type instanceof DoubleType) {
        return type.getDouble(block, position);
    }
    if (type instanceof TimestampType) {
        return MILLISECONDS.toMicros(type.getLong(block, position));
    }
    if (type instanceof TimestampWithTimeZoneType) {
        return MILLISECONDS.toMicros(unpackMillisUtc(type.getLong(block, position)));
    }
    if (type instanceof VarbinaryType) {
        return type.getSlice(block, position).getBytes();
    }
    if (type instanceof VarcharType) {
        return type.getSlice(block, position).toStringUtf8();
    }
    throw new UnsupportedOperationException("Type not supported as partition column: " + type.getDisplayName());
}
 
Example 12
Source File: HiveBucketingV2.java    From presto with Apache License 2.0 4 votes vote down vote up
private static int hash(TypeInfo type, Block block, int position)
{
    // This function mirrors the behavior of function hashCodeMurmur in
    // HIVE-18910 (and following) 7dc47faddb serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
    // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java

    if (block.isNull(position)) {
        return 0;
    }

    switch (type.getCategory()) {
        case PRIMITIVE:
            PrimitiveTypeInfo typeInfo = (PrimitiveTypeInfo) type;
            PrimitiveCategory primitiveCategory = typeInfo.getPrimitiveCategory();
            Type prestoType = requireNonNull(HiveType.getPrimitiveType(typeInfo));
            switch (primitiveCategory) {
                case BOOLEAN:
                    return prestoType.getBoolean(block, position) ? 1 : 0;
                case BYTE:
                    return SignedBytes.checkedCast(prestoType.getLong(block, position));
                case SHORT:
                    return Murmur3.hash32(bytes(Shorts.checkedCast(prestoType.getLong(block, position))));
                case INT:
                    return Murmur3.hash32(bytes(toIntExact(prestoType.getLong(block, position))));
                case LONG:
                    return Murmur3.hash32(bytes(prestoType.getLong(block, position)));
                case FLOAT:
                    // convert to canonical NaN if necessary
                    // Sic! we're `floatToIntBits -> cast to float -> floatToRawIntBits` just as it is (implicitly) done in
                    // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java#L830
                    return Murmur3.hash32(bytes(floatToRawIntBits(floatToIntBits(intBitsToFloat(toIntExact(prestoType.getLong(block, position)))))));
                case DOUBLE:
                    // Sic! we're `doubleToLongBits -> cast to double -> doubleToRawLongBits` just as it is (implicitly) done in
                    // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java#L836
                    return Murmur3.hash32(bytes(doubleToRawLongBits(doubleToLongBits(prestoType.getDouble(block, position)))));
                case STRING:
                    return Murmur3.hash32(prestoType.getSlice(block, position).getBytes());
                case VARCHAR:
                    return Murmur3.hash32(prestoType.getSlice(block, position).getBytes());
                case DATE:
                    // day offset from 1970-01-01
                    return Murmur3.hash32(bytes(toIntExact(prestoType.getLong(block, position))));
                // case TIMESTAMP: // TODO (https://github.com/prestosql/presto/issues/1706): support bucketing v2 for timestamp
                default:
                    throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive primitive category: " + primitiveCategory);
            }
        case LIST:
            return hashOfList((ListTypeInfo) type, block.getObject(position, Block.class));
        case MAP:
            return hashOfMap((MapTypeInfo) type, block.getObject(position, Block.class));
        default:
            // TODO: support more types, e.g. ROW
            throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive category: " + type.getCategory());
    }
}
 
Example 13
Source File: HiveBucketingV1.java    From presto with Apache License 2.0 4 votes vote down vote up
static int hash(TypeInfo type, Block block, int position)
{
    // This function mirrors the behavior of function hashCode in
    // HIVE-12025 ba83fd7bff serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
    // https://github.com/apache/hive/blob/ba83fd7bff/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java

    if (block.isNull(position)) {
        return 0;
    }

    switch (type.getCategory()) {
        case PRIMITIVE:
            PrimitiveTypeInfo typeInfo = (PrimitiveTypeInfo) type;
            PrimitiveCategory primitiveCategory = typeInfo.getPrimitiveCategory();
            Type prestoType = requireNonNull(HiveType.getPrimitiveType(typeInfo));
            switch (primitiveCategory) {
                case BOOLEAN:
                    return prestoType.getBoolean(block, position) ? 1 : 0;
                case BYTE:
                    return SignedBytes.checkedCast(prestoType.getLong(block, position));
                case SHORT:
                    return Shorts.checkedCast(prestoType.getLong(block, position));
                case INT:
                    return toIntExact(prestoType.getLong(block, position));
                case LONG:
                    long bigintValue = prestoType.getLong(block, position);
                    return (int) ((bigintValue >>> 32) ^ bigintValue);
                case FLOAT:
                    // convert to canonical NaN if necessary
                    return floatToIntBits(intBitsToFloat(toIntExact(prestoType.getLong(block, position))));
                case DOUBLE:
                    long doubleValue = doubleToLongBits(prestoType.getDouble(block, position));
                    return (int) ((doubleValue >>> 32) ^ doubleValue);
                case STRING:
                    return hashBytes(0, prestoType.getSlice(block, position));
                case VARCHAR:
                    return hashBytes(1, prestoType.getSlice(block, position));
                case DATE:
                    // day offset from 1970-01-01
                    return toIntExact(prestoType.getLong(block, position));
                case TIMESTAMP:
                    return hashTimestamp(prestoType.getLong(block, position));
                default:
                    throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive primitive category: " + primitiveCategory);
            }
        case LIST:
            return hashOfList((ListTypeInfo) type, block.getObject(position, Block.class));
        case MAP:
            return hashOfMap((MapTypeInfo) type, block.getObject(position, Block.class));
        default:
            // TODO: support more types, e.g. ROW
            throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive category: " + type.getCategory());
    }
}