You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "zhangstar333 (via GitHub)" <gi...@apache.org> on 2023/04/14 07:07:16 UTC

[GitHub] [doris] zhangstar333 commented on a diff in pull request #18580: [feature](function) Add new parameters to 'trim'.

zhangstar333 commented on code in PR #18580:
URL: https://github.com/apache/doris/pull/18580#discussion_r1166358574


##########
docs/zh-CN/docs/sql-manual/sql-functions/string-functions/ltrim.md:
##########
@@ -27,11 +27,11 @@ under the License.
 ## ltrim
 ### description
 #### Syntax
-
+ 
 `VARCHAR ltrim(VARCHAR str)`
 

Review Comment:
   here add the rhs parameters in function define



##########
be/src/vec/functions/function_string.cpp:
##########
@@ -338,38 +338,134 @@ struct InitcapImpl {
 struct NameTrim {
     static constexpr auto name = "trim";
 };
-
 struct NameLTrim {
     static constexpr auto name = "ltrim";
 };
-
 struct NameRTrim {
     static constexpr auto name = "rtrim";
 };
-
 template <bool is_ltrim, bool is_rtrim>
-struct TrimImpl {
-    static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets,
+struct TrimUtil {
+    static Status vector(const ColumnString::Chars& str_data,
+                         const ColumnString::Offsets& str_offsets, const StringRef& rhs,
                          ColumnString::Chars& res_data, ColumnString::Offsets& res_offsets) {
-        size_t offset_size = offsets.size();
-        res_offsets.resize(offsets.size());
-
+        size_t offset_size = str_offsets.size();
+        res_offsets.resize(str_offsets.size());
         for (size_t i = 0; i < offset_size; ++i) {
-            const char* raw_str = reinterpret_cast<const char*>(&data[offsets[i - 1]]);
-            ColumnString::Offset size = offsets[i] - offsets[i - 1];
+            const char* raw_str = reinterpret_cast<const char*>(&str_data[str_offsets[i - 1]]);
+            ColumnString::Offset size = str_offsets[i] - str_offsets[i - 1];
             StringRef str(raw_str, size);
             if constexpr (is_ltrim) {
-                str = simd::VStringFunctions::ltrim(str);
+                str = simd::VStringFunctions::ltrim(str, rhs);
             }
             if constexpr (is_rtrim) {
-                str = simd::VStringFunctions::rtrim(str);
+                str = simd::VStringFunctions::rtrim(str, rhs);
             }
             StringOP::push_value_string(std::string_view((char*)str.data, str.size), i, res_data,
                                         res_offsets);
         }
         return Status::OK();
     }
 };
+// This is an implementation of a parameter for the Trim function.
+template <bool is_ltrim, bool is_rtrim, typename Name>
+struct Trim1Impl {
+    static constexpr auto name = Name::name;
+
+    static DataTypes get_variadic_argument_types() { return {std::make_shared<DataTypeString>()}; }
+
+    static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                          size_t result, size_t input_rows_count) {
+        const ColumnPtr column = block.get_by_position(arguments[0]).column;
+        if (const ColumnString* col = check_and_get_column<ColumnString>(column.get())) {
+            auto col_res = ColumnString::create();
+            char blank[] = " ";
+            StringRef rhs(blank, 1);
+            TrimUtil<is_ltrim, is_rtrim>::vector(col->get_chars(), col->get_offsets(), rhs,
+                                                 col_res->get_chars(), col_res->get_offsets());
+            block.replace_by_position(result, std::move(col_res));
+        } else {
+            return Status::RuntimeError("Illegal column {} of argument of function {}",
+                                        block.get_by_position(arguments[0]).column->get_name(),
+                                        name);
+        }
+        return Status::OK();
+    }
+};
+
+// This is an implementation of two parameters for the Trim function.
+template <bool is_ltrim, bool is_rtrim, typename Name>
+struct Trim2Impl {
+    static constexpr auto name = Name::name;
+
+    static DataTypes get_variadic_argument_types() {
+        return {std::make_shared<DataTypeString>(), std::make_shared<DataTypeString>()};
+    }
+
+    static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
+                          size_t result, size_t input_rows_count) {
+        const ColumnPtr column = block.get_by_position(arguments[0]).column;
+        const auto& rcol =
+                assert_cast<const ColumnConst*>(block.get_by_position(arguments[1]).column.get())
+                        ->get_data_column_ptr();
+        if (const ColumnString* col = check_and_get_column<ColumnString>(column.get())) {
+            if (auto col_right = check_and_get_column<ColumnString>(rcol.get())) {

Review Comment:
   In my opinion, can directly assert_ cast get those column of string,



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org