diff --git a/be/src/exprs/string_functions.cpp b/be/src/exprs/string_functions.cpp index 2d1699444666c..f2c51ad52939d 100644 --- a/be/src/exprs/string_functions.cpp +++ b/be/src/exprs/string_functions.cpp @@ -2837,16 +2837,23 @@ static ColumnPtr regexp_replace_const(re2::RE2* const_re, const Columns& columns return result.build(ColumnHelper::is_all_const(columns)); } -static ColumnPtr regexp_replace_use_hyperscan(StringFunctionsState* state, const Columns& columns) { +static StatusOr regexp_replace_use_hyperscan(StringFunctionsState* state, const Columns& columns) { auto str_viewer = ColumnViewer(columns[0]); auto rpl_viewer = ColumnViewer(columns[2]); hs_scratch_t* scratch = nullptr; hs_error_t status; if ((status = hs_clone_scratch(state->scratch, &scratch)) != HS_SUCCESS) { - CHECK(false) << "ERROR: Unable to clone scratch space." - << " status: " << status; + return Status::InternalError(strings::Substitute("Unable to clone scratch space. status: $0", status)); } + DeferOp op([&] { + if (scratch != nullptr) { + hs_error_t st; + if ((st = hs_free_scratch(scratch)) != HS_SUCCESS) { + LOG(ERROR) << "free scratch space failure. status: " << st; + } + } + }); auto size = columns[0]->size(); ColumnBuilder result(size); @@ -2868,7 +2875,7 @@ static ColumnPtr regexp_replace_use_hyperscan(StringFunctionsState* state, const const char* data = (value_size) ? str_viewer.value(row).data : &StringFunctions::_DUMMY_STRING_FOR_EMPTY_PATTERN; - auto status = hs_scan( + auto st = hs_scan( // Use &_DUMMY_STRING_FOR_EMPTY_PATTERN instead of nullptr to avoid crash. state->database, data, value_size, 0, scratch, [](unsigned int id, unsigned long long from, unsigned long long to, unsigned int flags, @@ -2885,7 +2892,7 @@ static ColumnPtr regexp_replace_use_hyperscan(StringFunctionsState* state, const return 0; }, &match_info_chain); - DCHECK(status == HS_SUCCESS || status == HS_SCAN_TERMINATED) << " status: " << status; + DCHECK(st == HS_SUCCESS || st == HS_SCAN_TERMINATED) << " status: " << st; std::string result_str; result_str.reserve(value_size); diff --git a/test/sql/test_function/R/test_regex b/test/sql/test_function/R/test_regex index 8b9ee54c4ff2c..94bf6c8aaf0ec 100644 --- a/test/sql/test_function/R/test_regex +++ b/test/sql/test_function/R/test_regex @@ -1,4 +1,4 @@ --- name: test regex +-- name: test_regex CREATE TABLE `ts` ( `str` varchar(65533) NULL COMMENT "", `regex` varchar(65533) NULL COMMENT "", @@ -47,6 +47,10 @@ select regexp_replace('abc中文def', '[\\p{Han}]+', 'xx'); -- result: abcxxdef -- !result +SELECT regexp_replace('a b c', " ", "-"); +-- result: +a-b-c +-- !result select str, regex, replaced, regexp_replace(str, regex, replaced) from ts order by str, regex, replaced; -- result: None xx None diff --git a/test/sql/test_function/T/test_regex b/test/sql/test_function/T/test_regex index b06991a26f24b..53b25a897fd9b 100644 --- a/test/sql/test_function/T/test_regex +++ b/test/sql/test_function/T/test_regex @@ -1,4 +1,4 @@ --- name: test regex +-- name: test_regex CREATE TABLE `ts` ( `str` varchar(65533) NULL COMMENT "", @@ -20,6 +20,6 @@ select regexp_replace('', '', 'xx'); select regexp_replace(NULL, '', 'xx'); select regexp_replace('abc中文def', '中文', 'xx'); select regexp_replace('abc中文def', '[\\p{Han}]+', 'xx'); - +select regexp_replace('a b c', " ", "-"); select str, regex, replaced, regexp_replace(str, regex, replaced) from ts order by str, regex, replaced;