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 @@ -19,27 +19,27 @@

#include <memory>

#include "olap/rowset/segment_v2/inverted_index/query_v2/bitmap_query/bitmap_weight.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/bit_set_query/bit_set_weight.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/query.h"
#include "roaring/roaring.hh"

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

class BitmapQuery : public Query {
class BitSetQuery : public Query {
public:
explicit BitmapQuery(std::shared_ptr<roaring::Roaring> bitmap) : _bitmap(std::move(bitmap)) {}
BitmapQuery(const roaring::Roaring& bitmap)
explicit BitSetQuery(std::shared_ptr<roaring::Roaring> bitmap) : _bitmap(std::move(bitmap)) {}
BitSetQuery(const roaring::Roaring& bitmap)
: _bitmap(std::make_shared<roaring::Roaring>(bitmap)) {}
~BitmapQuery() override = default;
~BitSetQuery() override = default;

WeightPtr weight(bool /*enable_scoring*/) override {
return std::make_shared<BitmapWeight>(_bitmap);
return std::make_shared<BitSetWeight>(_bitmap);
}

private:
std::shared_ptr<roaring::Roaring> _bitmap;
};

using BitmapQueryPtr = std::shared_ptr<BitmapQuery>;
using BitSetQueryPtr = std::shared_ptr<BitSetQuery>;

} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@
#include <algorithm>
#include <limits>
#include <memory>
#include <optional>

#include "olap/rowset/segment_v2/inverted_index/query_v2/scorer.h"
#include "roaring/roaring.hh"

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

