5353import java .util .List ;
5454import java .util .Map ;
5555import java .util .Set ;
56+ import java .util .function .BiFunction ;
5657import 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.
0 commit comments