Skip to content

Commit 65dff57

Browse files
ovrMazterQyou
authored andcommitted
feat: Support KILL [CONNECTION | QUERY] processlist_id
1 parent 34f20f1 commit 65dff57

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/ast/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,13 @@ pub enum Statement {
951951
data_types: Vec<DataType>,
952952
statement: Box<Statement>,
953953
},
954+
/// See <https://clickhouse.com/docs/ru/sql-reference/statements/kill/>
955+
/// See <https://dev.mysql.com/doc/refman/8.0/en/kill.html>
956+
Kill {
957+
modifier: Option<KillType>,
958+
// processlist_id
959+
id: u64,
960+
},
954961
/// EXPLAIN TABLE
955962
/// Note: this is a MySQL-specific statement. See <https://dev.mysql.com/doc/refman/8.0/en/explain.html>
956963
ExplainTable {
@@ -993,6 +1000,15 @@ impl fmt::Display for Statement {
9931000
#[allow(clippy::cognitive_complexity)]
9941001
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9951002
match self {
1003+
Statement::Kill { modifier, id } => {
1004+
write!(f, "KILL ")?;
1005+
1006+
if let Some(m) = modifier {
1007+
write!(f, "{} ", m)?;
1008+
}
1009+
1010+
write!(f, "{}", id)
1011+
}
9961012
Statement::ExplainTable {
9971013
describe_alias,
9981014
table_name,
@@ -2058,6 +2074,26 @@ impl fmt::Display for ObjectType {
20582074
}
20592075
}
20602076

2077+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2078+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2079+
pub enum KillType {
2080+
Connection,
2081+
Query,
2082+
Mutation,
2083+
}
2084+
2085+
impl fmt::Display for KillType {
2086+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2087+
f.write_str(match self {
2088+
// MySQL
2089+
KillType::Connection => "CONNECTION",
2090+
KillType::Query => "QUERY",
2091+
// Clickhouse supports Mutation
2092+
KillType::Mutation => "Mutation",
2093+
})
2094+
}
2095+
}
2096+
20612097
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20622098
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20632099
pub enum HiveDistributionStyle {

src/keywords.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ define_keywords!(
137137
COMPUTE,
138138
CONDITION,
139139
CONNECT,
140+
CONNECTION,
140141
CONSTRAINT,
141142
CONTAINS,
142143
CONVERT,
@@ -280,6 +281,7 @@ define_keywords!(
280281
JSONFILE,
281282
JULIAN,
282283
KEY,
284+
KILL,
283285
LAG,
284286
LANGUAGE,
285287
LARGE,
@@ -383,6 +385,7 @@ define_keywords!(
383385
PROGRAM,
384386
PURGE,
385387
QUARTER,
388+
QUERY,
386389
QUOTE,
387390
RANGE,
388391
RANK,

src/parser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'a> Parser<'a> {
154154
pub fn parse_statement(&mut self) -> Result<Statement, ParserError> {
155155
match self.next_token() {
156156
Token::Word(w) => match w.keyword {
157+
Keyword::KILL => Ok(self.parse_kill()?),
157158
Keyword::DESCRIBE => Ok(self.parse_explain(true)?),
158159
Keyword::EXPLAIN => Ok(self.parse_explain(false)?),
159160
Keyword::ANALYZE => Ok(self.parse_analyze()?),
@@ -2797,6 +2798,21 @@ impl<'a> Parser<'a> {
27972798
})
27982799
}
27992800

2801+
// KILL [CONNECTION | QUERY] processlist_id
2802+
pub fn parse_kill(&mut self) -> Result<Statement, ParserError> {
2803+
let modifier_keyword = self.parse_one_of_keywords(&[Keyword::CONNECTION, Keyword::QUERY]);
2804+
2805+
let id = self.parse_literal_uint()?;
2806+
2807+
let modifier = match modifier_keyword {
2808+
Some(Keyword::CONNECTION) => Some(KillType::Connection),
2809+
Some(Keyword::QUERY) => Some(KillType::Query),
2810+
_ => None,
2811+
};
2812+
2813+
Ok(Statement::Kill { modifier, id })
2814+
}
2815+
28002816
pub fn parse_explain(&mut self, describe_alias: bool) -> Result<Statement, ParserError> {
28012817
let analyze = self.parse_keyword(Keyword::ANALYZE);
28022818
let verbose = self.parse_keyword(Keyword::VERBOSE);

tests/sqlparser_mysql.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,36 @@ fn parse_show_variables() {
819819
mysql().verified_stmt("SHOW VARIABLES WHERE value = '3306'");
820820
}
821821

822+
#[test]
823+
fn parse_kill() {
824+
let stmt = mysql_and_generic().verified_stmt("KILL CONNECTION 5");
825+
assert_eq!(
826+
stmt,
827+
Statement::Kill {
828+
modifier: Some(KillType::Connection),
829+
id: 5,
830+
}
831+
);
832+
833+
let stmt = mysql_and_generic().verified_stmt("KILL QUERY 5");
834+
assert_eq!(
835+
stmt,
836+
Statement::Kill {
837+
modifier: Some(KillType::Query),
838+
id: 5,
839+
}
840+
);
841+
842+
let stmt = mysql_and_generic().verified_stmt("KILL 5");
843+
assert_eq!(
844+
stmt,
845+
Statement::Kill {
846+
modifier: None,
847+
id: 5,
848+
}
849+
);
850+
}
851+
822852
#[test]
823853
fn parse_set_names() {
824854
let stmt = mysql_and_generic().verified_stmt("SET NAMES utf8mb4");

0 commit comments

Comments
 (0)