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
@@ -0,0 +1,112 @@
// 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 <algorithm>
#include <memory>

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

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

class AllScorer;
class AllWeight;
class AllQuery;

using AllScorerPtr = std::shared_ptr<AllScorer>;
using AllWeightPtr = std::shared_ptr<AllWeight>;
using AllQueryPtr = std::shared_ptr<AllQuery>;

class AllScorer : public Scorer {
public:
explicit AllScorer(uint32_t max_doc) : _max_doc(max_doc) {
if (_max_doc == 0) {
_doc = TERMINATED;
} else {
_doc = 0;
}
}

~AllScorer() override = default;

uint32_t doc() const override { return _doc; }

uint32_t advance() override {
if (_doc == TERMINATED) {
return TERMINATED;
}
if (_doc + 1 >= _max_doc) {
_doc = TERMINATED;
return TERMINATED;
}
++_doc;
return _doc;
}

uint32_t seek(uint32_t target) override {
if (_doc == TERMINATED) {
return TERMINATED;
}
if (target >= _max_doc) {
_doc = TERMINATED;
return TERMINATED;
}
_doc = std::max(_doc, target);
return _doc;
}

float score() override { return 1.0F; }

uint32_t size_hint() const override { return _max_doc; }

private:
uint32_t _max_doc = 0;
uint32_t _doc = TERMINATED;
};

class AllWeight : public Weight {
public:
explicit AllWeight(uint32_t max_doc) : _max_doc(max_doc) {}

~AllWeight() override = default;

ScorerPtr scorer(const QueryExecutionContext& context) override {
return std::make_shared<AllScorer>(_max_doc);
}

private:
uint32_t _max_doc = 0;
};

class AllQuery : public Query {
public:
explicit AllQuery(uint32_t max_doc) : _max_doc(max_doc) {}

~AllQuery() override = default;

WeightPtr weight(bool /*enable_scoring*/) override {
return std::make_shared<AllWeight>(_max_doc);
}

private:
uint32_t _max_doc = 0;
};

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

#include "olap/rowset/segment_v2/inverted_index/query_v2/boolean_query/occur.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/boolean_query/occur_boolean_query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/boolean_query/operator_boolean_query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/query.h"

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

class OccurBooleanQueryBuilder {
public:
OccurBooleanQueryBuilder() = default;
~OccurBooleanQueryBuilder() = default;

void add(const QueryPtr& query, Occur occur) { _sub_queries.emplace_back(occur, query); }

void set_minimum_number_should_match(size_t value) { _minimum_number_should_match = value; }

QueryPtr build() {
if (_minimum_number_should_match.has_value()) {
return std::make_shared<OccurBooleanQuery>(std::move(_sub_queries),
_minimum_number_should_match.value());
}
return std::make_shared<OccurBooleanQuery>(std::move(_sub_queries));
}

private:
std::vector<std::pair<Occur, QueryPtr>> _sub_queries;
std::optional<size_t> _minimum_number_should_match;
};

using OccurBooleanQueryBuilderPtr = std::shared_ptr<OccurBooleanQueryBuilder>;

class OperatorBooleanQueryBuilder {
public:
OperatorBooleanQueryBuilder(OperatorType type) : _type(type) {}
~OperatorBooleanQueryBuilder() = default;

void add(const QueryPtr& query, std::string binding_key = {}) {
_sub_queries.emplace_back(query);
_binding_keys.emplace_back(std::move(binding_key));
}

QueryPtr build() {
return std::make_shared<OperatorBooleanQuery>(_type, std::move(_sub_queries),
std::move(_binding_keys));
}

private:
OperatorType _type;
std::vector<QueryPtr> _sub_queries;
std::vector<std::string> _binding_keys;
};

using OperatorBooleanQueryBuilderPtr = std::shared_ptr<OperatorBooleanQueryBuilder>;

inline OccurBooleanQueryBuilderPtr create_occur_boolean_query_builder() {
return std::make_shared<OccurBooleanQueryBuilder>();
}

inline OperatorBooleanQueryBuilderPtr create_operator_boolean_query_builder(OperatorType type) {
return std::make_shared<OperatorBooleanQueryBuilder>(type);
}

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

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

enum class Occur { MUST = 0, SHOULD = 1, MUST_NOT = 2 };

} // namespace doris::segment_v2::inverted_index::query_v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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/boolean_query/occur.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/boolean_query/occur_boolean_weight.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/score_combiner.h"

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

class OccurBooleanQuery;
using OccurBooleanQueryPtr = std::shared_ptr<OccurBooleanQuery>;

class OccurBooleanQuery : public Query {
public:
explicit OccurBooleanQuery(std::vector<std::pair<Occur, QueryPtr>> clauses)
: _sub_queries(std::move(clauses)),
_minimum_number_should_match(compute_default_minimum_should_match(_sub_queries)) {}

OccurBooleanQuery(std::vector<std::pair<Occur, QueryPtr>> clauses,
size_t minimum_number_should_match)
: _sub_queries(std::move(clauses)),
_minimum_number_should_match(minimum_number_should_match) {}

~OccurBooleanQuery() override = default;

WeightPtr weight(bool enable_scoring) override {
std::vector<std::pair<Occur, WeightPtr>> sub_weights;
sub_weights.reserve(_sub_queries.size());
for (const auto& [occur, query] : _sub_queries) {
sub_weights.emplace_back(occur, query->weight(enable_scoring));
}
return std::make_shared<OccurBooleanWeight<SumCombinerPtr>>(
std::move(sub_weights), _minimum_number_should_match, enable_scoring,
std::make_shared<SumCombiner>());
}

const std::vector<std::pair<Occur, QueryPtr>>& clauses() const { return _sub_queries; }
size_t minimum_number_should_match() const { return _minimum_number_should_match; }

private:
static size_t compute_default_minimum_should_match(
const std::vector<std::pair<Occur, QueryPtr>>& clauses) {
size_t minimum_required = 0;
for (const auto& [occur, _] : clauses) {
if (occur == Occur::SHOULD) {
minimum_required = 1;
} else if (occur == Occur::MUST || occur == Occur::MUST_NOT) {
return 0;
}
}
return minimum_required;
}

std::vector<std::pair<Occur, QueryPtr>> _sub_queries;
size_t _minimum_number_should_match = 0;
};

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