Search in sources :

Example 56 with Description

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

the class StringFunctions method replace.

@Description("greedily replaces occurrences of a pattern with a string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z", "u" })
@Constraint(variable = "u", expression = "min(2147483647, x + z * (x + 1))")
@SqlType("varchar(u)")
public static Slice replace(@SqlType("varchar(x)") Slice str, @SqlType("varchar(y)") Slice search, @SqlType("varchar(z)") Slice replace) {
    // Empty search?
    if (search.length() == 0) {
        // With empty `search` we insert `replace` in front of every character and at the end
        int resultLength;
        try {
            resultLength = Math.addExact(Math.multiplyExact(countCodePoints(str) + 1, replace.length()), str.length());
        } catch (ArithmeticException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "inputs to \"replace\" function are too large: when \"search\" parameter is empty, length of \"string\" times length of \"replace\" must not exceed " + Integer.MAX_VALUE);
        }
        Slice buffer = Slices.allocate(resultLength);
        // Always start with replace
        buffer.setBytes(0, replace);
        int indexBuffer = replace.length();
        // After every code point insert `replace`
        int index = 0;
        while (index < str.length()) {
            int codePointLength = lengthOfCodePointSafe(str, index);
            // Append current code point
            buffer.setBytes(indexBuffer, str, index, codePointLength);
            indexBuffer += codePointLength;
            // Append `replace`
            buffer.setBytes(indexBuffer, replace);
            indexBuffer += replace.length();
            // Advance pointer to current code point
            index += codePointLength;
        }
        return buffer;
    }
    // Allocate a reasonable buffer
    Slice buffer = Slices.allocate(str.length());
    int index = 0;
    int indexBuffer = 0;
    while (index < str.length()) {
        int matchIndex = str.indexOf(search, index);
        // Found a match?
        if (matchIndex < 0) {
            // No match found so copy the rest of string
            int bytesToCopy = str.length() - index;
            buffer = Slices.ensureSize(buffer, indexBuffer + bytesToCopy);
            buffer.setBytes(indexBuffer, str, index, bytesToCopy);
            indexBuffer += bytesToCopy;
            break;
        }
        int bytesToCopy = matchIndex - index;
        buffer = Slices.ensureSize(buffer, indexBuffer + bytesToCopy + replace.length());
        // Non empty match?
        if (bytesToCopy > 0) {
            buffer.setBytes(indexBuffer, str, index, bytesToCopy);
            indexBuffer += bytesToCopy;
        }
        // Non empty replace?
        if (replace.length() > 0) {
            buffer.setBytes(indexBuffer, replace);
            indexBuffer += replace.length();
        }
        // Continue searching after match
        index = matchIndex + search.length();
    }
    return buffer.slice(0, indexBuffer);
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) PrestoException(com.facebook.presto.spi.PrestoException) Constraint(com.facebook.presto.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) Constraint(com.facebook.presto.type.Constraint) SqlType(com.facebook.presto.spi.function.SqlType)

Example 57 with Description

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

the class AggregationCompiler method getDescription.

private static String getDescription(AnnotatedElement base, AnnotatedElement override) {
    Description description = override.getAnnotation(Description.class);
    if (description != null) {
        return description.value();
    }
    description = base.getAnnotation(Description.class);
    return (description == null) ? null : description.value();
}
Also used : Description(com.facebook.presto.spi.function.Description)

Example 58 with Description

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

the class TDigestFunctions method destructureTDigest.

@ScalarFunction(value = "destructure_tdigest", visibility = EXPERIMENTAL)
@Description("Return the raw TDigest, including arrays of centroid means and weights, as well as min, max, sum, count, and compression factor.")
@SqlType("row(centroid_means array(double), centroid_weights array(integer), compression double, min double, max double, sum double, count bigint)")
public static Block destructureTDigest(@SqlType("tdigest(double)") Slice input) {
    TDigest tDigest = createTDigest(input);
    BlockBuilder blockBuilder = TDIGEST_CENTROIDS_ROW_TYPE.createBlockBuilder(null, 1);
    BlockBuilder rowBuilder = blockBuilder.beginBlockEntry();
    // Centroid means / weights
    BlockBuilder meansBuilder = DOUBLE.createBlockBuilder(null, tDigest.centroidCount());
    BlockBuilder weightsBuilder = INTEGER.createBlockBuilder(null, tDigest.centroidCount());
    for (Centroid centroid : tDigest.centroids()) {
        int weight = (int) centroid.getWeight();
        DOUBLE.writeDouble(meansBuilder, centroid.getMean());
        INTEGER.writeLong(weightsBuilder, weight);
    }
    rowBuilder.appendStructure(meansBuilder);
    rowBuilder.appendStructure(weightsBuilder);
    // Compression, min, max, sum, count
    DOUBLE.writeDouble(rowBuilder, tDigest.getCompressionFactor());
    DOUBLE.writeDouble(rowBuilder, tDigest.getMin());
    DOUBLE.writeDouble(rowBuilder, tDigest.getMax());
    DOUBLE.writeDouble(rowBuilder, tDigest.getSum());
    BIGINT.writeLong(rowBuilder, (long) tDigest.getSize());
    blockBuilder.closeEntry();
    return TDIGEST_CENTROIDS_ROW_TYPE.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : Centroid(com.facebook.presto.tdigest.Centroid) TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) TDigest(com.facebook.presto.tdigest.TDigest) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 59 with Description

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

