Skip to content

Commit

Permalink
[improvement](statistics)Drop column stats after schema change. (#39101)
Browse files Browse the repository at this point in the history
Drop column stats after schema change. So the auto analyze could be
triggered and will collect stats under new schema.
  • Loading branch information
Jibing-Li authored and dataroaring committed Aug 16, 2024
1 parent 7abcbed commit c6be1b9
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2870,6 +2870,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, null);
} catch (Exception e) {
LOG.info("Failed to drop stats after light schema change. Reason: {}", e.getMessage());
}

if (isDropIndex) {
// send drop rpc to be
Expand All @@ -2896,6 +2902,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, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,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);
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) {
Expand Down
5 changes: 5 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -5068,6 +5068,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, null);
} catch (Exception e) {
LOG.info("Failed to drop stats after rename column. Reason: {}", e.getMessage());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,9 @@ public void invalidateLocalStats(long catalogId, long dbId, long tableId, Set<St
}
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());
}
Expand Down Expand Up @@ -777,6 +779,9 @@ public void invalidateLocalStats(long catalogId, long dbId, long tableId, Set<St
}
}
}
if (allColumn && allPartition) {
tableStats.removeAllColumn();
}
tableStats.updatedTime = 0;
tableStats.userInjected = false;
tableStats.rowCount = table.getRowCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public void removeColumn(String indexName, String colName) {
colToColStatsMeta.remove(Pair.of(indexName, colName));
}

public void removeAllColumn() {
colToColStatsMeta.clear();
}

public Set<Pair<String, String>> analyzeColumns() {
return colToColStatsMeta.keySet();
}
Expand Down Expand Up @@ -191,6 +195,9 @@ public void gsonPostProcess() throws IOException {
if (indexesRowCount == null) {
indexesRowCount = new ConcurrentHashMap<>();
}
if (colToColStatsMeta == null) {
colToColStatsMeta = new ConcurrentHashMap<>();
}
}

public long getRowCount(long indexId) {
Expand Down
171 changes: 171 additions & 0 deletions regression-test/suites/statistics/test_schema_change_statistics.groovy
Original file line number Diff line number Diff line change
@@ -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"""
}

0 comments on commit c6be1b9

Please sign in to comment.