hello-stephen commented on code in PR #70352:
URL: https://github.com/apache/doris/pull/70352#discussion_r4698369358
##########
fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java:
##########
@@ -3440,25 +3501,86 @@ public void buildOrDeleteTableInvertedIndices(Database
db, OlapTable olapTable,
}
}
+ // Inverted index supports light schema change in both cloud and local
mode.
+ // NGRAM_BF supports it only in cloud mode.
+ private static boolean isLightDropIndexSupported(Index index, boolean
enableAddIndexForNewData) {
+ if (Config.isCloudMode()) {
+ return enableAddIndexForNewData
+ && (index.getIndexType() == IndexType.NGRAM_BF
+ || index.getIndexType() == IndexType.INVERTED);
+ }
+ return index.getIndexType() == IndexType.INVERTED ||
index.getIndexType() == IndexType.ANN;
+ }
+
+ // Reject conflicting jobs and mixed add/drop operations before publishing
index metadata.
+ private void checkLightDropIndexPreconditions(Database db, OlapTable
olapTable, List<AlterClause> alterClauses,
+ boolean enableAddIndexForNewData) throws UserException {
+ if (!Config.enable_light_index_change) {
+ return;
+ }
+ List<Index> droppedIndexes = new ArrayList<>();
+ for (AlterClause alterClause : alterClauses) {
+ if (!(alterClause instanceof DropIndexClause)
+ || ((DropIndexClause) alterClause).hasPartitionSpec()) {
+ continue;
+ }
+ String indexName = ((DropIndexClause) alterClause).getIndexName();
+ olapTable.getIndexes().stream()
+ .filter(index ->
index.getIndexName().equalsIgnoreCase(indexName))
+ .filter(index -> isLightDropIndexSupported(index,
enableAddIndexForNewData))
+ .findFirst()
+ .ifPresent(droppedIndexes::add);
+ }
+ if (droppedIndexes.isEmpty()) {
+ return;
+ }
+ if (alterClauses.stream().anyMatch(alterClause -> alterClause
instanceof CreateIndexClause
+ && ((CreateIndexClause)
alterClause).getIndex().isLightAddIndexSupported(enableAddIndexForNewData))) {
+ throw new DdlException("Can not add and drop indexes in one ALTER
statement,"
+ + " please run them in separate statements.");
+ }
+ getCloudClusterName();
+ checkNoIndexChangeJobOnPartitions(db, olapTable, droppedIndexes,
olapTable.getPartitionNames());
+ }
+
+ // Throws if any of the partitions still has an unfinished index change
job on the base index.
+ private void checkNoIndexChangeJobOnPartitions(Database db, OlapTable
olapTable, List<Index> alterIndexes,
+ Set<String> partitionNames) throws DdlException {
+ // Scan the retained job history once instead of once per partition,
the table write lock is held here.
+ for (IndexChangeJob indexChangeJob :
ImmutableList.copyOf(indexChangeJobs.values())) {
+ // Cheap partition check first, the predicate compares index lists.
+ if (partitionNames.contains(indexChangeJob.getPartitionName())
Review Comment:
[P2] Compare partition job conflicts by ID after a rename
A pending `BUILD INDEX idx ON t PARTITION(p1)` can be followed by `ALTER
TABLE t RENAME PARTITION p1 p2` and `DROP INDEX idx ON t PARTITION(p2)`. Rename
is allowed while index jobs run and leaves the job's captured name as `p1`, so
this predicate and `hasIndexChangeJobOnPartition` both miss the conflict. The
jobs still target the same partition ID and are scheduled independently; if
DROP executes first, the earlier BUILD restores the index after the deletion.
Compare stable partition IDs in both checks, or prevent/update renames while
jobs are unfinished.
##########
be/src/exprs/function/match.cpp:
##########
@@ -53,6 +53,253 @@ const InvertedIndexAnalyzerCtx*
get_match_analyzer_ctx(FunctionContext* context)
return analyzer_ctx;
}
+enum class PhraseMode { EXACT, PREFIX, EDGE };
+
+class StreamingPhraseMatcher {
+public:
+ StreamingPhraseMatcher(const std::vector<segment_v2::TermInfo>&
query_tokens, PhraseMode mode)
+ : _mode(mode),
+ _query_size(query_tokens.size()),
+ _last_word((_query_size - 1) / 64),
+ _last_bit(uint64_t {1} << ((_query_size - 1) % 64)),
+ _state(_last_word + 1, 0),
+ _pending_masks(_last_word + 1, 0),
+ _active_marks(_last_word + 1, 0),
+ _first_term(query_tokens.front().get_single_term()),
+ _last_term(query_tokens.back().get_single_term()) {
+ for (size_t pos = 0; pos < _query_size; ++pos) {
+ if ((_mode == PhraseMode::PREFIX && pos == _query_size - 1) ||
+ (_mode == PhraseMode::EDGE && (pos == 0 || pos == _query_size
- 1))) {
+ continue;
+ }
+ auto& words = _exact_masks[query_tokens[pos].get_single_term()];
+ const size_t word = pos / 64;
+ const uint64_t bit = uint64_t {1} << (pos % 64);
+ if (!words.empty() && words.back().first == word) {
+ words.back().second |= bit;
+ } else {
+ words.emplace_back(word, bit);
+ }
+ }
+ }
+
+ void reset_row() {
+ for (size_t word : _pending_touched) {
+ _pending_masks[word] = 0;
+ }
+ _pending_touched.clear();
+ _pending_sorted = true;
+ _pending_position = -1;
+ _previous_position = -1;
+ _active_words.clear();
+ ++_generation;
+ }
+
+ bool feed(const std::string& term, int64_t position) {
+ if (_pending_position >= 0 && position != _pending_position) {
+ if (advance()) {
+ return true;
+ }
+ }
+ if (_pending_position < 0) {
+ start_position(position);
+ }
+ const auto mask_it = _exact_masks.find(term);
+ if (mask_it != _exact_masks.end()) {
+ const auto& mask_words = mask_it->second;
+ if (mask_words.size() / 4 > _candidate_words.size()) {
+ for (size_t word : _candidate_words) {
+ auto it = std::lower_bound(
+ mask_words.begin(), mask_words.end(), word,
+ [](const auto& entry, size_t value) { return
entry.first < value; });
+ if (it != mask_words.end() && it->first == word) {
+ add_pending_mask(word, it->second);
+ }
+ }
+ } else {
+ size_t candidate = 0;
+ for (const auto& [word, mask] : mask_words) {
+ while (candidate < _candidate_words.size() &&
+ _candidate_words[candidate] < word) {
+ ++candidate;
+ }
+ if (candidate == _candidate_words.size()) {
+ break;
+ }
+ if (_candidate_words[candidate] == word) {
+ add_pending_mask(word, mask);
+ }
+ }
+ }
+ }
+ const bool first_matches = _mode == PhraseMode::EDGE &&
+ (_query_size == 1 ? term.find(_first_term)
!= std::string::npos
+ :
term.ends_with(_first_term));
+ const bool last_matches =
+ (_mode == PhraseMode::PREFIX || (_mode == PhraseMode::EDGE &&
_query_size > 1)) &&
+ term.starts_with(_last_term);
+
+ if (first_matches) {
+ add_pending_mask(0, 1);
+ }
+ if (last_matches &&
+ std::binary_search(_candidate_words.begin(),
_candidate_words.end(), _last_word)) {
+ add_pending_mask(_last_word, _last_bit);
+ }
+ return false;
+ }
+
+ bool finish() { return _pending_position >= 0 && advance(); }
+
+private:
+ void start_position(int64_t position) {
+ if (_previous_position >= 0 && position != _previous_position + 1) {
+ _active_words.clear();
+ ++_generation;
+ }
+ _pending_position = position;
+ _candidate_words.clear();
+ _candidate_words.push_back(0);
+ for (size_t word : _active_words) {
+ if (_candidate_words.back() != word) {
+ _candidate_words.push_back(word);
+ }
+ if (word < _last_word && (_state[word] >> 63) != 0) {
+ _candidate_words.push_back(word + 1);
+ }
+ }
+ }
+
+ void add_pending_mask(size_t word, uint64_t mask) {
+ if (_pending_masks[word] == 0) {
+ if (!_pending_touched.empty() && word < _pending_touched.back()) {
+ _pending_sorted = false;
+ }
+ _pending_touched.push_back(word);
+ }
+ _pending_masks[word] |= mask;
+ }
+
+ bool advance() {
+ const int64_t position = _pending_position;
+ _pending_position = -1;
+ _previous_position = position;
+
+ const uint64_t old_generation = _generation++;
+ _next_active_words.clear();
+ if (!_pending_touched.empty()) {
+ if (!_pending_sorted) {
+ std::sort(_pending_touched.begin(), _pending_touched.end());
+ }
+ for (auto it = _pending_touched.rbegin(); it !=
_pending_touched.rend(); ++it) {
+ const size_t word = *it;
+ const uint64_t previous = _active_marks[word] ==
old_generation ? _state[word] : 0;
+ const uint64_t carry = word > 0 && _active_marks[word - 1] ==
old_generation
+ ? _state[word - 1] >> 63
+ : 0;
+ const uint64_t next =
+ ((previous << 1) | carry | uint64_t {word == 0}) &
_pending_masks[word];
+ if (next != 0) {
+ _state[word] = next;
+ _active_marks[word] = _generation;
+ _next_active_words.push_back(word);
+ }
+ }
+ }
+ for (size_t word : _pending_touched) {
+ _pending_masks[word] = 0;
+ }
+ _pending_touched.clear();
+ _pending_sorted = true;
+ std::reverse(_next_active_words.begin(), _next_active_words.end());
+ _active_words.swap(_next_active_words);
+ return !_active_words.empty() && _active_words.back() == _last_word &&
+ (_state[_last_word] & _last_bit) != 0;
+ }
+
+ PhraseMode _mode;
+ size_t _query_size;
+ size_t _last_word;
+ uint64_t _last_bit;
+ std::vector<uint64_t> _state;
+ std::vector<uint64_t> _pending_masks;
+ std::vector<uint64_t> _active_marks;
+ std::vector<size_t> _pending_touched;
+ std::vector<size_t> _candidate_words;
+ std::vector<size_t> _active_words;
+ std::vector<size_t> _next_active_words;
+ std::string _first_term;
+ std::string _last_term;
+ std::unordered_map<std::string, std::vector<std::pair<size_t, uint64_t>>>
_exact_masks;
+ int64_t _pending_position = -1;
+ int64_t _previous_position = -1;
+ uint64_t _generation = 1;
+ bool _pending_sorted = true;
+};
+
+template <typename Callback>
+bool for_each_data_element_tokens(const FunctionMatchBase& function, const
std::string& column_name,
+ const InvertedIndexAnalyzerCtx* analyzer_ctx,
+ const ColumnString* string_col, size_t row,
+ const ColumnArray::Offsets64* array_offsets,
+ const ColumnUInt8::Container*
array_element_null_map,
+ Callback&& callback) {
+ const size_t begin = array_offsets ? (row == 0 ? 0 : (*array_offsets)[row
- 1]) : row;
+ const size_t end = array_offsets ? (*array_offsets)[row] : row + 1;
+ const bool keyword = analyzer_ctx &&
+ (!analyzer_ctx->requires_analysis() ||
analyzer_ctx->analyzer == nullptr);
+ segment_v2::TermInfo keyword_token;
+ int32_t unused_array_offset = 0;
+ for (size_t element = begin; element < end; ++element) {
+ if (array_element_null_map && (*array_element_null_map)[element]) {
+ continue;
+ }
+ if (keyword) {
+ keyword_token.term = string_col->get_data_at(element).to_string();
+ if (callback(std::span(&keyword_token, 1))) {
+ return true;
+ }
+ continue;
+ }
+ auto tokens = function.analyse_data_token(column_name, analyzer_ctx,
string_col, element,
+ nullptr,
unused_array_offset);
+ if (tokens.empty()) {
+ continue;
+ }
+ if (callback(std::span<const segment_v2::TermInfo>(tokens))) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool match_phrase_data_tokens(const FunctionMatchBase& function, const
std::string& column_name,
+ const InvertedIndexAnalyzerCtx* analyzer_ctx,
+ const ColumnString* string_col, size_t row,
+ const ColumnArray::Offsets64* array_offsets,
+ const ColumnUInt8::Container*
array_element_null_map,
+ StreamingPhraseMatcher& matcher) {
+ matcher.reset_row();
+ int64_t position_base = 0;
+ const bool matched = for_each_data_element_tokens(
+ function, column_name, analyzer_ctx, string_col, row,
array_offsets,
+ array_element_null_map, [&](std::span<const segment_v2::TermInfo>
tokens) {
+ const bool analyzed = analyzer_ctx &&
analyzer_ctx->requires_analysis() &&
+ analyzer_ctx->analyzer != nullptr;
+ int32_t last_position = 0;
+ for (const auto& token : tokens) {
+ const int32_t position = analyzed ? token.position : 1;
+ if (matcher.feed(token.get_single_term(), position_base +
position)) {
+ return true;
+ }
+ last_position = position;
+ }
+ position_base += last_position;
Review Comment:
[P2] Preserve SNII's gap between analyzed array elements
For an SNII ARRAY index, `['hello', 'world']` is written at positions 1 and
3 because `SniiIndexColumnWriter::add_array_values` advances its base to
`max_position + 1` after each element. This helper instead feeds positions 1
and 2, so `MATCH_PHRASE 'hello world'` now matches during fallback while the
physical index rejects the same row. With MATCH fallback enabled, dropping a
partition's physical index can therefore change query results without changing
data. Preserve the writer's element-position convention and cover
indexed-versus-fallback execution over the same array.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]