Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ class ConstScoreScorer : public Scorer {

float score() override { return _score; }

bool has_null_bitmap(const NullBitmapResolver* resolver = nullptr) override {
return _scorer && _scorer->has_null_bitmap(resolver);
}

const roaring::Roaring* get_null_bitmap(const NullBitmapResolver* resolver = nullptr) override {
return _scorer ? _scorer->get_null_bitmap(resolver) : nullptr;
}

private:
ScorerPtrT _scorer;

float _score = 1.0F;
};

} // namespace doris::segment_v2::inverted_index::query_v2
} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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.

#pragma once

#include <glog/logging.h>

#include <memory>
#include <roaring/roaring.hh>
#include <string>

#include "olap/rowset/segment_v2/index_iterator.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/weight.h"
#include "olap/rowset/segment_v2/inverted_index_cache.h"

namespace doris::segment_v2::inverted_index::query_v2 {

// Small helper that centralizes "field NULL bitmap" lookups so weights/scorers
// don't have to duplicate resolver plumbing.
Comment on lines +33 to +34
Copy link

Copilot AI Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The class documentation uses informal language ('Small helper'). Consider more professional phrasing such as: 'Utility class that centralizes field NULL bitmap lookups to avoid duplicating resolver logic across weights and scorers.'

Suggested change
// Small helper that centralizes "field NULL bitmap" lookups so weights/scorers
// don't have to duplicate resolver plumbing.
// Utility class that centralizes field NULL bitmap lookups to avoid duplicating
// resolver logic across weights and scorers.

Copilot uses AI. Check for mistakes.
class FieldNullBitmapFetcher {
public:
FieldNullBitmapFetcher() = delete;

static std::shared_ptr<roaring::Roaring> fetch(const QueryExecutionContext& context,
const std::string& logical_field,
const Scorer* scorer = nullptr) {
return fetch(context.null_resolver, logical_field, scorer);
}

static std::shared_ptr<roaring::Roaring> fetch(const NullBitmapResolver* resolver,
const std::string& logical_field,
const Scorer* scorer = nullptr) {
if (resolver == nullptr || logical_field.empty()) {
return nullptr;
}

EmptyScorer fallback_scorer;
const Scorer* resolver_scorer = scorer != nullptr ? scorer : &fallback_scorer;

auto iterator = resolver->iterator_for(*resolver_scorer, logical_field);
if (iterator == nullptr) {
return nullptr;
}

auto has_null = iterator->has_null();
if (!has_null.has_value() || !has_null.value()) {
return nullptr;
}

segment_v2::InvertedIndexQueryCacheHandle cache_handle;
auto status = iterator->read_null_bitmap(&cache_handle);
if (!status.ok()) {
LOG(WARNING) << "Failed to read null bitmap for field '" << logical_field
<< "': " << status.to_string();
return nullptr;
}

auto bitmap_ptr = cache_handle.get_bitmap();
if (bitmap_ptr == nullptr) {
return nullptr;
}

return std::make_shared<roaring::Roaring>(*bitmap_ptr);
}
};

} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ class PhraseQuery : public Query {
std::vector<std::wstring> _terms;
};

} // namespace doris::segment_v2::inverted_index::query_v2
} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#pragma once

#include "olap/rowset/segment_v2/index_query_context.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/bit_set_query/bit_set_scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/const_score_query/const_score_scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/null_bitmap_fetcher.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/phrase_query/phrase_scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/weight.h"
Expand All @@ -37,12 +40,29 @@ class PhraseWeight : public Weight {
~PhraseWeight() override = default;

ScorerPtr scorer(const QueryExecutionContext& ctx, const std::string& binding_key) override {
auto scorer = phrase_scorer(ctx, binding_key);
if (scorer) {
return scorer;
} else {
return std::make_shared<EmptyScorer>();
auto phrase = phrase_scorer(ctx, binding_key);
auto logical_field = logical_field_or_fallback(ctx, binding_key, _field);
auto null_bitmap = FieldNullBitmapFetcher::fetch(ctx, logical_field);

auto doc_bitset = std::make_shared<roaring::Roaring>();
if (phrase) {
uint32_t doc = phrase->doc();
if (doc == TERMINATED) {
doc = phrase->advance();
}
while (doc != TERMINATED) {
doc_bitset->add(doc);
doc = phrase->advance();
}
}

auto bit_set =
std::make_shared<BitSetScorer>(std::move(doc_bitset), std::move(null_bitmap));
if (!phrase) {
return bit_set;
}
// Wrap with const score for consistency with other non-scoring paths
return std::make_shared<ConstScoreScorer<BitSetScorerPtr>>(std::move(bit_set));
}

private:
Expand Down Expand Up @@ -78,4 +98,4 @@ class PhraseWeight : public Weight {
bool _enable_scoring = false;
};

} // namespace doris::segment_v2::inverted_index::query_v2
} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ class RegexpQuery : public Query {
std::string _pattern;
};

} // namespace doris::segment_v2::inverted_index::query_v2
} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/bit_set_query/bit_set_scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/const_score_query/const_score_scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/null_bitmap_fetcher.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/segment_postings.h"
#include "olap/rowset/segment_v2/inverted_index/util/string_helper.h"
#include "olap/rowset/segment_v2/inverted_index_iterator.h"

