Skip to content

Commit

Permalink
Support char type text for lpad function
Browse files Browse the repository at this point in the history
  • Loading branch information
JunhyungSong authored and martint committed Jan 16, 2024
1 parent 1096866 commit b3b82ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3b82ba

Please sign in to comment.