Skip to content

[Enhancement] (nereids) implement ShowBuildIndexCommand in nereids #50794

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

Open
wants to merge 16 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 @@ -407,6 +407,8 @@ supportedShowStatement
| SHOW CATALOG RECYCLE BIN (WHERE expression)? #showCatalogRecycleBin
| SHOW TABLET tabletId=INTEGER_VALUE #showTabletId
| SHOW DICTIONARIES wildWhere? #showDictionaries
| SHOW BUILD INDEX ((FROM | IN) database=multipartIdentifier)?
wildWhere? sortClause? limitClause? #showBuildIndex
| SHOW TRANSACTION ((FROM | IN) database=multipartIdentifier)? wildWhere? #showTransaction
| SHOW REPLICA STATUS FROM baseTableRef whereClause? #showReplicaStatus
| SHOW WORKLOAD GROUPS (LIKE STRING_LITERAL)? #showWorkloadGroups
Expand Down Expand Up @@ -484,8 +486,6 @@ unsupportedShowStatement
(FROM |IN) tableName=multipartIdentifier
((FROM | IN) database=multipartIdentifier)? #showIndex
| SHOW CACHE HOTSPOT tablePath=STRING_LITERAL #showCacheHotSpot
| SHOW BUILD INDEX ((FROM | IN) database=multipartIdentifier)?
wildWhere? sortClause? limitClause? #showBuildIndex
;

