You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/05/25 08:24:56 UTC

[GitHub] [incubator-doris] jackwener commented on a diff in pull request #8862: [feature-wip](statistics) step5: show statistics job information

jackwener commented on code in PR #8862:
URL: https://github.com/apache/incubator-doris/pull/8862#discussion_r881366908


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/ShowAnalyzeStmt.java:
##########
@@ -0,0 +1,354 @@
+// 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.Catalog;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Database;
+import org.apache.doris.catalog.ScalarType;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.util.OrderByPair;
+import org.apache.doris.mysql.privilege.PaloAuth;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.ShowResultSetMetaData;
+import org.apache.doris.statistics.StatisticsJob;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Sets;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * ShowAnalyzeStmt is used to show statistics job info.
+ * syntax:
+ *    SHOW ANALYZE
+ *        [TABLE | ID]
+ *        [
+ *            WHERE
+ *            [STATE = ["PENDING"|"SCHEDULING"|"RUNNING"|"FINISHED"|"FAILED"|"CANCELLED"]]
+ *        ]
+ *        [ORDER BY ...]
+ *        [LIMIT limit][OFFSET offset];
+ */
+public class ShowAnalyzeStmt extends ShowStmt {
+    private static final String STATE_NAME = "state";
+    private static final ImmutableList<String> TITLE_NAMES = new ImmutableList.Builder<String>()
+            .add("id")
+            .add("create_time")
+            .add("start_time")
+            .add("finish_time")
+            .add("error_msg")
+            .add("scope")
+            .add("progress")
+            .add("state")
+            .build();
+
+    private List<Long> jobIds;
+    private TableName dbTableName;
+    private Expr whereClause;
+    private LimitElement limitElement;
+    private List<OrderByElement> orderByElements;
+
+    // after analyzed
+    private long dbId;
+    private final Set<Long> tblIds = Sets.newHashSet();
+
+    private String stateValue;
+    private ArrayList<OrderByPair> orderByPairs;
+
+    public ShowAnalyzeStmt() {
+    }
+
+    public ShowAnalyzeStmt(List<Long> jobIds) {
+        this.jobIds = jobIds;
+    }
+
+    public ShowAnalyzeStmt(TableName dbTableName,
+                           Expr whereClause,
+                           List<OrderByElement> orderByElements,
+                           LimitElement limitElement) {
+        this.dbTableName = dbTableName;
+        this.whereClause = whereClause;
+        this.orderByElements = orderByElements;
+        this.limitElement = limitElement;
+    }
+
+    public List<Long> getJobIds() {
+        return jobIds;
+    }
+
+    public long getDbId() {
+        Preconditions.checkArgument(isAnalyzed(),
+                "The dbId must be obtained after the parsing is complete");
+        return dbId;
+    }
+
+    public Set<Long> getTblIds() {
+        Preconditions.checkArgument(isAnalyzed(),
+                "The dbId must be obtained after the parsing is complete");
+        return tblIds;
+    }
+
+    public String getStateValue() {
+        Preconditions.checkArgument(isAnalyzed(),
+                "The tbl name must be obtained after the parsing is complete");
+        return stateValue;
+    }
+
+    public ArrayList<OrderByPair> getOrderByPairs() {
+        Preconditions.checkArgument(isAnalyzed(),
+                "The tbl name must be obtained after the parsing is complete");
+        return orderByPairs;
+    }
+
+    public long getLimit() {
+        if (limitElement != null && limitElement.hasLimit()) {
+            return limitElement.getLimit();
+        }
+        return -1L;
+    }
+
+    public long getOffset() {
+        if (limitElement != null && limitElement.hasOffset()) {
+            return limitElement.getOffset();
+        }
+        return -1L;
+    }
+
+    @Override
+    public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
+        super.analyze(analyzer);
+
+        if (dbTableName != null) {
+            dbTableName.analyze(analyzer);
+            String dbName = dbTableName.getDb();
+            String tblName = dbTableName.getTbl();
+            checkShowAnalyzePriv(dbName, tblName);
+
+            Database db = analyzer.getCatalog().getDbOrAnalysisException(dbName);
+            Table table = db.getTableOrAnalysisException(tblName);
+
+            dbId = db.getId();
+            tblIds.add(table.getId());
+        } else {
+            // analyze the current default db
+            String dbName = analyzer.getDefaultDb();
+            if (Strings.isNullOrEmpty(dbName)) {
+                ErrorReport.reportAnalysisException(ErrorCode.ERR_NO_DB_ERROR);
+            }
+
+            Database db = analyzer.getCatalog().getDbOrAnalysisException(dbName);
+
+            db.readLock();
+            try {
+                List<Table> tables = db.getTables();
+                for (Table table : tables) {
+                    checkShowAnalyzePriv(dbName, table.getName());
+                }
+
+                dbId = db.getId();
+                for (Table table : tables) {
+                    long tblId = table.getId();
+                    tblIds.add(tblId);
+                }
+            } finally {
+                db.readUnlock();
+            }
+        }
+
+        // analyze where clause if not null
+        if (whereClause != null) {
+            if (whereClause instanceof CompoundPredicate) {
+                CompoundPredicate cp = (CompoundPredicate) whereClause;
+                if (cp.getOp() != CompoundPredicate.Operator.AND) {
+                    throw new AnalysisException("Only allow compound predicate with operator AND");
+                }
+                // check whether left.columnName equals to right.columnName
+                checkPredicateName(cp.getChild(0), cp.getChild(1));
+                analyzeSubPredicate(cp.getChild(0));
+                analyzeSubPredicate(cp.getChild(1));
+            } else {
+                analyzeSubPredicate(whereClause);
+            }
+        }
+
+        // analyze order by
+        if (orderByElements != null && !orderByElements.isEmpty()) {
+            orderByPairs = new ArrayList<>();
+            for (OrderByElement orderByElement : orderByElements) {
+                if (!(orderByElement.getExpr() instanceof SlotRef)) {
+                    throw new AnalysisException("Should order by column");
+                }
+                SlotRef slotRef = (SlotRef) orderByElement.getExpr();
+                int index = analyzeColumn(slotRef.getColumnName());
+                OrderByPair orderByPair = new OrderByPair(index, !orderByElement.getIsAsc());
+                orderByPairs.add(orderByPair);
+            }
+        }
+    }
+
+    @Override
+    public ShowResultSetMetaData getMetaData() {
+        ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
+        for (String title : TITLE_NAMES) {
+            builder.addColumn(new Column(title, ScalarType.createVarchar(128)));
+        }
+        return builder.build();
+    }
+
+    @Override
+    public RedirectStatus getRedirectStatus() {
+        return RedirectStatus.FORWARD_NO_SYNC;
+    }
+
+    private void checkShowAnalyzePriv(String dbName, String tblName) throws AnalysisException {
+        PaloAuth auth = Catalog.getCurrentCatalog().getAuth();
+        if (!auth.checkTblPriv(ConnectContext.get(), dbName, tblName, PrivPredicate.SHOW)) {
+            ErrorReport.reportAnalysisException(
+                    ErrorCode.ERR_TABLEACCESS_DENIED_ERROR,
+                    "SHOW ANALYZE",
+                    ConnectContext.get().getQualifiedUser(),
+                    ConnectContext.get().getRemoteIP(),
+                    dbName + ": " + tblName);
+        }
+    }
+
+    private void checkPredicateName(Expr leftChild, Expr rightChild) throws AnalysisException {
+        String leftChildColumnName = ((SlotRef) leftChild.getChild(0)).getColumnName();
+        String rightChildColumnName = ((SlotRef) rightChild.getChild(0)).getColumnName();
+        if (leftChildColumnName.equals(rightChildColumnName)) {
+            throw new AnalysisException("column names on both sides of operator AND should be different");
+        }
+    }
+
+    private void analyzeSubPredicate(Expr subExpr) throws AnalysisException {
+        if (subExpr == null) {
+            return;
+        }
+
+        boolean valid = true;
+
+        CHECK:
+        {
+            if (subExpr instanceof BinaryPredicate) {
+                BinaryPredicate binaryPredicate = (BinaryPredicate) subExpr;
+                if (binaryPredicate.getOp() != BinaryPredicate.Operator.EQ) {
+                    valid = false;
+                    break CHECK;
+                }
+            } else {
+                valid = false;
+                break CHECK;
+            }
+
+            // left child
+            if (!(subExpr.getChild(0) instanceof SlotRef)) {
+                valid = false;
+                break CHECK;
+            }
+            String leftKey = ((SlotRef) subExpr.getChild(0)).getColumnName();
+            if (!STATE_NAME.equalsIgnoreCase(leftKey)) {
+                valid = false;
+                break CHECK;
+            }
+
+            // right child
+            if (!(subExpr.getChild(1) instanceof StringLiteral)) {
+                valid = false;
+                break CHECK;
+            }
+
+            String value = subExpr.getChild(1).getStringValue();
+            if (Strings.isNullOrEmpty(value)) {
+                valid = false;
+                break CHECK;
+            }
+
+            stateValue = value.toUpperCase();
+            try {
+                StatisticsJob.JobState.valueOf(stateValue);
+            } catch (Exception e) {
+                valid = false;
+            }
+        }
+
+        if (!valid) {
+            throw new AnalysisException("Where clause should looks like: " +
+                    "STATE = \"PENDING|SCHEDULING|RUNNING|FINISHED|FAILED|CANCELLED\", " +
+                    "or compound predicate with operator AND");

Review Comment:
   "or compound predicate with operator AND"
   ->
   "binary predicate must be OR"



-- 
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-unsubscribe@doris.apache.org

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


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