Skip to content

Commit 15e31d7

Browse files
authored
[feature](expr) Support kill query by query_id (#28530)
Issue Number: open #28517
1 parent 71b7dcf commit 15e31d7

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
@@ -5062,6 +5062,10 @@ kill_stmt ::=
50625062
{:
50635063
RESULT = new KillStmt(false, value.intValue());
50645064
:}
5065+
| KW_KILL KW_QUERY STRING_LITERAL:value
5066+
{:
5067+
RESULT = new KillStmt(value);
5068+
:}
50655069
;
50665070

50675071
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
@@ -126,6 +126,15 @@ public ConnectContext getContext(int connectionId) {
126126
return connectionMap.get(connectionId);
127127
}
128128

129+
public ConnectContext getContextWithQueryId(String queryId) {
130+
for (ConnectContext context : connectionMap.values()) {
131+
if (queryId.equals(DebugUtil.printId(context.queryId))) {
132+
return context;
133+
}
134+
}
135+
return null;
136+
}
137+
129138
public ConnectContext getContext(String flightToken) {
130139
if (flightToken2ConnectionId.containsKey(flightToken)) {
131140
int connectionId = flightToken2ConnectionId.get(flightToken);

‎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
@@ -1242,11 +1242,21 @@ public void cancel() {
12421242
// Handle kill statement.
12431243
private void handleKill() throws DdlException {
12441244
KillStmt killStmt = (KillStmt) parsedStmt;
1245+
ConnectContext killCtx = null;
12451246
int id = killStmt.getConnectionId();
1246-
ConnectContext killCtx = context.getConnectScheduler().getContext(id);
1247-
if (killCtx == null) {
1248-
ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id);
1247+
String queryId = killStmt.getQueryId();
1248+
if (id == -1) {
1249+
killCtx = context.getConnectScheduler().getContextWithQueryId(queryId);
1250+
if (killCtx == null) {
1251+
ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_QUERY, queryId);
1252+
}
1253+
} else {
1254+
killCtx = context.getConnectScheduler().getContext(id);
1255+
if (killCtx == null) {
1256+
ErrorReport.reportDdlException(ErrorCode.ERR_NO_SUCH_THREAD, id);
1257+
}
12491258
}
1259+
12501260
if (context == killCtx) {
12511261
// Suicide
12521262
context.setKilled();

0 commit comments

Comments
 (0)