Skip to content

Commit

Permalink
add mysql compatible function bit_length
Browse files Browse the repository at this point in the history
  • Loading branch information
shenh062326 authored and yuling.sh committed Feb 13, 2025
1 parent 099bd42 commit 4de2500
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions presto-docs/src/main/sphinx/functions/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ String Functions
some languages. Specifically, this will return incorrect results for
Lithuanian, Turkish and Azeri.

.. function:: bit_length(string) -> boolean

Returns the count of bits for the given ``string``.

.. function:: chr(n) -> varchar

Returns the Unicode code point ``n`` as a single character string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public static long charLength(@LiteralParameter("x") long x, @SqlType("char(x)")
return x;
}

@Description("count of bits for the given string")
@ScalarFunction("bit_length")
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long bitLength(@SqlType("varchar(x)") Slice slice)
{
return (long) slice.length() * 8;
}

@Description("greedily removes occurrences of a pattern in a string")
@ScalarFunction
@LiteralParameters({"x", "y"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public void testLength()
assertFunction("LENGTH('\u4FE1\u5FF5,\u7231,\u5E0C\u671B')", BIGINT, 7L);
}

@Test
public void testBitLength()
{
assertFunction("BIT_LENGTH('')", BIGINT, 0L);
assertFunction("BIT_LENGTH('hello')", BIGINT, 40L);
// Test bit_length for non-ASCII
assertFunction("BIT_LENGTH('hell o\u00EF')", BIGINT, 64L);
assertFunction("BIT_LENGTH('中123')", BIGINT, 48L);
}

@Test
public void testCharLength()
{
Expand Down

0 comments on commit 4de2500

Please sign in to comment.