Skip to content

Commit

Permalink
support cancel commands
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Nov 21, 2024
1 parent 2953055 commit f0c9882
Show file tree
Hide file tree
Showing 10 changed files with 560 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ statementBase
| supportedUnsetStatement #supportedUnsetStatementAlias
| supportedRefreshStatement #supportedRefreshStatementAlias
| supportedShowStatement #supportedShowStatementAlias
| supportedCancelStatement #supportedCancelStatementAlias
| supportedRecoverStatement #supportedRecoverStatementAlias
| unsupportedStatement #unsupported
;
Expand Down Expand Up @@ -205,7 +206,7 @@ supportedShowStatement
: SHOW (GLOBAL | SESSION | LOCAL)? VARIABLES wildWhere? #showVariables
| SHOW AUTHORS #showAuthors
| SHOW DYNAMIC PARTITION TABLES ((FROM | IN) database=multipartIdentifier)? #showDynamicPartition
| SHOW LAST INSERT #showLastInsert
| SHOW LAST INSERT #showLastInsert
| SHOW ALL? GRANTS #showGrants
| SHOW GRANTS FOR userIdentify #showGrantsForUser
| SHOW VIEW
Expand All @@ -228,7 +229,7 @@ supportedShowStatement
| SHOW TABLE tableId=INTEGER_VALUE #showTableId
| SHOW WHITELIST #showWhitelist
| SHOW TABLETS BELONG
tabletIds+=INTEGER_VALUE (COMMA tabletIds+=INTEGER_VALUE)* #showTabletsBelong
tabletIds+=INTEGER_VALUE (COMMA tabletIds+=INTEGER_VALUE)* #showTabletsBelong
;

unsupportedOtherStatement
Expand Down Expand Up @@ -444,10 +445,14 @@ unsupportedCleanStatement
| CLEAN ALL QUERY STATS #cleanAllQueryStats
;

unsupportedCancelStatement
supportedCancelStatement
: CANCEL LOAD ((FROM | IN) database=identifier)? wildWhere? #cancelLoad
| CANCEL EXPORT ((FROM | IN) database=identifier)? wildWhere? #cancelExport
| CANCEL ALTER TABLE (ROLLUP | (MATERIALIZED VIEW) | COLUMN)
| CANCEL WARM UP JOB wildWhere? #cancelWarmUpJob
;

unsupportedCancelStatement
: CANCEL ALTER TABLE (ROLLUP | (MATERIALIZED VIEW) | COLUMN)
FROM tableName=multipartIdentifier (LEFT_PAREN jobIds+=INTEGER_VALUE
(COMMA jobIds+=INTEGER_VALUE)* RIGHT_PAREN)? #cancelAlterTable
| CANCEL BUILD INDEX ON tableName=multipartIdentifier
Expand All @@ -457,7 +462,6 @@ unsupportedCancelStatement
(COMMA hostPorts+=STRING_LITERAL)* #cancelDecommisionBackend
| CANCEL BACKUP ((FROM | IN) database=identifier)? #cancelBackup
| CANCEL RESTORE ((FROM | IN) database=identifier)? #cancelRestore
| CANCEL WARM UP JOB wildWhere? #cancelWarmUp
;

supportedRecoverStatement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public CancelExportStmt(String dbName, Expr whereClause) {
this.whereClause = whereClause;
}

public CancelExportStmt(String dbName, Expr whereClause, String label, CompoundPredicate.Operator operator,
String state) {
this.dbName = dbName;
this.whereClause = whereClause;
this.label = label;
this.operator = operator;
this.state = state;
}

