|
| 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 | +// A storage TopN (ORDER BY key LIMIT n) stops each segment after n rows. It must not leave |
| 19 | +// a condition cache entry behind, or a later query with the same predicate skips row ranges |
| 20 | +// that do have matching rows. |
| 21 | +// |
| 22 | +// The bug needs a rowset with overlapping segments, which is read by VMergeIterator: it asks |
| 23 | +// every segment for one more batch, so a segment that hit its LIMIT reaches EOF. |
| 24 | +// MemTable.need_flush writes one segment per inserted block. |
| 25 | +suite("test_segment_limit_condition_cache", "nonConcurrent") { |
| 26 | + def tbl = "test_segment_limit_condition_cache" |
| 27 | + sql "DROP TABLE IF EXISTS ${tbl}" |
| 28 | + sql """ |
| 29 | + CREATE TABLE ${tbl} (k INT, v INT) |
| 30 | + DUPLICATE KEY(k) |
| 31 | + DISTRIBUTED BY HASH(k) BUCKETS 1 |
| 32 | + PROPERTIES ('replication_num' = '1', 'disable_auto_compaction' = 'true') |
| 33 | + """ |
| 34 | + |
| 35 | + try { |
| 36 | + GetDebugPoint().enableDebugPointForAllBEs("MemTable.need_flush") |
| 37 | + // 4 blocks of 4096 rows -> 4 segments, each larger than one 2048-row cache granule. |
| 38 | + // Every block has keys from the whole range, so the segments overlap. |
| 39 | + sql "SET batch_size = 4096" |
| 40 | + sql """ |
| 41 | + INSERT INTO ${tbl} |
| 42 | + SELECT (number * 7919) % 16384, (number * 7919) % 16384 % 7 - 3 |
| 43 | + FROM numbers('number' = '16384') |
| 44 | + """ |
| 45 | + } finally { |
| 46 | + GetDebugPoint().disableDebugPointForAllBEs("MemTable.need_flush") |
| 47 | + } |
| 48 | + |
| 49 | + def tablets = sql_return_maparray("SHOW TABLETS FROM ${tbl}") |
| 50 | + def (code, out, err) = curl("GET", tablets[0].MetaUrl) |
| 51 | + assertEquals(0, code) |
| 52 | + def hasMergeLayout = parseJson(out.trim()).rs_metas.any { meta -> |
| 53 | + meta.segments_overlap_pb == "OVERLAPPING" && meta.num_segments > 1 |
| 54 | + } |
| 55 | + assertTrue(hasMergeLayout, "expected an OVERLAPPING rowset with more than one segment") |
| 56 | + |
| 57 | + // Both switches are randomized in fuzzy runs; this case needs both on. |
| 58 | + sql "SET enable_condition_cache = true" |
| 59 | + sql "SET enable_segment_limit_pushdown = true" |
| 60 | + // Disable TopN runtime filters so the queries exercise storage TopN and condition cache. |
| 61 | + sql "SET topn_filter_ratio = 0" |
| 62 | + // Keep each segment on the merge path that requests EOF after its local LIMIT. |
| 63 | + sql "SET enable_parallel_scan = false" |
| 64 | + explain { |
| 65 | + sql "SELECT k, v FROM ${tbl} WHERE abs(v) > 0 ORDER BY k LIMIT 1" |
| 66 | + contains "SORT LIMIT: 1" |
| 67 | + notContains "TOPN filter targets" |
| 68 | + } |
| 69 | + |
| 70 | + // Each case runs the TopN first, then the same predicate without LIMIT. |
| 71 | + qt_topn_asc "SELECT k, v FROM ${tbl} WHERE abs(v) > 0 ORDER BY k LIMIT 1" |
| 72 | + qt_all_after_asc "SELECT count(*), sum(k) FROM ${tbl} WHERE abs(v) > 0" |
| 73 | + |
| 74 | + qt_topn_desc "SELECT k, v FROM ${tbl} WHERE abs(v) > 1 ORDER BY k DESC LIMIT 1" |
| 75 | + qt_all_after_desc "SELECT count(*), sum(k) FROM ${tbl} WHERE abs(v) > 1" |
| 76 | +} |
0 commit comments