use of com.facebook.presto.spi.function.SqlType 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));
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class ColorFunctions method color.
@ScalarFunction
@LiteralParameters("x")
@SqlType(ColorType.NAME)
public static long color(@SqlType("varchar(x)") Slice color) {
int rgb = parseRgb(color);
if (rgb != -1) {
return rgb;
}
// encode system colors (0-15) as negative values, offset by one
try {
SystemColor systemColor = SystemColor.valueOf(upper(color).toStringUtf8());
int index = systemColor.getIndex();
return -(index + 1);
} catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid color: '%s'", color.toStringUtf8()), e);
}
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
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));
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class ArrayLessThanOrEqualOperator method lessThanOrEqualLong.
@TypeParameter("E")
@TypeParameterSpecialization(name = "E", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanOrEqualLong(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
int index = 0;
while (index < len) {
checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
long leftElement = type.getLong(leftArray, index);
long rightElement = type.getLong(rightArray, index);
try {
if ((boolean) lessThanFunction.invokeExact(leftElement, rightElement)) {
return true;
}
if ((boolean) lessThanFunction.invokeExact(rightElement, leftElement)) {
return false;
}
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
}
index++;
}
return leftArray.getPositionCount() <= rightArray.getPositionCount();
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BOOLEAN) boolean value) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if (((token == VALUE_TRUE) && value) || ((token == VALUE_FALSE) && (!value))) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
Aggregations