diff --git a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java index 09e8c984c070cf..633028ddaee976 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java +++ b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java @@ -2766,6 +2766,12 @@ public void modifyTableLightSchemaChange(String rawSql, Database db, OlapTable o LOG.debug("logModifyTableAddOrDropInvertedIndices info:{}", info); } Env.getCurrentEnv().getEditLog().logModifyTableAddOrDropInvertedIndices(info); + // Drop table column stats after light schema change finished. + try { + Env.getCurrentEnv().getAnalysisManager().dropStats(olapTable); + } catch (Exception e) { + LOG.info("Failed to drop stats after light schema change. Reason: {}", e.getMessage()); + } if (isDropIndex) { // send drop rpc to be @@ -2792,6 +2798,12 @@ public void modifyTableLightSchemaChange(String rawSql, Database db, OlapTable o LOG.debug("logModifyTableAddOrDropColumns info:{}", info); } Env.getCurrentEnv().getEditLog().logModifyTableAddOrDropColumns(info); + // Drop table column stats after light schema change finished. + try { + Env.getCurrentEnv().getAnalysisManager().dropStats(olapTable); + } catch (Exception e) { + LOG.info("Failed to drop stats after light schema change. Reason: {}", e.getMessage()); + } } LOG.info("finished modify table's add or drop or modify columns. table: {}, job: {}, is replay: {}", olapTable.getName(), jobId, isReplay); diff --git a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeJobV2.java b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeJobV2.java index 86f0e24a62771e..f92c0fcf0b6f9c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeJobV2.java +++ b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeJobV2.java @@ -599,6 +599,12 @@ protected void runRunningJob() throws AlterCancelException { changeTableState(dbId, tableId, OlapTableState.NORMAL); LOG.info("set table's state to NORMAL, table id: {}, job id: {}", tableId, jobId); + // Drop table column stats after schema change finished. + try { + Env.getCurrentEnv().getAnalysisManager().dropStats(tbl); + } catch (Exception e) { + LOG.info("Failed to drop stats after schema change finished. Reason: {}", e.getMessage()); + } } private void onFinished(OlapTable tbl) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 1d33cbbe42a5a1..f5e41a1bf1cfe9 100755 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -4900,6 +4900,11 @@ private void renameColumn(Database db, OlapTable table, String colName, indexIdToSchemaVersion); editLog.logColumnRename(info); LOG.info("rename coloumn[{}] to {}", colName, newColName); + try { + Env.getCurrentEnv().getAnalysisManager().dropStats(table); + } catch (Exception e) { + LOG.info("Failed to drop stats after rename column. Reason: {}", e.getMessage()); + } } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisManager.java b/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisManager.java index d4d0231505f029..4a19ea964f2864 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisManager.java +++ b/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisManager.java @@ -658,8 +658,10 @@ public void invalidateLocalStats(long catalogId, long dbId, long tableId, return; } TableIf table = StatisticsUtil.findTable(catalogId, dbId, tableId); - StatisticsCache statisticsCache = Env.getCurrentEnv().getStatisticsCache(); + StatisticsCache statsCache = Env.getCurrentEnv().getStatisticsCache(); + boolean allColumn = false; if (columns == null) { + allColumn = true; columns = table.getSchemaAllIndexes(false) .stream().map(Column::getName).collect(Collectors.toSet()); } @@ -682,9 +684,13 @@ public void invalidateLocalStats(long catalogId, long dbId, long tableId, } } tableStats.removeColumn(indexName, column); - statisticsCache.invalidate(catalogId, dbId, tableId, indexId, column); + statsCache.invalidate(catalogId, dbId, tableId, indexId, column); } } + // To remove stale column name that is changed before. + if (allColumn) { + tableStats.removeAllColumn(); + } tableStats.updatedTime = 0; tableStats.userInjected = false; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/statistics/TableStatsMeta.java b/fe/fe-core/src/main/java/org/apache/doris/statistics/TableStatsMeta.java index 2d38b7ccd34880..0f71ff691b0407 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/statistics/TableStatsMeta.java +++ b/fe/fe-core/src/main/java/org/apache/doris/statistics/TableStatsMeta.java @@ -124,6 +124,10 @@ public void removeColumn(String indexName, String colName) { colToColStatsMeta.remove(Pair.of(indexName, colName)); } + public void removeAllColumn() { + colToColStatsMeta.clear(); + } + public Set> analyzeColumns() { return colToColStatsMeta.keySet(); } @@ -174,6 +178,9 @@ public void gsonPostProcess() throws IOException { if (newPartitionLoaded == null) { newPartitionLoaded = new AtomicBoolean(false); } + if (colToColStatsMeta == null) { + colToColStatsMeta = new ConcurrentHashMap<>(); + } } public long getRowCount(long indexId) { diff --git a/regression-test/suites/statistics/test_schema_change_statistics.groovy b/regression-test/suites/statistics/test_schema_change_statistics.groovy new file mode 100644 index 00000000000000..c8e9d8202714ac --- /dev/null +++ b/regression-test/suites/statistics/test_schema_change_statistics.groovy @@ -0,0 +1,170 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_schema_change_statistics") { + + def wait_schema_change_finished = { -> + Thread.sleep(1000) + def result = sql """SHOW ALTER TABLE COLUMN from test_schema_change_statistics""" + boolean finished = false; + for (int i = 0; i < 120; i++) { + finished = true; + for (int j = 0; j < result.size(); j++) { + if (result[j][9] != "FINISHED") { + finished = false; + break; + } + } + if (finished) { + break; + } + logger.info("Schema change not finished yet: " + result) + result = sql """SHOW ALTER TABLE COLUMN from test_schema_change_statistics""" + Thread.sleep(1000) + } + } + + def stats_dropped = { table -> + def result1 = sql """show column cached stats $table""" + def result2 = sql """show column stats $table""" + boolean dropped = false + for (int i = 0; i < 120; i++) { + if (0 == result1.size() && 0 == result2.size()) { + dropped = true; + break; + } + Thread.sleep(1000) + result1 = sql """show column cached stats $table""" + result2 = sql """show column stats $table""" + } + assertTrue(dropped) + } + + sql """drop database if exists test_schema_change_statistics""" + sql """create database test_schema_change_statistics""" + sql """use test_schema_change_statistics""" + sql """set global enable_auto_analyze=false""" + + + sql """CREATE TABLE change ( + key1 bigint NOT NULL, + key2 bigint NOT NULL, + value1 int NOT NULL, + value2 int NOT NULL, + value3 int NOT NULL + )ENGINE=OLAP + DUPLICATE KEY(`key1`, `key2`) + COMMENT "OLAP" + DISTRIBUTED BY HASH(`key1`) BUCKETS 2 + PROPERTIES ("replication_num" = "1"); + """ + createMV("create materialized view mv1 as select key1,value1,value2 from change;") + sql """insert into change values (1, 2, 3, 4, 5), (1, 2, 3, 4, 5), (10, 20, 30, 40, 50), (10, 20, 30, 40, 50), (100, 200, 300, 400, 500), (1001, 2001, 3001, 4001, 5001);""" + + sql """analyze table change with sync;""" + def result = sql """show column stats change""" + assertEquals(8, result.size()) + result = sql """show column cached stats change""" + assertEquals(8, result.size()) + + sql """ALTER TABLE change ADD COLUMN new_value INT DEFAULT "0" AFTER value3;""" + stats_dropped("change") + + sql """analyze table change with sync;""" + result = sql """show column stats change""" + assertEquals(9, result.size()) + result = sql """show column cached stats change""" + assertEquals(9, result.size()) + + sql """ALTER TABLE change DROP COLUMN new_value;""" + stats_dropped("change") + + sql """analyze table change with sync;""" + result = sql """show column stats change""" + assertEquals(8, result.size()) + result = sql """show column cached stats change""" + assertEquals(8, result.size()) + + sql """ALTER TABLE change DROP COLUMN mv_value1 from mv1;""" + wait_schema_change_finished() + stats_dropped("change") + + sql """analyze table change with sync;""" + result = sql """show column stats change""" + assertEquals(7, result.size()) + result = sql """show column cached stats change""" + assertEquals(7, result.size()) + + sql """ALTER TABLE change ADD COLUMN new_key INT key DEFAULT "0" AFTER key2;""" + wait_schema_change_finished() + stats_dropped("change") + + sql """analyze table change with sync;""" + result = sql """show column stats change""" + assertEquals(8, result.size()) + result = sql """show column cached stats change""" + assertEquals(8, result.size()) + + sql """ALTER TABLE change DROP COLUMN new_key;""" + wait_schema_change_finished() + stats_dropped("change") + + sql """analyze table change with sync;""" + result = sql """show column stats change""" + assertEquals(7, result.size()) + result = sql """show column cached stats change""" + assertEquals(7, result.size()) + + sql """ALTER TABLE change MODIFY COLUMN value2 BIGINT AFTER value3;""" + wait_schema_change_finished() + stats_dropped("change") + + sql """analyze table change with sync;""" + result = sql """show column stats change""" + assertEquals(7, result.size()) + result = sql """show column cached stats change""" + assertEquals(7, result.size()) + + + sql """CREATE TABLE change_no_index ( + key1 bigint NOT NULL, + key2 bigint NOT NULL, + value1 int NOT NULL, + value2 int NOT NULL, + value3 int NOT NULL + )ENGINE=OLAP + DUPLICATE KEY(`key1`, `key2`) + COMMENT "OLAP" + DISTRIBUTED BY HASH(`key1`) BUCKETS 2 + PROPERTIES ("replication_num" = "1"); + """ + + sql """insert into change_no_index values (1, 2, 3, 4, 5), (1, 2, 3, 4, 5), (10, 20, 30, 40, 50), (10, 20, 30, 40, 50), (100, 200, 300, 400, 500), (1001, 2001, 3001, 4001, 5001);""" + sql """analyze table change_no_index with sync;""" + result = sql """show column stats change_no_index""" + assertEquals(5, result.size()) + result = sql """show column cached stats change_no_index""" + assertEquals(5, result.size()) + + sql """ALTER TABLE change_no_index RENAME COLUMN value1 value1_new;""" + wait_schema_change_finished() + stats_dropped("change_no_index") + + + sql """drop database if exists test_schema_change_statistics""" +} +