class BitmapScorer final : public Scorer {
class BitSetScorer final : public Scorer {
public:
BitmapScorer(std::shared_ptr<roaring::Roaring> bitmap,
BitSetScorer(std::shared_ptr<roaring::Roaring> bitmap,
std::shared_ptr<roaring::Roaring> null_bitmap = nullptr)
: _bitmap(std::move(bitmap)),
: _bit_set(std::move(bitmap)),
_null_bitmap(std::move(null_bitmap)),
_it(_bitmap->begin()) {
_it(_bit_set->begin()) {
_doc = (_it.i.has_value) ? *_it : TERMINATED;
}
~BitmapScorer() override = default;
~BitSetScorer() override = default;

uint32_t advance() override {
if (_doc == TERMINATED) {
Expand Down Expand Up @@ -70,7 +69,7 @@ class BitmapScorer final : public Scorer {
uint32_t doc() const override { return _doc; }

uint32_t size_hint() const override {
uint64_t card = _bitmap->cardinality();
uint64_t card = _bit_set->cardinality();
return static_cast<uint32_t>(
std::min<uint64_t>(card, std::numeric_limits<uint32_t>::max()));
}
Expand All @@ -87,10 +86,11 @@ class BitmapScorer final : public Scorer {
}

private:
std::shared_ptr<roaring::Roaring> _bitmap;
std::shared_ptr<roaring::Roaring> _bit_set;
std::shared_ptr<roaring::Roaring> _null_bitmap;
roaring::Roaring::const_iterator _it;
uint32_t _doc = TERMINATED;
};
using BitSetScorerPtr = std::shared_ptr<BitSetScorer>;

} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,30 @@

#include <memory>

#include "olap/rowset/segment_v2/inverted_index/query_v2/bitmap_query/bitmap_scorer.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/weight.h"
#include "roaring/roaring.hh"

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

class BitmapWeight final : public Weight {
class BitSetWeight final : public Weight {
public:
BitmapWeight(std::shared_ptr<roaring::Roaring> bitmap,
BitSetWeight(std::shared_ptr<roaring::Roaring> bitmap,
std::shared_ptr<roaring::Roaring> null_bitmap = nullptr)
: _bitmap(std::move(bitmap)), _null_bitmap(std::move(null_bitmap)) {}
~BitmapWeight() override = default;
~BitSetWeight() override = default;

ScorerPtr scorer(const QueryExecutionContext& /*context*/) override {
if (_bitmap == nullptr || _bitmap->isEmpty()) {
return std::make_shared<EmptyScorer>();
}
return std::make_shared<BitmapScorer>(_bitmap, _null_bitmap);
return std::make_shared<BitSetScorer>(_bitmap, _null_bitmap);
}

private:
std::shared_ptr<roaring::Roaring> _bitmap;
std::shared_ptr<roaring::Roaring> _null_bitmap;
};

using BitmapWeightPtr = std::shared_ptr<BitmapWeight>;
using BitSetWeightPtr = std::shared_ptr<BitSetWeight>;

} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <utility>
#include <vector>

#include "olap/rowset/segment_v2/inverted_index/query_v2/bitmap_query/bitmap_scorer.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/buffered_union_scorer.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/doc_set.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/intersection_scorer.h"
Expand Down Expand Up @@ -271,7 +271,7 @@ class BooleanWeight : public Weight {
if (!result.null_bitmap.isEmpty()) {
null_ptr = std::make_shared<roaring::Roaring>(std::move(result.null_bitmap));
}
return std::make_shared<BitmapScorer>(std::move(true_ptr), std::move(null_ptr));
return std::make_shared<BitSetScorer>(std::move(true_ptr), std::move(null_ptr));
}

OperatorType _type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 "olap/rowset/segment_v2/inverted_index/query_v2/scorer.h"

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

template <typename ScorerPtrT>
class ConstScoreScorer : public Scorer {
public:
ConstScoreScorer(ScorerPtrT scorer) : _scorer(std::move(scorer)) {}
~ConstScoreScorer() override = default;

uint32_t advance() override { return _scorer->advance(); }
uint32_t seek(uint32_t target) override { return _scorer->seek(target); }
uint32_t doc() const override { return _scorer->doc(); }
uint32_t size_hint() const override { return _scorer->size_hint(); }

float score() override { return _score; }

private:
ScorerPtrT _scorer;

float _score = 1.0F;
};

} // namespace doris::segment_v2::inverted_index::query_v2
74 changes: 74 additions & 0 deletions be/src/olap/rowset/segment_v2/inverted_index/query_v2/doc_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,80 @@ class DocSet {
throw doris::Exception(doris::ErrorCode::NOT_IMPLEMENTED_ERROR,
"size_hint() method not implemented in base DocSet class");
}

virtual uint32_t freq() const {
throw doris::Exception(doris::ErrorCode::NOT_IMPLEMENTED_ERROR,
"freq() method not implemented in base DocSet class");
}

virtual uint32_t norm() const {
throw doris::Exception(doris::ErrorCode::NOT_IMPLEMENTED_ERROR,
"norm() method not implemented in base DocSet class");
}
};

class MockDocSet : public DocSet {
public:
MockDocSet(std::vector<uint32_t> docs, uint32_t size_hint_val = 0, uint32_t norm_val = 1)
: _docs(std::move(docs)), _size_hint_val(size_hint_val), _norm_val(norm_val) {
if (_docs.empty()) {
_current_doc = TERMINATED;
} else {
std::ranges::sort(_docs.begin(), _docs.end());
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.

Using std::ranges::sort with .begin() and .end() iterators is incorrect. Either use std::ranges::sort(_docs) directly or use std::sort(_docs.begin(), _docs.end()).

Suggested change
std::ranges::sort(_docs.begin(), _docs.end());
std::ranges::sort(_docs);

Copilot uses AI. Check for mistakes.
_current_doc = _docs[0];
}
if (_size_hint_val == 0) {
_size_hint_val = static_cast<uint32_t>(_docs.size());
}
}

uint32_t advance() override {
if (_docs.empty() || _index >= _docs.size()) {
_current_doc = TERMINATED;
return TERMINATED;
}
++_index;
if (_index >= _docs.size()) {
_current_doc = TERMINATED;
return TERMINATED;
}
_current_doc = _docs[_index];
return _current_doc;
}

uint32_t seek(uint32_t target) override {
if (_docs.empty() || _index >= _docs.size()) {
_current_doc = TERMINATED;
return TERMINATED;
}
if (_current_doc >= target) {
return _current_doc;
}
auto it = std::lower_bound(_docs.begin() + _index, _docs.end(), target);
if (it == _docs.end()) {
_index = _docs.size();
_current_doc = TERMINATED;
return TERMINATED;
}
_index = static_cast<size_t>(it - _docs.begin());
_current_doc = *it;
return _current_doc;
}

uint32_t doc() const override { return _current_doc; }

uint32_t size_hint() const override { return _size_hint_val; }

uint32_t norm() const override { return _norm_val; }

private:
std::vector<uint32_t> _docs;
size_t _index = 0;
uint32_t _current_doc = TERMINATED;
uint32_t _size_hint_val = 0;
uint32_t _norm_val = 1;
};

using MockDocSetPtr = std::shared_ptr<MockDocSet>;

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