Search in sources :

Example 16 with ScalarOperator

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

the class ScalarImplementationHeader method fromAnnotatedElement.

public static List<ScalarImplementationHeader> fromAnnotatedElement(AnnotatedElement annotated) {
    ScalarFunction scalarFunction = annotated.getAnnotation(ScalarFunction.class);
    ScalarOperator scalarOperator = annotated.getAnnotation(ScalarOperator.class);
    Optional<String> description = parseDescription(annotated);
    ImmutableList.Builder<ScalarImplementationHeader> builder = ImmutableList.builder();
    if (scalarFunction != null) {
        String baseName = scalarFunction.value().isEmpty() ? camelToSnake(annotatedName(annotated)) : scalarFunction.value();
        builder.add(new ScalarImplementationHeader(baseName, new ScalarHeader(description, scalarFunction.visibility(), scalarFunction.deterministic(), scalarFunction.calledOnNullInput())));
        for (String alias : scalarFunction.alias()) {
            builder.add(new ScalarImplementationHeader(alias, new ScalarHeader(description, scalarFunction.visibility(), scalarFunction.deterministic(), scalarFunction.calledOnNullInput())));
        }
    }
    if (scalarOperator != null) {
        builder.add(new ScalarImplementationHeader(scalarOperator.value(), new ScalarHeader(description, HIDDEN, true, scalarOperator.value().isCalledOnNullInput())));
    }
    List<ScalarImplementationHeader> result = builder.build();
    checkArgument(!result.isEmpty());
    return result;
}
Also used : ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) ImmutableList(com.google.common.collect.ImmutableList) ScalarHeader(com.facebook.presto.operator.scalar.ScalarHeader)

Example 17 with ScalarOperator

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

the class TranslatorAnnotationParser method findScalarsFromSetClass.

private static List<ScalarHeaderAndMethods> findScalarsFromSetClass(Class<?> clazz) {
    ImmutableList.Builder<ScalarHeaderAndMethods> builder = ImmutableList.builder();
    for (Method method : findPublicMethodsWithAnnotation(clazz, SqlType.class, ScalarFunction.class, ScalarOperator.class)) {
        boolean annotationCondition = (method.getAnnotation(ScalarFunction.class) != null) || (method.getAnnotation(ScalarOperator.class) != null);
        checkArgument(annotationCondition, format("Method [%s] annotated with @SqlType is missing @ScalarFunction or @ScalarOperator", method));
        for (ScalarTranslationHeader header : ScalarTranslationHeader.fromAnnotatedElement(method)) {
            builder.add(new ScalarHeaderAndMethods(header, ImmutableSet.of(method)));
        }
    }
    List<ScalarHeaderAndMethods> methods = builder.build();
    checkArgument(!methods.isEmpty(), "Class [%s] does not have any methods annotated with @ScalarFunction or @ScalarOperator", clazz.getName());
    return methods;
}
Also used : ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) ImmutableList(com.google.common.collect.ImmutableList) Method(java.lang.reflect.Method)

Example 18 with ScalarOperator

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

the class JsonOperators method castToReal.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(REAL)
public static Long castToReal(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsReal(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to REAL");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), REAL), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) 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 19 with ScalarOperator

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

the class JsonOperators method castToTinyint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(TINYINT)
public static Long castToTinyint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsTinyint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to TINYINT");
        return result;
    } catch (PrestoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (IllegalArgumentException | IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), TINYINT), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) 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 20 with ScalarOperator

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

the class JsonOperators method castToVarchar.

@ScalarOperator(CAST)
@SqlNullable
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToVarchar(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Slice result = currentTokenAsVarchar(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to VARCHAR");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR), e);
    }
}
Also used : Slice(io.airlift.slice.Slice) JsonCastException(com.facebook.presto.util.JsonCastException) 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) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)26 SqlType (com.facebook.presto.spi.function.SqlType)24 PrestoException (com.facebook.presto.spi.PrestoException)13 ISOChronology (org.joda.time.chrono.ISOChronology)9 SqlNullable (com.facebook.presto.spi.function.SqlNullable)8 JsonCastException (com.facebook.presto.util.JsonCastException)8 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)8 JsonParser (com.fasterxml.jackson.core.JsonParser)8 IOException (java.io.IOException)8 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)5 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)4 ImmutableList (com.google.common.collect.ImmutableList)2 Slice (io.airlift.slice.Slice)2 UnknownHostException (java.net.UnknownHostException)2 HyperLogLog (com.facebook.airlift.stats.cardinality.HyperLogLog)1 ScalarHeader (com.facebook.presto.operator.scalar.ScalarHeader)1 JsonUtil.currentTokenAsBoolean (com.facebook.presto.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (com.facebook.presto.util.JsonUtil.currentTokenAsDouble)1 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)1 Method (java.lang.reflect.Method)1