|
| 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 | +// Dropping a column that an IVM references, then re-adding a column with the same name |
| 22 | +// (schema ABA), used to let a strict INCREMENTAL refresh report SUCCESS while silently |
| 23 | +// keeping the rows computed under the old column epoch. |
| 24 | +// |
| 25 | +// The base table change is metadata-only (light schema change) and emits no binlog, so the |
| 26 | +// delta is empty and the refresh has nothing to apply -- the MV baseline is simply stale. |
| 27 | +// |
| 28 | +// Expected: dropping a referenced column invalidates the IVM baseline, so a strict |
| 29 | +// INCREMENTAL refresh is rejected and the user is told to run a COMPLETE refresh. |
| 30 | +// Dropping an unreferenced column must still leave the incremental path untouched. |
| 31 | +suite("test_ivm_drop_referenced_column_baseline_rebuild") { |
| 32 | + def tableName = "ivm_drop_ref_col_t" |
| 33 | + def mvName = "ivm_drop_ref_col_mv" |
| 34 | + |
| 35 | + sql """DROP MATERIALIZED VIEW IF EXISTS ${mvName}""" |
| 36 | + sql """DROP TABLE IF EXISTS ${tableName}""" |
| 37 | + |
| 38 | + sql """ |
| 39 | + CREATE TABLE ${tableName} ( |
| 40 | + id BIGINT NOT NULL, |
| 41 | + grp INT NULL, |
| 42 | + amount BIGINT NULL, |
| 43 | + spare INT NULL |
| 44 | + ) |
| 45 | + UNIQUE KEY(id) |
| 46 | + DISTRIBUTED BY HASH(id) BUCKETS 1 |
| 47 | + PROPERTIES ( |
| 48 | + "replication_num" = "1", |
| 49 | + "enable_unique_key_merge_on_write" = "true", |
| 50 | + "binlog.enable" = "true", |
| 51 | + "binlog.format" = "ROW", |
| 52 | + "binlog.need_historical_value" = "true" |
| 53 | + ) |
| 54 | + """ |
| 55 | + sql """INSERT INTO ${tableName} VALUES (1, 10, 100, 7), (2, 10, 200, 8), (3, 20, 300, 9)""" |
| 56 | + |
| 57 | + sql """ |
| 58 | + CREATE MATERIALIZED VIEW ${mvName} |
| 59 | + BUILD DEFERRED REFRESH INCREMENTAL ON MANUAL |
| 60 | + KEY(grp) |
| 61 | + DISTRIBUTED BY HASH(grp) BUCKETS 1 |
| 62 | + PROPERTIES ("replication_num" = "1") |
| 63 | + AS SELECT grp, COUNT(*) AS cnt, SUM(amount) AS total |
| 64 | + FROM ${tableName} GROUP BY grp |
| 65 | + """ |
| 66 | + |
| 67 | + def ddlJobCount = { String table -> |
| 68 | + return sql("""SHOW ALTER TABLE COLUMN WHERE TableName = '${table}'""").size() |
| 69 | + } |
| 70 | + |
| 71 | + // `SHOW ALTER TABLE COLUMN` keeps finished jobs, so wait for a *new* job that is FINISHED. |
| 72 | + def waitDdlFinished = { String table, int previousJobCount -> |
| 73 | + Awaitility.await().atMost(300, SECONDS).pollInterval(2, SECONDS).until({ |
| 74 | + def jobs = sql """SHOW ALTER TABLE COLUMN WHERE TableName = '${table}'""" |
| 75 | + return jobs.size() > previousJobCount |
| 76 | + && jobs.every({ row -> row[9].toString() == 'FINISHED' }) |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + def lastTaskId = null |
| 81 | + // tasks('type'='mv') can briefly miss the just-finished task, so require a *new* TaskId. |
| 82 | + def waitTerminalTask = { String mv -> |
| 83 | + def taskResult |
| 84 | + Awaitility.await().atMost(300, SECONDS).pollInterval(2, SECONDS).until({ |
| 85 | + taskResult = sql_return_maparray(""" |
| 86 | + SELECT TaskId, Status, RefreshMode, IvmFallbackReason, ErrorMsg |
| 87 | + FROM tasks('type'='mv') |
| 88 | + WHERE MvDatabaseName = '${context.dbName}' AND MvName = '${mv}' |
| 89 | + ORDER BY CreateTime DESC, TaskId DESC LIMIT 1 |
| 90 | + """) |
| 91 | + return !taskResult.isEmpty() |
| 92 | + && taskResult[0].TaskId.toString() != lastTaskId |
| 93 | + && taskResult[0].Status.toString() != 'PENDING' |
| 94 | + && taskResult[0].Status.toString() != 'RUNNING' |
| 95 | + }) |
| 96 | + lastTaskId = taskResult[0].TaskId.toString() |
| 97 | + return taskResult[0] |
| 98 | + } |
| 99 | + |
| 100 | + // ---------------------------------------------------------------- 1. baseline |
| 101 | + sql """REFRESH MATERIALIZED VIEW ${mvName} COMPLETE""" |
| 102 | + def task = waitTerminalTask(mvName) |
| 103 | + assertEquals("SUCCESS", task.Status.toString(), "baseline COMPLETE refresh: " + task.ErrorMsg) |
| 104 | + order_qt_mv_rows_baseline "SELECT grp, cnt, total FROM ${mvName}" |
| 105 | + |
| 106 | + // ------------------------------------- 2. unreferenced column: no baseline invalidation |
| 107 | + def before = ddlJobCount(tableName) |
| 108 | + sql """ALTER TABLE ${tableName} DROP COLUMN spare""" |
| 109 | + waitDdlFinished(tableName, before) |
| 110 | + |
| 111 | + sql """REFRESH MATERIALIZED VIEW ${mvName} INCREMENTAL""" |
| 112 | + task = waitTerminalTask(mvName) |
| 113 | + assertEquals("SUCCESS", task.Status.toString(), |
| 114 | + "dropping an unreferenced column must not invalidate the IVM baseline: " + task.ErrorMsg) |
| 115 | + |
| 116 | + // ---------------------------------------- 3. referenced column: strict INCREMENTAL rejected |
| 117 | + before = ddlJobCount(tableName) |
| 118 | + sql """ALTER TABLE ${tableName} DROP COLUMN grp""" |
| 119 | + waitDdlFinished(tableName, before) |
| 120 | + |
| 121 | + sql """REFRESH MATERIALIZED VIEW ${mvName} INCREMENTAL""" |
| 122 | + task = waitTerminalTask(mvName) |
| 123 | + assertEquals("FAILED", task.Status.toString(), |
| 124 | + "dropping a referenced column must reject a strict INCREMENTAL refresh") |
| 125 | + |
| 126 | + // -------------------------------- 4. same-name re-add (schema ABA) is still rejected |
| 127 | + before = ddlJobCount(tableName) |
| 128 | + sql """ALTER TABLE ${tableName} ADD COLUMN grp INT NULL DEFAULT '0'""" |
| 129 | + waitDdlFinished(tableName, before) |
| 130 | + |
| 131 | + sql """REFRESH MATERIALIZED VIEW ${mvName} INCREMENTAL""" |
| 132 | + task = waitTerminalTask(mvName) |
| 133 | + assertEquals("FAILED", task.Status.toString(), |
| 134 | + "schema ABA must not be silently accepted by a strict INCREMENTAL refresh") |
| 135 | + assertTrue(task.ErrorMsg.toString().contains("baseline rebuild is pending"), |
| 136 | + "expected a pending baseline rebuild hint, got: " + task.ErrorMsg) |
| 137 | + |
| 138 | + // ------------------------------------ 5. COMPLETE rebuild reflects current base semantics |
| 139 | + // Every pre-existing row now reads the re-added column's default value. |
| 140 | + sql """REFRESH MATERIALIZED VIEW ${mvName} COMPLETE""" |
| 141 | + task = waitTerminalTask(mvName) |
| 142 | + assertEquals("SUCCESS", task.Status.toString(), "COMPLETE rebuild after ABA: " + task.ErrorMsg) |
| 143 | + order_qt_mv_rows_after_aba "SELECT grp, cnt, total FROM ${mvName}" |
| 144 | +} |
0 commit comments