weizhengte commented on code in PR #8862: URL: https://github.com/apache/doris/pull/8862#discussion_r935847810
########## fe/fe-core/src/main/java/org/apache/doris/analysis/ShowAnalyzeStmt.java: ########## @@ -0,0 +1,366 @@ +// 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.Column; +import org.apache.doris.catalog.Database; +import org.apache.doris.catalog.Env; +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; +import java.util.stream.IntStream; + +/** + * 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.getEnv() + .getInternalDataSource().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.getEnv() + .getInternalDataSource().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)); Review Comment: for statistical job, I think the user mainly cares which state it belongs to, and I've simplified it. -- 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