CL_NS_USE(index)

Expand All @@ -44,6 +46,9 @@ RegexpWeight::RegexpWeight(IndexQueryContextPtr context, std::wstring field, std

ScorerPtr RegexpWeight::scorer(const QueryExecutionContext& context,
const std::string& binding_key) {
auto logical_field = logical_field_or_fallback(context, binding_key, _field);
VLOG_DEBUG << "RegexpWeight::scorer() called - pattern=" << _pattern << ", logical_field='"
<< logical_field << "'";
auto prefix = get_regex_prefix(_pattern);

hs_database_t* database = nullptr;
Expand Down Expand Up @@ -76,7 +81,10 @@ ScorerPtr RegexpWeight::scorer(const QueryExecutionContext& context,
hs_free_database(database);

if (matching_terms.empty()) {
return std::make_shared<EmptyScorer>();
// Even when there are no matching terms, we must honor NULL semantics for the field.
auto empty_true = std::make_shared<roaring::Roaring>();
auto null_bitmap = FieldNullBitmapFetcher::fetch(context, logical_field);
return std::make_shared<BitSetScorer>(std::move(empty_true), std::move(null_bitmap));
}

auto doc_bitset = std::make_shared<roaring::Roaring>();
Expand All @@ -93,7 +101,8 @@ ScorerPtr RegexpWeight::scorer(const QueryExecutionContext& context,
}
}

auto bit_set = std::make_shared<BitSetScorer>(doc_bitset);
auto null_bitmap = FieldNullBitmapFetcher::fetch(context, logical_field);
auto bit_set = std::make_shared<BitSetScorer>(doc_bitset, null_bitmap);
auto const_score = std::make_shared<ConstScoreScorer<BitSetScorerPtr>>(std::move(bit_set));
return const_score;
}
Expand Down Expand Up @@ -220,4 +229,4 @@ void RegexpWeight::collect_matching_terms(const QueryExecutionContext& context,
}
}

} // namespace doris::segment_v2::inverted_index::query_v2
} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ namespace doris::segment_v2::inverted_index::query_v2 {

class TermQuery : public Query {
public:
TermQuery(IndexQueryContextPtr context, std::wstring field, std::wstring term,
std::string logical_field = {})
: _context(std::move(context)),
_field(std::move(field)),
_term(std::move(term)),
_logical_field(std::move(logical_field)) {}
TermQuery(IndexQueryContextPtr context, std::wstring field, std::wstring term)
: _context(std::move(context)), _field(std::move(field)), _term(std::move(term)) {}
~TermQuery() override = default;

WeightPtr weight(bool enable_scoring) override {
Expand All @@ -43,15 +39,14 @@ class TermQuery : public Query {
}
return std::make_shared<TermWeight>(std::move(_context), std::move(_field),
std::move(_term), std::move(bm25_similarity),
enable_scoring, _logical_field);
enable_scoring);
}

private:
IndexQueryContextPtr _context;

std::wstring _field;
std::wstring _term;
std::string _logical_field;
};

} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <optional>
#include <roaring/roaring.hh>

