DarvenDuan commented on code in PR #33630:
URL: https://github.com/apache/doris/pull/33630#discussion_r1565544780


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/StmtRewriter.java:
##########
@@ -1365,4 +1370,116 @@ public static boolean rewriteByPolicy(StatementBase 
statementBase, Analyzer anal
         }
         return reAnalyze;
     }
+
+    /**
+     *
+     * @param ref the SlotRef to rewrite
+     * @param selectList new selectList for selectStmt
+     * @param groupByExprs group by Exprs for selectStmt
+     * @return true if ref can be rewritten
+     */
+    private static boolean rewriteSelectList(SlotRef ref, SelectList 
selectList, ArrayList<Expr> groupByExprs,
+                                             ArrayList<FunctionCallExpr> 
aggExprs) {
+        Column column = ref.getColumn();
+        if (column.isKey()) {
+            selectList.addItem(new SelectListItem(ref, null));
+            groupByExprs.add(ref);
+            return true;
+        } else {
+            AggregateType aggregateType = column.getAggregationType();
+            if (aggregateType != AggregateType.SUM && aggregateType != 
AggregateType.MAX
+                    && aggregateType != AggregateType.MIN) {
+                return false;
+            } else {
+                FunctionName funcName = new 
FunctionName(aggregateType.toString().toLowerCase());
+                List<Expr> arrayList = Lists.newArrayList(ref);
+                FunctionCallExpr func =  new FunctionCallExpr(funcName, new 
FunctionParams(false, arrayList));
+                selectList.addItem(new SelectListItem(func, null));
+                aggExprs.add(func);
+                return true;
+            }
+        }
+    }
+
+    public static boolean rewriteForRandomDistribution(StatementBase 
statementBase, Analyzer analyzer)
+            throws UserException {
+        boolean reAnalyze = false;
+        if (!(statementBase instanceof SelectStmt)) {
+            return false;
+        }
+        SelectStmt selectStmt = (SelectStmt) statementBase;
+        for (int i = 0; i < selectStmt.fromClause.size(); i++) {
+            TableRef tableRef = selectStmt.fromClause.get(i);
+            // Recursively rewrite subquery
+            if (tableRef instanceof InlineViewRef) {
+                InlineViewRef viewRef = (InlineViewRef) tableRef;
+                if (rewriteForRandomDistribution(viewRef.getQueryStmt(), 
viewRef.getAnalyzer())) {
+                    reAnalyze = true;
+                }
+                continue;
+            }
+            // already has agg and group by info
+            if (selectStmt.hasAggInfo() && selectStmt.hasGroupByClause()) {
+                continue;
+            }
+            TableIf table = tableRef.getTable();
+            if (!(table instanceof OlapTable)) {
+                continue;
+            }
+            OlapTable olapTable = (OlapTable) table;
+            if (olapTable.getKeysType() != KeysType.AGG_KEYS) {
+                continue;
+            }
+            DistributionInfo distributionInfo = 
olapTable.getDefaultDistributionInfo();
+            if (distributionInfo.getType() != 
DistributionInfo.DistributionInfoType.RANDOM) {
+                continue;
+            }
+
+            SelectList selectList = selectStmt.getSelectList();
+            SelectList newSelectList = new SelectList();
+            ArrayList<Expr> groupingExprs = new ArrayList<>();
+            ArrayList<FunctionCallExpr> aggExprs = new ArrayList<>();
+            boolean canRewrite = true;
+            for (SelectListItem item : selectList.getItems()) {
+                if (item.isStar()) {
+                    TupleDescriptor desc = tableRef.getDesc();
+                    for (Column col : desc.getTable().getBaseSchema()) {
+                        SlotRef slot = new SlotRef(null, col.getName());
+                        slot.setTable(desc.getTable());
+                        slot.setTupleId(desc.getId());
+                        slot.setDesc(desc.getColumnSlot(col.getName()));
+                        if (!rewriteSelectList(slot, newSelectList, 
groupingExprs, aggExprs)) {
+                            canRewrite = false;
+                            break;
+                        }
+                    }
+                    if (!canRewrite) {
+                        break;
+                    }
+                } else {
+                    Expr expr = item.getExpr();
+                    // just for SlotRef
+                    if (!(expr instanceof SlotRef)) {
+                        break;

Review Comment:
   I had refactored the logical of rewriting, instead of change original 
selectStmt, I add an aggregation node for pre-agg, which ignores the complexity 
of the expression in the query.



-- 
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