Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat](Nereids) refactor show table command #48833

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,27 @@
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.PatternMatcher;
import org.apache.doris.common.PatternMatcherWrapper;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.commands.info.AliasInfo;
import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.nereids.util.Utils;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.GlobalVariable;
import org.apache.doris.qe.ShowResultSet;
import org.apache.doris.qe.ShowResultSetMetaData;
import org.apache.doris.qe.StmtExecutor;
import org.apache.doris.statistics.ResultRow;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* ShowTableCommand
Expand Down Expand Up @@ -107,39 +105,8 @@ public void validate(ConnectContext ctx) throws AnalysisException {
}

/**
* sql to logical plan
* @param sql sql
* isShowTablesCaseSensitive
*/
private LogicalPlan toLogicalPlan(String sql) {
return new NereidsParser().parseSingle(sql);
}

/**
* Construct a basic SQL without query conditions
* @param columns (original name, alias)
* @param tableName ctl.db.tbl
*/
private String toBaseSql(List<Pair<String, String>> columns, String tableName) throws AnalysisException {
if (columns == null || columns.isEmpty()) {
throw new AnalysisException("columns cannot be empty");
}
if (tableName == null || tableName.isEmpty()) {
throw new AnalysisException("tableName cannot be empty");
}

StringBuilder sb = new StringBuilder("SELECT ");
columns.forEach(column -> {
sb.append(column.first);
if (column.second != null && !column.second.isEmpty()) {
sb.append(" AS ").append(column.second);
}
sb.append(", ");
});
sb.setLength(sb.length() - 2);
sb.append(" FROM ").append(tableName);
return sb.toString();
}

public boolean isShowTablesCaseSensitive() {
if (GlobalVariable.lowerCaseTableNames == 0) {
return CaseSensibility.TABLE.getCaseSensibility();
Expand All @@ -149,27 +116,20 @@ public boolean isShowTablesCaseSensitive() {

private ShowResultSet executeWhere(ConnectContext ctx, StmtExecutor executor)
throws AnalysisException {
List<Pair<String, String>> columns = new ArrayList<>();
columns.add(Pair.of("`TABLE_NAME`",
List<AliasInfo> selectList = new ArrayList<>();
selectList.add(AliasInfo.of("TABLE_NAME",
NAME_COL_PREFIX + ClusterNamespace.getNameFromFullName(db)));
if (isVerbose) {
columns.add(Pair.of("`TABLE_TYPE`", TYPE_COL));
selectList.add(AliasInfo.of("TABLE_TYPE", TYPE_COL));
}

String fullTblName = String.format("`%s`.`%s`.`%s`",
catalog,
InfoSchemaDb.DATABASE_NAME,
"tables");
TableNameInfo fullTblName = new TableNameInfo(catalog, InfoSchemaDb.DATABASE_NAME, "tables");

// We need to use TABLE_SCHEMA as a condition to query When querying external catalogs.
// This also applies to the internal catalog.
LogicalPlan plan = toLogicalPlan(toBaseSql(columns, fullTblName) + " " + whereClause
+ " and `TABLE_SCHEMA` = '" + db + "'");
LogicalPlanAdapter adapter = new LogicalPlanAdapter(plan, ctx.getStatementContext());
executor.setParsedStmt(adapter);
List<ResultRow> resultRows = executor.executeInternalQuery();
List<List<String>> rows = resultRows.stream()
.map(ResultRow::getValues).collect(Collectors.toList());
LogicalPlan plan = Utils.buildLogicalPlan(selectList, fullTblName,
whereClause + " and `TABLE_SCHEMA` = '" + db + "'");
List<List<String>> rows = Utils.executePlan(ctx, executor, plan);
return new ShowResultSet(getMetaData(), rows);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.
// This file is copied from
// https://github.com/apache/impala/blob/branch-2.9.0/fe/src/main/java/org/apache/impala/TableName.java
// and modified by Doris

package org.apache.doris.nereids.trees.plans.commands.info;

import java.util.Objects;

/**
* alias info
*/
public class AliasInfo {
private final String name;
private final String alias;

public AliasInfo(String name, String alias) {
this.name = Objects.requireNonNull(name);
this.alias = alias;
}

public static AliasInfo of(String name, String alias) {
return new AliasInfo(name, alias);
}

public String getName() {
return name;
}

public String getAlias() {
return alias;
}

@Override
public String toString() {
return alias == null || alias.isEmpty()
? String.format("`%s`", name)
: String.format("`%s` AS `%s`", name, alias);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,19 @@ public String toSql() {
stringBuilder.append("`").append(tbl).append("`");
return stringBuilder.toString();
}

/**
* toFullyQualified
*/
public String toFullyQualified() {
StringBuilder stringBuilder = new StringBuilder();
if (ctl != null) {
stringBuilder.append("`").append(ctl).append("`.");
}
if (db != null) {
stringBuilder.append("`").append(db).append("`.");
}
stringBuilder.append("`").append(tbl).append("`");
return stringBuilder.toString();
}
}
42 changes: 42 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/nereids/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@
package org.apache.doris.nereids.util;

import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.plans.commands.info.AliasInfo;
import org.apache.doris.nereids.trees.plans.commands.info.TableNameInfo;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;
import org.apache.doris.statistics.ResultRow;

import com.google.common.base.CaseFormat;
import com.google.common.base.Preconditions;
Expand All @@ -40,6 +48,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -589,4 +598,37 @@ public static String addLinePrefix(String str, String prefix) {
}
return newStr.toString();
}

/**
* Builds a logical plan for a SQL query.
*
* @param selectList the list of columns and their aliases to be selected
* @param tableName the name of the table from which to select
* @param whereClause the where clause to filter the results
* @return the logical plan representing the SQL query
*/
public static LogicalPlan buildLogicalPlan(List<AliasInfo> selectList, TableNameInfo tableName,
String whereClause) {
StringJoiner columnJoiner = new StringJoiner(", ");
for (AliasInfo aliasInfo : selectList) {
columnJoiner.add(aliasInfo.toString());
}
String sql = "SELECT " + columnJoiner.toString() + " FROM " + tableName.toFullyQualified() + " " + whereClause;
return new NereidsParser().parseSingle(sql);
}

/**
* Execute a logical plan and return the results.
*
* @param ctx the context in which to execute the plan
* @param executor the executor to use to execute the plan
* @param plan the plan to execute
* @return the results of executing the plan
*/
public static List<List<String>> executePlan(ConnectContext ctx, StmtExecutor executor, LogicalPlan plan) {
LogicalPlanAdapter adapter = new LogicalPlanAdapter(plan, ctx.getStatementContext());
executor.setParsedStmt(adapter);
List<ResultRow> resultRows = executor.executeInternalQuery();
return resultRows.stream().map(ResultRow::getValues).collect(Collectors.toList());
}
}