imay commented on a change in pull request #2431: Support to create 
materialized view
URL: https://github.com/apache/incubator-doris/pull/2431#discussion_r357465794
 
 

 ##########
 File path: 
fe/src/main/java/org/apache/doris/analysis/AddMaterializedViewClause.java
 ##########
 @@ -0,0 +1,224 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.analysis;
+
+import org.apache.doris.catalog.AggregateType;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.FeNameFormat;
+import org.apache.doris.common.UserException;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Materialized view is performed to materialize the results of query.
+ * This clause is used to create a new materialized view for a specified table
+ * through a specified query stmt.
+ * <p>
+ * Syntax:
+ * ALTER TABLE [Table name] ADD Materialized View [MV name] (
+ *     SELECT select_expr[, select_expr ...]
+ *     FROM [Base view name]
+ *     GROUP BY column_name[, column_name ...]
+ *     ORDER BY column_name[, column_name ...])
+ * [PROPERTIES ("key": "value")]
+ */
+public class AddMaterializedViewClause extends AlterClause {
+    private String mvName;
+    private SelectStmt selectStmt;
+    private Map<String, String> properties;
+
+    /**
+     * origin stmt: select k1, k2, v1, sum(v2) from base_table group by k1, 
k2, v1 order by k1, k2
+     * mvColumns: [k1: {isKey: true, aggType: null, isAggregationTypeImplicit: 
false},
+     *             k2: {isKey: true, aggType: null, isAggregationTypeImplicit: 
false},
+     *             v1: {isKey: false, aggType: none, 
isAggregationTypeImplicit: true},
+     *             v2: {isKey: false, aggType: sum, isAggregationTypeImplicit: 
false}]
+     * This order of mvColumns is meaningful.
+     */
+    private List<Column> mvColumns = Lists.newArrayList();
+    private String baseIndexName;
+
+    public AddMaterializedViewClause(String mvName, SelectStmt selectStmt,
+                                     Map<String, String> properties) {
+        this.mvName = mvName;
+        this.selectStmt = selectStmt;
+        this.properties = properties;
+    }
+
+    public String getMVName() {
+        return mvName;
+    }
+
+    public List<Column> getMVColumns() {
+        return mvColumns;
+    }
+
+    public String getBaseIndexName() {
+        return baseIndexName;
+    }
+
+    @Override
+    public void analyze(Analyzer analyzer) throws UserException {
+        FeNameFormat.checkTableName(mvName);
+        // TODO(ml): the mv name in from clause should pass the analyze 
without error.
+        selectStmt.analyze(analyzer);
+        analyzeSelectClause();
+        analyzeFromClause();
+        if (selectStmt.getWhereClause() != null) {
+            throw new AnalysisException("The where clause is not supported in 
add materialized view clause, expr:"
+                                                + 
selectStmt.getWhereClause().toSql());
+        }
+        if (selectStmt.getHavingPred() != null) {
+            throw new AnalysisException("The having clause is not supported in 
add materialized view clause, expr:"
+                                                + 
selectStmt.getHavingPred().toSql());
+        }
+        analyzeOrderByClause();
+        if (selectStmt.getLimit() != -1) {
+            throw new AnalysisException("The limit clause is not supported in 
add materialized view clause, expr:"
+                                                + " limit " + 
selectStmt.getLimit());
+        }
+    }
+
+    private void analyzeSelectClause() throws AnalysisException {
+        SelectList selectList = selectStmt.getSelectList();
+        if (selectList.getItems().isEmpty()) {
+            throw new AnalysisException("The materialized view must contain at 
least one column");
+        }
+        boolean meetAggregate = false;
+        Set<String> mvColumnNameSet = Sets.newHashSet();
+        for (SelectListItem selectListItem : selectList.getItems()) {
+            Expr selectListItemExpr = selectListItem.getExpr();
+            if (!(selectListItemExpr instanceof SlotRef) && 
!(selectListItemExpr instanceof FunctionCallExpr)) {
+                throw new AnalysisException("The materialized view only 
support the single column or function expr. "
+                                                    + "Error column: " + 
selectListItemExpr.toSql());
+            }
+            if (selectListItem.getExpr() instanceof SlotRef) {
+                if (meetAggregate) {
+                    throw new AnalysisException("The aggregate column should 
be after the single column");
+                }
+                SlotRef slotRef = (SlotRef) selectListItem.getExpr();
+                String columnName = slotRef.getColumnName();
+                if (!mvColumnNameSet.add(columnName)) {
+                    
ErrorReport.reportAnalysisException(ErrorCode.ERR_DUP_FIELDNAME, columnName);
+                }
+                Column column = new Column(columnName);
+                mvColumns.add(column);
+            }
+            if (selectListItem.getExpr() instanceof FunctionCallExpr) {
 
 Review comment:
   else if

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to