Search in sources :

Example 1 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction 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 ScalarFunction

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

the class CreateHll method createHll.

@ScalarFunction
@SqlType(StandardTypes.HYPER_LOG_LOG)
public static Slice createHll(@SqlType(StandardTypes.BIGINT) long value) {
    HyperLogLog hll = HyperLogLog.newInstance(4096);
    hll.add(value);
    return hll.serialize();
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with ScalarFunction

use of io.prestosql.spi.function.ScalarFunction 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 4 with ScalarFunction

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

the class DateTimeFunctions method toUnixTimeFromTimestampWithTimeZone.

@ScalarFunction("to_unixtime")
@SqlType(StandardTypes.DOUBLE)
public static double toUnixTimeFromTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) {
    ISOChronology localChronology = getChronology(session.getTimeZoneKey());
    long localMillis = localChronology.getZone().convertUTCToLocal(unpackMillisUtc(timestampWithTimeZone));
    ISOChronology zoneChronology = unpackChronology(timestampWithTimeZone);
    long zoneMillis = zoneChronology.getZone().convertLocalToUTC(localMillis, false);
    return zoneMillis / 1000.0;
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with ScalarFunction

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

the class DateTimeFunctions method toISO8601FromTimestampWithTimeZone.

@ScalarFunction("to_iso8601")
@SqlType("varchar(35)")
public static // the maximum year represented by 64bits timestamp is ~584944387 it may require up to 35 characters.
Slice toISO8601FromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) {
    long millisUtc = unpackMillisUtc(timestampWithTimeZone);
    DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withChronology(getChronology(unpackZoneKey(timestampWithTimeZone)));
    return utf8Slice(formatter.print(millisUtc));
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

ScalarFunction (io.prestosql.spi.function.ScalarFunction)97 SqlType (io.prestosql.spi.function.SqlType)96 Description (io.prestosql.spi.function.Description)76 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 SqlNullable (io.prestosql.spi.function.SqlNullable)40 LiteralParameters (io.prestosql.spi.function.LiteralParameters)20 Slice (io.airlift.slice.Slice)18 BlockBuilder (io.prestosql.spi.block.BlockBuilder)17 Constraint (io.prestosql.type.Constraint)16 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 PrestoException (io.prestosql.spi.PrestoException)12 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)8 JsonParser (com.fasterxml.jackson.core.JsonParser)7 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)7 IOException (java.io.IOException)7 JsonToken (com.fasterxml.jackson.core.JsonToken)6 GeometryType (io.prestosql.geospatial.GeometryType)6