Skip to content

Commit

Permalink
4
Browse files Browse the repository at this point in the history
  • Loading branch information
morningman committed Sep 14, 2024
1 parent 7674353 commit 2b4ff1f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.FeConstants;
import org.apache.doris.common.Pair;
import org.apache.doris.common.UserException;
import org.apache.doris.common.proc.IndexSchemaProcNode;
import org.apache.doris.common.proc.ProcNodeInterface;
Expand All @@ -46,6 +47,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import jdk.internal.joptsimple.internal.Strings;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -55,6 +57,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class DescribeStmt extends ShowStmt implements NotFallbackInParser {
Expand Down Expand Up @@ -123,6 +126,25 @@ public boolean isAllTables() {

@Override
public void analyze(Analyzer analyzer) throws UserException {
// First handle meta table.
// It will convert this to corresponding table valued functions
// eg: DESC table$partitions -> partition_values(...)
if (dbTableName != null) {
dbTableName.analyze(analyzer);
CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalogOrAnalysisException(dbTableName.getCtl());
Pair<String, String> sourceTableNameWithMetaName = catalog.getSourceTableNameWithMetaTableName(
dbTableName.getTbl());
if (!Strings.isNullOrEmpty(sourceTableNameWithMetaName.second)) {
isTableValuedFunction = true;
Optional<TableValuedFunctionRef> optTvfRef = catalog.getMetaTableFunctionRef(
dbTableName.getDb(), dbTableName.getTbl());
if (!optTvfRef.isPresent()) {
throw new AnalysisException("meta table not found: " + sourceTableNameWithMetaName.second);
}
tableValuedFunctionRef = optTvfRef.get();
}
}

if (!isAllTables && isTableValuedFunction) {
tableValuedFunctionRef.analyze(analyzer);
List<Column> columns = tableValuedFunctionRef.getTable().getBaseSchema();
Expand All @@ -148,8 +170,6 @@ public void analyze(Analyzer analyzer) throws UserException {
}
}

dbTableName.analyze(analyzer);

if (!Env.getCurrentEnv().getAccessManager()
.checkTblPriv(ConnectContext.get(), dbTableName, PrivPredicate.SHOW)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "DESCRIBE",
Expand All @@ -159,8 +179,7 @@ public void analyze(Analyzer analyzer) throws UserException {

CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalogOrAnalysisException(dbTableName.getCtl());
DatabaseIf db = catalog.getDbOrAnalysisException(dbTableName.getDb());
TableIf table = db.getTableOrAnalysisException(dbTableName.getTbl());

TableIf table = db.getTableOrDdlException(dbTableName.getTbl());
table.readLock();
try {
if (!isAllTables) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.doris.analysis.DropDbStmt;
import org.apache.doris.analysis.DropTableStmt;
import org.apache.doris.analysis.TableName;
import org.apache.doris.analysis.TableValuedFunctionRef;
import org.apache.doris.analysis.TruncateTableStmt;
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.Env;
Expand Down Expand Up @@ -212,7 +213,11 @@ default Pair<String, String> getSourceTableNameWithMetaTableName(String tableNam
return Pair.of(tableName, "");
}

default Optional<TableValuedFunction> getMetaTableFunction(TableIf table, String sourceNameWithMetaName) {
default Optional<TableValuedFunction> getMetaTableFunction(String dbName, String sourceNameWithMetaName) {
return Optional.empty();
}

default Optional<TableValuedFunctionRef> getMetaTableFunctionRef(String dbName, String sourceNameWithMetaName) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package org.apache.doris.datasource.hive;

import org.apache.doris.analysis.TableValuedFunctionRef;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.HdfsResource;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.cluster.ClusterNamespace;
import org.apache.doris.common.Config;
import org.apache.doris.common.DdlException;
Expand Down Expand Up @@ -49,6 +49,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Getter;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.hadoop.hive.conf.HiveConf;
Expand Down Expand Up @@ -295,10 +296,20 @@ public Pair<String, String> getSourceTableNameWithMetaTableName(String tableName
}

@Override
public Optional<TableValuedFunction> getMetaTableFunction(TableIf table, String sourceNameWithMetaName) {
public Optional<TableValuedFunction> getMetaTableFunction(String dbName, String sourceNameWithMetaName) {
for (MetaTableFunction metaFunction : MetaTableFunction.values()) {
if (metaFunction.containsMetaTable(sourceNameWithMetaName)) {
return Optional.of(metaFunction.createFunction(table));
return Optional.of(metaFunction.createFunction(name, dbName, sourceNameWithMetaName));
}
}
return Optional.empty();
}

@Override
public Optional<TableValuedFunctionRef> getMetaTableFunctionRef(String dbName, String sourceNameWithMetaName) {
for (MetaTableFunction metaFunction : MetaTableFunction.values()) {
if (metaFunction.containsMetaTable(sourceNameWithMetaName)) {
return Optional.of(metaFunction.createFunctionRef(name, dbName, sourceNameWithMetaName));
}
}
return Optional.empty();
Expand All @@ -325,12 +336,14 @@ public boolean isEnableHmsEventsIncrementalSync() {
* eg: tbl$partitions
*/
private enum MetaTableFunction {
PARTITIONS;
PARTITIONS("partition_values");

private final String suffix;
private final String tvfName;

MetaTableFunction() {
MetaTableFunction(String tvfName) {
this.suffix = "$" + name().toLowerCase();
this.tvfName = tvfName;
}

boolean containsMetaTable(String tableName) {
Expand All @@ -341,15 +354,33 @@ String getSourceTableName(String tableName) {
return tableName.substring(0, tableName.length() - suffix.length());
}

public TableValuedFunction createFunction(TableIf table) {
public TableValuedFunction createFunction(String ctlName, String dbName, String sourceNameWithMetaName) {
switch (this) {
case PARTITIONS:
List<String> nameParts = Lists.newArrayList(table.getDatabase().getCatalog().getName(),
table.getDatabase().getFullName(), table.getName());
List<String> nameParts = Lists.newArrayList(ctlName, dbName,
getSourceTableName(sourceNameWithMetaName));
return PartitionValues.create(nameParts);
default:
throw new AnalysisException("Unsupported meta function type: " + this);
}
}

public TableValuedFunctionRef createFunctionRef(String ctlName, String dbName, String sourceNameWithMetaName) {
switch (this) {
case PARTITIONS:
Map<String, String> params = Maps.newHashMap();
params.put("catalog", ctlName);
params.put("database", dbName);
params.put("tblName", getSourceTableName(sourceNameWithMetaName));
try {
return new TableValuedFunctionRef(tvfName, null, params);
} catch (org.apache.doris.common.AnalysisException e) {
LOG.warn("should not happen. {}.{}.{}", ctlName, dbName, tblName);
return null;
}
default:
throw new AnalysisException("Unsupported meta function type: " + this);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public static LogicalPlan checkAndAddDeleteSignFilter(LogicalOlapScan scan, Conn
private Optional<LogicalPlan> handleMetaTable(TableIf table, UnboundRelation unboundRelation,
List<String> qualifiedTableName) {
Optional<TableValuedFunction> tvf = table.getDatabase().getCatalog().getMetaTableFunction(
table, qualifiedTableName.get(2));
qualifiedTableName.get(1), qualifiedTableName.get(2));
if (tvf.isPresent()) {
return Optional.of(new LogicalTVFRelation(unboundRelation.getRelationId(), tvf.get()));
}
Expand Down

0 comments on commit 2b4ff1f

Please sign in to comment.