From e34247a0326709d7e8c772cf80f2d89f536b213d Mon Sep 17 00:00:00 2001 From: Junhyung Song Date: Thu, 6 Apr 2023 06:57:30 +0000 Subject: [PATCH] Support char type text for lpad function --- .../io/trino/operator/scalar/StringFunctions.java | 10 ++++++++++ .../io/trino/operator/scalar/TestStringFunctions.java | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/core/trino-main/src/main/java/io/trino/operator/scalar/StringFunctions.java b/core/trino-main/src/main/java/io/trino/operator/scalar/StringFunctions.java index 14262bcf9cbc..c18d0e566145 100644 --- a/core/trino-main/src/main/java/io/trino/operator/scalar/StringFunctions.java +++ b/core/trino-main/src/main/java/io/trino/operator/scalar/StringFunctions.java @@ -710,6 +710,16 @@ private static Slice pad(Slice text, long targetLength, Slice padString, int pad return buffer; } + @Description("Pads a string on the left") + @ScalarFunction("lpad") + @LiteralParameters({"x", "y"}) + @SqlType(StandardTypes.VARCHAR) + public static Slice leftPad(@LiteralParameter("x") long x, @SqlType("char(x)") Slice text, @SqlType(StandardTypes.BIGINT) long targetLength, @SqlType("varchar(y)") Slice padString) + { + text = padSpaces(text, toIntExact(x)); + return leftPad(text, targetLength, padString); + } + @Description("Pads a string on the left") @ScalarFunction("lpad") @LiteralParameters({"x", "y"}) diff --git a/core/trino-main/src/test/java/io/trino/operator/scalar/TestStringFunctions.java b/core/trino-main/src/test/java/io/trino/operator/scalar/TestStringFunctions.java index ece6cc31a1d3..ab3bb5bf7e28 100644 --- a/core/trino-main/src/test/java/io/trino/operator/scalar/TestStringFunctions.java +++ b/core/trino-main/src/test/java/io/trino/operator/scalar/TestStringFunctions.java @@ -2096,6 +2096,17 @@ public void testLeftPad() assertTrinoExceptionThrownBy(assertions.function("lpad", "'abc'", Long.toString(maxSize + 1), "''")::evaluate) .hasMessage("Target length must be in the range [0.." + maxSize + "]"); + + assertThat(assertions.function("lpad", "CHAR 'abc '", "6", "'def'")) + .matches("VARCHAR 'abc '"); + assertThat(assertions.function("lpad", "CHAR 'abc '", "4", "'def'")) + .matches("VARCHAR 'abc '"); + assertThat(assertions.function("lpad", "CHAR 'abc '", "8", "'def'")) + .matches("VARCHAR 'deabc '"); + assertThat(assertions.function("lpad", "CHAR 'abc '", "10", "'def'")) + .matches("VARCHAR 'defdabc '"); + assertThat(assertions.function("lpad", "CAST('abc' AS char(6))", "10", "'def'")) + .matches("VARCHAR 'defdabc '"); } @Test