Skip to content

Commit 55fd955

Browse files
authored
fix(ast): filter reserved keywords in all_reserved_keywords (#19749)
* fix(ast): filter reserved keywords in all_reserved_keywords (#9485) * test(ast): update stmt-error golden for reserved keywords
1 parent f9afef4 commit 55fd955

4 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/query/ast/src/parser/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ pub fn display_parser_error(error: Error, source: &str) -> String {
263263
let mut msg = if span_text.is_empty() {
264264
"unexpected end of input".to_string()
265265
} else if all_reserved_keywords()
266-
.iter()
267266
.any(|keyword| keyword.to_lowercase() == span_text.to_lowercase())
268267
&& has_suggestion.is_none()
269268
{

src/query/ast/src/parser/token.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,10 +1995,24 @@ impl TokenKind {
19951995
}
19961996
}
19971997

1998-
pub fn all_reserved_keywords() -> Vec<String> {
1999-
let mut result = Vec::new();
2000-
for token in TokenKind::iter() {
2001-
result.push(format!("{:?}", token));
1998+
pub fn all_reserved_keywords() -> impl Iterator<Item = String> {
1999+
TokenKind::iter()
2000+
.filter(TokenKind::is_keyword)
2001+
.filter(|token| token.is_reserved_ident(false) || token.is_reserved_function_name())
2002+
.map(|token| format!("{:?}", token))
2003+
}
2004+
2005+
#[cfg(test)]
2006+
mod tests {
2007+
use super::all_reserved_keywords;
2008+
2009+
#[test]
2010+
fn test_all_reserved_keywords_only_returns_reserved_keywords() {
2011+
let keywords = all_reserved_keywords().collect::<Vec<_>>();
2012+
2013+
assert!(keywords.contains(&"SELECT".to_string()));
2014+
assert!(keywords.contains(&"TABLE".to_string()));
2015+
assert!(!keywords.contains(&"DATABASE".to_string()));
2016+
assert!(!keywords.contains(&"Whitespace".to_string()));
20022017
}
2003-
result
20042018
}

src/query/ast/tests/it/testdata/stmt-error.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ error:
560560
--> SQL:1:43
561561
|
562562
1 | alter table t1 create branch from_dev at (branch => dev)
563-
| ----- ^^^^^^ unexpected `branch`. it's reserved keyword, you may avoid using it, expecting `STREAM`, `SNAPSHOT`, `TAG`, `TIMESTAMP`, or `OFFSET`
563+
| ----- ^^^^^^ unexpected `branch`, expecting `STREAM`, `SNAPSHOT`, `TAG`, `TIMESTAMP`, or `OFFSET`
564564
| |
565565
| while parsing `ALTER TABLE [<database>.]<table> <action>`
566566

@@ -618,7 +618,7 @@ error:
618618
--> SQL:1:35
619619
|
620620
1 | SELECT * FROM t GROUP BY GROUPING SETS a, b
621-
| ^^^^ unexpected `SETS`. it's reserved keyword, you may avoid using it, expecting `SELECT`, `INTERSECT`, `WITH`, `EXCEPT`, `VALUES`, `OFFSET`, `IGNORE_RESULT`, `,`, `HAVING`, `WINDOW`, `QUALIFY`, `(`, `UNION`, `FROM`, `ORDER`, `LIMIT`, `FORMAT`, or `;`
621+
| ^^^^ unexpected `SETS`, expecting `SELECT`, `INTERSECT`, `WITH`, `EXCEPT`, `VALUES`, `OFFSET`, `IGNORE_RESULT`, `,`, `HAVING`, `WINDOW`, `QUALIFY`, `(`, `UNION`, `FROM`, `ORDER`, `LIMIT`, `FORMAT`, or `;`
622622

623623

624624
---------- Input ----------
@@ -1006,7 +1006,7 @@ error:
10061006
--> SQL:1:34
10071007
|
10081008
1 | GRANT OWNERSHIP ON d20_0014.* TO USER A;
1009-
| ----- ^^^^ unexpected `USER`. it's reserved keyword, you may avoid using it, expecting `ROLE`
1009+
| ----- ^^^^ unexpected `USER`, expecting `ROLE`
10101010
| |
10111011
| while parsing GRANT OWNERSHIP ON <privileges_level> TO ROLE <role_name>
10121012

@@ -1018,7 +1018,7 @@ error:
10181018
--> SQL:1:8
10191019
|
10201020
1 | REVOKE OWNERSHIP, SELECT ON d20_0014.* FROM ROLE 'd20_0015_owner';
1021-
| ------ ^^^^^^^^^ unexpected `OWNERSHIP`. it's reserved keyword, you may avoid using it, expecting `INSERT`, `ALTER`, `SUPER`, `ROLE`, `ACCESS`, `WRITE`, `SET`, `SELECT`, `UPDATE`, `DELETE`, `DROP`, `READ`, `USAGE`, `GRANT`, `CREATE`, `ALL`, or `APPLY`
1021+
| ------ ^^^^^^^^^ unexpected `OWNERSHIP`, expecting `INSERT`, `ALTER`, `SUPER`, `ROLE`, `ACCESS`, `WRITE`, `SET`, `SELECT`, `UPDATE`, `DELETE`, `DROP`, `READ`, `USAGE`, `GRANT`, `CREATE`, `ALL`, or `APPLY`
10221022
| |
10231023
| while parsing `REVOKE { ROLE <role_name> | schemaObjectPrivileges | ALL [ PRIVILEGES ] ON <privileges_level> } FROM { [ROLE <role_name>] | [USER] <user> }`
10241024

@@ -1030,7 +1030,7 @@ error:
10301030
--> SQL:1:8
10311031
|
10321032
1 | REVOKE OWNERSHIP ON d20_0014.* FROM USER A;
1033-
| ------ ^^^^^^^^^ unexpected `OWNERSHIP`. it's reserved keyword, you may avoid using it, expecting `INSERT`, `ALTER`, `SUPER`, `ROLE`, `ACCESS`, `WRITE`, `SET`, `SELECT`, `UPDATE`, `DELETE`, `DROP`, `READ`, `USAGE`, `GRANT`, `CREATE`, `ALL`, or `APPLY`
1033+
| ------ ^^^^^^^^^ unexpected `OWNERSHIP`, expecting `INSERT`, `ALTER`, `SUPER`, `ROLE`, `ACCESS`, `WRITE`, `SET`, `SELECT`, `UPDATE`, `DELETE`, `DROP`, `READ`, `USAGE`, `GRANT`, `CREATE`, `ALL`, or `APPLY`
10341034
| |
10351035
| while parsing `REVOKE { ROLE <role_name> | schemaObjectPrivileges | ALL [ PRIVILEGES ] ON <privileges_level> } FROM { [ROLE <role_name>] | [USER] <user> }`
10361036

@@ -1042,7 +1042,7 @@ error:
10421042
--> SQL:1:8
10431043
|
10441044
1 | REVOKE OWNERSHIP ON d20_0014.* FROM ROLE A;
1045-
| ------ ^^^^^^^^^ unexpected `OWNERSHIP`. it's reserved keyword, you may avoid using it, expecting `INSERT`, `ALTER`, `SUPER`, `ROLE`, `ACCESS`, `WRITE`, `SET`, `SELECT`, `UPDATE`, `DELETE`, `DROP`, `READ`, `USAGE`, `GRANT`, `CREATE`, `ALL`, or `APPLY`
1045+
| ------ ^^^^^^^^^ unexpected `OWNERSHIP`, expecting `INSERT`, `ALTER`, `SUPER`, `ROLE`, `ACCESS`, `WRITE`, `SET`, `SELECT`, `UPDATE`, `DELETE`, `DROP`, `READ`, `USAGE`, `GRANT`, `CREATE`, `ALL`, or `APPLY`
10461046
| |
10471047
| while parsing `REVOKE { ROLE <role_name> | schemaObjectPrivileges | ALL [ PRIVILEGES ] ON <privileges_level> } FROM { [ROLE <role_name>] | [USER] <user> }`
10481048

@@ -1081,7 +1081,7 @@ error:
10811081
--> SQL:1:85
10821082
|
10831083
1 | CREATE FUNCTION my_agg (INT) STATE { s STRING } RETURNS BOOLEAN LANGUAGE javascript HANDLER = 'my_agg' ADDRESS = 'http://0.0.0.0:8815';
1084-
| ------ - ^^^^^^^ unexpected `HANDLER`. it's reserved keyword, you may avoid using it, expecting `HEADERS`, `ADDRESS`, `PACKAGES`, `AS`, or `IMPORTS`
1084+
| ------ - ^^^^^^^ unexpected `HANDLER`, expecting `HEADERS`, `ADDRESS`, `PACKAGES`, `AS`, or `IMPORTS`
10851085
| | |
10861086
| | while parsing (<[arg_name] arg_type>, ...) STATE {<state_field>, ...} RETURNS <return_type> LANGUAGE <language> { ADDRESS=<udf_server_address> | AS <language_codes> }
10871087
| while parsing `CREATE [OR REPLACE] FUNCTION [IF NOT EXISTS] <udf_name> <udf_definition> [DESC = <description>]`

src/query/storages/information_schema/src/keywords_table.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub struct KeywordsTable {}
3131

3232
impl KeywordsTable {
3333
pub fn create(table_id: u64, ctl_name: &str) -> Arc<dyn Table> {
34-
let all_keywords_vec = all_reserved_keywords();
35-
let all_keywords = all_keywords_vec.join(", ");
34+
let all_keywords = all_reserved_keywords().collect::<Vec<_>>().join(", ");
3635
let query = "SELECT '".to_owned() + &all_keywords + "' AS KEYWORDS, 1 AS RESERVED";
3736

3837
let mut options = BTreeMap::new();

0 commit comments

Comments
 (0)