morningman commented on a change in pull request #3345: URL: https://github.com/apache/incubator-doris/pull/3345#discussion_r412258949
########## File path: docs/documentation/cn/sql-reference/sql-functions/string-functions/char_length_utf8.md ########## @@ -0,0 +1,47 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# length +## description +### Syntax + +`INT char_length_utf8(VARCHAR str)` + + +返回字符串的长度,对于多字节字符,返回的字符数。 + +## example + +``` +mysql> select length("abc"); ++---------------+ +| length('abc') | ++---------------+ +| 3 | ++---------------+ + +mysql> select length("中国"); ++------------------+ +| length('中国') | ++------------------+ +| 6 | Review comment: Should it be `2`? ########## File path: be/src/exprs/string_functions.cpp ########## @@ -19,37 +19,78 @@ #include <re2/re2.h> -#include "exprs/expr.h" #include "exprs/anyval_util.h" +#include "exprs/expr.h" +#include "math_functions.h" #include "runtime/string_value.hpp" #include "runtime/tuple_row.h" #include "util/url_parser.h" -#include "math_functions.h" // NOTE: be careful not to use string::append. It is not performant. namespace doris { void StringFunctions::init() { } +size_t get_utf8_byte_length(unsigned char byte) { + size_t char_size = 0; + if (byte >= 0xFC) { + char_size = 6; + } else if (byte >= 0xF8) { + char_size = 5; + } else if (byte >= 0xF0) { + char_size = 4; + } else if (byte >= 0xE0) { + char_size = 3; + } else if (byte >= 0xC0) { + char_size = 2; + } else { + char_size = 1; + } + return char_size; +} + // This behaves identically to the mysql implementation, namely: // - 1-indexed positions // - supported negative positions (count from the end of the string) // - [optional] len. No len indicates longest substr possible StringVal StringFunctions::substring( - FunctionContext* context, const StringVal& str, + FunctionContext* context, const StringVal& str, const IntVal& pos, const IntVal& len) { if (str.is_null || pos.is_null || len.is_null) { return StringVal::null(); } + if (len.val <= 0 || str.len == 0) { + return StringVal(); + } + + // create index indicate every char start byte + // e.g. "hello word 你好" => [0,1,2,3,4,5,6,7,8,9,10,11,14] 你 and 好 are 3 bytes + // why use a vector as index? It is unnecessary if there is no negative pos val, + // but if has pos is negative it is not easy to determin where to start, so need a + // index save every character's length + size_t byte_pos = 0; + std::vector<size_t> index; + for (size_t i = 0, char_size = 0; i < str.len; i += char_size) { Review comment: Can an optimization be done here? 1. Determine first, when `pos` is positive and greater than the length of str, return null directly, without for loop. 2. When `pos` is positive, can the for loop terminate after `pos + len` cycles? Instead of looping until the end of str? ########## File path: docs/documentation/cn/sql-reference/sql-functions/string-functions/char_length_utf8.md ########## @@ -0,0 +1,47 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# length Review comment: should it be `char_length_utf8` ########## File path: run-ut.sh ########## @@ -24,6 +24,7 @@ ROOT=`cd "$ROOT"; pwd` export DORIS_HOME=${ROOT} . ${DORIS_HOME}/env.sh +export BUILD_TYPE=DEBUG Review comment: Why using this type? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org