Search in sources :

Example 11 with ScalarOperator

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);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 12 with ScalarOperator

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);
}
Also used : SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) IntList(it.unimi.dsi.fastutil.ints.IntList) ScalarOperator(io.trino.spi.function.ScalarOperator) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 13 with ScalarOperator

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);
}
Also used : IntList(it.unimi.dsi.fastutil.ints.IntList) ScalarOperator(io.trino.spi.function.ScalarOperator) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 14 with ScalarOperator

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();
}
Also used : DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Constraint(io.trino.type.Constraint) ScalarOperator(io.trino.spi.function.ScalarOperator) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Aggregations

ScalarOperator (io.trino.spi.function.ScalarOperator)14 SqlType (io.trino.spi.function.SqlType)13 TrinoException (io.trino.spi.TrinoException)9 JsonParser (com.fasterxml.jackson.core.JsonParser)8 SqlNullable (io.trino.spi.function.SqlNullable)8 JsonCastException (io.trino.util.JsonCastException)8 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)8 IOException (java.io.IOException)8 LiteralParameters (io.trino.spi.function.LiteralParameters)5 IntList (it.unimi.dsi.fastutil.ints.IntList)2 ImmutableList (com.google.common.collect.ImmutableList)1 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)1 Slice (io.airlift.slice.Slice)1 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)1 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)1 ScalarHeader (io.trino.operator.scalar.ScalarHeader)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1 Constraint (io.trino.type.Constraint)1 JsonUtil.currentTokenAsBoolean (io.trino.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (io.trino.util.JsonUtil.currentTokenAsDouble)1