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