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);
}
}
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);
}
}
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);
}
}
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));
}
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);
}
Aggregations