|
| 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 | +import org.awaitility.Awaitility |
| 19 | +import static java.util.concurrent.TimeUnit.SECONDS |
| 20 | + |
| 21 | +suite("test_ivm_minmax_count") { |
| 22 | + // The base-table queries below assert the same rows as the MV, so transparent rewrite must be off. |
| 23 | + sql """set enable_materialized_view_rewrite = false;""" |
| 24 | + sql """set enable_dml_materialized_view_rewrite = false;""" |
| 25 | + sql """drop materialized view if exists ivm_mm_count_mv""" |
| 26 | + sql """drop table if exists ivm_mm_count_t""" |
| 27 | + |
| 28 | + sql """ |
| 29 | + CREATE TABLE ivm_mm_count_t ( |
| 30 | + id BIGINT NOT NULL, |
| 31 | + category VARCHAR(16) NOT NULL, |
| 32 | + score BIGINT |
| 33 | + ) |
| 34 | + UNIQUE KEY(id) |
| 35 | + DISTRIBUTED BY HASH(id) BUCKETS 1 |
| 36 | + PROPERTIES ( |
| 37 | + "replication_num" = "1", |
| 38 | + "enable_unique_key_merge_on_write" = "true", |
| 39 | + "binlog.enable" = "true", |
| 40 | + "binlog.format" = "ROW", |
| 41 | + "binlog.need_historical_value" = "true" |
| 42 | + ) |
| 43 | + """ |
| 44 | + |
| 45 | + // category a: min=10 twice, max=30 once |
| 46 | + // category b: min=5 three times, max=5 |
| 47 | + sql """INSERT INTO ivm_mm_count_t VALUES |
| 48 | + (1, 'a', 10), (2, 'a', 10), (3, 'a', 30), |
| 49 | + (4, 'b', 5), (5, 'b', 5), (6, 'b', 5)""" |
| 50 | + |
| 51 | + sql """ |
| 52 | + CREATE MATERIALIZED VIEW ivm_mm_count_mv |
| 53 | + BUILD DEFERRED REFRESH INCREMENTAL FALLBACK ON MANUAL |
| 54 | + KEY(category) |
| 55 | + DISTRIBUTED BY HASH(category) BUCKETS 1 |
| 56 | + PROPERTIES ("replication_num" = "1") |
| 57 | + AS SELECT category, MIN(score) AS min_score, MAX(score) AS max_score, COUNT(*) AS row_count |
| 58 | + FROM ivm_mm_count_t |
| 59 | + GROUP BY category |
| 60 | + """ |
| 61 | + |
| 62 | + def assertNoFallback = { task -> |
| 63 | + def reason = task.IvmFallbackReason |
| 64 | + assertTrue(reason == null || reason.toString() == "\\N", |
| 65 | + "unexpected IVM fallback: ${reason}") |
| 66 | + } |
| 67 | + |
| 68 | + def latestTask = { |
| 69 | + def taskResult |
| 70 | + Awaitility.await().atMost(300, SECONDS).pollInterval(2, SECONDS).until({ |
| 71 | + taskResult = sql_return_maparray(""" |
| 72 | + SELECT Status, RefreshMode, IvmFallbackReason, ErrorMsg |
| 73 | + FROM tasks('type'='mv') |
| 74 | + WHERE MvDatabaseName = '${context.dbName}' |
| 75 | + AND MvName = 'ivm_mm_count_mv' |
| 76 | + ORDER BY CreateTime DESC, TaskId DESC LIMIT 1 |
| 77 | + """) |
| 78 | + return !taskResult.isEmpty() |
| 79 | + && taskResult[0].Status.toString() != 'PENDING' |
| 80 | + && taskResult[0].Status.toString() != 'RUNNING' |
| 81 | + }) |
| 82 | + return taskResult[0] |
| 83 | + } |
| 84 | + |
| 85 | + sql """REFRESH MATERIALIZED VIEW ivm_mm_count_mv INCREMENTAL""" |
| 86 | + waitingMTMVTaskFinishedByMvName("ivm_mm_count_mv") |
| 87 | + order_qt_initial """ |
| 88 | + SELECT category, min_score, max_score, row_count |
| 89 | + FROM ivm_mm_count_mv |
| 90 | + """ |
| 91 | + |
| 92 | + // Delete one of the two rows that hold the minimum of category a. The minimum survives, |
| 93 | + // so the hidden min-count state keeps the refresh incremental. |
| 94 | + sql """DELETE FROM ivm_mm_count_t WHERE id = 1""" |
| 95 | + Thread.sleep(1000) |
| 96 | + sql """REFRESH MATERIALIZED VIEW ivm_mm_count_mv INCREMENTAL""" |
| 97 | + def duplicateMinTask = latestTask() |
| 98 | + assertEquals("SUCCESS", duplicateMinTask.Status.toString()) |
| 99 | + assertNoFallback(duplicateMinTask) |
| 100 | + order_qt_after_duplicate_min_delete """ |
| 101 | + SELECT category, min_score, max_score, row_count |
| 102 | + FROM ivm_mm_count_mv |
| 103 | + """ |
| 104 | + |
| 105 | + // Delete one of the three rows that hold the maximum of category b. The maximum survives. |
| 106 | + sql """DELETE FROM ivm_mm_count_t WHERE id = 4""" |
| 107 | + Thread.sleep(1000) |
| 108 | + sql """REFRESH MATERIALIZED VIEW ivm_mm_count_mv INCREMENTAL""" |
| 109 | + def duplicateMaxTask = latestTask() |
| 110 | + assertEquals("SUCCESS", duplicateMaxTask.Status.toString()) |
| 111 | + assertNoFallback(duplicateMaxTask) |
| 112 | + order_qt_after_duplicate_max_delete """ |
| 113 | + SELECT category, min_score, max_score, row_count |
| 114 | + FROM ivm_mm_count_mv |
| 115 | + """ |
| 116 | + |
| 117 | + // Delete the last row holding category a's minimum. Its count reaches zero and no smaller |
| 118 | + // value was inserted, so the refresh still falls back to a complete refresh. |
| 119 | + sql """DELETE FROM ivm_mm_count_t WHERE id = 2""" |
| 120 | + Thread.sleep(1000) |
| 121 | + sql """REFRESH MATERIALIZED VIEW ivm_mm_count_mv INCREMENTAL FALLBACK""" |
| 122 | + def lastMinTask = latestTask() |
| 123 | + assertEquals("SUCCESS", lastMinTask.Status.toString()) |
| 124 | + assertEquals("COMPLETE", lastMinTask.RefreshMode.toString()) |
| 125 | + assertEquals("MIN_MAX_BOUNDARY_HIT", lastMinTask.IvmFallbackReason.toString()) |
| 126 | + order_qt_after_last_min_delete """ |
| 127 | + SELECT category, min_score, max_score, row_count |
| 128 | + FROM ivm_mm_count_mv |
| 129 | + """ |
| 130 | + |
| 131 | + // A smaller inserted value becomes the new minimum without a complete refresh. |
| 132 | + sql """INSERT INTO ivm_mm_count_t VALUES (7, 'a', 7), (8, 'a', 7)""" |
| 133 | + Thread.sleep(1000) |
| 134 | + sql """REFRESH MATERIALIZED VIEW ivm_mm_count_mv INCREMENTAL""" |
| 135 | + def smallerInsertTask = latestTask() |
| 136 | + assertEquals("SUCCESS", smallerInsertTask.Status.toString()) |
| 137 | + assertNoFallback(smallerInsertTask) |
| 138 | + order_qt_after_smaller_insert """ |
| 139 | + SELECT category, min_score, max_score, row_count |
| 140 | + FROM ivm_mm_count_mv |
| 141 | + """ |
| 142 | + |
| 143 | + // The source query must agree with the MV after every step. |
| 144 | + order_qt_source """ |
| 145 | + SELECT category, MIN(score), MAX(score), COUNT(*) |
| 146 | + FROM ivm_mm_count_t |
| 147 | + GROUP BY category |
| 148 | + """ |
| 149 | +} |
0 commit comments