Search in sources :

Example 21 with SqlType

use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.

the class JsonOperators method castToInteger.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(INTEGER)
public static Long castToInteger(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result;
        switch(parser.getCurrentToken()) {
            case VALUE_NULL:
                result = null;
                break;
            case VALUE_STRING:
                result = VarcharOperators.castToInteger(Slices.utf8Slice(parser.getText()));
                break;
            case VALUE_NUMBER_FLOAT:
                result = DoubleOperators.castToInteger(parser.getDoubleValue());
                break;
            case VALUE_NUMBER_INT:
                result = (long) toIntExact(parser.getLongValue());
                break;
            case VALUE_TRUE:
                result = BooleanOperators.castToInteger(true);
                break;
            case VALUE_FALSE:
                result = BooleanOperators.castToInteger(false);
                break;
            default:
                throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER));
        }
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to INTEGER");
        return result;
    } catch (ArithmeticException | IOException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER));
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 22 with SqlType

use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.

the class ArrayDistinctFromOperator method isDistinctFrom.

@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean isDistinctFrom(@OperatorDependency(operator = IS_DISTINCT_FROM, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle function, @TypeParameter("E") Type type, @SqlType("array(E)") Block left, @IsNull boolean leftNull, @SqlType("array(E)") Block right, @IsNull boolean rightNull) {
    if (leftNull != rightNull) {
        return true;
    }
    if (leftNull) {
        return false;
    }
    if (left.getPositionCount() != right.getPositionCount()) {
        return true;
    }
    for (int i = 0; i < left.getPositionCount(); i++) {
        Object leftValue = readNativeValue(type, left, i);
        boolean leftValueNull = leftValue == null;
        if (leftValueNull) {
            leftValue = defaultValue(type.getJavaType());
        }
        Object rightValue = readNativeValue(type, right, i);
        boolean rightValueNull = rightValue == null;
        if (rightValueNull) {
            rightValue = defaultValue(type.getJavaType());
        }
        try {
            if ((boolean) function.invoke(leftValue, leftValueNull, rightValue, rightValueNull)) {
                return true;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    return false;
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 23 with SqlType

use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.

the class ArrayEqualOperator method equals.

@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean equals(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle equalsFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    if (leftArray.getPositionCount() != rightArray.getPositionCount()) {
        return false;
    }
    for (int i = 0; i < leftArray.getPositionCount(); i++) {
        checkElementNotNull(leftArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        Object leftElement = readNativeValue(type, leftArray, i);
        Object rightElement = readNativeValue(type, rightArray, i);
        try {
            if (!(boolean) equalsFunction.invoke(leftElement, rightElement)) {
                return false;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    return true;
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 24 with SqlType

use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.

the class ArrayExceptFunction method except.

@TypeParameter("E")
@SqlType("array(E)")
public static Block except(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    int leftPositionCount = leftArray.getPositionCount();
    int rightPositionCount = rightArray.getPositionCount();
    if (leftPositionCount == 0) {
        return leftArray;
    }
    TypedSet typedSet = new TypedSet(type, leftPositionCount + rightPositionCount);
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), leftPositionCount);
    for (int i = 0; i < rightPositionCount; i++) {
        typedSet.add(rightArray, i);
    }
    for (int i = 0; i < leftPositionCount; i++) {
        if (!typedSet.contains(leftArray, i)) {
            typedSet.add(leftArray, i);
            type.appendTo(leftArray, i, distinctElementBlockBuilder);
        }
    }
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 25 with SqlType

use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.

the class ArrayFilterFunction method filterLong.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position++) {
        Long input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getLong(arrayBlock, position);
        }
        Boolean keep;
        try {
            keep = (Boolean) function.invokeExact(input);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

SqlType (com.facebook.presto.spi.function.SqlType)74 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)36 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)24 TypeParameter (com.facebook.presto.spi.function.TypeParameter)24 PrestoException (com.facebook.presto.spi.PrestoException)22 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)20 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)19 Description (com.facebook.presto.spi.function.Description)18 Constraint (com.facebook.presto.type.Constraint)15 SqlNullable (com.facebook.presto.spi.function.SqlNullable)14 Slice (io.airlift.slice.Slice)13 IOException (java.io.IOException)12 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)11 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)11 JsonParser (com.fasterxml.jackson.core.JsonParser)11 TypeParameterSpecialization (com.facebook.presto.spi.function.TypeParameterSpecialization)9 JsonToken (com.fasterxml.jackson.core.JsonToken)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)6 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)5