createRoutineLoad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.util.ListComparator;
import org.apache.doris.common.util.OrderByPair;
import org.apache.doris.nereids.trees.expressions.EqualPredicate;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.GreaterThan;
import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
import org.apache.doris.nereids.trees.expressions.LessThan;
import org.apache.doris.nereids.trees.expressions.LessThanEqual;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand All @@ -39,6 +49,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuildIndexProcDir implements ProcDirInterface {
public static final ImmutableList<String> TITLE_NAMES = new ImmutableList.Builder<String>()
Expand All @@ -59,6 +70,54 @@ public BuildIndexProcDir(SchemaChangeHandler schemaChangeHandler, Database db) {
this.db = db;
}

boolean filterResult(String columnName, Comparable element, Map<String, Expression> filter)
throws AnalysisException {
if (filter == null) {
return true;
}
Expression subExpr = filter.get(columnName.toLowerCase());
if (subExpr == null) {
return true;
}

Expression right;
if (subExpr instanceof Not && subExpr.child(0) instanceof EqualTo) {
right = subExpr.child(0).child(1);
} else {
right = subExpr.child(1);
}

if (right instanceof StringLikeLiteral && subExpr instanceof EqualTo) {
return ((StringLikeLiteral) right).getValue().equals(element);
}

if (right instanceof org.apache.doris.nereids.trees.expressions.literal.DateLiteral) {
if (!(right instanceof DateTimeLiteral)) {
throw new AnalysisException("Invalid date type: " + right.getDataType());
}

Long leftVal = new DateTimeLiteral((String) element).getValue();
Long rightVal = ((DateTimeLiteral) right).getValue();

if (subExpr instanceof EqualPredicate) {
return leftVal.equals(rightVal);
} else if (subExpr instanceof GreaterThan) {
return leftVal > rightVal;
} else if (subExpr instanceof GreaterThanEqual) {
return leftVal >= rightVal;
} else if (subExpr instanceof LessThan) {
return leftVal < rightVal;
} else if (subExpr instanceof LessThanEqual) {
return leftVal <= rightVal;
} else if (subExpr instanceof Not) {
return !leftVal.equals(rightVal);
} else {
Preconditions.checkState(false, "No defined binary operator.");
}
}
return true;
}

boolean filterResult(String columnName, Comparable element, HashMap<String, Expr> filter) throws AnalysisException {
if (filter == null) {
return true;
Expand Down Expand Up @@ -110,6 +169,68 @@ boolean filterResult(String columnName, Comparable element, HashMap<String, Expr
return true;
}

public ProcResult fetchResultByFilter(Map<String, Expression> filter, ArrayList<OrderByPair> orderByPairs,
LimitElement limitElement) throws AnalysisException {
Preconditions.checkNotNull(db);
Preconditions.checkNotNull(schemaChangeHandler);

List<List<Comparable>> indexChangeJobInfos = schemaChangeHandler.getAllIndexChangeJobInfos(db);

//where
List<List<Comparable>> jobInfos;
if (filter == null || filter.size() == 0) {
jobInfos = indexChangeJobInfos;
} else {
jobInfos = Lists.newArrayList();
for (List<Comparable> infoStr : indexChangeJobInfos) {
if (infoStr.size() != TITLE_NAMES.size()) {
LOG.warn("indexChangeJobInfos.size() " + indexChangeJobInfos.size()
+ " not equal TITLE_NAMES.size() " + TITLE_NAMES.size());
continue;
}
boolean isNeed = true;
for (int i = 0; i < infoStr.size(); i++) {
isNeed = filterResult(TITLE_NAMES.get(i), infoStr.get(i), filter);
if (!isNeed) {
break;
}
}
if (isNeed) {
jobInfos.add(infoStr);
}
}
}

// order by
if (orderByPairs != null) {
ListComparator<List<Comparable>> comparator = null;
OrderByPair[] orderByPairArr = new OrderByPair[orderByPairs.size()];
comparator = new ListComparator<List<Comparable>>(orderByPairs.toArray(orderByPairArr));
Collections.sort(jobInfos, comparator);
}

//limit
if (limitElement != null && limitElement.hasLimit()) {
int beginIndex = (int) limitElement.getOffset();
int endIndex = (int) (beginIndex + limitElement.getLimit());
if (endIndex > jobInfos.size()) {
endIndex = jobInfos.size();
}
jobInfos = jobInfos.subList(beginIndex, endIndex);
}

BaseProcResult result = new BaseProcResult();
result.setNames(TITLE_NAMES);
for (List<Comparable> jobInfo : jobInfos) {
List<String> oneResult = new ArrayList<String>(jobInfos.size());
for (Comparable column : jobInfo) {
oneResult.add(column.toString());
}
result.addRow(oneResult);
}
return result;
}

public ProcResult fetchResultByFilter(HashMap<String, Expr> filter, ArrayList<OrderByPair> orderByPairs,
LimitElement limitElement) throws AnalysisException {
Preconditions.checkNotNull(db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowBackendsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBackupCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBrokerCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBuildIndexCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCatalogCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCatalogRecycleBinCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCharsetCommand;
Expand Down Expand Up @@ -8010,6 +8011,54 @@ public LogicalPlan visitRefreshDictionary(RefreshDictionaryContext ctx) {
return new RefreshDictionaryCommand(dbName, dictName);
}

@Override
public LogicalPlan visitShowBuildIndex(DorisParser.ShowBuildIndexContext ctx) {
String ctlName = null;
String dbName = null;
if (ctx.database != null) {
List<String> nameParts = visitMultipartIdentifier(ctx.database);
if (nameParts.size() == 1) {
dbName = nameParts.get(0);
} else if (nameParts.size() == 2) {
ctlName = nameParts.get(0);
dbName = nameParts.get(1);
} else {
throw new AnalysisException("nameParts in analyze database should be [ctl.]db");
}
}

Expression whereClause = null;
if (ctx.wildWhere() != null) {
if (ctx.wildWhere().WHERE() != null) {
whereClause = getExpression(ctx.wildWhere().expression());
}
}

List<OrderKey> orderKeys = new ArrayList<>();
if (ctx.sortClause() != null) {
orderKeys = visit(ctx.sortClause().sortItem(), OrderKey.class);
}

long limit = -1L;
long offset = 0L;
if (ctx.limitClause() != null) {
limit = ctx.limitClause().limit != null
? Long.parseLong(ctx.limitClause().limit.getText())
: 0;
if (limit < 0) {
throw new ParseException("Limit requires non-negative number", ctx.limitClause());
}
offset = ctx.limitClause().offset != null
? Long.parseLong(ctx.limitClause().offset.getText())
: 0;
if (offset < 0) {
throw new ParseException("Offset requires non-negative number", ctx.limitClause());
}
}

return new ShowBuildIndexCommand(ctlName, dbName, orderKeys, whereClause, limit, offset);
}

@Override
public LogicalPlan visitShowRoutineLoad(DorisParser.ShowRoutineLoadContext ctx) {
LabelNameInfo labelNameInfo = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ public enum PlanType {
CREATE_DATA_SYNC_JOB_COMMAND,
CREATE_STAGE_COMMAND,
DROP_STAGE_COMMAND,
SHOW_BUILD_INDEX_COMMAND,
CANCEL_DECOMMISSION_BACKEND_COMMAND,
SHOW_TRANSACTION_COMMAND,
SHOW_WORKLOAD_GROUP_COMMAND,
Expand Down
Loading