Skip to content

Commit 9b950f6

Browse files
committed
collect min max, also collect count
1 parent 62d9a8d commit 9b950f6

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

fe/fe-core/src/main/java/org/apache/doris/statistics/OlapAnalysisTask.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.List;
5454
import java.util.Map;
5555
import java.util.Set;
56+
import java.util.function.BiFunction;
5657
import java.util.stream.Collectors;
5758

5859
/**
@@ -62,7 +63,8 @@ public class OlapAnalysisTask extends BaseAnalysisTask {
6263

6364
private static final String BASIC_STATS_TEMPLATE = "SELECT "
6465
+ "SUBSTRING(CAST(MIN(`${colName}`) AS STRING), 1, 1024) as min, "
65-
+ "SUBSTRING(CAST(MAX(`${colName}`) AS STRING), 1, 1024) as max "
66+
+ "SUBSTRING(CAST(MAX(`${colName}`) AS STRING), 1, 1024) as max, "
67+
+ "COUNT(1) as row_count "
6668
+ "FROM `${dbName}`.`${tblName}` ${index}";
6769

6870
private boolean keyColumnSampleTooManyRows = false;
@@ -109,19 +111,30 @@ protected void doSample() {
109111
LOG.debug("Will do sample collection for column {}", col.getName());
110112
}
111113
// Get basic stats, including min and max.
112-
ResultRow minMax = collectMinMax();
113-
String min = StatisticsUtil.escapeSQL(minMax != null && minMax.getValues().size() > 0
114-
? minMax.get(0) : null);
115-
String max = StatisticsUtil.escapeSQL(minMax != null && minMax.getValues().size() > 1
116-
? minMax.get(1) : null);
114+
ResultRow minMaxCount = collectMinMaxCount();
115+
BiFunction<ResultRow, Integer, String> escapeCell = (row, i) ->
116+
StatisticsUtil.escapeSQL(row != null && row.getValues().size() > i ? row.getValues().get(i) : null);
117+
String min = escapeCell.apply(minMaxCount, 0);
118+
String max = escapeCell.apply(minMaxCount, 1);
119+
String tableRowCountStr = escapeCell.apply(minMaxCount, 2);
120+
long tableRowCount = -1;
121+
if (tableRowCountStr != null) {
122+
try {
123+
tableRowCount = Long.parseLong(tableRowCountStr);
124+
} catch (Exception e) {
125+
// ignore parse exception
126+
}
127+
}
128+
if (tableRowCount < 0) {
129+
tableRowCount = info.indexId == -1
130+
? tbl.getRowCount()
131+
: ((OlapTable) tbl).getRowCountForIndex(info.indexId, false);
132+
}
117133

118134
Map<String, String> params = buildSqlParams();
119135
params.put("min", StatisticsUtil.quote(min));
120136
params.put("max", StatisticsUtil.quote(max));
121137
params.put("hotValueCollectCount", String.valueOf(SessionVariable.getHotValueCollectCount()));
122-
long tableRowCount = info.indexId == -1
123-
? tbl.getRowCount()
124-
: ((OlapTable) tbl).getRowCountForIndex(info.indexId, false);
125138
getSampleParams(params, tableRowCount);
126139
StringSubstitutor stringSubstitutor = new StringSubstitutor(params);
127140
String sql;
@@ -136,7 +149,7 @@ protected void doSample() {
136149
runQuery(sql);
137150
}
138151

139-
protected ResultRow collectMinMax() {
152+
protected ResultRow collectMinMaxCount() {
140153
long startTime = System.currentTimeMillis();
141154
Map<String, String> params = buildSqlParams();
142155
StringSubstitutor stringSubstitutor = new StringSubstitutor(params);
@@ -146,7 +159,7 @@ protected ResultRow collectMinMax() {
146159
stmtExecutor = new StmtExecutor(r.connectContext, sql);
147160
resultRow = stmtExecutor.executeInternalQuery().get(0);
148161
if (LOG.isDebugEnabled()) {
149-
LOG.debug("Cost time in millisec: " + (System.currentTimeMillis() - startTime) + " Min max SQL: "
162+
LOG.debug("Cost time in millisec: " + (System.currentTimeMillis() - startTime) + " Min max count SQL: "
150163
+ sql + " QueryId: " + DebugUtil.printId(stmtExecutor.getContext().queryId()));
151164
}
152165
// Release the reference to stmtExecutor, reduce memory usage.

fe/fe-core/src/test/java/org/apache/doris/statistics/OlapAnalysisTaskTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public void testKeyColumnUseLimitAndNot(@Mocked CatalogIf catalogIf, @Mocked Dat
111111

112112
new Expectations() {
113113
{
114-
tableIf.getRowCount();
115-
result = 20000000;
114+
// tableIf.getRowCount();
115+
// result = 20000000;
116116
tableIf.getId();
117117
result = 30001;
118118
catalogIf.getId();
@@ -126,10 +126,11 @@ public void testKeyColumnUseLimitAndNot(@Mocked CatalogIf catalogIf, @Mocked Dat
126126

127127
new MockUp<OlapAnalysisTask>() {
128128
@Mock
129-
public ResultRow collectMinMax() {
129+
public ResultRow collectMinMaxCount() {
130130
List<String> values = Lists.newArrayList();
131131
values.add("1");
132132
values.add("2");
133+
values.add("20000000");
133134
return new ResultRow(values);
134135
}
135136

0 commit comments

Comments
 (0)