This is an automated email from the ASF dual-hosted git repository.
lijibing pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.1 by this push:
new b3597ea8982 [improvement](statistics)Drop column stats after schema
change. (#39101) (#39401)
b3597ea8982 is described below
commit b3597ea8982592b23677d76e57470d166b535b01
Author: Jibing-Li <[email protected]>
AuthorDate: Thu Aug 15 17:02:42 2024 +0800
[improvement](statistics)Drop column stats after schema change. (#39101)
(#39401)
backport: https://github.com/apache/doris/pull/39101
---
.../apache/doris/alter/SchemaChangeHandler.java | 12 ++
.../org/apache/doris/alter/SchemaChangeJobV2.java | 6 +
.../main/java/org/apache/doris/catalog/Env.java | 5 +
.../apache/doris/statistics/AnalysisManager.java | 10 +-
.../apache/doris/statistics/TableStatsMeta.java | 7 +
.../test_schema_change_statistics.groovy | 170 +++++++++++++++++++++
6 files changed, 208 insertions(+), 2 deletions(-)
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 09e8c984c07..633028ddaee 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 class SchemaChangeHandler extends AlterHandler {
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 class SchemaChangeHandler extends AlterHandler {
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 e74754de755..2bcb33adbcd 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 @@ public class SchemaChangeJobV2 extends AlterJobV2 {
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 1d33cbbe42a..f5e41a1bf1c 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 @@ public class Env {
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 d4d0231505f..4a19ea964f2 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 class AnalysisManager implements Writable {
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 class AnalysisManager implements Writable {
}
}
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 2d38b7ccd34..0f71ff691b0 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 class TableStatsMeta implements Writable,
GsonPostProcessable {
colToColStatsMeta.remove(Pair.of(indexName, colName));
}
+ public void removeAllColumn() {
+ colToColStatsMeta.clear();
+ }
+
public Set<Pair<String, String>> analyzeColumns() {
return colToColStatsMeta.keySet();
}
@@ -174,6 +178,9 @@ public class TableStatsMeta implements Writable,
GsonPostProcessable {
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 00000000000..c8e9d820271
--- /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"""
+}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]