use of io.trino.spi.function.ScalarOperator in project trino by trinodb.
the class JsonOperators method castToReal.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(REAL)
public static Long castToReal(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Long result = currentTokenAsReal(parser);
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to REAL");
return result;
} catch (IOException | JsonCastException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), REAL), e);
}
}
use of io.trino.spi.function.ScalarOperator in project trino by trinodb.
the class CharacterStringCasts method varcharToCharSaturatedFloorCast.
@ScalarOperator(OperatorType.SATURATED_FLOOR_CAST)
@SqlType("char(y)")
@LiteralParameters({ "x", "y" })
public static Slice varcharToCharSaturatedFloorCast(@LiteralParameter("y") long y, @SqlType("varchar(x)") Slice slice) {
IntList codePoints = toCodePoints(slice);
// if Varchar(x) value length (including spaces) is greater than y, we can just truncate it
if (codePoints.size() >= y) {
// char(y) slice representation doesn't contain trailing spaces
codePoints.size(Math.min(toIntExact(y), codePoints.size()));
trimTrailing(codePoints, ' ');
return codePointsToSliceUtf8(codePoints);
}
/*
* Value length is smaller than same-represented char(y) value because input varchar has length lower than y.
* We decrement last character in input (in fact, we decrement last non-zero character) and pad the value with
* max code point up to y characters.
*/
trimTrailing(codePoints, '\0');
if (codePoints.isEmpty()) {
// No non-zero characters in input and input is shorter than y. Input value is smaller than any char(4) casted back to varchar, so we return the smallest char(4) possible
return Slices.allocate(toIntExact(y));
}
codePoints.set(codePoints.size() - 1, codePoints.get(codePoints.size() - 1) - 1);
int toAdd = toIntExact(y) - codePoints.size();
for (int i = 0; i < toAdd; i++) {
codePoints.add(Character.MAX_CODE_POINT);
}
// no trailing spaces to trim
verify(codePoints.getInt(codePoints.size() - 1) != ' ');
return codePointsToSliceUtf8(codePoints);
}
use of io.trino.spi.function.ScalarOperator in project trino by trinodb.
the class CharacterStringCasts method varcharToVarcharSaturatedFloorCast.
@ScalarOperator(OperatorType.SATURATED_FLOOR_CAST)
@SqlType("varchar(y)")
@LiteralParameters({ "x", "y" })
public static Slice varcharToVarcharSaturatedFloorCast(@LiteralParameter("y") long y, @SqlType("varchar(x)") Slice slice) {
if (countCodePoints(slice) <= y) {
return slice;
}
IntList codePoints = toCodePoints(slice);
codePoints.size(toIntExact(y));
return codePointsToSliceUtf8(codePoints);
}
use of io.trino.spi.function.ScalarOperator in project trino by trinodb.
the class TimeOperators method castToVarchar.
@ScalarOperator(CAST)
@LiteralParameters({ "x", "p" })
@SqlType("varchar(x)")
public static Slice castToVarchar(@LiteralParameter("p") long precision, @SqlType("time(p)") long value) {
int size = (int) (// hour:minute:second
8 + // period
(precision > 0 ? 1 : 0) + // fraction
precision);
DynamicSliceOutput output = new DynamicSliceOutput(size);
String formatted = format("%02d:%02d:%02d", value / PICOSECONDS_PER_HOUR, (value / PICOSECONDS_PER_MINUTE) % MINUTES_PER_HOUR, (value / PICOSECONDS_PER_SECOND) % SECONDS_PER_MINUTE);
output.appendBytes(formatted.getBytes(UTF_8));
if (precision > 0) {
long scaledFraction = (value % PICOSECONDS_PER_SECOND) / scaleFactor((int) precision, MAX_PRECISION);
output.appendByte('.');
output.appendBytes(format("%0" + precision + "d", scaledFraction).getBytes(UTF_8));
}
return output.slice();
}
Aggregations