#include "olap/rowset/segment_v2/inverted_index/query_v2/null_bitmap_fetcher.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/segment_postings.h"
#include "olap/rowset/segment_v2/inverted_index/similarity/similarity.h"
Expand Down Expand Up @@ -70,27 +71,9 @@ class TermScorer final : public Scorer {

_null_bitmap_checked = true;

auto iterator = resolver->iterator_for(*this, _logical_field);
if (iterator == nullptr) {
return;
}

auto has_null_result = iterator->has_null();
if (!has_null_result.has_value() || !has_null_result.value()) {
return;
}

segment_v2::InvertedIndexQueryCacheHandle cache_handle;
auto status = iterator->read_null_bitmap(&cache_handle);
if (!status.ok()) {
LOG(WARNING) << "TermScorer failed to read null bitmap for field '" << _logical_field
<< "': " << status.to_string();
return;
}

auto bitmap_ptr = cache_handle.get_bitmap();
if (bitmap_ptr != nullptr) {
_null_bitmap = *bitmap_ptr;
auto bitmap = FieldNullBitmapFetcher::fetch(resolver, _logical_field, this);
if (bitmap != nullptr) {
_null_bitmap = *bitmap;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,21 @@ namespace doris::segment_v2::inverted_index::query_v2 {
class TermWeight : public Weight {
public:
TermWeight(IndexQueryContextPtr context, std::wstring field, std::wstring term,
SimilarityPtr similarity, bool enable_scoring, std::string logical_field = {})
SimilarityPtr similarity, bool enable_scoring)
: _context(std::move(context)),
_field(std::move(field)),
_term(std::move(term)),
_similarity(std::move(similarity)),
_enable_scoring(enable_scoring),
_logical_field(std::move(logical_field)) {}
_enable_scoring(enable_scoring) {}
~TermWeight() override = default;

ScorerPtr scorer(const QueryExecutionContext& ctx, const std::string& binding_key) override {
auto reader = lookup_reader(_field, ctx, binding_key);
auto field_name =
_logical_field.empty() ? std::string(_field.begin(), _field.end()) : _logical_field;
auto logical_field = logical_field_or_fallback(ctx, binding_key, _field);
auto make_scorer = [&](auto segment_postings) -> ScorerPtr {
using PostingsT = decltype(segment_postings);
return std::make_shared<TermScorer<PostingsT>>(std::move(segment_postings), _similarity,
field_name);
logical_field);
};

if (!reader) {
Expand Down Expand Up @@ -76,7 +74,6 @@ class TermWeight : public Weight {
std::wstring _term;
SimilarityPtr _similarity;
bool _enable_scoring = false;
std::string _logical_field;
};

} // namespace doris::segment_v2::inverted_index::query_v2
31 changes: 31 additions & 0 deletions be/src/olap/rowset/segment_v2/inverted_index/query_v2/weight.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ class IndexReader;

namespace doris::segment_v2::inverted_index::query_v2 {

struct FieldBindingContext {
std::string logical_field_name;
std::string stored_field_name;
std::wstring stored_field_wstr;
};

struct QueryExecutionContext {
uint32_t segment_num_rows = 0;
std::vector<std::shared_ptr<lucene::index::IndexReader>> readers;
std::unordered_map<std::string, std::shared_ptr<lucene::index::IndexReader>> reader_bindings;
std::unordered_map<std::wstring, std::shared_ptr<lucene::index::IndexReader>>
field_reader_bindings;
std::unordered_map<std::string, FieldBindingContext> binding_fields;
const NullBitmapResolver* null_resolver = nullptr;
};

Expand All @@ -52,6 +59,30 @@ class Weight {
}

protected:
const FieldBindingContext* get_field_binding(const QueryExecutionContext& ctx,
const std::string& binding_key) const {
auto it = ctx.binding_fields.find(binding_key);
if (it != ctx.binding_fields.end()) {
return &it->second;
}
return nullptr;
}

std::string logical_field_or_fallback(const QueryExecutionContext& ctx,
const std::string& binding_key,
const std::wstring& fallback) const {
const auto* binding = get_field_binding(ctx, binding_key);
if (binding != nullptr) {
if (!binding->logical_field_name.empty()) {
return binding->logical_field_name;
}
if (!binding->stored_field_name.empty()) {
return binding->stored_field_name;
}
}
return std::string(fallback.begin(), fallback.end());
}

std::shared_ptr<lucene::index::IndexReader> lookup_reader(
const std::wstring& field, const QueryExecutionContext& ctx,
const std::string& binding_key) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ class WildcardQuery : public Query {
std::string _pattern;
};

} // namespace doris::segment_v2::inverted_index::query_v2
} // namespace doris::segment_v2::inverted_index::query_v2
Loading
Loading