| 1 | #include "duckdb/function/scalar/string_functions.hpp" |
| 2 | |
| 3 | #include "duckdb/common/exception.hpp" |
| 4 | #include "duckdb/common/vector_operations/vector_operations.hpp" |
| 5 | #include "utf8proc.hpp" |
| 6 | |
| 7 | using namespace std; |
| 8 | |
| 9 | namespace duckdb { |
| 10 | |
| 11 | // length returns the size in characters |
| 12 | struct StringLengthOperator { |
| 13 | template <class TA, class TR> static inline TR Operation(TA input) { |
| 14 | return LengthFun::Length<TA, TR>(input); |
| 15 | } |
| 16 | }; |
| 17 | |
| 18 | // strlen returns the size in bytes |
| 19 | struct StrLenOperator { |
| 20 | template <class TA, class TR> static inline TR Operation(TA input) { |
| 21 | return input.GetSize(); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | // bitlen returns the size in bits |
| 26 | struct BitLenOperator { |
| 27 | template <class TA, class TR> static inline TR Operation(TA input) { |
| 28 | return 8 * input.GetSize(); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | void LengthFun::RegisterFunction(BuiltinFunctions &set) { |
| 33 | set.AddFunction({"length" , "len" }, |
| 34 | ScalarFunction({SQLType::VARCHAR}, SQLType::BIGINT, |
| 35 | ScalarFunction::UnaryFunction<string_t, int64_t, StringLengthOperator, true>)); |
| 36 | set.AddFunction(ScalarFunction("strlen" , {SQLType::VARCHAR}, SQLType::BIGINT, |
| 37 | ScalarFunction::UnaryFunction<string_t, int64_t, StrLenOperator, true>)); |
| 38 | set.AddFunction(ScalarFunction("bit_length" , {SQLType::VARCHAR}, SQLType::BIGINT, |
| 39 | ScalarFunction::UnaryFunction<string_t, int64_t, BitLenOperator, true>)); |
| 40 | // length for BLOB type |
| 41 | set.AddFunction(ScalarFunction("octet_length" , {SQLType::BLOB}, SQLType::BIGINT, |
| 42 | ScalarFunction::UnaryFunction<string_t, int64_t, StrLenOperator, true>)); |
| 43 | } |
| 44 | |
| 45 | struct UnicodeOperator { |
| 46 | template <class TA, class TR> static inline TR Operation(const TA &input) { |
| 47 | const auto str = reinterpret_cast<const utf8proc_uint8_t *>(input.GetData()); |
| 48 | const auto len = input.GetSize(); |
| 49 | utf8proc_int32_t codepoint; |
| 50 | (void)utf8proc_iterate(str, len, &codepoint); |
| 51 | return codepoint; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | void UnicodeFun::RegisterFunction(BuiltinFunctions &set) { |
| 56 | set.AddFunction(ScalarFunction("unicode" , {SQLType::VARCHAR}, SQLType::INTEGER, |
| 57 | ScalarFunction::UnaryFunction<string_t, int32_t, UnicodeOperator, true>)); |
| 58 | } |
| 59 | |
| 60 | } // namespace duckdb |
| 61 | |