Search in sources :

Example 1 with SqlType

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

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

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

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

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

SqlType (io.prestosql.spi.function.SqlType)138 ScalarFunction (io.prestosql.spi.function.ScalarFunction)96 Description (io.prestosql.spi.function.Description)76 SqlNullable (io.prestosql.spi.function.SqlNullable)52 BlockBuilder (io.prestosql.spi.block.BlockBuilder)42 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 PrestoException (io.prestosql.spi.PrestoException)27 Slice (io.airlift.slice.Slice)25 LiteralParameters (io.prestosql.spi.function.LiteralParameters)24 TypeParameter (io.prestosql.spi.function.TypeParameter)20 ScalarOperator (io.prestosql.spi.function.ScalarOperator)16 Constraint (io.prestosql.type.Constraint)16 JsonParser (com.fasterxml.jackson.core.JsonParser)15 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)15 IOException (java.io.IOException)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 Block (io.prestosql.spi.block.Block)9 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8