Skip to content

Commit

Permalink
chore(query): improve variable parser (#16599)
Browse files Browse the repository at this point in the history
* chore(query): improve variable parser

* chore(query): improve variable parser

* chore(query): improve variable parser
  • Loading branch information
sundy-li authored Oct 13, 2024
1 parent 96106c4 commit 01bc7ed
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/query/ast/src/parser/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn column_id(i: Input) -> IResult<ColumnID> {
}

pub fn variable_ident(i: Input) -> IResult<String> {
map(rule! { "$" ~ ^#plain_ident }, |(_, name)| name.name)(i)
map(rule! { IdentVariable }, |t| t.text()[1..].to_string())(i)
}

/// Parse one to two idents separated by a dot, fulfilling from the right.
Expand Down
3 changes: 3 additions & 0 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ pub enum TokenKind {
#[regex(r#"[_a-zA-Z][_$a-zA-Z0-9]*"#)]
Ident,

#[regex(r#"\$[_a-zA-Z][_$a-zA-Z0-9]*"#)]
IdentVariable,

#[regex(r#"\$[0-9]+"#)]
ColumnPosition,

Expand Down
3 changes: 2 additions & 1 deletion src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ fn test_statement() {
r#"DROP FUNCTION binary_reverse;"#,
r#"DROP FUNCTION isnotempty;"#,
r#"
EXECUTE IMMEDIATE
EXECUTE IMMEDIATE
$$
BEGIN
LOOP
Expand Down Expand Up @@ -1239,6 +1239,7 @@ fn test_expr_error() {
r#"1 a"#,
r#"CAST(col1)"#,
r#"a.add(b)"#,
r#"$ abc + 3"#,
r#"[ x * 100 FOR x in [1,2,3] if x % 2 = 0 ]"#,
r#"
G.E.B IS NOT NULL
Expand Down
13 changes: 13 additions & 0 deletions src/query/ast/tests/it/testdata/expr-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ error:
| while parsing expression


---------- Input ----------
$ abc + 3
---------- Output ---------
error:
--> SQL:1:1
|
1 | $ abc + 3
| ^
| |
| unexpected `$`, expecting `IS`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `DATE_ADD`, `DATE_DIFF`, `DATE_SUB`, `DATE_TRUNC`, `DATE`, `TIMESTAMP`, `INTERVAL`, or 31 more ...
| while parsing expression


---------- Input ----------
[ x * 100 FOR x in [1,2,3] if x % 2 = 0 ]
---------- Output ---------
Expand Down
2 changes: 1 addition & 1 deletion src/query/ast/tests/it/testdata/stmt-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ error:
--> SQL:1:23
|
1 | drop table IDENTIFIER(a)
| ---- ^ unexpected `a`, expecting `$` or `:`
| ---- ^ unexpected `a`, expecting `IdentVariable` or `:`
| |
| while parsing `DROP TABLE [IF EXISTS] [<database>.]<table>`

Expand Down
2 changes: 1 addition & 1 deletion src/query/ast/tests/it/testdata/stmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22377,7 +22377,7 @@ DropUDF {


---------- Input ----------
EXECUTE IMMEDIATE
EXECUTE IMMEDIATE
$$
BEGIN
LOOP
Expand Down

0 comments on commit 01bc7ed

Please sign in to comment.