EmmyMiao87 commented on a change in pull request #3677:
URL: https://github.com/apache/incubator-doris/pull/3677#discussion_r439917670



##########
File path: fe/src/main/java/org/apache/doris/analysis/InsertStmt.java
##########
@@ -453,19 +454,25 @@ private void analyzeSubquery(Analyzer analyzer) throws 
UserException {
             }
         }
 
-        //Get the correspondence of this column to the original column through 
defineExpr
-        Map<Integer, Integer> origColIdx2MvColIdx = Maps.newHashMap();
-        for (int mvColumnIdx = 0; mvColumnIdx < 
targetTable.getFullSchema().size(); ++mvColumnIdx) {
-            Column column = targetTable.getFullSchema().get(mvColumnIdx);
+        /*
+         * When table have materialized view, there may be some materialized 
view columns.
+         * we should add them to the end of targetColumns.
+         * eg: origin targetColumns: (A,B,C), shadow column: 
__doris_materialized_view_bitmap_union_C
+         * after processing, targetColumns: (A, B, C, 
__doris_materialized_view_bitmap_union_C), and
+         * origColIdx2MVColumn has 1 element: "3, 
__doris_materialized_view_bitmap_union_C"

Review comment:
       ```suggestion
            * origColIdx2MVColumn has 1 element: "2, 
__doris_materialized_view_bitmap_union_C"
   ```

##########
File path: fe/src/main/java/org/apache/doris/analysis/InsertStmt.java
##########
@@ -453,19 +454,25 @@ private void analyzeSubquery(Analyzer analyzer) throws 
UserException {
             }
         }
 
-        //Get the correspondence of this column to the original column through 
defineExpr
-        Map<Integer, Integer> origColIdx2MvColIdx = Maps.newHashMap();
-        for (int mvColumnIdx = 0; mvColumnIdx < 
targetTable.getFullSchema().size(); ++mvColumnIdx) {
-            Column column = targetTable.getFullSchema().get(mvColumnIdx);
+        /*
+         * When table have materialized view, there may be some materialized 
view columns.
+         * we should add them to the end of targetColumns.
+         * eg: origin targetColumns: (A,B,C), shadow column: 
__doris_materialized_view_bitmap_union_C
+         * after processing, targetColumns: (A, B, C, 
__doris_materialized_view_bitmap_union_C), and
+         * origColIdx2MVColumn has 1 element: "3, 
__doris_materialized_view_bitmap_union_C"
+         * will be used in as a mapping from queryStmt.getResultExprs() to 
targetColumns define expr
+         */
+        List<Pair<Integer, Column>>  origColIdx2MVColumn = 
Lists.newArrayList();
+        for (Column column : targetTable.getFullSchema()) {
             if 
(column.isNameWithPrefix(CreateMaterializedViewStmt.MATERIALIZED_VIEW_NAME_PRFIX))
 {
-                List<Expr> slots = new ArrayList<>();
-                column.getDefineExpr().collect(SlotRef.class, slots);
-                //We only support one children of define expr in materialized 
view column
-                Preconditions.checkArgument(slots.size() == 1);
-                String origName = ((SlotRef) slots.get(0)).getColumnName();
+                SlotRef refColumn = column.getRefColumn();
+                if (refColumn == null) {
+                    
ErrorReport.reportAnalysisException(ErrorCode.ERR_BAD_FIELD_ERROR);

Review comment:
       Better  to show which field is error.

##########
File path: fe/src/main/java/org/apache/doris/analysis/InsertStmt.java
##########
@@ -521,22 +530,22 @@ private void analyzeSubquery(Analyzer analyzer) throws 
UserException {
                 // extend the result expr by duplicating the related exprs
                 for (Integer idx : origColIdxsForShadowCols) {
                     
queryStmt.getResultExprs().add(queryStmt.getResultExprs().get(idx));
+                    
queryStmt.getBaseTblResultExprs().add(queryStmt.getResultExprs().get(idx));
                 }
             }
 
-            if (!origColIdx2MvColIdx.isEmpty()) {
-                origColIdx2MvColIdx.forEach((key, value) -> {
-                    Column mvColumn = targetTable.getFullSchema().get(key);
-                    Expr expr = mvColumn.getDefineExpr();
-                    ArrayList<SlotRef> slots = new ArrayList<>();
-                    expr.collect(SlotRef.class, slots);
-
+            if (!origColIdx2MVColumn.isEmpty()) {
+                origColIdx2MVColumn.forEach(entry -> {
+                    Integer origColIdx = entry.first;
+                    Column mvColumn = entry.second;
                     //substitute define expr slot with select statement result 
expr
                     ExprSubstitutionMap smap = new ExprSubstitutionMap();
-                    smap.getLhs().add(slots.get(0));
-                    smap.getRhs().add(queryStmt.getResultExprs().get(value));
+                    smap.getLhs().add(mvColumn.getRefColumn());
+                    
smap.getRhs().add(queryStmt.getResultExprs().get(origColIdx));
 
-                    
queryStmt.getResultExprs().add(Expr.substituteList(Lists.newArrayList(expr), 
smap, analyzer, false).get(0));
+                    Expr e = 
Expr.substituteList(Lists.newArrayList(mvColumn.getDefineExpr()), smap, 
analyzer, false).get(0);
+                    queryStmt.getResultExprs().add(e);
+                    queryStmt.getBaseTblResultExprs().add(e);

Review comment:
       What's different between `resultExpr` and `baseTblResultExpr`




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



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

Reply via email to