Search in sources :

Example 76 with Description

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

the class MathFunctions method widthBucket.

@Description("The bucket number of a value given an array of bins")
@ScalarFunction("width_bucket")
@SqlType(StandardTypes.BIGINT)
public static long widthBucket(@SqlType(StandardTypes.DOUBLE) double operand, @SqlType("array(double)") Block bins) {
    int numberOfBins = bins.getPositionCount();
    checkCondition(numberOfBins > 0, INVALID_FUNCTION_ARGUMENT, "Bins cannot be an empty array");
    checkCondition(!isNaN(operand), INVALID_FUNCTION_ARGUMENT, "Operand cannot be NaN");
    int lower = 0;
    int upper = numberOfBins;
    int index;
    double bin;
    while (lower < upper) {
        if (DOUBLE.getDouble(bins, lower) > DOUBLE.getDouble(bins, upper - 1)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bin values are not sorted in ascending order");
        }
        index = (lower + upper) / 2;
        bin = DOUBLE.getDouble(bins, index);
        if (!isFinite(bin)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bin value must be finite, got " + bin);
        }
        if (operand < bin) {
            upper = index;
        } else {
            lower = index + 1;
        }
    }
    return lower;
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) Constraint(com.facebook.presto.type.Constraint) DecimalOperators.modulusScalarFunction(com.facebook.presto.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 77 with Description

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

the class MathFunctions method cauchyCdf.

@Description("Cauchy cdf for a given value, median, and scale (gamma)")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double cauchyCdf(@SqlType(StandardTypes.DOUBLE) double median, @SqlType(StandardTypes.DOUBLE) double scale, @SqlType(StandardTypes.DOUBLE) double value) {
    checkCondition(scale > 0, INVALID_FUNCTION_ARGUMENT, "scale must be greater than 0");
    CauchyDistribution distribution = new CauchyDistribution(null, median, scale, CauchyDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    return distribution.cumulativeProbability(value);
}
Also used : CauchyDistribution(org.apache.commons.math3.distribution.CauchyDistribution) DecimalOperators.modulusScalarFunction(com.facebook.presto.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 78 with Description

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

the class MapFilterFunction method mapFilter.

@CodegenScalarFunction(value = "map_filter", deterministic = false)
@Description("return map containing entries that match the given predicate")
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
public static MethodHandle mapFilter(@SqlType("map(K,V)") Type inputType, @SqlType("function(K,V,boolean)") Type filter) {
    MapType mapType = (MapType) inputType;
    CallSiteBinder binder = new CallSiteBinder();
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    Class<?> keyJavaType = Primitives.wrap(keyType.getJavaType());
    Class<?> valueJavaType = Primitives.wrap(valueType.getJavaType());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("MapFilter"), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    Parameter block = arg("block", Block.class);
    Parameter function = arg("function", BinaryFunctionInterface.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "filter", type(Block.class), ImmutableList.of(block, function));
    BytecodeBlock body = method.getBody();
    Scope scope = method.getScope();
    Variable positionCount = scope.declareVariable(int.class, "positionCount");
    Variable position = scope.declareVariable(int.class, "position");
    Variable mapBlockBuilder = scope.declareVariable(BlockBuilder.class, "mapBlockBuilder");
    Variable singleMapBlockWriter = scope.declareVariable(BlockBuilder.class, "singleMapBlockWriter");
    Variable keyElement = scope.declareVariable(keyJavaType, "keyElement");
    Variable valueElement = scope.declareVariable(valueJavaType, "valueElement");
    Variable keep = scope.declareVariable(Boolean.class, "keep");
    // invoke block.getPositionCount()
    body.append(positionCount.set(block.invoke("getPositionCount", int.class)));
    // prepare the single map block builder
    body.append(mapBlockBuilder.set(constantType(binder, mapType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(0))));
    body.append(singleMapBlockWriter.set(mapBlockBuilder.invoke("beginBlockEntry", BlockBuilder.class)));
    SqlTypeBytecodeExpression keySqlType = constantType(binder, keyType);
    BytecodeNode loadKeyElement;
    if (!keyType.equals(UNKNOWN)) {
        // key element must be non-null
        loadKeyElement = new BytecodeBlock().append(keyElement.set(keySqlType.getValue(block, position).cast(keyJavaType)));
    } else {
        loadKeyElement = new BytecodeBlock().append(keyElement.set(constantNull(keyJavaType)));
    }
    SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType);
    BytecodeNode loadValueElement;
    if (!valueType.equals(UNKNOWN)) {
        loadValueElement = new IfStatement().condition(block.invoke("isNull", boolean.class, add(position, constantInt(1)))).ifTrue(valueElement.set(constantNull(valueJavaType))).ifFalse(valueElement.set(valueSqlType.getValue(block, add(position, constantInt(1))).cast(valueJavaType)));
    } else {
        loadValueElement = new BytecodeBlock().append(valueElement.set(constantNull(valueJavaType)));
    }
    body.append(new ForLoop().initialize(position.set(constantInt(0))).condition(lessThan(position, positionCount)).update(incrementVariable(position, (byte) 2)).body(new BytecodeBlock().append(loadKeyElement).append(loadValueElement).append(keep.set(function.invoke("apply", Object.class, keyElement.cast(Object.class), valueElement.cast(Object.class)).cast(Boolean.class))).append(new IfStatement("if (keep != null && keep) ...").condition(and(notEqual(keep, constantNull(Boolean.class)), keep.cast(boolean.class))).ifTrue(new BytecodeBlock().append(keySqlType.invoke("appendTo", void.class, block, position, singleMapBlockWriter)).append(valueSqlType.invoke("appendTo", void.class, block, add(position, constantInt(1)), singleMapBlockWriter))))));
    body.append(mapBlockBuilder.invoke("closeEntry", BlockBuilder.class).pop());
    body.append(constantType(binder, mapType).invoke("getObject", Object.class, mapBlockBuilder.cast(Block.class), subtract(mapBlockBuilder.invoke("getPositionCount", int.class), constantInt(1))).ret());
    Class<?> generatedClass = defineClass(definition, Object.class, binder.getBindings(), MapFilterFunction.class.getClassLoader());
    return methodHandle(generatedClass, "filter", Block.class, BinaryFunctionInterface.class);
}
Also used : Variable(com.facebook.presto.bytecode.Variable) VariableInstruction.incrementVariable(com.facebook.presto.bytecode.instruction.VariableInstruction.incrementVariable) ForLoop(com.facebook.presto.bytecode.control.ForLoop) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) MapType(com.facebook.presto.common.type.MapType) IfStatement(com.facebook.presto.bytecode.control.IfStatement) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) SqlType(com.facebook.presto.spi.function.SqlType) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) TypeParameter(com.facebook.presto.spi.function.TypeParameter) Parameter(com.facebook.presto.bytecode.Parameter) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Block(com.facebook.presto.common.block.Block) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) BlockBuilderStatus(com.facebook.presto.common.block.BlockBuilderStatus) SqlTypeBytecodeExpression(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression) TypeParameter(com.facebook.presto.spi.function.TypeParameter) Description(com.facebook.presto.spi.function.Description) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 79 with Description

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

