Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to_utf8 supports char type #20158

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toIntExact(x)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I have just updated the code.

}

// 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
Loading