Search in sources :

Example 21 with SqlType

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

the class JsonOperators method castToVarchar.

@ScalarOperator(CAST)
@SqlNullable
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToVarchar(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Slice result = currentTokenAsVarchar(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to VARCHAR");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR), e);
    }
}
Also used : Slice(io.airlift.slice.Slice) JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 22 with SqlType

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

the class JsonOperators method castToBigint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BIGINT)
public static Long castToBigint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsBigint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BIGINT");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), BIGINT), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) SqlType(io.prestosql.spi.function.SqlType)

Example 23 with SqlType

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

the class JsonOperators method castToTinyint.

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

Example 24 with SqlType

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

the class TimestampWithTimeZoneOperators method castToTimeWithTimeZone.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long castToTimeWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) {
    long millis = modulo24Hour(castToTimestamp(value));
    ISOChronology localChronology = unpackChronology(value);
    // This is done due to inadequate TIME WITH TIME ZONE representation.
    return packDateTimeWithZone(millis - localChronology.getZone().getOffset(millis), unpackZoneKey(value));
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlType(io.prestosql.spi.function.SqlType)

Example 25 with SqlType

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

the class TimestampWithTimeZoneOperators method castToDate.

@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) {
    // round down the current timestamp to days
    ISOChronology chronology = unpackChronology(value);
    long date = chronology.dayOfYear().roundFloor(unpackMillisUtc(value));
    // date is currently midnight in timezone of the original value
    // convert to UTC
    long millis = date + chronology.getZone().getOffset(date);
    return TimeUnit.MILLISECONDS.toDays(millis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(io.prestosql.spi.function.ScalarOperator) 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