the class UrlFunctions method urlExtractParameter.

@SqlNullable
@Description("extract query parameter from url")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("varchar(x)")
public static Slice urlExtractParameter(@SqlType("varchar(x)") Slice url, @SqlType("varchar(y)") Slice parameterName) {
    URI uri = parseUrl(url);
    if ((uri == null) || (uri.getQuery() == null)) {
        return null;
    }
    Slice query = slice(uri.getQuery());
    String parameter = parameterName.toStringUtf8();
    Iterable<String> queryArgs = QUERY_SPLITTER.split(query.toStringUtf8());
    for (String queryArg : queryArgs) {
        Iterator<String> arg = ARG_SPLITTER.split(queryArg).iterator();
        if (arg.next().equals(parameter)) {
            if (arg.hasNext()) {
                return utf8Slice(arg.next());
            }
            // first matched key is empty
            return Slices.EMPTY_SLICE;
        }
    }
    // no key matched
    return null;
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) URI(java.net.URI) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 60 with Description

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

the class CodegenScalarFromAnnotationsParser method createSqlScalarFunction.

private static SqlScalarFunction createSqlScalarFunction(Method method) {
    CodegenScalarFunction codegenScalarFunction = method.getAnnotation(CodegenScalarFunction.class);
    Signature signature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, codegenScalarFunction.value()), FunctionKind.SCALAR, Arrays.stream(method.getAnnotationsByType(TypeParameter.class)).map(t -> withVariadicBound(t.value(), t.boundedBy().isEmpty() ? null : t.boundedBy())).collect(toImmutableList()), ImmutableList.of(), parseTypeSignature(method.getAnnotation(SqlType.class).value()), Arrays.stream(method.getParameters()).map(p -> parseTypeSignature(p.getAnnotation(SqlType.class).value())).collect(toImmutableList()), false);
    return new SqlScalarFunction(signature) {

        @Override
        public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
            Signature boundSignature = applyBoundVariables(signature, boundVariables, arity);
            MethodHandle handle;
            try {
                handle = (MethodHandle) method.invoke(null, boundSignature.getArgumentTypes().stream().map(t -> functionAndTypeManager.getType(t)).toArray());
            } catch (Exception e) {
                throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Method %s does not return valid MethodHandle", method), e);
            }
            return new BuiltInScalarFunctionImplementation(method.getAnnotation(SqlNullable.class) != null, getArgumentProperties(method), handle, Optional.empty());
        }

        @Override
        public SqlFunctionVisibility getVisibility() {
            return codegenScalarFunction.visibility();
        }

        @Override
        public boolean isDeterministic() {
            return codegenScalarFunction.deterministic();
        }

        @Override
        public String getDescription() {
            Description description = method.getAnnotation(Description.class);
            return description == null ? "" : description.value();
        }

        @Override
        public boolean isCalledOnNullInput() {
            return codegenScalarFunction.calledOnNullInput();
        }
    };
}
Also used : SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) BoundVariables(com.facebook.presto.metadata.BoundVariables) Description(com.facebook.presto.spi.function.Description) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) SqlType(com.facebook.presto.spi.function.SqlType) PrestoException(com.facebook.presto.spi.PrestoException) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) PrestoException(com.facebook.presto.spi.PrestoException) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

Description (com.facebook.presto.spi.function.Description)105 SqlType (com.facebook.presto.spi.function.SqlType)103 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)101 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)41 SqlNullable (com.facebook.presto.spi.function.SqlNullable)37 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)20 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)20 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)20 Geometry (org.locationtech.jts.geom.Geometry)20 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)19 PrestoException (com.facebook.presto.spi.PrestoException)18 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)15 Slice (io.airlift.slice.Slice)15 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)14 Constraint (com.facebook.presto.type.Constraint)14 DecimalOperators.modulusScalarFunction (com.facebook.presto.type.DecimalOperators.modulusScalarFunction)13 Point (com.esri.core.geometry.Point)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 Envelope (com.esri.core.geometry.Envelope)6