Skip to content

Commit 652026b

Browse files
shenh062326jaystarshot
authored andcommitted
add mysql compatible function bit_length
1 parent 03b8cd9 commit 652026b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

presto-docs/src/main/sphinx/functions/string.rst

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ String Functions
2828
some languages. Specifically, this will return incorrect results for
2929
Lithuanian, Turkish and Azeri.
3030

31+
.. function:: bit_length(string) -> boolean
32+
33+
Returns the count of bits for the given ``string``.
34+
3135
.. function:: chr(n) -> varchar
3236

3337
Returns the Unicode code point ``n`` as a single character string.

presto-main/src/main/java/com/facebook/presto/operator/scalar/StringFunctions.java

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ public static long charLength(@LiteralParameter("x") long x, @SqlType("char(x)")
104104
return x;
105105
}
106106

107+
@Description("count of bits for the given string")
108+
@ScalarFunction("bit_length")
109+
@LiteralParameters("x")
110+
@SqlType(StandardTypes.BIGINT)
111+
public static long bitLength(@SqlType("varchar(x)") Slice slice)
112+
{
113+
return (long) slice.length() * 8;
114+
}
115+
107116
@Description("greedily removes occurrences of a pattern in a string")
108117
@ScalarFunction
109118
@LiteralParameters({"x", "y"})

presto-main/src/test/java/com/facebook/presto/operator/scalar/TestStringFunctions.java

+10
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ public void testLength()
136136
assertFunction("LENGTH('\u4FE1\u5FF5,\u7231,\u5E0C\u671B')", BIGINT, 7L);
137137
}
138138

139+
@Test
140+
public void testBitLength()
141+
{
142+
assertFunction("BIT_LENGTH('')", BIGINT, 0L);
143+
assertFunction("BIT_LENGTH('hello')", BIGINT, 40L);
144+
// Test bit_length for non-ASCII
145+
assertFunction("BIT_LENGTH('hell o\u00EF')", BIGINT, 64L);
146+
assertFunction("BIT_LENGTH('中123')", BIGINT, 48L);
147+
}
148+
139149
@Test
140150
public void testCharLength()
141151
{

0 commit comments

Comments
 (0)