|
| 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 | +// Sliding ROWS frames over floating-point columns must not carry the rounding |
| 19 | +// loss of a value that already left the frame into the current result. |
| 20 | +// |
| 21 | +// Expected values must not depend on how the compiler groups the additions |
| 22 | +// (the accumulators allow floating-point reassociation). The data therefore |
| 23 | +// uses 2^54 as the large value and 0.5 as the small value: half an ulp of 2^54 |
| 24 | +// is 2, so as long as the small values inside a frame sum to less than 2 in |
| 25 | +// magnitude, any grouping rounds the frame sum back to 2^54, while removing |
| 26 | +// 2^54 incrementally would still leave 0 instead of the exact small sum. |
| 27 | +// Frames that mix the large positive and negative value hold two operands |
| 28 | +// only, because a two-operand IEEE addition has a single correctly rounded |
| 29 | +// result. |
| 30 | +suite("test_window_float_sliding_frame") { |
| 31 | + sql "DROP TABLE IF EXISTS test_window_float_sliding_frame" |
| 32 | + sql """ |
| 33 | + CREATE TABLE test_window_float_sliding_frame ( |
| 34 | + id INT, |
| 35 | + grp INT, |
| 36 | + v_double DOUBLE, |
| 37 | + v_float FLOAT, |
| 38 | + v_nullable DOUBLE NULL |
| 39 | + ) ENGINE = OLAP |
| 40 | + DUPLICATE KEY(id) |
| 41 | + DISTRIBUTED BY HASH(id) BUCKETS 1 |
| 42 | + PROPERTIES ("replication_allocation" = "tag.location.default: 1") |
| 43 | + """ |
| 44 | + |
| 45 | + // grp 1: a large value (2^54) leaves the frame, then only small values remain. |
| 46 | + // grp 2: sign flip of a large value around small values. |
| 47 | + // grp 3: consecutive small values after a large one, with NULL in the nullable column. |
| 48 | + sql """ |
| 49 | + INSERT INTO test_window_float_sliding_frame VALUES |
| 50 | + (1, 1, 18014398509481984, 18014398509481984, 18014398509481984), |
| 51 | + (2, 1, 0.5, 0.5, 0.5), |
| 52 | + (3, 1, 0.5, 0.5, 0.5), |
| 53 | + (4, 1, 0.5, 0.5, NULL), |
| 54 | + (5, 2, 18014398509481984, 18014398509481984, 18014398509481984), |
| 55 | + (6, 2, 0.5, 0.5, 0.5), |
| 56 | + (7, 2, -18014398509481984, -18014398509481984, -18014398509481984), |
| 57 | + (8, 2, 0.5, 0.5, 0.5), |
| 58 | + (9, 2, 0.5, 0.5, 0.5), |
| 59 | + (10, 3, 18014398509481984, 18014398509481984, NULL), |
| 60 | + (11, 3, 0.5, 0.5, 0.5), |
| 61 | + (12, 3, 0.5, 0.5, 0.5), |
| 62 | + (13, 3, 0.5, 0.5, 0.5), |
| 63 | + (14, 3, 0.5, 0.5, 0.5) |
| 64 | + """ |
| 65 | + |
| 66 | + // The reported case: after row 1 leaves the frame, avg over [0.5, 0.5] must be 0.5. |
| 67 | + order_qt_avg_double_preceding_1 """ |
| 68 | + SELECT id, v_double, |
| 69 | + avg(v_double) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got |
| 70 | + FROM test_window_float_sliding_frame |
| 71 | + WHERE grp = 1 |
| 72 | + """ |
| 73 | + |
| 74 | + order_qt_sum_double_preceding_1 """ |
| 75 | + SELECT id, v_double, |
| 76 | + sum(v_double) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got |
| 77 | + FROM test_window_float_sliding_frame |
| 78 | + WHERE grp = 1 |
| 79 | + """ |
| 80 | + |
| 81 | + order_qt_avg_sum_float_preceding_1 """ |
| 82 | + SELECT id, v_float, |
| 83 | + avg(v_float) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got_avg, |
| 84 | + sum(v_float) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got_sum |
| 85 | + FROM test_window_float_sliding_frame |
| 86 | + WHERE grp = 1 |
| 87 | + """ |
| 88 | + |
| 89 | + // Sign flip: a large positive value leaves, then a large negative value |
| 90 | + // enters and leaves. Two-operand frames keep the expected sums exact. |
| 91 | + order_qt_avg_sum_double_sign_flip """ |
| 92 | + SELECT id, v_double, |
| 93 | + avg(v_double) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got_avg, |
| 94 | + sum(v_double) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got_sum |
| 95 | + FROM test_window_float_sliding_frame |
| 96 | + WHERE grp = 2 |
| 97 | + """ |
| 98 | + |
| 99 | + // Consecutive small values after the large one, wider frames and following bounds. |
| 100 | + order_qt_avg_sum_double_wider_frames """ |
| 101 | + SELECT id, v_double, |
| 102 | + avg(v_double) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS avg_p2, |
| 103 | + sum(v_double) OVER (ORDER BY id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS sum_p2, |
| 104 | + avg(v_double) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS avg_p1f1, |
| 105 | + sum(v_double) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sum_p1f1, |
| 106 | + avg(v_double) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS avg_f2 |
| 107 | + FROM test_window_float_sliding_frame |
| 108 | + WHERE grp = 3 |
| 109 | + """ |
| 110 | + |
| 111 | + // Nullable input: NULL rows are skipped while the frame keeps sliding. |
| 112 | + order_qt_avg_sum_nullable_partitioned """ |
| 113 | + SELECT id, grp, v_nullable, |
| 114 | + avg(v_nullable) OVER (PARTITION BY grp ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got_avg, |
| 115 | + sum(v_nullable) OVER (PARTITION BY grp ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got_sum |
| 116 | + FROM test_window_float_sliding_frame |
| 117 | + """ |
| 118 | + |
| 119 | + // Frames that only grow keep the incremental path and stay unchanged. |
| 120 | + order_qt_avg_sum_double_unbounded """ |
| 121 | + SELECT id, v_double, |
| 122 | + avg(v_double) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS got_avg, |
| 123 | + sum(v_double) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS got_sum |
| 124 | + FROM test_window_float_sliding_frame |
| 125 | + WHERE grp = 1 |
| 126 | + """ |
| 127 | + |
| 128 | + // Exact accumulators (integer / decimal) still use the incremental path and stay exact. |
| 129 | + order_qt_avg_sum_exact_types """ |
| 130 | + SELECT id, v_double, |
| 131 | + avg(CAST(v_double * 2 AS BIGINT)) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS avg_bigint, |
| 132 | + sum(CAST(v_double * 2 AS BIGINT)) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS sum_bigint, |
| 133 | + avg(CAST(v_double AS DECIMAL(27, 3))) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS avg_decimal, |
| 134 | + sum(CAST(v_double AS DECIMAL(27, 3))) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS sum_decimal |
| 135 | + FROM test_window_float_sliding_frame |
| 136 | + WHERE grp = 1 |
| 137 | + """ |
| 138 | + |
| 139 | + // The reported shape without a table. |
| 140 | + order_qt_avg_double_cte """ |
| 141 | + WITH t AS ( |
| 142 | + SELECT 1 AS id, CAST(18014398509481984 AS DOUBLE) AS v |
| 143 | + UNION ALL SELECT 2, CAST(1 AS DOUBLE) |
| 144 | + UNION ALL SELECT 3, CAST(1 AS DOUBLE) |
| 145 | + ) |
| 146 | + SELECT id, v, |
| 147 | + avg(v) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS got |
| 148 | + FROM t |
| 149 | + """ |
| 150 | +} |
0 commit comments