presto-main/src/main/java/io/prestosql/operator/scalar/CharacterStringCasts.java/CharacterStringCasts/varcharToCharSaturatedFloorCast
class CharacterStringCasts:    @ScalarOperator(OperatorType.SATURATED_FLOOR_CAST)
    @SqlType("char(y)")
    @LiteralParameters({"x", "y"})
    public static Slice varcharToCharSaturatedFloorCast(@LiteralParameter("y") long y, @SqlType("varchar(x)") Slice slice)
    {
        List<Integer> codePoints = new ArrayList<>(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 = trimTrailing(codePoints, ' ');
            List<Integer> codePointsTruncated = codePoints.stream()
                    .limit(y)
                    .collect(toImmutableList());
            return codePointsToSliceUtf8(codePointsTruncated);
        }

        /*
         * 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.
         */
        codePoints = 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 codePointsToSliceUtf8(nCopies(toIntExact(y), (int) '\0'));
        }

        codePoints = new ArrayList<>(codePoints);
        codePoints.set(codePoints.size() - 1, codePoints.get(codePoints.size() - 1) - 1);
        codePoints.addAll(nCopies(toIntExact(y) - codePoints.size(), Character.MAX_CODE_POINT));

        verify(codePoints.get(codePoints.size() - 1) != ' '); // no trailing spaces to trim

        return codePointsToSliceUtf8(codePoints);
    }