Skip to content

Commit

Permalink
[fix](statistics)Fix clear stale mv row count bug. (#41653)
Browse files Browse the repository at this point in the history
When drop an mv, the row count for that mv should be removed in memory
as well. Before we use OlapTable.getIndexIds to get all index ids, but
this function always returns null. This pr is to fix this bug, use
getIndexIdList instead.
  • Loading branch information
Jibing-Li authored Oct 14, 2024
1 parent c72c090 commit b8b9333
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ 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 @@ -230,21 +226,22 @@ public long getRowCount(long indexId) {
return indexesRowCount.getOrDefault(indexId, -1L);
}

public void clearIndexesRowCount() {
indexesRowCount.clear();
}

private void clearStaleIndexRowCount(OlapTable table) {
protected void clearStaleIndexRowCount(OlapTable table) {
Iterator<Long> iterator = indexesRowCount.keySet().iterator();
List<Long> indexIds = table.getIndexIds();
List<Long> indexIds = table.getIndexIdList();
while (iterator.hasNext()) {
long key = iterator.next();
if (indexIds.contains(key)) {
if (!indexIds.contains(key)) {
iterator.remove();
}
}
}

// For unit test only.
protected void addIndexRowForTest(long indexId, long rowCount) {
indexesRowCount.put(indexId, rowCount);
}

public long getBaseIndexDeltaRowCount(OlapTable table) {
if (colToColStatsMeta == null || colToColStatsMeta.isEmpty() || userInjected) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

import org.apache.doris.catalog.OlapTable;

import com.google.common.collect.Lists;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.List;

class TableStatsMetaTest {

Expand All @@ -35,4 +39,33 @@ void update(@Mocked OlapTable table) {
tableStatsMeta.update(jobInfo, table);
Assertions.assertEquals(4, tableStatsMeta.rowCount);
}

@Test
void testClearStaleIndexRowCount() {
TableStatsMeta meta = new TableStatsMeta();
meta.addIndexRowForTest(1, 1);
meta.addIndexRowForTest(2, 2);
meta.addIndexRowForTest(3, 3);
Assertions.assertEquals(1, meta.getRowCount(1));
Assertions.assertEquals(2, meta.getRowCount(2));
Assertions.assertEquals(3, meta.getRowCount(3));
Assertions.assertEquals(-1, meta.getRowCount(4));

new MockUp<OlapTable>() {
@Mock
public List<Long> getIndexIdList() {
List<Long> result = Lists.newArrayList();
result.add(1L);
return result;
}
};

OlapTable table = new OlapTable();

meta.clearStaleIndexRowCount(table);
Assertions.assertEquals(1, meta.getRowCount(1));
Assertions.assertEquals(-1, meta.getRowCount(2));
Assertions.assertEquals(-1, meta.getRowCount(3));
Assertions.assertEquals(-1, meta.getRowCount(4));
}
}
24 changes: 24 additions & 0 deletions regression-test/suites/statistics/test_analyze_mv.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,30 @@ suite("test_analyze_mv") {
assertEquals("5001", result_sample[0][8])
assertEquals("FULL", result_sample[0][9])

// Test drop mv and other indexes' row still exist.
sql """DROP MATERIALIZED VIEW mv1 ON mvTestAgg;"""
sql """analyze table mvTestAgg with sync;"""
result_row = sql """show index stats mvTestAgg mvTestAgg"""
assertEquals(1, result_row.size())
assertEquals("mvTestAgg", result_row[0][0])
assertEquals("mvTestAgg", result_row[0][1])
assertEquals("5", result_row[0][2])
result_row = sql """show index stats mvTestAgg mv3"""
assertEquals(1, result_row.size())
assertEquals("mvTestAgg", result_row[0][0])
assertEquals("mv3", result_row[0][1])
assertEquals("5", result_row[0][2])
result_row = sql """show index stats mvTestAgg mv6"""
assertEquals(1, result_row.size())
assertEquals("mvTestAgg", result_row[0][0])
assertEquals("mv6", result_row[0][1])
assertEquals("4", result_row[0][2])
result_row = sql """show index stats mvTestAgg rollup1"""
assertEquals(1, result_row.size())
assertEquals("mvTestAgg", result_row[0][0])
assertEquals("rollup1", result_row[0][1])
assertEquals("4", result_row[0][2])


sql """
CREATE TABLE mvTestUni (
Expand Down

0 comments on commit b8b9333

Please sign in to comment.