This is an automated email from the ASF dual-hosted git repository.

lijibing pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 9866542786a [improvement](statistics)Drop column stats after schema 
change. (#39101)
9866542786a is described below

commit 9866542786a9f9e3fa82ef54104f9f1e56e4299e
Author: Jibing-Li <[email protected]>
AuthorDate: Thu Aug 8 22:12:57 2024 +0800

    [improvement](statistics)Drop column stats after schema change. (#39101)
    
    Drop column stats after schema change. So the auto analyze could be
    triggered and will collect stats under new schema.
---
 .../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   |   5 +
 .../apache/doris/statistics/TableStatsMeta.java    |   7 +
 .../test_schema_change_statistics.groovy           | 171 +++++++++++++++++++++
 6 files changed, 206 insertions(+)

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 7d581272648..9bcfa1cde04 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
@@ -2870,6 +2870,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, null);
+                } catch (Exception e) {
+                    LOG.info("Failed to drop stats after light schema change. 
Reason: {}", e.getMessage());
+                }
 
                 if (isDropIndex) {
                     // send drop rpc to be
@@ -2896,6 +2902,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, null);
+                } 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 111bfbce1f7..f0d0df319c9 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
@@ -639,6 +639,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);
         postProcessOriginIndex();
+        // Drop table column stats after schema change finished.
+        try {
+            Env.getCurrentEnv().getAnalysisManager().dropStats(tbl, null);
+        } 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 326a48d2c45..7cbbc644733 100644
--- 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
@@ -5068,6 +5068,11 @@ public class Env {
                     indexIdToSchemaVersion);
             editLog.logColumnRename(info);
             LOG.info("rename coloumn[{}] to {}", colName, newColName);
+            try {
+                Env.getCurrentEnv().getAnalysisManager().dropStats(table, 
null);
+            } 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 0f9e833d496..7bc7011db89 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
@@ -726,7 +726,9 @@ public class AnalysisManager implements Writable {
         }
         TableIf table = StatisticsUtil.findTable(catalogId, dbId, tableId);
         StatisticsCache statsCache = Env.getCurrentEnv().getStatisticsCache();
+        boolean allColumn = false;
         if (columns == null) {
+            allColumn = true;
             columns = table.getSchemaAllIndexes(false)
                 .stream().map(Column::getName).collect(Collectors.toSet());
         }
@@ -777,6 +779,9 @@ public class AnalysisManager implements Writable {
                 }
             }
         }
+        if (allColumn && allPartition) {
+            tableStats.removeAllColumn();
+        }
         tableStats.updatedTime = 0;
         tableStats.userInjected = false;
         tableStats.rowCount = table.getRowCount();
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 4296cb02526..15509525c34 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
@@ -130,6 +130,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();
     }
@@ -191,6 +195,9 @@ public class TableStatsMeta implements Writable, 
GsonPostProcessable {
         if (indexesRowCount == null) {
             indexesRowCount = new ConcurrentHashMap<>();
         }
+        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..0dcff2f9f91
--- /dev/null
+++ b/regression-test/suites/statistics/test_schema_change_statistics.groovy
@@ -0,0 +1,171 @@
+// 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 force_sample_analyze=false"""
+    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]

Reply via email to