From b053a207efc476fe3fc4cd9653056f8e78f51e05 Mon Sep 17 00:00:00 2001 From: philo Date: Mon, 27 Dec 2021 17:32:39 +0800 Subject: [PATCH 1/3] Add trim expression support in WSCG --- .../ext/expression_codegen_visitor.cc | 27 +++++++++++++++++++ 1 file changed, 27 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 0966c2e30..e3a9b715b 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 @@ -289,6 +289,33 @@ 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] << ";"; + 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(); From d579b13e2671079884df1ae7554c6f9f9dfcb03e Mon Sep 17 00:00:00 2001 From: philo Date: Mon, 27 Dec 2021 18:06:07 +0800 Subject: [PATCH 2/3] Fix a bug --- .../src/codegen/arrow_compute/ext/expression_codegen_visitor.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e3a9b715b..92a58261f 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 @@ -300,7 +300,7 @@ arrow::Status ExpressionCodegenVisitor::Visit(const gandiva::FunctionNode& node) 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] << ";"; + 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; From 6b9ccbf1a39e10eb227f9628298f3bd61e4d7482 Mon Sep 17 00:00:00 2001 From: philo Date: Mon, 27 Dec 2021 18:09:30 +0800 Subject: [PATCH 3/3] Format the code --- .../arrow_compute/ext/expression_codegen_visitor.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 92a58261f..9c79730fa 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 @@ -300,15 +300,19 @@ arrow::Status ExpressionCodegenVisitor::Visit(const gandiva::FunctionNode& node) 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 << "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 << "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 << "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 << 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();