the class JoniRegexpFunctions method regexpLike.

@Description("returns whether the pattern is contained within the string")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static boolean regexpLike(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern) {
    Matcher matcher;
    int offset;
    if (source.hasByteArray()) {
        offset = source.byteArrayOffset();
        matcher = pattern.matcher(source.byteArray(), offset, offset + source.length());
    } else {
        offset = 0;
        matcher = pattern.matcher(source.getBytes());
    }
    return getMatchingOffset(matcher, offset, offset + source.length()) != -1;
}
Also used : Matcher(io.airlift.joni.Matcher) Constraint(com.facebook.presto.type.Constraint) 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 80 with Description

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

the class JoniRegexpFunctions method regexpExtract.

@SqlNullable
@Description("returns regex group of extracted string with a pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice regexpExtract(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
    Matcher matcher = pattern.matcher(source.getBytes());
    int group = toIntExact(groupIndex);
    int offset = getMatchingOffset(matcher, 0, source.length(), false);
    if (offset == -1) {
        return null;
    }
    validateGroup(groupIndex, matcher.getEagerRegion());
    Region region = matcher.getEagerRegion();
    int beg = region.beg[group];
    int end = region.end[group];
    if (beg == -1) {
        // end == -1 must be true
        return null;
    }
    return source.slice(beg, end - beg);
}
Also used : Matcher(io.airlift.joni.Matcher) Region(io.airlift.joni.Region) Constraint(com.facebook.presto.type.Constraint) 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)

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