[improvement](statistics)Support partition row count return -1 when it is not fully reported. (#41348) (#41408)
backport: https://github.com/apache/doris/pull/41348
This commit is contained in:
@ -1402,18 +1402,48 @@ public class OlapTable extends Table implements MTMVRelatedTableIf {
|
||||
return getRowCountForIndex(baseIndexId, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If strict is true, -1 if there are some tablets whose row count is not reported to FE
|
||||
* If strict is false, return the sum of all partition's index current reported row count.
|
||||
*/
|
||||
public long getRowCountForIndex(long indexId, boolean strict) {
|
||||
long rowCount = 0;
|
||||
for (Map.Entry<Long, Partition> entry : idToPartition.entrySet()) {
|
||||
MaterializedIndex index = entry.getValue().getIndex(indexId);
|
||||
if (index == null) {
|
||||
LOG.warn("Index {} not exist in partition {}, table {}, {}",
|
||||
indexId, entry.getValue().getName(), id, name);
|
||||
return -1;
|
||||
}
|
||||
if (strict && !index.getRowCountReported()) {
|
||||
return -1;
|
||||
}
|
||||
rowCount += (index == null || index.getRowCount() == -1) ? 0 : index.getRowCount();
|
||||
rowCount += index.getRowCount() == -1 ? 0 : index.getRowCount();
|
||||
}
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If strict is true, -1 if there are some tablets whose row count is not reported to FE.
|
||||
* If strict is false, return the sum of partition's all indexes current reported row count.
|
||||
*/
|
||||
public long getRowCountForPartitionIndex(long partitionId, long indexId, boolean strict) {
|
||||
Partition partition = idToPartition.get(partitionId);
|
||||
if (partition == null) {
|
||||
LOG.warn("Partition {} not exist in table {}, {}", partitionId, id, name);
|
||||
return -1;
|
||||
}
|
||||
MaterializedIndex index = partition.getIndex(indexId);
|
||||
if (index == null) {
|
||||
LOG.warn("Index {} not exist in partition {}, table {}, {}", indexId, partitionId, id, name);
|
||||
return -1;
|
||||
}
|
||||
if (strict && !index.getRowCountReported()) {
|
||||
return -1;
|
||||
}
|
||||
return index.getRowCount() == -1 ? 0 : index.getRowCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getAvgRowLength() {
|
||||
long rowCount = 0;
|
||||
|
||||
@ -121,4 +121,51 @@ public class OlapTableTest {
|
||||
Assert.assertFalse(olapTable.getTableProperty().getDynamicPartitionProperty().getEnable());
|
||||
Assert.assertEquals((short) 3, olapTable.getDefaultReplicaAllocation().getTotalReplicaNum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPartitionRowCount() {
|
||||
OlapTable olapTable = new OlapTable();
|
||||
// Partition is null.
|
||||
long row = olapTable.getRowCountForPartitionIndex(0, 0, true);
|
||||
Assert.assertEquals(-1, row);
|
||||
|
||||
// Index is null.
|
||||
MaterializedIndex index = new MaterializedIndex(10, MaterializedIndex.IndexState.NORMAL);
|
||||
Partition partition = new Partition(11, "p1", index, null);
|
||||
olapTable.addPartition(partition);
|
||||
row = olapTable.getRowCountForPartitionIndex(11, 0, true);
|
||||
Assert.assertEquals(-1, row);
|
||||
|
||||
// Strict is true and index is not reported.
|
||||
index.setRowCountReported(false);
|
||||
index.setRowCount(100);
|
||||
row = olapTable.getRowCountForPartitionIndex(11, 10, true);
|
||||
Assert.assertEquals(-1, row);
|
||||
|
||||
// Strict is true and index is reported.
|
||||
index.setRowCountReported(true);
|
||||
index.setRowCount(101);
|
||||
row = olapTable.getRowCountForPartitionIndex(11, 10, true);
|
||||
Assert.assertEquals(101, row);
|
||||
|
||||
// Strict is false and index is not reported.
|
||||
index.setRowCountReported(false);
|
||||
index.setRowCount(102);
|
||||
row = olapTable.getRowCountForPartitionIndex(11, 10, false);
|
||||
Assert.assertEquals(102, row);
|
||||
|
||||
// Reported row is -1, we should return 0
|
||||
index.setRowCountReported(true);
|
||||
index.setRowCount(-1);
|
||||
row = olapTable.getRowCountForPartitionIndex(11, 10, false);
|
||||
Assert.assertEquals(0, row);
|
||||
|
||||
// Return reported row.
|
||||
index.setRowCountReported(true);
|
||||
index.setRowCount(103);
|
||||
row = olapTable.getRowCountForPartitionIndex(11, 10, false);
|
||||
Assert.assertEquals(103, row);
|
||||
|
||||
olapTable.getRowCountForPartitionIndex(11, 10, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user