Skip to content

Commit

Permalink
Function to_utf8 supports char type
Browse files Browse the repository at this point in the history
  • Loading branch information
borderlayout committed Jan 2, 2024
1 parent 5912370 commit 870d9e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.trino.spi.function.ScalarOperator;
import io.trino.spi.function.SqlNullable;
import io.trino.spi.function.SqlType;
import io.trino.spi.type.Chars;
import io.trino.spi.type.StandardTypes;
import io.trino.type.CodePointsType;
import io.trino.type.Constraint;
Expand Down Expand Up @@ -880,6 +881,15 @@ public static Slice toUtf8(@SqlType("varchar(x)") Slice slice)
return slice;
}

@Description("Encodes the string to UTF-8")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.VARBINARY)
public static Slice toUtf8(@LiteralParameter("x") long x, @SqlType("char(x)") Slice slice)
{
return Chars.padSpaces(slice, (int) x);
}

// TODO: implement N arguments char concat
@Description("Concatenates given character strings")
@ScalarFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,10 @@ public void testFromUtf8()
.hasType(VARCHAR)
.isEqualTo("hello");

assertThat(assertions.function("from_utf8", "to_utf8(cast('hello' as char(10)))"))
.hasType(VARCHAR)
.isEqualTo("hello ");

assertThat(assertions.function("from_utf8", "from_hex('58BF')"))
.hasType(VARCHAR)
.isEqualTo("X\uFFFD");
Expand Down Expand Up @@ -2269,6 +2273,12 @@ public void testFromUtf8()

assertTrinoExceptionThrownBy(assertions.function("from_utf8", "to_utf8('hello')", "1114112")::evaluate)
.hasErrorCode(INVALID_FUNCTION_ARGUMENT);

assertTrinoExceptionThrownBy(assertions.function("from_utf8", "to_utf8(cast('hello' as char(10)))", "'foo'")::evaluate)
.hasErrorCode(INVALID_FUNCTION_ARGUMENT);

assertTrinoExceptionThrownBy(assertions.function("from_utf8", "to_utf8(cast('hello' as char(10)))", "1114112")::evaluate)
.hasErrorCode(INVALID_FUNCTION_ARGUMENT);
}

@Test
Expand Down

0 comments on commit 870d9e4

Please sign in to comment.