Search in sources :

Example 1 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class TeradataStringFunctions method char2HexInt.

@Description("Returns the hexadecimal representation of the UTF-16BE encoding of the argument")
@ScalarFunction("char2hexint")
@SqlType(StandardTypes.VARCHAR)
public static Slice char2HexInt(@SqlType(StandardTypes.VARCHAR) Slice string) {
    Slice utf16 = Slices.wrappedBuffer(UTF_16BE.encode(string.toStringUtf8()));
    String encoded = BaseEncoding.base16().encode(utf16.getBytes());
    return Slices.utf8Slice(encoded);
}
Also used : Slice(io.airlift.slice.Slice) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 2 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class DataSizeFunctions method parsePrestoDataSize.

@Description("converts data size string to bytes")
@ScalarFunction("parse_presto_data_size")
@LiteralParameters("x")
@SqlType("decimal(38,0)")
public static Slice parsePrestoDataSize(@SqlType("varchar(x)") Slice input) {
    String dataSize = input.toStringUtf8();
    int valueLength = 0;
    for (int i = 0; i < dataSize.length(); i++) {
        char c = dataSize.charAt(i);
        if (isDigit(c) || c == '.') {
            valueLength++;
        } else {
            break;
        }
    }
    if (valueLength == 0) {
        throw invalidDataSize(dataSize);
    }
    BigDecimal value = parseValue(dataSize.substring(0, valueLength), dataSize);
    Unit unit = Unit.parse(dataSize.substring(valueLength), dataSize);
    BigInteger bytes = value.multiply(unit.getFactor()).toBigInteger();
    try {
        return encodeUnscaledValue(bytes);
    } catch (ArithmeticException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("Value out of range: '%s' ('%sB')", dataSize, bytes));
    }
}
Also used : BigInteger(java.math.BigInteger) PrestoException(io.prestosql.spi.PrestoException) BigDecimal(java.math.BigDecimal) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class JoniRegexpFunctions method regexpReplace.

@Description("replaces substrings matching a regular expression by given string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z" })
// to get the formula: x + max(x * y / 2, y) * (x + 1)
@Constraint(variable = "z", expression = "min(2147483647, x + max(x * y / 2, y) * (x + 1))")
@SqlType("varchar(z)")
public static Slice regexpReplace(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType("varchar(y)") Slice replacement) {
    Matcher matcher = pattern.matcher(source.getBytes());
    SliceOutput sliceOutput = new DynamicSliceOutput(source.length() + replacement.length() * 5);
    int lastEnd = 0;
    // nextStart is the same as lastEnd, unless the last match was zero-width. In such case, nextStart is lastEnd + 1.
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        sliceOutput.appendBytes(sliceBetweenReplacements);
        appendReplacement(sliceOutput, source, pattern, matcher.getEagerRegion(), replacement);
    }
    sliceOutput.appendBytes(source.slice(lastEnd, source.length() - lastEnd));
    return sliceOutput.slice();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Constraint(io.prestosql.type.Constraint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) Constraint(io.prestosql.type.Constraint) SqlType(io.prestosql.spi.function.SqlType)

Example 4 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

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());
    validateGroup(groupIndex, matcher.getEagerRegion());
    int group = toIntExact(groupIndex);
    int offset = matcher.search(0, source.length(), Option.DEFAULT);
    if (offset == -1) {
        return null;
    }
    Region region = matcher.getEagerRegion();
    int beg = region.beg[group];
    int end = region.end[group];
    if (beg == -1) {
        // end == -1 must be true
        return null;
    }
    Slice slice = source.slice(beg, end - beg);
    return slice;
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Region(io.airlift.joni.Region) Constraint(io.prestosql.type.Constraint) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class GeoFunctions method invalidReason.

@Description("Returns the reason for why the input geometry is not valid. Returns null if the input is valid.")
@ScalarFunction("geometry_invalid_reason")
@SqlType(VARCHAR)
@SqlNullable
public static Slice invalidReason(@SqlType(GEOMETRY_TYPE_NAME) Slice input) {
    GeometryCursor cursor = deserialize(input).getEsriGeometryCursor();
    NonSimpleResult result = new NonSimpleResult();
    while (true) {
        com.esri.core.geometry.Geometry geometry = cursor.next();
        if (geometry == null) {
            return null;
        }
        if (!OperatorSimplifyOGC.local().isSimpleOGC(geometry, null, true, result, null)) {
            String reasonText = NON_SIMPLE_REASONS.getOrDefault(result.m_reason, result.m_reason.name());
            if (!(geometry instanceof MultiVertexGeometry)) {
                return utf8Slice(reasonText);
            }
            MultiVertexGeometry multiVertexGeometry = (MultiVertexGeometry) geometry;
            if (result.m_vertexIndex1 >= 0 && result.m_vertexIndex2 >= 0) {
                Point point1 = multiVertexGeometry.getPoint(result.m_vertexIndex1);
                Point point2 = multiVertexGeometry.getPoint(result.m_vertexIndex2);
                return utf8Slice(format("%s at or near (%s %s) and (%s %s)", reasonText, point1.getX(), point1.getY(), point2.getX(), point2.getY()));
            }
            if (result.m_vertexIndex1 >= 0) {
                Point point = multiVertexGeometry.getPoint(result.m_vertexIndex1);
                return utf8Slice(format("%s at or near (%s %s)", reasonText, point.getX(), point.getY()));
            }
            return utf8Slice(reasonText);
        }
    }
}
Also used : GeometryCursor(com.esri.core.geometry.GeometryCursor) ListeningGeometryCursor(com.esri.core.geometry.ListeningGeometryCursor) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) NonSimpleResult(com.esri.core.geometry.NonSimpleResult) OGCLineString(com.esri.core.geometry.ogc.OGCLineString) MultiPoint(com.esri.core.geometry.MultiPoint) Point(com.esri.core.geometry.Point) OGCPoint(com.esri.core.geometry.ogc.OGCPoint) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

Description (io.prestosql.spi.function.Description)76 ScalarFunction (io.prestosql.spi.function.ScalarFunction)76 SqlType (io.prestosql.spi.function.SqlType)76 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 SqlNullable (io.prestosql.spi.function.SqlNullable)34 Slice (io.airlift.slice.Slice)18 LiteralParameters (io.prestosql.spi.function.LiteralParameters)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 Constraint (io.prestosql.type.Constraint)13 MultiPoint (com.esri.core.geometry.MultiPoint)12 BlockBuilder (io.prestosql.spi.block.BlockBuilder)11 PrestoException (io.prestosql.spi.PrestoException)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 GeometryType (io.prestosql.geospatial.GeometryType)6 Envelope (com.esri.core.geometry.Envelope)5 MultiPath (com.esri.core.geometry.MultiPath)5 Matcher (io.airlift.joni.Matcher)5 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)5