|
| 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 "exec/runtime_filter/runtime_filter_selectivity.h" |
| 22 | +#include "exec/runtime_filter/runtime_filter_test_utils.h" |
| 23 | +#include "exprs/vexpr_context.h" |
| 24 | +#include "exprs/vruntimefilter_wrapper.h" |
| 25 | + |
| 26 | +namespace doris { |
| 27 | + |
| 28 | +// Minimal VExpr implementation for testing VRuntimeFilterWrapper in isolation. |
| 29 | +class StubVExpr : public VExpr { |
| 30 | +public: |
| 31 | + StubVExpr() : VExpr(make_texpr_node()) {} |
| 32 | + |
| 33 | + const std::string& expr_name() const override { |
| 34 | + static const std::string name = "StubVExpr"; |
| 35 | + return name; |
| 36 | + } |
| 37 | + |
| 38 | + Status execute(VExprContext*, Block*, int*) const override { return Status::OK(); } |
| 39 | + |
| 40 | + Status execute_column(VExprContext*, const Block*, Selector*, size_t, |
| 41 | + ColumnPtr&) const override { |
| 42 | + return Status::OK(); |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + static TExprNode make_texpr_node() { |
| 47 | + return TExprNodeBuilder( |
| 48 | + TExprNodeType::SLOT_REF, |
| 49 | + TTypeDescBuilder() |
| 50 | + .set_types(TTypeNodeBuilder() |
| 51 | + .set_type(TTypeNodeType::SCALAR) |
| 52 | + .set_scalar_type(TPrimitiveType::INT) |
| 53 | + .build()) |
| 54 | + .build(), |
| 55 | + 0) |
| 56 | + .build(); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +class VRuntimeFilterWrapperSamplingTest : public RuntimeFilterTest {}; |
| 61 | + |
| 62 | +// Test that VRuntimeFilterWrapper stores and propagates sampling_frequency |
| 63 | +// through open() to VExprContext. This is the core fix for the bug where |
| 64 | +// sampling_frequency was lost when _append_rf_into_conjuncts creates a new |
| 65 | +// VExprContext via VExprContext::create_shared(expr). |
| 66 | +TEST_F(VRuntimeFilterWrapperSamplingTest, open_propagates_sampling_frequency) { |
| 67 | + auto stub = std::make_shared<StubVExpr>(); |
| 68 | + auto node = TExprNodeBuilder( |
| 69 | + TExprNodeType::SLOT_REF, |
| 70 | + TTypeDescBuilder() |
| 71 | + .set_types(TTypeNodeBuilder() |
| 72 | + .set_type(TTypeNodeType::SCALAR) |
| 73 | + .set_scalar_type(TPrimitiveType::INT) |
| 74 | + .build()) |
| 75 | + .build(), |
| 76 | + 0) |
| 77 | + .build(); |
| 78 | + |
| 79 | + const int expected_frequency = 32; |
| 80 | + auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, false, /*filter_id=*/1, |
| 81 | + expected_frequency); |
| 82 | + |
| 83 | + // Simulate the VExprContext recreation that happens in _append_rf_into_conjuncts. |
| 84 | + // A fresh VExprContext has default sampling_frequency = DISABLE_SAMPLING (-1). |
| 85 | + auto context = std::make_shared<VExprContext>(wrapper); |
| 86 | + ASSERT_EQ(context->get_runtime_filter_selectivity().maybe_always_true_can_ignore(), false); |
| 87 | + |
| 88 | + RowDescriptor row_desc; |
| 89 | + ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, context.get()).ok()); |
| 90 | + ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context.get(), |
| 91 | + FunctionContext::FRAGMENT_LOCAL) |
| 92 | + .ok()); |
| 93 | + |
| 94 | + // After open(), sampling_frequency should be propagated from VRuntimeFilterWrapper |
| 95 | + // to VExprContext. Verify by accumulating low-selectivity data and checking |
| 96 | + // that always_true can now be detected. |
| 97 | + auto& selectivity = context->get_runtime_filter_selectivity(); |
| 98 | + selectivity.update_judge_selectivity(1, 2000, 50000, 0.1); |
| 99 | + EXPECT_TRUE(selectivity.maybe_always_true_can_ignore()); |
| 100 | +} |
| 101 | + |
| 102 | +// Test that default sampling_frequency (DISABLE_SAMPLING) disables the always_true |
| 103 | +// optimization, matching the behavior when disable_always_true_logic is set. |
| 104 | +TEST_F(VRuntimeFilterWrapperSamplingTest, default_sampling_frequency_disables_optimization) { |
| 105 | + auto stub = std::make_shared<StubVExpr>(); |
| 106 | + auto node = TExprNodeBuilder( |
| 107 | + TExprNodeType::SLOT_REF, |
| 108 | + TTypeDescBuilder() |
| 109 | + .set_types(TTypeNodeBuilder() |
| 110 | + .set_type(TTypeNodeType::SCALAR) |
| 111 | + .set_scalar_type(TPrimitiveType::INT) |
| 112 | + .build()) |
| 113 | + .build(), |
| 114 | + 0) |
| 115 | + .build(); |
| 116 | + |
| 117 | + // No sampling_frequency argument - uses default DISABLE_SAMPLING |
| 118 | + auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, false, /*filter_id=*/1); |
| 119 | + |
| 120 | + auto context = std::make_shared<VExprContext>(wrapper); |
| 121 | + RowDescriptor row_desc; |
| 122 | + ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, context.get()).ok()); |
| 123 | + ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context.get(), |
| 124 | + FunctionContext::FRAGMENT_LOCAL) |
| 125 | + .ok()); |
| 126 | + |
| 127 | + // Even with low-selectivity data, always_true should NOT be detected |
| 128 | + // because sampling is disabled |
| 129 | + auto& selectivity = context->get_runtime_filter_selectivity(); |
| 130 | + selectivity.update_judge_selectivity(1, 2000, 50000, 0.1); |
| 131 | + EXPECT_FALSE(selectivity.maybe_always_true_can_ignore()); |
| 132 | +} |
| 133 | + |
| 134 | +// Test that sampling_frequency survives VExprContext recreation, which is the |
| 135 | +// exact scenario that caused the original bug. |
| 136 | +TEST_F(VRuntimeFilterWrapperSamplingTest, sampling_frequency_survives_context_recreation) { |
| 137 | + auto stub = std::make_shared<StubVExpr>(); |
| 138 | + auto node = TExprNodeBuilder( |
| 139 | + TExprNodeType::SLOT_REF, |
| 140 | + TTypeDescBuilder() |
| 141 | + .set_types(TTypeNodeBuilder() |
| 142 | + .set_type(TTypeNodeType::SCALAR) |
| 143 | + .set_scalar_type(TPrimitiveType::INT) |
| 144 | + .build()) |
| 145 | + .build(), |
| 146 | + 0) |
| 147 | + .build(); |
| 148 | + |
| 149 | + const int expected_frequency = 32; |
| 150 | + auto wrapper = VRuntimeFilterWrapper::create_shared(node, stub, 0.4, false, /*filter_id=*/1, |
| 151 | + expected_frequency); |
| 152 | + |
| 153 | + // First context - prepare and open work |
| 154 | + auto context1 = std::make_shared<VExprContext>(wrapper); |
| 155 | + RowDescriptor row_desc; |
| 156 | + ASSERT_TRUE(wrapper->prepare(_runtime_states[0].get(), row_desc, context1.get()).ok()); |
| 157 | + ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context1.get(), |
| 158 | + FunctionContext::FRAGMENT_LOCAL) |
| 159 | + .ok()); |
| 160 | + |
| 161 | + // Simulate context recreation (what _append_rf_into_conjuncts does): |
| 162 | + // Create a brand new VExprContext with the same VRuntimeFilterWrapper. |
| 163 | + // The new context starts with default sampling_frequency = -1. |
| 164 | + auto context2 = std::make_shared<VExprContext>(wrapper); |
| 165 | + EXPECT_FALSE(context2->get_runtime_filter_selectivity().maybe_always_true_can_ignore()); |
| 166 | + |
| 167 | + // After open() on the new context, sampling_frequency should be propagated |
| 168 | + ASSERT_TRUE(wrapper->open(_runtime_states[0].get(), context2.get(), |
| 169 | + FunctionContext::THREAD_LOCAL) |
| 170 | + .ok()); |
| 171 | + |
| 172 | + auto& selectivity2 = context2->get_runtime_filter_selectivity(); |
| 173 | + selectivity2.update_judge_selectivity(1, 2000, 50000, 0.1); |
| 174 | + EXPECT_TRUE(selectivity2.maybe_always_true_can_ignore()); |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace doris |
0 commit comments