Skip to content

Commit

Permalink
[fix](hyperscan) Fix hyper scan fall back to re2 (#44547)
Browse files Browse the repository at this point in the history
* Core modification
When hyper scan failed, we should not set_error in FunctionContext.
Since set_error will try cancel query, but actually we want to fall back
to re2 in this case.

* Some refactor
Rename FunctionRegexp so that we can distinguish regexp match with
regexp_extract.

* Reproduce
```cpp
SELECT * FROM regexp_test_chinese WHERE city REGEXP "^上海|^北京" ORDER BY id;
```
Note, the `|` in above sql is a Chinese character.
  • Loading branch information
zhiqiang-hhhh authored Nov 27, 2024
1 parent aca0eaa commit df5bfe8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
15 changes: 8 additions & 7 deletions be/src/vec/functions/function_regexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,13 @@ struct RegexpExtractAllImpl {
}
};

// template FunctionRegexpFunctionality is used for regexp_xxxx series functions, not for regexp match.
template <typename Impl>
class FunctionRegexp : public IFunction {
class FunctionRegexpFunctionality : public IFunction {
public:
static constexpr auto name = Impl::name;

static FunctionPtr create() { return std::make_shared<FunctionRegexp>(); }
static FunctionPtr create() { return std::make_shared<FunctionRegexpFunctionality>(); }

String get_name() const override { return name; }

Expand Down Expand Up @@ -486,11 +487,11 @@ class FunctionRegexp : public IFunction {
};

void register_function_regexp_extract(SimpleFunctionFactory& factory) {
factory.register_function<FunctionRegexp<RegexpReplaceImpl>>();
factory.register_function<FunctionRegexp<RegexpExtractImpl<true>>>();
factory.register_function<FunctionRegexp<RegexpExtractImpl<false>>>();
factory.register_function<FunctionRegexp<RegexpReplaceOneImpl>>();
factory.register_function<FunctionRegexp<RegexpExtractAllImpl>>();
factory.register_function<FunctionRegexpFunctionality<RegexpReplaceImpl>>();
factory.register_function<FunctionRegexpFunctionality<RegexpExtractImpl<true>>>();
factory.register_function<FunctionRegexpFunctionality<RegexpExtractImpl<false>>>();
factory.register_function<FunctionRegexpFunctionality<RegexpReplaceOneImpl>>();
factory.register_function<FunctionRegexpFunctionality<RegexpExtractAllImpl>>();
}

} // namespace doris::vectorized
21 changes: 9 additions & 12 deletions be/src/vec/functions/like.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,23 +486,19 @@ Status FunctionLikeBase::hs_prepare(FunctionContext* context, const char* expres

if (res != HS_SUCCESS) {
*database = nullptr;
if (context) {
context->set_error("hs_compile regex pattern error");
}
return Status::RuntimeError("hs_compile regex pattern error:" +
std::string(compile_err->message));
std::string error_message = compile_err->message;
hs_free_compile_error(compile_err);
// Do not call FunctionContext::set_error here, since we do not want to cancel the query here.
return Status::RuntimeError<false>("hs_compile regex pattern error:" + error_message);
}
hs_free_compile_error(compile_err);

if (hs_alloc_scratch(*database, scratch) != HS_SUCCESS) {
hs_free_database(*database);
*database = nullptr;
*scratch = nullptr;
if (context) {
context->set_error("hs_alloc_scratch allocate scratch space error");
}
return Status::RuntimeError("hs_alloc_scratch allocate scratch space error");
// Do not call FunctionContext::set_error here, since we do not want to cancel the query here.
return Status::RuntimeError<false>("hs_alloc_scratch allocate scratch space error");
}

return Status::OK();
Expand Down Expand Up @@ -934,7 +930,8 @@ Status FunctionLike::open(FunctionContext* context, FunctionContext::FunctionSta
return Status::OK();
}

Status FunctionRegexp::open(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
Status FunctionRegexpLike::open(FunctionContext* context,
FunctionContext::FunctionStateScope scope) {
if (scope != FunctionContext::THREAD_LOCAL) {
return Status::OK();
}
Expand Down Expand Up @@ -1001,8 +998,8 @@ void register_function_like(SimpleFunctionFactory& factory) {
}

void register_function_regexp(SimpleFunctionFactory& factory) {
factory.register_function<FunctionRegexp>();
factory.register_alias(FunctionRegexp::name, FunctionRegexp::alias);
factory.register_function<FunctionRegexpLike>();
factory.register_alias(FunctionRegexpLike::name, FunctionRegexpLike::alias);
}

} // namespace doris::vectorized
4 changes: 2 additions & 2 deletions be/src/vec/functions/like.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ class FunctionLike : public FunctionLikeBase {
static void remove_escape_character(std::string* search_string);
};

class FunctionRegexp : public FunctionLikeBase {
class FunctionRegexpLike : public FunctionLikeBase {
public:
static constexpr auto name = "regexp";
static constexpr auto alias = "rlike";

static FunctionPtr create() { return std::make_shared<FunctionRegexp>(); }
static FunctionPtr create() { return std::make_shared<FunctionRegexpLike>(); }

String get_name() const override { return name; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql_regexp --

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

suite("test_regexp_chinese") {
sql "DROP TABLE IF EXISTS regexp_test_chinese;"
sql """
CREATE TABLE regexp_test_chinese (
id int NULL DEFAULT "0",
city varchar(50) NOT NULL DEFAULT ""
) DISTRIBUTED BY HASH(id) BUCKETS 5 properties("replication_num" = "1");
"""

sql """
INSERT INTO regexp_test_chinese VALUES(1, "上海"),(2, "深圳"),(3, "上海测试"), (4, "北京测试");
"""

qt_sql_regexp """
SELECT * FROM regexp_test_chinese WHERE city REGEXP "^上海|^北京" ORDER BY id;
"""
}

0 comments on commit df5bfe8

Please sign in to comment.