private void checkColumn(Expr expr, boolean like) throws AnalysisException {
String inputCol = ((SlotRef) expr.getChild(0)).getColumnName();
if (!SUPPORT_COLUMNS.contains(inputCol.toLowerCase())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public CancelLoadStmt(String dbName, Expr whereClause) {
this.SUPPORT_COLUMNS.add("state");
}

public CancelLoadStmt(String dbName, Expr whereClause, String label, CompoundPredicate.Operator operator,
String state) {
this.dbName = dbName;
this.whereClause = whereClause;
this.label = label;
this.operator = operator;
this.state = state;
}

private void checkColumn(Expr expr, boolean like) throws AnalysisException {
String inputCol = ((SlotRef) expr.getChild(0)).getColumnName();
if (!SUPPORT_COLUMNS.contains(inputCol)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,11 @@
import org.apache.doris.nereids.trees.plans.commands.AlterStorageVaultCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand;
import org.apache.doris.nereids.trees.plans.commands.CallCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelExportCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelJobTaskCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelLoadCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelMTMVTaskCommand;
import org.apache.doris.nereids.trees.plans.commands.CancelWarmUpJobCommand;
import org.apache.doris.nereids.trees.plans.commands.CleanAllProfileCommand;
import org.apache.doris.nereids.trees.plans.commands.Command;
import org.apache.doris.nereids.trees.plans.commands.Constraint;
Expand Down Expand Up @@ -1028,6 +1031,41 @@ public ShowCreateMTMVCommand visitShowCreateMTMV(ShowCreateMTMVContext ctx) {
return new ShowCreateMTMVCommand(new ShowCreateMTMVInfo(new TableNameInfo(nameParts)));
}

@Override
public CancelExportCommand visitCancelExport(DorisParser.CancelExportContext ctx) {
String databaseName = null;
if (ctx.database != null) {
databaseName = stripQuotes(ctx.database.getText());
}
Expression wildWhere = null;
if (ctx.wildWhere() != null) {
wildWhere = getWildWhere(ctx.wildWhere());
}
return new CancelExportCommand(databaseName, wildWhere);
}

@Override
public CancelLoadCommand visitCancelLoad(DorisParser.CancelLoadContext ctx) {
String databaseName = null;
if (ctx.database != null) {
databaseName = stripQuotes(ctx.database.getText());
}
Expression wildWhere = null;
if (ctx.wildWhere() != null) {
wildWhere = getWildWhere(ctx.wildWhere());
}
return new CancelLoadCommand(databaseName, wildWhere);
}

@Override
public CancelWarmUpJobCommand visitCancelWarmUpJob(DorisParser.CancelWarmUpJobContext ctx) {
Expression wildWhere = null;
if (ctx.wildWhere() != null) {
wildWhere = getWildWhere(ctx.wildWhere());
}
return new CancelWarmUpJobCommand(wildWhere);
}

@Override
public CancelMTMVTaskCommand visitCancelMTMVTask(CancelMTMVTaskContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.mvName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public enum PlanType {
PAUSE_MTMV_COMMAND,
RESUME_MTMV_COMMAND,
SHOW_CREATE_MTMV_COMMAND,
CANCEL_EXPORT_COMMAND,
CANCEL_LOAD_COMMAND,
CANCEL_WARM_UP_JOB_COMMAND,
CANCEL_MTMV_TASK_COMMAND,
CALL_COMMAND,
CREATE_PROCEDURE_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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.nereids.trees.plans.commands;

import org.apache.doris.analysis.Expr;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.analyzer.UnboundSlot;
import org.apache.doris.nereids.glue.translator.ExpressionTranslator;
import org.apache.doris.nereids.glue.translator.PlanTranslatorContext;
import org.apache.doris.nereids.properties.PhysicalProperties;
import org.apache.doris.nereids.trees.expressions.BinaryOperator;
import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Like;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.base.Strings;

import java.util.ArrayList;
import java.util.Map;

/**
* cancel load command
*/
public abstract class CancelCommand extends Command implements ForwardWithSync {
public CancelCommand(PlanType type) {
super(type);
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {

}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return null;
}

/**
* translate to legacy expr, which do not need complex expression and table columns
*/
public Expr translateToLegacyExpr(ConnectContext ctx, Expression expression) {
LogicalEmptyRelation plan = new LogicalEmptyRelation(
ConnectContext.get().getStatementContext().getNextRelationId(),
new ArrayList<>());
CascadesContext cascadesContext = CascadesContext.initContext(ctx.getStatementContext(), plan,
PhysicalProperties.ANY);
PlanTranslatorContext planTranslatorContext = new PlanTranslatorContext(cascadesContext);
return ExpressionTranslator.translate(expression, planTranslatorContext);
}

/**
* check where filter for cancel load/export commands
* @param expression where clause
* @param supportedColumns only these kind of columns is supported
* @throws AnalysisException analyze exceptions
*/
public void checkWhereFilter(Expression expression, Map<String, String> supportedColumns) throws AnalysisException {
if (null == expression) {
throw new AnalysisException("Where clause can't be null");
} else if (expression instanceof Like) {
likeCheck(expression, supportedColumns);
} else if (expression instanceof BinaryOperator) {
binaryCheck(expression, supportedColumns);
} else if (expression instanceof CompoundPredicate) {
compoundCheck(expression, supportedColumns);
} else {
throw new AnalysisException("Only support like/binary/compound predicate");
}
}

private void checkColumn(Expression expr, boolean like, Map<String, String> supportedColumns)
throws AnalysisException {
if (!(expr.child(0) instanceof UnboundSlot)) {
throw new AnalysisException("Current only support label and state, invalid column: "
+ expr.child(0).toSql());
}
String inputCol = ((UnboundSlot) expr.child(0)).getName();
if (!supportedColumns.keySet().contains(inputCol.toLowerCase())) {
throw new AnalysisException("Current only support label and state, invalid column: " + inputCol);
}
if (!(expr.child(1) instanceof StringLikeLiteral)) {
throw new AnalysisException("Value must be a string");
}

String inputValue = ((StringLikeLiteral) expr.child(1)).getStringValue();
if (Strings.isNullOrEmpty(inputValue)) {
throw new AnalysisException("Value can't be null");
}

if (inputCol.equalsIgnoreCase("label")) {
supportedColumns.put("label", inputValue);
}

if (inputCol.equalsIgnoreCase("state")) {
if (like) {
throw new AnalysisException("Only label can use like");
}
supportedColumns.put("state", inputValue);
}
}

private void likeCheck(Expression expr, Map<String, String> supportedColumns) throws AnalysisException {
checkColumn(expr, true, supportedColumns);
}

private void binaryCheck(Expression expr, Map<String, String> supportedColumns) throws AnalysisException {
checkColumn(expr, false, supportedColumns);
}

private void compoundCheck(Expression expr, Map<String, String> supportedColumns) throws AnalysisException {
// current only support label and state
if (expr instanceof Not) {
throw new AnalysisException("Current not support NOT operator");
}
for (int i = 0; i < 2; i++) {
Expression child = expr.child(i);
if (child instanceof CompoundPredicate) {
throw new AnalysisException("Current not support nested clause");
} else if (child instanceof Like) {
likeCheck(child, supportedColumns);
} else if (child instanceof BinaryOperator) {
binaryCheck(child, supportedColumns);
} else {
throw new AnalysisException("Only support like/binary predicate");
}
}
}
}
Loading

0 comments on commit f0c9882

Please sign in to comment.