forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature](NGram BloomFilter Index) add new ngram bloom filter index t…
…o speed up like query (apache#11579) This PR implement the new bloom filter index: NGram bloom filter index, which was proposed in apache#10733. The new index can improve the like query performance greatly, from our some test case , can get order of magnitude improve. For how to use it you can check the docs in this PR, and the index based on the ```enable_function_pushdown```, you need set it to ```true```, to make the index work for like query.
- Loading branch information
Showing
44 changed files
with
1,720 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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. | ||
|
||
#include "itoken_extractor.h" | ||
|
||
#include "util/simd/vstring_function.h" | ||
|
||
namespace doris { | ||
|
||
bool NgramTokenExtractor::next_in_string(const char* data, size_t length, size_t* __restrict pos, | ||
size_t* __restrict token_start, | ||
size_t* __restrict token_length) const { | ||
*token_start = *pos; | ||
*token_length = 0; | ||
size_t code_points = 0; | ||
for (; code_points < n && *token_start + *token_length < length; ++code_points) { | ||
size_t sz = get_utf8_byte_length(static_cast<uint8_t>(data[*token_start + *token_length])); | ||
*token_length += sz; | ||
} | ||
*pos += get_utf8_byte_length(static_cast<uint8_t>(data[*pos])); | ||
return code_points == n; | ||
} | ||
|
||
bool NgramTokenExtractor::next_in_string_like(const char* data, size_t length, size_t* pos, | ||
std::string& token) const { | ||
token.clear(); | ||
|
||
size_t code_points = 0; | ||
bool escaped = false; | ||
for (size_t i = *pos; i < length;) { | ||
if (escaped && (data[i] == '%' || data[i] == '_' || data[i] == '\\')) { | ||
token += data[i]; | ||
++code_points; | ||
escaped = false; | ||
++i; | ||
} else if (!escaped && (data[i] == '%' || data[i] == '_')) { | ||
/// This token is too small, go to the next. | ||
token.clear(); | ||
code_points = 0; | ||
escaped = false; | ||
*pos = ++i; | ||
} else if (!escaped && data[i] == '\\') { | ||
escaped = true; | ||
++i; | ||
} else { | ||
const size_t sz = get_utf8_byte_length(static_cast<uint8_t>(data[i])); | ||
for (size_t j = 0; j < sz; ++j) { | ||
token += data[i + j]; | ||
} | ||
i += sz; | ||
++code_points; | ||
escaped = false; | ||
} | ||
|
||
if (code_points == n) { | ||
*pos += get_utf8_byte_length(static_cast<uint8_t>(data[*pos])); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} // namespace doris |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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. | ||
|
||
#ifndef DORIS_ITOKEN_EXTRACTOR_H | ||
#define DORIS_ITOKEN_EXTRACTOR_H | ||
|
||
#include <stddef.h> | ||
|
||
#include <string> | ||
|
||
#include "olap/rowset/segment_v2/bloom_filter.h" | ||
|
||
namespace doris { | ||
|
||
/// Interface for string parsers. | ||
struct ITokenExtractor { | ||
virtual ~ITokenExtractor() = default; | ||
|
||
/// Fast inplace implementation for regular use. | ||
/// Gets string (data ptr and len) and start position for extracting next token (state of extractor). | ||
/// Returns false if parsing is finished, otherwise returns true. | ||
virtual bool next_in_string(const char* data, size_t length, size_t* __restrict pos, | ||
size_t* __restrict token_start, | ||
size_t* __restrict token_length) const = 0; | ||
|
||
/// Special implementation for creating bloom filter for LIKE function. | ||
/// It skips unescaped `%` and `_` and supports escaping symbols, but it is less lightweight. | ||
virtual bool next_in_string_like(const char* data, size_t length, size_t* pos, | ||
std::string& out) const = 0; | ||
|
||
virtual void string_to_bloom_filter(const char* data, size_t length, | ||
segment_v2::BloomFilter& bloom_filter) const = 0; | ||
|
||
virtual bool string_like_to_bloom_filter(const char* data, size_t length, | ||
segment_v2::BloomFilter& bloom_filter) const = 0; | ||
}; | ||
|
||
template <typename Derived> | ||
class ITokenExtractorHelper : public ITokenExtractor { | ||
public: | ||
void string_to_bloom_filter(const char* data, size_t length, | ||
segment_v2::BloomFilter& bloom_filter) const override { | ||
size_t cur = 0; | ||
size_t token_start = 0; | ||
size_t token_len = 0; | ||
|
||
while (cur < length && static_cast<const Derived*>(this)->next_in_string( | ||
data, length, &cur, &token_start, &token_len)) | ||
bloom_filter.add_bytes(data + token_start, token_len); | ||
} | ||
|
||
bool string_like_to_bloom_filter(const char* data, size_t length, | ||
segment_v2::BloomFilter& bloom_filter) const override { | ||
size_t cur = 0; | ||
bool added = false; | ||
std::string token; | ||
while (cur < length && | ||
static_cast<const Derived*>(this)->next_in_string_like(data, length, &cur, token)) { | ||
bloom_filter.add_bytes(token.data(), token.size()); | ||
added = true; | ||
} | ||
|
||
return added; | ||
} | ||
}; | ||
|
||
/// Parser extracting all ngrams from string. | ||
struct NgramTokenExtractor final : public ITokenExtractorHelper<NgramTokenExtractor> { | ||
public: | ||
explicit NgramTokenExtractor(size_t n_) : n(n_) {} | ||
|
||
bool next_in_string(const char* data, size_t length, size_t* __restrict pos, | ||
size_t* __restrict token_start, | ||
size_t* __restrict token_length) const override; | ||
|
||
bool next_in_string_like(const char* data, size_t length, size_t* pos, | ||
std::string& token) const override; | ||
|
||
private: | ||
size_t n; | ||
}; | ||
} // namespace doris | ||
|
||
#endif //DORIS_ITOKEN_EXTRACTOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.