Skip to content

Commit d7153c0

Browse files
Gabriel39zclllyybb
authored andcommitted
[fix](predicate) Avoid recomputing predicates (#60484)
1 parent f0bed3a commit d7153c0

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

be/src/olap/column_predicate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ class ColumnPredicate : public std::enable_shared_from_this<ColumnPredicate> {
225225
}
226226

227227
virtual double get_ignore_threshold() const { return 0; }
228+
// If this predicate acts on the key column, this predicate should be erased.
229+
virtual bool could_be_erased() const { return false; }
228230
// Return the size of value set for IN/NOT IN predicates and 0 for others.
229231
virtual std::string debug_string() const {
230232
fmt::memory_buffer debug_string_buffer;

be/src/olap/comparison_predicate.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class ComparisonPredicateBase final : public ColumnPredicate {
5252
ColumnPredicate::debug_string());
5353
return fmt::to_string(debug_string_buffer);
5454
}
55+
bool could_be_erased() const override {
56+
if ((PT == PredicateType::NE && !_opposite) || (PT == PredicateType::EQ && _opposite)) {
57+
return false;
58+
}
59+
return true;
60+
}
5561

5662
PredicateType type() const override { return PT; }
5763

be/src/olap/in_list_predicate.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ class InListPredicateBase final : public ColumnPredicate {
145145

146146
PredicateType type() const override { return PT; }
147147

148+
bool could_be_erased() const override {
149+
if ((PT == PredicateType::NOT_IN_LIST && !_opposite) ||
150+
(PT == PredicateType::IN_LIST && _opposite)) {
151+
return false;
152+
}
153+
return true;
154+
}
148155
Status evaluate(const vectorized::IndexFieldNameAndTypePair& name_with_type,
149156
IndexIterator* iterator, uint32_t num_rows,
150157
roaring::Roaring* result) const override {

be/src/olap/null_predicate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class NullPredicate final : public ColumnPredicate {
6161
ColumnPredicate::debug_string(), _is_null);
6262
return fmt::to_string(debug_string_buffer);
6363
}
64+
bool could_be_erased() const override { return true; }
6465

6566
PredicateType type() const override;
6667

be/src/pipeline/exec/olap_scan_operator.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <memory>
2323
#include <numeric>
24+
#include <optional>
2425

2526
#include "cloud/cloud_meta_mgr.h"
2627
#include "cloud/cloud_storage_engine.h"
@@ -883,6 +884,8 @@ Status OlapScanLocalState::_build_key_ranges_and_filters() {
883884
DCHECK(_slot_id_to_predicates.count(iter->first) > 0);
884885
const auto& value_range = iter->second;
885886

887+
std::optional<int> key_to_erase;
888+
886889
RETURN_IF_ERROR(std::visit(
887890
[&](auto&& range) {
888891
// make a copy or range and pass to extend_scan_key, keep the range unchanged
@@ -894,7 +897,7 @@ Status OlapScanLocalState::_build_key_ranges_and_filters() {
894897
_scan_keys.extend_scan_key(temp_range, p._max_scan_key_num,
895898
&exact_range, &eos, &should_break));
896899
if (exact_range) {
897-
_slot_id_to_value_range.erase(iter->first);
900+
key_to_erase = iter->first;
898901
}
899902
} else {
900903
// if exceed max_pushdown_conditions_per_column, use whole_value_rang instead
@@ -907,6 +910,24 @@ Status OlapScanLocalState::_build_key_ranges_and_filters() {
907910
return Status::OK();
908911
},
909912
value_range));
913+
914+
// Perform the erase operation after the visit is complete, still under lock
915+
if (key_to_erase.has_value()) {
916+
_slot_id_to_value_range.erase(*key_to_erase);
917+
918+
std::vector<std::shared_ptr<ColumnPredicate>> new_predicates;
919+
for (const auto& it : _slot_id_to_predicates[*key_to_erase]) {
920+
if (!it->could_be_erased()) {
921+
new_predicates.push_back(it);
922+
}
923+
}
924+
if (new_predicates.empty()) {
925+
_slot_id_to_predicates.erase(*key_to_erase);
926+
} else {
927+
_slot_id_to_predicates[*key_to_erase] = new_predicates;
928+
}
929+
}
930+
// lock is released here when it goes out of scope
910931
}
911932
if (eos) {
912933
_eos = true;

0 commit comments

Comments
 (0)