Skip to content

Commit 16ff43c

Browse files
committed
feat: support arithmetic with in expr
1 parent af42dce commit 16ff43c

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

pegjs/mariadb.pegjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,7 +2915,11 @@ not_expr
29152915
comparison_expr
29162916
= left:additive_expr __ rh:comparison_op_right? {
29172917
if (rh === null) return left;
2918-
else if (rh.type === 'arithmetic') return createBinaryExprChain(left, rh.tail);
2918+
else if (rh.type === 'arithmetic') {
2919+
if (!rh.in) return createBinaryExprChain(left, rh.tail);
2920+
const leftExpr = createBinaryExprChain(left, rh.tail);
2921+
return createBinaryExpr(rh.in.op, leftExpr, rh.in.right);
2922+
}
29192923
else return createBinaryExpr(rh.op, left, rh.right);
29202924
}
29212925
/ literal_string
@@ -2940,8 +2944,8 @@ comparison_op_right
29402944
/ regexp_op_right
29412945

29422946
arithmetic_op_right
2943-
= l:(__ arithmetic_comparison_operator __ additive_expr)+ {
2944-
return { type: 'arithmetic', tail: l };
2947+
= l:(__ arithmetic_comparison_operator __ additive_expr)+ __ i:in_op_right? {
2948+
return { type: 'arithmetic', tail: l, in: i };
29452949
}
29462950

29472951
arithmetic_comparison_operator

pegjs/mysql.pegjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,7 +3177,11 @@ not_expr
31773177
comparison_expr
31783178
= left:additive_expr __ rh:comparison_op_right? {
31793179
if (rh === null) return left;
3180-
else if (rh.type === 'arithmetic') return createBinaryExprChain(left, rh.tail);
3180+
else if (rh.type === 'arithmetic') {
3181+
if (!rh.in) return createBinaryExprChain(left, rh.tail);
3182+
const leftExpr = createBinaryExprChain(left, rh.tail);
3183+
return createBinaryExpr(rh.in.op, leftExpr, rh.in.right);
3184+
}
31813185
else return createBinaryExpr(rh.op, left, rh.right);
31823186
}
31833187
/ literal_string
@@ -3202,8 +3206,8 @@ comparison_op_right
32023206
/ regexp_op_right
32033207

32043208
arithmetic_op_right
3205-
= l:(__ arithmetic_comparison_operator __ additive_expr)+ {
3206-
return { type: 'arithmetic', tail: l };
3209+
= l:(__ arithmetic_comparison_operator __ additive_expr)+ __ i:in_op_right? {
3210+
return { type: 'arithmetic', tail: l, in: i };
32073211
}
32083212

32093213
arithmetic_comparison_operator

test/mysql-mariadb.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,13 @@ describe('mysql', () => {
12841284
'SELECT * FROM `A` INNER JOIN (`B` INNER JOIN `C` ON `B`.`x` = `C`.`x`) ON `A`.`y` = `C`.`y`'
12851285
]
12861286
},
1287+
{
1288+
title: 'arithmetic and in expr',
1289+
sql: [
1290+
'SELECT * FROM T1 WHERE a = b IN (SELECT flag FROM T2)',
1291+
'SELECT * FROM `T1` WHERE `a` = `b` IN (SELECT `flag` FROM `T2`)'
1292+
]
1293+
},
12871294
]
12881295
SQL_LIST.forEach(sqlInfo => {
12891296
const { title, sql } = sqlInfo

0 commit comments

Comments
 (0)