weizhengte commented on code in PR #12765:
URL: https://github.com/apache/doris/pull/12765#discussion_r976116625


##########
fe/fe-core/src/main/java/org/apache/doris/statistics/SQLStatisticsTask.java:
##########
@@ -17,47 +17,119 @@
 
 package org.apache.doris.statistics;
 
-import org.apache.doris.analysis.SelectStmt;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.InvalidFormatException;
+import org.apache.doris.statistics.StatisticsTaskResult.TaskResult;
+import org.apache.doris.statistics.StatsGranularity.Granularity;
+import org.apache.doris.statistics.util.InternalQuery;
+import org.apache.doris.statistics.util.InternalQueryResult;
+import org.apache.doris.statistics.util.InternalQueryResult.ResultRow;
+import org.apache.doris.statistics.util.InternalSqlTemplate;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * A statistics task that collects statistics by executing query.
  * The results of the query will be returned as @StatisticsTaskResult.
  */
 public class SQLStatisticsTask extends StatisticsTask {
-    private SelectStmt query;
+    private String statement;
 
     public SQLStatisticsTask(long jobId, List<StatisticsDesc> statsDescs) {
         super(jobId, statsDescs);
     }
 
     @Override
     public StatisticsTaskResult call() throws Exception {
-        // TODO
-        // step1: construct query by statsDescList
-        constructQuery();
-        // step2: execute query
-        // the result should be sequence by @statsTypeList
-        List<String> queryResultList = executeQuery(query);
-        // step3: construct StatisticsTaskResult by query result
-        constructTaskResult(queryResultList);
-        return null;
+        checkStatisticsDesc();
+        List<TaskResult> taskResults = Lists.newArrayList();
+
+        for (StatisticsDesc statsDesc : statsDescs) {
+            statement = constructQuery(statsDesc);
+            TaskResult taskResult = executeQuery(statsDesc);
+            taskResults.add(taskResult);
+        }
+
+        return new StatisticsTaskResult(taskResults);
     }
 
-    protected void constructQuery() {
-        // TODO
-        // step1: construct FROM by @granularityDesc
-        // step2: construct SELECT LIST by @statsTypeList
+    protected String constructQuery(StatisticsDesc statsDesc) throws 
DdlException,
+            InvalidFormatException {
+        Map<String, String> params = getQueryParams(statsDesc);
+        List<StatsType> statsTypes = statsDesc.getStatsTypes();
+        StatsType type = statsTypes.get(0);
+
+        StatsGranularity statsGranularity = statsDesc.getStatsGranularity();
+        Granularity granularity = statsGranularity.getGranularity();
+        boolean nonPartitioned = granularity != Granularity.PARTITION;
+
+        switch (type) {
+            case ROW_COUNT:
+                return nonPartitioned ? 
InternalSqlTemplate.buildStatsRowCountSql(params)
+                        : 
InternalSqlTemplate.buildStatsPartitionRowCountSql(params);
+            case NUM_NULLS:
+                return nonPartitioned ? 
InternalSqlTemplate.buildStatsNumNullsSql(params)
+                        : 
InternalSqlTemplate.buildStatsPartitionNumNullsSql(params);
+            case MAX_SIZE:
+            case AVG_SIZE:
+                return nonPartitioned ? 
InternalSqlTemplate.buildStatsMaxAvgSizeSql(params)
+                        : 
InternalSqlTemplate.buildStatsPartitionMaxAvgSizeSql(params);
+            case NDV:
+            case MAX_VALUE:
+            case MIN_VALUE:
+                return nonPartitioned ? 
InternalSqlTemplate.buildStatsMinMaxNdvValueSql(params)
+                        : 
InternalSqlTemplate.buildStatsPartitionMinMaxNdvValueSql(params);
+            case DATA_SIZE:
+            default:
+                throw new DdlException("Unsupported statistics type: " + type);
+        }
     }
 
-    protected List<String> executeQuery(SelectStmt query) {
-        // TODO (ML)
-        return null;
+    protected TaskResult executeQuery(StatisticsDesc statsDesc) throws 
Exception {
+        StatsGranularity granularity = statsDesc.getStatsGranularity();
+        List<StatsType> statsTypes = statsDesc.getStatsTypes();
+        StatsCategory category = statsDesc.getStatsCategory();
+
+        String dbName = Env.getCurrentInternalCatalog()
+                .getDbOrDdlException(category.getDbId()).getFullName();
+        InternalQuery query = new InternalQuery(dbName, statement);
+        InternalQueryResult queryResult = query.query();
+        List<ResultRow> resultRows = queryResult.getResultRows();
+
+        if (resultRows != null && resultRows.size() == 1) {
+            ResultRow resultRow = resultRows.get(0);
+            List<String> columns = resultRow.getColumns();
+            TaskResult result = createNewTaskResult(category, granularity);
+
+            if (columns.size() == statsTypes.size()) {
+                for (int i = 0; i < columns.size(); i++) {
+                    StatsType statsType = StatsType.fromString(columns.get(i));
+                    result.getStatsTypeToValue().put(statsType, 
resultRow.getString(i));
+                }
+                return result;
+            }
+        }
+
+        // Statistics statements are executed singly and return only one row 
data
+        throw new DdlException("Statistics query result is incorrect, " + 
queryResult);

Review Comment:
   sure



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to