morningman commented on a change in pull request #3135: support subquery in 
case when statement
URL: https://github.com/apache/incubator-doris/pull/3135#discussion_r393729725
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/analysis/SelectStmt.java
 ##########
 @@ -1244,6 +1213,46 @@ public void rewriteExprs(ExprRewriter rewriter) throws 
AnalysisException {
                 orderByElem.setExpr(rewriter.rewrite(orderByElem.getExpr(), 
analyzer));
             }
         }
+        if (subqueryInCase) {
+            for (SelectListItem item : selectList.getItems()) {
+                if (!(item.getExpr() instanceof CaseExpr)) {
+                    continue;
+                }
+                if 
(!item.getExpr().contains(Predicates.instanceOf(Subquery.class))) {
+                    continue;
+                }
+                item.setExpr(rewriteSubquery(item.getExpr()));
+            }
+        }
+    }
+
+
+    private Expr rewriteSubquery(Expr expr) throws AnalysisException {
+        if (expr instanceof Subquery) {
+            if (!(((Subquery) expr).getStatement() instanceof SelectStmt) ) {
+                throw new AnalysisException("only support select subquery in 
case statement.");
+            }
+            SelectStmt subquery = (SelectStmt)((Subquery) expr).getStatement();
+            if (subquery.getSelectList().getItems().size() != 1) {
+                throw new AnalysisException("only support scala select 
subquery in case statement.");
+            }
+            String alias = getTableAliasGenerator().getNextAlias();
+            String colAlias = getColumnAliasGenerator().getNextAlias();
+            InlineViewRef inlineViewRef = new InlineViewRef(alias, subquery, 
Arrays.asList(colAlias));
+            try {
+                inlineViewRef.analyze(analyzer);
+            } catch (UserException e) {
+                throw new AnalysisException(e.getMessage());
+            }
+            fromClause_.add(inlineViewRef);
+           expr = new SlotRef(new TableName(null, alias), colAlias);
 
 Review comment:
   ```suggestion
               expr = new SlotRef(new TableName(null, alias), colAlias);
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to