|
| 1 | +// Copyright 2023 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <Debug/MockRuntimeFilter.h> |
| 16 | +#include <Interpreters/Context.h> |
| 17 | +#include <TestUtils/ExecutorTestUtils.h> |
| 18 | + |
| 19 | +namespace DB |
| 20 | +{ |
| 21 | +namespace tests |
| 22 | +{ |
| 23 | + |
| 24 | +/// Coverage for the conservative guard in `PhysicalJoin::build`: |
| 25 | +/// - If join-side key protobuf field types are not compatible, we should skip creating/registering runtime filter. |
| 26 | +/// |
| 27 | +/// Behavioral contract in this test: |
| 28 | +/// - We still attach a RuntimeFilter request in the mock DAG. |
| 29 | +/// - Because key types mismatch, the optimization should be disabled (i.e. no filtering happens). |
| 30 | +/// - The join result should remain correct (same row count as “without runtime filter”). |
| 31 | +class RuntimeFilterDisableOnTypeMismatchTestRunner : public DB::tests::ExecutorTest |
| 32 | +{ |
| 33 | +public: |
| 34 | + void initializeContext() override |
| 35 | + { |
| 36 | + ExecutorTest::initializeContext(); |
| 37 | + context.mockStorage()->setUseDeltaMerge(true); |
| 38 | + } |
| 39 | + |
| 40 | + static constexpr size_t concurrency = 10; |
| 41 | +}; |
| 42 | + |
| 43 | +#define WrapForRuntimeFilterTestBegin \ |
| 44 | + std::vector<bool> pipelineBools{false, true}; \ |
| 45 | + for (auto enablePipeline : pipelineBools) \ |
| 46 | + { \ |
| 47 | + enablePipeline(enablePipeline); |
| 48 | + |
| 49 | +#define WrapForRuntimeFilterTestEnd } |
| 50 | + |
| 51 | +TEST_F(RuntimeFilterDisableOnTypeMismatchTestRunner, DisableRuntimeFilterWhenJoinKeyFieldTypeMismatch) |
| 52 | +try |
| 53 | +{ |
| 54 | + context.context->getSettingsRef().dt_segment_stable_pack_rows = 1; |
| 55 | + context.context->getSettingsRef().dt_segment_limit_rows = 1; |
| 56 | + context.context->getSettingsRef().dt_segment_delta_cache_limit_rows = 1; |
| 57 | + context.context->getSettingsRef().dt_segment_force_split_size = 70; |
| 58 | + context.context->getSettingsRef().enable_hash_join_v2 = false; |
| 59 | + |
| 60 | + // Probe(left) join key: Int32 |
| 61 | + context.addMockDeltaMerge( |
| 62 | + {"test_db", "left_table"}, |
| 63 | + {{"k1", TiDB::TP::TypeLong}}, |
| 64 | + {toNullableVec<Int32>("k1", {1, 2, 3})}, |
| 65 | + concurrency); |
| 66 | + |
| 67 | + // Build(right) join key: Int64 (mismatch) |
| 68 | + context.addExchangeReceiver( |
| 69 | + "right_exchange_table_i64", |
| 70 | + {{"k1", TiDB::TP::TypeLongLong}}, |
| 71 | + {toNullableVec<Int64>("k1", {2, 2, 3, 4})}); |
| 72 | + |
| 73 | + // Build(right) join key: UInt32 (mismatch due to unsigned flag) |
| 74 | + context.addExchangeReceiver( |
| 75 | + "right_exchange_table_u32", |
| 76 | + {{"k1", TiDB::TP::TypeLong, true}}, |
| 77 | + {toNullableVec<UInt32>("k1", {2, 2, 3, 4})}); |
| 78 | + |
| 79 | + WrapForRuntimeFilterTestBegin |
| 80 | + { |
| 81 | + // Baseline: without runtime filter. |
| 82 | + auto request |
| 83 | + = context.scan("test_db", "left_table") |
| 84 | + .join(context.receive("right_exchange_table_i64"), tipb::JoinType::TypeInnerJoin, {col("k1")}) |
| 85 | + .build(context); |
| 86 | + Expect expect{ |
| 87 | + {"table_scan_0", {3, enable_pipeline ? concurrency : 1}}, |
| 88 | + {"exchange_receiver_1", {4, concurrency}}, |
| 89 | + {"Join_2", {2, concurrency}}}; |
| 90 | + testForExecutionSummary(request, expect); |
| 91 | + } |
| 92 | + |
| 93 | + { |
| 94 | + // With runtime filter requested, but type mismatch => runtime filter should be disabled. |
| 95 | + mock::MockRuntimeFilter rf(1, col("k1"), col("k1"), "exchange_receiver_1", "table_scan_0"); |
| 96 | + auto request |
| 97 | + = context.scan("test_db", "left_table", std::vector<int>{1}) |
| 98 | + .join(context.receive("right_exchange_table_i64"), tipb::JoinType::TypeInnerJoin, {col("k1")}, rf) |
| 99 | + .build(context); |
| 100 | + // Expect no RF pruning, same as baseline. |
| 101 | + Expect expect{ |
| 102 | + {"table_scan_0", {3, enable_pipeline ? concurrency : 1}}, |
| 103 | + {"exchange_receiver_1", {4, concurrency}}, |
| 104 | + {"Join_2", {2, concurrency}}}; |
| 105 | + testForExecutionSummary(request, expect); |
| 106 | + } |
| 107 | + |
| 108 | + { |
| 109 | + // With runtime filter requested, but signed/unsigned mismatch => runtime filter should be disabled. |
| 110 | + mock::MockRuntimeFilter rf(1, col("k1"), col("k1"), "exchange_receiver_1", "table_scan_0"); |
| 111 | + auto request |
| 112 | + = context.scan("test_db", "left_table", std::vector<int>{1}) |
| 113 | + .join(context.receive("right_exchange_table_u32"), tipb::JoinType::TypeInnerJoin, {col("k1")}, rf) |
| 114 | + .build(context); |
| 115 | + // Expect no RF pruning, same as baseline. |
| 116 | + Expect expect{ |
| 117 | + {"table_scan_0", {3, enable_pipeline ? concurrency : 1}}, |
| 118 | + {"exchange_receiver_1", {4, concurrency}}, |
| 119 | + {"Join_2", {2, concurrency}}}; |
| 120 | + testForExecutionSummary(request, expect); |
| 121 | + } |
| 122 | + WrapForRuntimeFilterTestEnd |
| 123 | +} |
| 124 | +CATCH |
| 125 | + |
| 126 | +#undef WrapForRuntimeFilterTestBegin |
| 127 | +#undef WrapForRuntimeFilterTestEnd |
| 128 | + |
| 129 | +} // namespace tests |
| 130 | +} // namespace DB |
0 commit comments