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 18, 2025
1 parent 730842b commit 209c23c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions velox/functions/prestosql/StringFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@

namespace facebook::velox::functions {

/// Returns the count of bits for the given string.
template <typename T>
struct BitLengthFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(int64_t& result, const StringView& input) {
result = input.size() * 8;
}
};

/// chr(n) → varchar
/// Returns the Unicode code point n as a single character string.
template <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void registerSimpleFunctions(const std::string& prefix) {
using namespace stringImpl;

// Register string functions.
registerFunction<BitLengthFunction, Varchar, int64_t>({prefix + "bit_length"});
registerFunction<ChrFunction, Varchar, int64_t>({prefix + "chr"});
registerFunction<CodePointFunction, int32_t, Varchar>({prefix + "codepoint"});
registerFunction<HammingDistanceFunction, int64_t, Varchar, Varchar>(
Expand Down
8 changes: 8 additions & 0 deletions velox/functions/prestosql/tests/StringFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,14 @@ TEST_F(StringFunctionsTest, normalize) {
"Normalization form must be one of [NFD, NFC, NFKD, NFKC]");
}

TEST_F(StringFunctionsTest, bitLength) {
const auto runBitLength = [&](std::optional<std::string> value) {
return evaluateOnce<int64_t>("bit_length(c0)", value);
};
EXPECT_EQ(8, runBitLength("3"));
EXPECT_EQ(40, runBitLength("ab中"));
}

TEST_F(StringFunctionsTest, trail) {
auto trail = [&](std::optional<std::string> string,
std::optional<int32_t> N) {
Expand Down

0 comments on commit 209c23c

Please sign in to comment.