|
| 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 | +suite("test_merge_topn_offset") { |
| 19 | + // DORIS-26301: when MERGE_TOP_N merges a parent TopN that carries a non-zero OFFSET into its |
| 20 | + // child TopN, the merged limit must be clamped by (childLimit - parentOffset). Otherwise rows |
| 21 | + // beyond the child's limit leak through. Before the fix, an outer "LIMIT 3 OFFSET 4" over an |
| 22 | + // inner "LIMIT 5" wrongly returned 3 rows (k=5,6,7) instead of the single correct row (k=5). |
| 23 | + sql "SET enable_nereids_planner=true" |
| 24 | + sql "SET enable_fallback_to_original_planner=false" |
| 25 | + |
| 26 | + def tableName = "test_merge_topn_offset" |
| 27 | + sql """ DROP TABLE IF EXISTS ${tableName} """ |
| 28 | + sql """ |
| 29 | + CREATE TABLE ${tableName} ( |
| 30 | + k INT NOT NULL, |
| 31 | + v INT NOT NULL |
| 32 | + ) |
| 33 | + DUPLICATE KEY(k) |
| 34 | + DISTRIBUTED BY HASH(k) BUCKETS 1 |
| 35 | + PROPERTIES ("replication_num" = "1") |
| 36 | + """ |
| 37 | + sql """ INSERT INTO ${tableName} VALUES |
| 38 | + (1, 10), (2, 20), (3, 30), (4, 40), (5, 50), |
| 39 | + (6, 60), (7, 70), (8, 80), (9, 90), (10, 100) """ |
| 40 | + |
| 41 | + // MERGE_TOP_N is enabled by default; stacking an outer TopN with an OFFSET over an inner TopN |
| 42 | + // triggers it. The inner "ORDER BY k LIMIT 5" yields k = 1..5. |
| 43 | + |
| 44 | + // outer offset (4) consumes 4 of the inner's 5 rows -> only k=5 remains |
| 45 | + qt_merge_topn_off4lim3 """ SELECT * FROM (SELECT k, v FROM ${tableName} ORDER BY k LIMIT 5) s ORDER BY k LIMIT 3 OFFSET 4 """ |
| 46 | + // outer offset (2) is within the inner limit -> k = 3,4,5 |
| 47 | + qt_merge_topn_off2lim3 """ SELECT * FROM (SELECT k, v FROM ${tableName} ORDER BY k LIMIT 5) s ORDER BY k LIMIT 3 OFFSET 2 """ |
| 48 | + // outer offset (6) exceeds the inner limit (5) -> empty |
| 49 | + qt_merge_topn_off6lim3 """ SELECT * FROM (SELECT k, v FROM ${tableName} ORDER BY k LIMIT 5) s ORDER BY k LIMIT 3 OFFSET 6 """ |
| 50 | +} |
0 commit comments