From 946a1db9f406cb59fe8836a5ddaa13018f27cc8b Mon Sep 17 00:00:00 2001 From: PHILO-HE Date: Thu, 30 Dec 2021 17:06:39 +0800 Subject: [PATCH] [NSE-661] Add trim expression support in WSCG (#664) * Add trim expression support in WSCG * Fix a bug * Format the code --- .../ext/expression_codegen_visitor.cc | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc b/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc index 75b278ff4..b4a807289 100644 --- a/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc +++ b/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc @@ -303,6 +303,37 @@ arrow::Status ExpressionCodegenVisitor::Visit(const gandiva::FunctionNode& node) prepare_ss << "if (" << check_str_ << ")" << std::endl; prepare_ss << codes_str_ << " = " << ss.str() << ";" << std::endl; prepare_str_ += prepare_ss.str(); + } else if (func_name.compare("btrim") == 0) { + codes_str_ = func_name + "_" + std::to_string(cur_func_id); + auto validity = codes_str_ + "_validity"; + real_codes_str_ = codes_str_; + real_validity_str_ = validity; + std::stringstream prepare_ss; + prepare_ss << GetCTypeString(node.return_type()) << " " << codes_str_ << ";" + << std::endl; + prepare_ss << "bool " << validity << " = " << child_visitor_list[0]->GetPreCheck() + << ";" << std::endl; + prepare_ss << "if (" << validity << ") {" << std::endl; + prepare_ss << "std::string arg = " << child_visitor_list[0]->GetResult() << ";" + << std::endl; + prepare_ss << "int start_index = 0, end_index = arg.length() - 1;" << std::endl; + prepare_ss << "while (start_index <= end_index && arg[start_index] == ' ') {" + << std::endl; + prepare_ss << "start_index++;" << std::endl; + prepare_ss << "}" << std::endl; + prepare_ss << "while (end_index >= start_index && arg[end_index] == ' ') {" + << std::endl; + prepare_ss << "end_index--;" << std::endl; + prepare_ss << "}" << std::endl; + prepare_ss << codes_str_ << " = arg.substr(start_index, end_index - start_index + 1);" + << std::endl; + prepare_ss << "}" << std::endl; + for (int i = 0; i < 1; i++) { + prepare_str_ += child_visitor_list[i]->GetPrepare(); + } + prepare_str_ += prepare_ss.str(); + check_str_ = validity; + } else if (func_name.compare("upper") == 0) { std::stringstream prepare_ss; auto child_name = child_visitor_list[0]->GetResult();