|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <glog/logging.h> |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include "core/data_type/data_type_factory.hpp" |
| 22 | +#include "core/data_type/data_type_number.h" |
| 23 | +#include "exec/operator/mock_scan_operator.h" |
| 24 | +#include "exec/runtime_filter/runtime_filter_consumer.h" |
| 25 | +#include "exec/runtime_filter/runtime_filter_consumer_helper.h" |
| 26 | +#include "exec/runtime_filter/runtime_filter_producer.h" |
| 27 | +#include "exec/runtime_filter/runtime_filter_test_utils.h" |
| 28 | +#include "exec/scan/scanner.h" |
| 29 | +#include "runtime/descriptors.h" |
| 30 | +#include "runtime/exec_env.h" |
| 31 | + |
| 32 | +namespace doris { |
| 33 | + |
| 34 | +// Minimal concrete Scanner so the abstract base can be instantiated. The |
| 35 | +// behavior under test (`Scanner::try_append_late_arrival_runtime_filter`) lives |
| 36 | +// entirely in the base class and never calls `_get_block_impl`. |
| 37 | +class TestScanner final : public Scanner { |
| 38 | +public: |
| 39 | + TestScanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit, |
| 40 | + RuntimeProfile* profile) |
| 41 | + : Scanner(state, local_state, limit, profile) {} |
| 42 | + |
| 43 | +protected: |
| 44 | + Status _get_block_impl(RuntimeState* /*state*/, Block* /*block*/, bool* eof) override { |
| 45 | + *eof = true; |
| 46 | + return Status::OK(); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +class ScannerLateArrivalRfTest : public RuntimeFilterTest { |
| 51 | +public: |
| 52 | + void SetUp() override { |
| 53 | + RuntimeFilterTest::SetUp(); |
| 54 | + // The runtime filter helper schedules a timer on the global queue when |
| 55 | + // it initialises, so the queue must exist before init() is called. |
| 56 | + ExecEnv::GetInstance()->_init_runtime_filter_timer_queue(); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +// Regression: PR #59786 accidentally removed `_applied_rf_num = arrived_rf_num` |
| 61 | +// from `Scanner::try_append_late_arrival_runtime_filter`, leaving the counter |
| 62 | +// at 0 forever. Subsequent calls then re-clone conjunct ctxs every time, the |
| 63 | +// `ApplyAllRuntimeFilters=True` info-string is never emitted, and the |
| 64 | +// `_applied_rf_num <= _total_rf_num` DCHECK becomes dead. This test asserts |
| 65 | +// the counter advances after RFs arrive and that the second call short-circuits |
| 66 | +// via the fast path at the top of the function. |
| 67 | +TEST_F(ScannerLateArrivalRfTest, applied_rf_num_advances_after_late_arrival) { |
| 68 | + std::vector<TRuntimeFilterDesc> rf_descs = { |
| 69 | + TRuntimeFilterDescBuilder().add_planId_to_target_expr(0).build(), |
| 70 | + TRuntimeFilterDescBuilder().add_planId_to_target_expr(0).build()}; |
| 71 | + |
| 72 | + SlotDescriptor slot_desc; |
| 73 | + slot_desc._type = DataTypeFactory::instance().create_data_type(PrimitiveType::TYPE_INT, false); |
| 74 | + TupleDescriptor tuple_desc; |
| 75 | + tuple_desc.add_slot(&slot_desc); |
| 76 | + RowDescriptor row_desc; |
| 77 | + _tbl._slot_desc_map[0] = &slot_desc; |
| 78 | + const_cast<std::vector<TupleDescriptor*>&>(row_desc._tuple_desc_map).push_back(&tuple_desc); |
| 79 | + |
| 80 | + // MockScanOperatorX feeds `_runtime_filter_descs` and `_row_descriptor` |
| 81 | + // into the local state's helper. Access to the protected members is |
| 82 | + // permitted by the build-wide `-fno-access-control` flag. |
| 83 | + auto op = std::make_shared<MockScanOperatorX>(); |
| 84 | + op->_runtime_filter_descs = rf_descs; |
| 85 | + op->_row_descriptor = row_desc; |
| 86 | + |
| 87 | + auto local_state = std::make_shared<MockScanLocalState>(_runtime_states[0].get(), op.get()); |
| 88 | + |
| 89 | + std::vector<std::shared_ptr<Dependency>> rf_dependencies; |
| 90 | + ASSERT_TRUE(local_state->_helper.init(_runtime_states[0].get(), true, 0, 0, rf_dependencies, "") |
| 91 | + .ok()); |
| 92 | + |
| 93 | + auto scanner = std::make_unique<TestScanner>(_runtime_states[0].get(), local_state.get(), |
| 94 | + -1 /*limit*/, &_profile); |
| 95 | + ASSERT_TRUE(scanner->init(_runtime_states[0].get(), {}).ok()); |
| 96 | + ASSERT_EQ(scanner->_total_rf_num, 2); |
| 97 | + ASSERT_EQ(scanner->_applied_rf_num, 0); |
| 98 | + |
| 99 | + std::shared_ptr<RuntimeFilterProducer> producer; |
| 100 | + ASSERT_TRUE(RuntimeFilterProducer::create(_query_ctx.get(), rf_descs.data(), &producer).ok()); |
| 101 | + producer->set_wrapper_state_and_ready_to_publish(RuntimeFilterWrapper::State::READY); |
| 102 | + local_state->_helper._consumers[0]->signal(producer.get()); |
| 103 | + local_state->_helper._consumers[1]->signal(producer.get()); |
| 104 | + |
| 105 | + // First call after both RFs arrived: counter must advance to total. Before |
| 106 | + // the fix this stayed at 0 because the assignment was missing. |
| 107 | + ASSERT_TRUE(scanner->try_append_late_arrival_runtime_filter().ok()); |
| 108 | + ASSERT_EQ(scanner->_applied_rf_num, 2); |
| 109 | + |
| 110 | + // Second call: must hit the fast-path early return without re-cloning. |
| 111 | + // We clear `_conjuncts` and verify the function does NOT repopulate them; |
| 112 | + // if `_applied_rf_num` were still 0 the function would call |
| 113 | + // `clone_conjunct_ctxs` and overwrite the cleared vector. |
| 114 | + scanner->_conjuncts.clear(); |
| 115 | + ASSERT_TRUE(scanner->try_append_late_arrival_runtime_filter().ok()); |
| 116 | + ASSERT_EQ(scanner->_applied_rf_num, 2); |
| 117 | + ASSERT_TRUE(scanner->_conjuncts.empty()); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace doris |
0 commit comments