Skip to content

Commit 6d314d8

Browse files
nanfeng1999morningman
authored andcommitted
[feature](expr) Support kill query by query_id (apache#28530)
Issue Number: open apache#28517
1 parent 2525d3e commit 6d314d8

7 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎docs/en/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ The thread process list identifier can be determined from the ID column of the I
3939
grammar:
4040

4141
```sql
42-
KILL [CONNECTION | QUERY] processlist_id
42+
KILL [CONNECTION] processlist_id
4343
````
4444

45+
In addition, you can also use processlist_id or query_id terminates the executing query command
46+
47+
grammar:
48+
49+
```sql
50+
KILL QUERY processlist_id | query_id
51+
````
52+
53+
4554
### Example
4655
4756
### Keywords

‎docs/zh-CN/docs/sql-manual/sql-reference/Database-Administration-Statements/KILL.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,19 @@ KILL
3939
语法:
4040

4141
```sql
42-
KILL [CONNECTION | QUERY] processlist_id
42+
KILL [CONNECTION] processlist_id
4343
```
4444

45+
除此之外,您还可以使用 processlist_id 或者 query_id 终止正在执行的查询命令
46+
47+
语法:
48+
49+
```sql
50+
KILL QUERY processlist_id | query_id
51+
```
52+
53+
54+
4555
### Example
4656

4757
### Keywords

‎fe/fe-core/src/main/cup/sql_parser.cup‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4928,6 +4928,10 @@ kill_stmt ::=
49284928
{:
49294929
RESULT = new KillStmt(false, value.intValue());
49304930
:}
4931+
| KW_KILL KW_QUERY STRING_LITERAL:value
4932+
{:
4933+
RESULT = new KillStmt(value);
4934+
:}
49314935
;
49324936

49334937
kill_analysis_job_stmt ::=

‎fe/fe-core/src/main/java/org/apache/doris/analysis/KillStmt.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@
2525
public class KillStmt extends StatementBase {
2626
private final boolean isConnectionKill;
2727
private final int connectionId;
28+
private final String queryId;
2829

2930
public KillStmt(boolean isConnectionKill, int connectionId) {
3031
this.isConnectionKill = isConnectionKill;
3132
this.connectionId = connectionId;
33+
this.queryId = "";
34+
}
35+
36+
public KillStmt(String queryId) {
37+
this.isConnectionKill = false;
38+
this.connectionId = -1;
39+
this.queryId = queryId;
3240
}
3341

3442
public boolean isConnectionKill() {
@@ -39,6 +47,10 @@ public int getConnectionId() {
3947
return connectionId;
4048
}
4149

50+
public String getQueryId() {
51+
return queryId;
52+
}
53+
4254
@Override
4355
public void analyze(Analyzer analyzer) {
4456
// No operation.

‎fe/fe-core/src/main/java/org/apache/doris/common/ErrorCode.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public enum ErrorCode {
4343
ERR_NO_SUCH_THREAD(1094, new byte[]{'H', 'Y', '0', '0', '0'}, "Unknown thread id: %d"),
4444
ERR_KILL_DENIED_ERROR(1095, new byte[]{'H', 'Y', '0', '0', '0'}, "You are not owner of thread %d"),
4545
ERR_NO_TABLES_USED(1096, new byte[]{'H', 'Y', '0', '0', '0'}, "No tables used"),
46+
ERR_NO_SUCH_QUERY(1097, new byte[]{'H', 'Y', '0', '0', '0'}, "Unknown query id: %s"),
4647
ERR_WRONG_DB_NAME(1102, new byte[]{'4', '2', '0', '0', '0'}, "Incorrect database name '%s'"),
4748
ERR_WRONG_TABLE_NAME(1103, new byte[]{'4', '2', '0', '0', '0'}, "Incorrect table name '%s'. Table name regex is '%s'"),
4849
ERR_TOO_BIG_SELECT(1104, new byte[]{'4', '2', '0', '0', '0'}, "The SELECT would examine more than MAX_JOIN_SIZE "

‎fe/fe-core/src/main/java/org/apache/doris/qe/ConnectScheduler.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ public ConnectContext getContext(int connectionId) {
122122
return connectionMap.get(connectionId);
123123
}
124124

125+
public ConnectContext getContextWithQueryId(String queryId) {
126+
for (ConnectContext context : connectionMap.values()) {
127+
if (queryId.equals(DebugUtil.printId(context.queryId))) {
128+
return context;
129+
}
130+
}
131+
return null;
132+
}
133+
125134
public void cancelQuery(String queryId) {
126135
for (ConnectContext ctx : connectionMap.values()) {
127136
TUniqueId qid = ctx.queryId();

‎fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,11 +1199,21 @@ public void cancel() {
11991199
// Handle kill statement.
12001200
private void handleKill() throws DdlException {
12011201
KillStmt killStmt = (KillStmt) parsedStmt;
1202+
ConnectContext killCtx = null;
12021203
int id = killStmt.getConnectionId();
1203-
ConnectContext killCtx = context.getConnectScheduler().getContext(id);
1204-
if (killCtx == null) {
1205-
ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id);
1204+
String queryId = killStmt.getQueryId();
1205+
if (id == -1) {
1206+
killCtx = context.getConnectScheduler().getContextWithQueryId(queryId);
1207+
if (killCtx == null) {
1208+
ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_QUERY, queryId);
1209+
}
1210+
} else {
1211+
killCtx = context.getConnectScheduler().getContext(id);
1212+
if (killCtx == null) {
1213+
ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id);
1214+
}
12061215
}
1216+
12071217
if (context == killCtx) {
12081218
// Suicide
12091219
context.setKilled();

0 commit comments

Comments
 (0)