Search in sources :

Example 1 with ScalarOperator

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

the class DateOperators method castToTimestampWithTimeZone.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) {
    long utcMillis = TimeUnit.DAYS.toMillis(value);
    // date is encoded as milliseconds at midnight in UTC
    // convert to midnight if the session timezone
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    long millis = utcMillis - chronology.getZone().getOffset(utcMillis);
    return packDateTimeWithZone(millis, session.getTimeZoneKey());
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with ScalarOperator

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

the class DateOperators method castToTimestamp.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP)
public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) {
    long utcMillis = TimeUnit.DAYS.toMillis(value);
    // date is encoded as milliseconds at midnight in UTC
    // convert to midnight if the session timezone
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    return utcMillis - chronology.getZone().getOffset(utcMillis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with ScalarOperator

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

the class TimestampOperators method castToDate.

@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) {
    // round down the current timestamp to days
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    long date = chronology.dayOfYear().roundFloor(value);
    // date is currently midnight in timezone of the session
    // convert to UTC
    long millis = date + chronology.getZone().getOffset(date);
    return TimeUnit.MILLISECONDS.toDays(millis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with ScalarOperator

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

the class CharacterStringCasts method varcharToCharSaturatedFloorCast.

@ScalarOperator(OperatorType.SATURATED_FLOOR_CAST)
@SqlType("char(y)")
@LiteralParameters({ "x", "y" })
public static // Char(y) value that is smaller than the original Varchar(x) value. This is fine though for usage in TupleDomainTranslator.
Slice varcharToCharSaturatedFloorCast(@LiteralParameter("y") Long y, @SqlType("varchar(x)") Slice slice) {
    Slice trimmedSlice = trimSpaces(slice);
    int trimmedTextLength = countCodePoints(trimmedSlice);
    int numberOfTrailingSpaces = slice.length() - trimmedSlice.length();
    // if Varchar(x) value length (including spaces) is greater than y, we can just truncate it
    if (trimmedTextLength + numberOfTrailingSpaces >= y) {
        return truncateToLength(trimmedSlice, y.intValue());
    }
    if (trimmedTextLength == 0) {
        return EMPTY_SLICE;
    }
    // and also remove one additional trailing character to get smaller Char(y) value
    return trimmedSlice.slice(0, offsetOfCodePoint(trimmedSlice, trimmedTextLength - 1));
}
Also used : Slice(io.airlift.slice.Slice) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with ScalarOperator

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

the class JsonOperators method castToInteger.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(INTEGER)
public static Long castToInteger(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsInteger(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to INTEGER");
        return result;
    } catch (PrestoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (ArithmeticException | IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)26 SqlType (com.facebook.presto.spi.function.SqlType)24 PrestoException (com.facebook.presto.spi.PrestoException)13 ISOChronology (org.joda.time.chrono.ISOChronology)9 SqlNullable (com.facebook.presto.spi.function.SqlNullable)8 JsonCastException (com.facebook.presto.util.JsonCastException)8 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)8 JsonParser (com.fasterxml.jackson.core.JsonParser)8 IOException (java.io.IOException)8 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)5 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)4 ImmutableList (com.google.common.collect.ImmutableList)2 Slice (io.airlift.slice.Slice)2 UnknownHostException (java.net.UnknownHostException)2 HyperLogLog (com.facebook.airlift.stats.cardinality.HyperLogLog)1 ScalarHeader (com.facebook.presto.operator.scalar.ScalarHeader)1 JsonUtil.currentTokenAsBoolean (com.facebook.presto.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (com.facebook.presto.util.JsonUtil.currentTokenAsDouble)1 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)1 Method (java.lang.reflect.Method)1