Skip to content

Commit

Permalink
[minor](stats) Update olap table row count after analyze (apache#27814)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikyou1997 authored Dec 1, 2023
1 parent e868c99 commit 3969226
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
package org.apache.doris.statistics;

import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.statistics.AnalysisInfo.JobType;
import org.apache.doris.statistics.util.StatisticsUtil;

import com.google.common.annotations.VisibleForTesting;
import com.google.gson.annotations.SerializedName;

import java.io.DataInput;
Expand Down Expand Up @@ -54,7 +56,7 @@ public class TableStatsMeta implements Writable {

// Used for external table.
@SerializedName("rowCount")
public final long rowCount;
public long rowCount;

@SerializedName("updateTime")
public long updatedTime;
Expand All @@ -65,6 +67,12 @@ public class TableStatsMeta implements Writable {
@SerializedName("trigger")
public JobType jobType;

@VisibleForTesting
public TableStatsMeta() {
tblId = 0;
idxId = 0;
}

// It's necessary to store these fields separately from AnalysisInfo, since the lifecycle between AnalysisInfo
// and TableStats is quite different.
public TableStatsMeta(long rowCount, AnalysisInfo analyzedJob, TableIf table) {
Expand Down Expand Up @@ -136,11 +144,16 @@ public void update(AnalysisInfo analyzedJob, TableIf tableIf) {
}
}
jobType = analyzedJob.jobType;
if (tableIf != null && analyzedJob.colToPartitions.keySet()
.containsAll(tableIf.getBaseSchema().stream()
.filter(c -> !StatisticsUtil.isUnsupportedType(c.getType()))
.map(Column::getName).collect(Collectors.toSet()))) {
updatedRows.set(0);
if (tableIf != null) {
if (tableIf instanceof OlapTable) {
rowCount = tableIf.getRowCount();
}
if (analyzedJob.colToPartitions.keySet()
.containsAll(tableIf.getBaseSchema().stream()
.filter(c -> !StatisticsUtil.isUnsupportedType(c.getType()))
.map(Column::getName).collect(Collectors.toSet()))) {
updatedRows.set(0);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public void testReAnalyze() {
new MockUp<OlapTable>() {

int count = 0;
int[] rowCount = new int[]{100, 200};
int[] rowCount = new int[]{100, 100, 200, 200};

final Column c = new Column("col1", PrimitiveType.INT);
@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public TableIf findTable(long catalogId, long dbId, long tblId) {
new MockUp<OlapTable>() {
int count = 0;

int[] rowCounts = {100, 0};
int[] rowCounts = {100, 100, 100, 0, 0, 0, 0};
@Mock
public long getRowCount() {
return rowCounts[count++];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.

package org.apache.doris.statistics;

import org.apache.doris.catalog.OlapTable;

import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

class TableStatsMetaTest {

@Test
void update(@Mocked OlapTable table) {
new MockUp<OlapTable>() {
@Mock
public long getRowCount() {
return 4;
}
};
TableStatsMeta tableStatsMeta = new TableStatsMeta();
AnalysisInfo jobInfo = new AnalysisInfoBuilder().setColToPartitions(new HashMap<>())
.setColName("col1").build();
tableStatsMeta.update(jobInfo, table);
Assertions.assertEquals(4, tableStatsMeta.rowCount);
}
}

0 comments on commit 3969226

Please sign in to comment.