|
| 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 | +// A materialized view re-validates its schema against a fresh analysis of its own query whenever a |
| 22 | +// base table changes (MTMVPlanUtil.ensureMTMVQueryUsable -> checkColumnIfChange). A table created |
| 23 | +// with row binlog carries hidden columns that the query never produces, and an MV carries them too |
| 24 | +// when its own properties enable row binlog (which cascade IVM requires). The analyzed schema has to |
| 25 | +// contain them as well, otherwise every refresh of such an MV fails with |
| 26 | +// "column length not equals, please check whether columns of base table have changed" -- including a |
| 27 | +// COMPLETE refresh, which is the only way out of a stale baseline. |
| 28 | +// |
| 29 | +// Covered here: the four MV shapes, and the DORIS-28306 shape where a cascade L1 could not recover |
| 30 | +// through COMPLETE after a schema ABA on a referenced column. |
| 31 | +suite("test_ivm_row_binlog_schema_validation") { |
| 32 | + def rowBinlogProps = "'replication_num' = '1', 'binlog.enable' = 'true', 'binlog.format' = 'ROW'" |
| 33 | + def plainProps = "'replication_num' = '1'" |
| 34 | + // A cascade source keeps historical values so its own downstream MV can read the binlog. |
| 35 | + def cascadeProps = rowBinlogProps + ", 'binlog.need_historical_value' = 'true'" |
| 36 | + |
| 37 | + def ddlJobCount = { String table -> |
| 38 | + return sql("""SHOW ALTER TABLE COLUMN WHERE TableName = '${table}'""").size() |
| 39 | + } |
| 40 | + |
| 41 | + // `SHOW ALTER TABLE COLUMN` keeps finished jobs, so wait for a *new* job that is FINISHED. |
| 42 | + def waitDdlFinished = { String table, int previousJobCount -> |
| 43 | + Awaitility.await().atMost(300, SECONDS).pollInterval(2, SECONDS).until({ |
| 44 | + def jobs = sql """SHOW ALTER TABLE COLUMN WHERE TableName = '${table}'""" |
| 45 | + return jobs.size() > previousJobCount |
| 46 | + && jobs.every({ row -> row[9].toString() == 'FINISHED' }) |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + def lastTaskId = null |
| 51 | + // tasks('type'='mv') can briefly miss the just-finished task, so require a *new* TaskId. |
| 52 | + def waitTerminalTask = { String mv -> |
| 53 | + def taskResult |
| 54 | + Awaitility.await().atMost(300, SECONDS).pollInterval(2, SECONDS).until({ |
| 55 | + taskResult = sql_return_maparray(""" |
| 56 | + SELECT TaskId, Status, RefreshMode, ErrorMsg |
| 57 | + FROM tasks('type'='mv') |
| 58 | + WHERE MvDatabaseName = '${context.dbName}' AND MvName = '${mv}' |
| 59 | + ORDER BY CreateTime DESC, TaskId DESC LIMIT 1 |
| 60 | + """) |
| 61 | + return !taskResult.isEmpty() |
| 62 | + && taskResult[0].TaskId.toString() != lastTaskId |
| 63 | + && taskResult[0].Status.toString() != 'PENDING' |
| 64 | + && taskResult[0].Status.toString() != 'RUNNING' |
| 65 | + }) |
| 66 | + lastTaskId = taskResult[0].TaskId.toString() |
| 67 | + return taskResult[0] |
| 68 | + } |
| 69 | + |
| 70 | + def refresh = { String mv, String mode -> |
| 71 | + sql """REFRESH MATERIALIZED VIEW ${mv} ${mode}""" |
| 72 | + return waitTerminalTask(mv) |
| 73 | + } |
| 74 | + |
| 75 | + // ---------------------------------------------------------------- 1. four MV shapes |
| 76 | + // Same query and same base table for all four; what differs is whether the MV itself enables |
| 77 | + // row binlog, and (for the IVM ones) whether the MV is a MOW unique table. |
| 78 | + def cases = [ |
| 79 | + [name: "rb_ivm", mvProps: rowBinlogProps, ivm: true], |
| 80 | + [name: "rb_ivm_no_binlog", mvProps: plainProps, ivm: true], |
| 81 | + [name: "rb_dup", mvProps: rowBinlogProps, ivm: false], |
| 82 | + [name: "rb_dup_no_binlog", mvProps: plainProps, ivm: false], |
| 83 | + ] |
| 84 | + |
| 85 | + for (def c : cases) { |
| 86 | + def table = c.name + "_base" |
| 87 | + def mv = c.name + "_mv" |
| 88 | + |
| 89 | + sql """DROP MATERIALIZED VIEW IF EXISTS ${mv}""" |
| 90 | + sql """DROP TABLE IF EXISTS ${table}""" |
| 91 | + sql """ |
| 92 | + CREATE TABLE ${table} ( |
| 93 | + k1 INT NOT NULL, |
| 94 | + v1 INT NULL, |
| 95 | + spare INT NULL |
| 96 | + ) |
| 97 | + UNIQUE KEY(k1) |
| 98 | + DISTRIBUTED BY HASH(k1) BUCKETS 1 |
| 99 | + PROPERTIES ('replication_num' = '1', 'enable_unique_key_merge_on_write' = 'true', |
| 100 | + 'binlog.enable' = 'true', 'binlog.format' = 'ROW', |
| 101 | + 'binlog.need_historical_value' = 'true') |
| 102 | + """ |
| 103 | + sql """INSERT INTO ${table} VALUES (1, 10, 1), (2, 20, 1)""" |
| 104 | + |
| 105 | + sql """ |
| 106 | + CREATE MATERIALIZED VIEW ${mv} |
| 107 | + BUILD DEFERRED REFRESH ${c.ivm ? 'INCREMENTAL' : 'COMPLETE'} ON MANUAL |
| 108 | + KEY(k1) |
| 109 | + DISTRIBUTED BY HASH(k1) BUCKETS 1 |
| 110 | + PROPERTIES (${c.mvProps}) |
| 111 | + AS SELECT k1, SUM(v1) AS total FROM ${table} GROUP BY k1 |
| 112 | + """ |
| 113 | + def task = refresh(mv, "COMPLETE") |
| 114 | + assertEquals("SUCCESS", task.Status.toString(), "${c.name} baseline refresh: " + task.ErrorMsg) |
| 115 | + |
| 116 | + // A schema change that leaves the query intact: the MV must stay refreshable. The dropped |
| 117 | + // column is not referenced, so nothing about the MV's result changes. |
| 118 | + def before = ddlJobCount(table) |
| 119 | + sql """ALTER TABLE ${table} DROP COLUMN spare""" |
| 120 | + waitDdlFinished(table, before) |
| 121 | + |
| 122 | + task = refresh(mv, c.ivm ? "INCREMENTAL" : "COMPLETE") |
| 123 | + assertEquals("SUCCESS", task.Status.toString(), |
| 124 | + "${c.name} refresh after an unrelated DROP COLUMN: " + task.ErrorMsg) |
| 125 | + } |
| 126 | + |
| 127 | + // The dropped column was never referenced, so all four MVs must still hold their baseline rows. |
| 128 | + order_qt_rb_ivm_rows "SELECT k1, total FROM rb_ivm_mv" |
| 129 | + order_qt_rb_ivm_no_binlog_rows "SELECT k1, total FROM rb_ivm_no_binlog_mv" |
| 130 | + order_qt_rb_dup_rows "SELECT k1, total FROM rb_dup_mv" |
| 131 | + order_qt_rb_dup_no_binlog_rows "SELECT k1, total FROM rb_dup_no_binlog_mv" |
| 132 | + |
| 133 | + // ------------------------------------- 2. DORIS-28306: cascade L1 must recover via COMPLETE |
| 134 | + // A cascade forces row binlog onto L1, so L1 is exactly the shape that used to make every |
| 135 | + // refresh of L1 fail after a schema change. An ABA on a referenced column must still leave the |
| 136 | + // explicit COMPLETE recovery path usable. |
| 137 | + def cTable = "cascade_aba_base" |
| 138 | + sql """DROP MATERIALIZED VIEW IF EXISTS cascade_l2""" |
| 139 | + sql """DROP MATERIALIZED VIEW IF EXISTS cascade_l1""" |
| 140 | + sql """DROP TABLE IF EXISTS ${cTable}""" |
| 141 | + sql """ |
| 142 | + CREATE TABLE ${cTable} ( |
| 143 | + id BIGINT NOT NULL, |
| 144 | + grp INT NULL, |
| 145 | + amount BIGINT NULL |
| 146 | + ) |
| 147 | + UNIQUE KEY(id) |
| 148 | + DISTRIBUTED BY HASH(id) BUCKETS 1 |
| 149 | + PROPERTIES ('replication_num' = '1', 'enable_unique_key_merge_on_write' = 'true', |
| 150 | + 'binlog.enable' = 'true', 'binlog.format' = 'ROW', |
| 151 | + 'binlog.need_historical_value' = 'true') |
| 152 | + """ |
| 153 | + sql """INSERT INTO ${cTable} VALUES (1, 10, 100), (2, 10, 200), (3, 20, 300)""" |
| 154 | + |
| 155 | + sql """ |
| 156 | + CREATE MATERIALIZED VIEW cascade_l1 |
| 157 | + BUILD DEFERRED REFRESH INCREMENTAL ON MANUAL KEY(id) |
| 158 | + PROPERTIES (${cascadeProps}) |
| 159 | + AS SELECT id, grp, amount FROM ${cTable} |
| 160 | + """ |
| 161 | + def cTask = refresh("cascade_l1", "COMPLETE") |
| 162 | + assertEquals("SUCCESS", cTask.Status.toString(), "cascade L1 baseline: " + cTask.ErrorMsg) |
| 163 | + |
| 164 | + sql """ |
| 165 | + CREATE MATERIALIZED VIEW cascade_l2 |
| 166 | + BUILD DEFERRED REFRESH INCREMENTAL ON MANUAL KEY(grp) |
| 167 | + PROPERTIES (${cascadeProps}) |
| 168 | + AS SELECT grp, COUNT(*) AS row_count, SUM(amount) AS total_amount FROM cascade_l1 GROUP BY grp |
| 169 | + """ |
| 170 | + cTask = refresh("cascade_l2", "COMPLETE") |
| 171 | + assertEquals("SUCCESS", cTask.Status.toString(), "cascade L2 baseline: " + cTask.ErrorMsg) |
| 172 | + order_qt_cascade_l2_baseline "SELECT grp, row_count, total_amount FROM cascade_l2" |
| 173 | + |
| 174 | + def cBefore = ddlJobCount(cTable) |
| 175 | + sql """ALTER TABLE ${cTable} DROP COLUMN grp""" |
| 176 | + waitDdlFinished(cTable, cBefore) |
| 177 | + cBefore = ddlJobCount(cTable) |
| 178 | + sql """ALTER TABLE ${cTable} ADD COLUMN grp INT NULL DEFAULT '0'""" |
| 179 | + waitDdlFinished(cTable, cBefore) |
| 180 | + |
| 181 | + // COMPLETE is the recovery path: it has to rebuild L1 from the current base-table semantics, |
| 182 | + // where every pre-existing row now reads the re-added column's default value. Whether the |
| 183 | + // strict INCREMENTAL in between is rejected is a separate contract, owned by the suite for the |
| 184 | + // baseline invalidation itself, so it is deliberately not asserted here -- this suite has to |
| 185 | + // hold with or without that change. |
| 186 | + cTask = refresh("cascade_l1", "COMPLETE") |
| 187 | + assertEquals("SUCCESS", cTask.Status.toString(), "COMPLETE after schema ABA: " + cTask.ErrorMsg) |
| 188 | + order_qt_cascade_l1_after_aba "SELECT grp, COUNT(*) AS c, SUM(amount) AS s FROM cascade_l1 GROUP BY grp" |
| 189 | + |
| 190 | + // ... and the downstream level stays refreshable afterwards. |
| 191 | + cTask = refresh("cascade_l2", "COMPLETE") |
| 192 | + assertEquals("SUCCESS", cTask.Status.toString(), "cascade L2 after L1 recovery: " + cTask.ErrorMsg) |
| 193 | + order_qt_cascade_l2_after_aba "SELECT grp, row_count, total_amount FROM cascade_l2" |
| 194 | +} |
0 commit comments