Skip to content

Commit e23f681

Browse files
Merge pull request taozhi8833998#790 from taozhi8833998/bug-case-when-mysql
bug: fix case when expr could wrap by parentheses in mysql
2 parents 1b294f6 + 6de58ef commit e23f681

10 files changed

Lines changed: 148 additions & 97 deletions

File tree

pegjs/bigquery.pegjs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ on_clause
556556
= KW_ON __ e:expr { return e; }
557557

558558
where_clause
559-
= KW_WHERE __ e:(or_and_where_expr / expr) { return e; }
559+
= KW_WHERE __ e:or_and_where_expr { return e; }
560560

561561
group_by_clause
562562
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
@@ -729,17 +729,10 @@ unary_expr
729729
}
730730

731731
or_and_where_expr
732-
= head:parentheses_or_expr tail:(__ (KW_AND / KW_OR) __ parentheses_or_expr)* {
732+
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
733733
return createBinaryExprChain(head, tail);
734734
}
735735

736-
parentheses_or_expr
737-
= lf:LPAREN __ head:or_expr __ rt:RPAREN {
738-
head.parentheses = true
739-
return head
740-
}
741-
/ or_expr
742-
743736
or_expr
744737
= head:and_expr tail:(___ KW_OR __ and_expr)* {
745738
return createBinaryExprChain(head, tail);
@@ -886,14 +879,25 @@ interval_expr
886879

887880
case_expr
888881
= KW_CASE __
889-
expr:expr? __
890882
condition_list:case_when_then+ __
891883
otherwise:case_else? __
892884
KW_END __ KW_CASE? {
893885
if (otherwise) condition_list.push(otherwise);
894886
return {
895887
type: 'case',
896-
expr: expr || null,
888+
expr: null,
889+
args: condition_list
890+
};
891+
}
892+
/ KW_CASE __
893+
expr:expr __
894+
condition_list:case_when_then+ __
895+
otherwise:case_else? __
896+
KW_END __ KW_CASE? {
897+
if (otherwise) condition_list.push(otherwise);
898+
return {
899+
type: 'case',
900+
expr: expr,
897901
args: condition_list
898902
};
899903
}

pegjs/db2.pegjs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ on_clause
11971197
= KW_ON __ e:expr { return e; }
11981198

11991199
where_clause
1200-
= KW_WHERE __ e:(or_and_where_expr / expr) { return e; }
1200+
= KW_WHERE __ e:or_and_where_expr { return e; }
12011201

12021202
group_by_clause
12031203
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
@@ -1477,14 +1477,25 @@ interval_expr
14771477

14781478
case_expr
14791479
= KW_CASE __
1480-
expr:expr? __
14811480
condition_list:case_when_then+ __
14821481
otherwise:case_else? __
14831482
KW_END __ KW_CASE? {
14841483
if (otherwise) condition_list.push(otherwise);
14851484
return {
14861485
type: 'case',
1487-
expr: expr || null,
1486+
expr: null,
1487+
args: condition_list
1488+
};
1489+
}
1490+
/ KW_CASE __
1491+
expr:expr __
1492+
condition_list:case_when_then+ __
1493+
otherwise:case_else? __
1494+
KW_END __ KW_CASE? {
1495+
if (otherwise) condition_list.push(otherwise);
1496+
return {
1497+
type: 'case',
1498+
expr: expr,
14881499
args: condition_list
14891500
};
14901501
}
@@ -1532,17 +1543,10 @@ unary_expr
15321543
}
15331544

15341545
or_and_where_expr
1535-
= head:parentheses_or_expr tail:(__ (KW_AND / KW_OR) __ parentheses_or_expr)* {
1546+
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
15361547
return createBinaryExprChain(head, tail);
15371548
}
15381549

1539-
parentheses_or_expr
1540-
= lf:LPAREN __ head:or_expr __ rt:RPAREN {
1541-
head.parentheses = true
1542-
return head
1543-
}
1544-
/ or_expr
1545-
15461550
or_expr
15471551
= head:and_expr tail:(___ KW_OR __ and_expr)* {
15481552
return createBinaryExprChain(head, tail);

pegjs/flinksql.pegjs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,17 +2047,10 @@ unary_expr
20472047
}
20482048

20492049
or_and_where_expr
2050-
= head:parentheses_or_expr tail:(__ (KW_AND / KW_OR) __ parentheses_or_expr)* {
2050+
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
20512051
return createBinaryExprChain(head, tail);
20522052
}
20532053

2054-
parentheses_or_expr
2055-
= lf:LPAREN __ head:or_expr __ rt:RPAREN {
2056-
head.parentheses = true
2057-
return head
2058-
}
2059-
/ or_expr
2060-
20612054
or_expr
20622055
= head:and_expr tail:(___ KW_OR __ and_expr)* {
20632056
// => binary_expr

pegjs/hive.pegjs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ on_clause
11601160
= KW_ON __ e:expr { return e; }
11611161

11621162
where_clause
1163-
= KW_WHERE __ e:(or_and_where_expr / expr) { return e; }
1163+
= KW_WHERE __ e:or_and_where_expr { return e; }
11641164

11651165
group_by_clause
11661166
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
@@ -1387,14 +1387,25 @@ interval_expr
13871387

13881388
case_expr
13891389
= KW_CASE __
1390-
expr:expr? __
13911390
condition_list:case_when_then+ __
13921391
otherwise:case_else? __
13931392
KW_END __ KW_CASE? {
13941393
if (otherwise) condition_list.push(otherwise);
13951394
return {
13961395
type: 'case',
1397-
expr: expr || null,
1396+
expr: null,
1397+
args: condition_list
1398+
};
1399+
}
1400+
/ KW_CASE __
1401+
expr:expr __
1402+
condition_list:case_when_then+ __
1403+
otherwise:case_else? __
1404+
KW_END __ KW_CASE? {
1405+
if (otherwise) condition_list.push(otherwise);
1406+
return {
1407+
type: 'case',
1408+
expr: expr,
13981409
args: condition_list
13991410
};
14001411
}
@@ -1442,17 +1453,10 @@ unary_expr
14421453
}
14431454

14441455
or_and_where_expr
1445-
= head:parentheses_or_expr tail:(__ (KW_AND / KW_OR) __ parentheses_or_expr)* {
1456+
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
14461457
return createBinaryExprChain(head, tail);
14471458
}
14481459

1449-
parentheses_or_expr
1450-
= lf:LPAREN __ head:or_expr __ rt:RPAREN {
1451-
head.parentheses = true
1452-
return head
1453-
}
1454-
/ or_expr
1455-
14561460
or_expr
14571461
= head:and_expr tail:(___ KW_OR __ and_expr)* {
14581462
return createBinaryExprChain(head, tail);

pegjs/mariadb.pegjs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ on_clause
13541354
= KW_ON __ e:expr { return e; }
13551355

13561356
where_clause
1357-
= KW_WHERE __ e:(or_and_where_expr / expr) { return e; }
1357+
= KW_WHERE __ e:(or_and_where_expr) { return e; }
13581358

13591359
group_by_clause
13601360
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
@@ -1587,14 +1587,25 @@ interval_expr
15871587

15881588
case_expr
15891589
= KW_CASE __
1590-
expr:expr? __
15911590
condition_list:case_when_then+ __
15921591
otherwise:case_else? __
15931592
KW_END __ KW_CASE? {
15941593
if (otherwise) condition_list.push(otherwise);
15951594
return {
15961595
type: 'case',
1597-
expr: expr || null,
1596+
expr: null,
1597+
args: condition_list
1598+
};
1599+
}
1600+
/ KW_CASE __
1601+
expr:expr __
1602+
condition_list:case_when_then+ __
1603+
otherwise:case_else? __
1604+
KW_END __ KW_CASE? {
1605+
if (otherwise) condition_list.push(otherwise);
1606+
return {
1607+
type: 'case',
1608+
expr: expr,
15981609
args: condition_list
15991610
};
16001611
}
@@ -1642,17 +1653,10 @@ unary_expr
16421653
}
16431654

16441655
or_and_where_expr
1645-
= head:parentheses_or_expr tail:(__ (KW_AND / KW_OR) __ parentheses_or_expr)* {
1656+
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
16461657
return createBinaryExprChain(head, tail);
16471658
}
16481659

1649-
parentheses_or_expr
1650-
= lf:LPAREN __ head:or_expr __ rt:RPAREN {
1651-
head.parentheses = true
1652-
return head
1653-
}
1654-
/ or_expr
1655-
16561660
or_expr
16571661
= head:and_expr tail:(___ KW_OR __ and_expr)* {
16581662
return createBinaryExprChain(head, tail);
@@ -2553,10 +2557,15 @@ data_type
25532557
/ text_type
25542558
/ enum_type
25552559
/ boolean_type
2560+
/ binary_type
25562561

25572562
boolean_type
25582563
= 'boolean'i { return { dataType: 'BOOLEAN' }; }
25592564

2565+
binary_type
2566+
= 'binary'i { return { dataType: 'BINARY' }; }
2567+
/ 'varbinary'i { return { dataType: 'VARBINARY' }; }
2568+
25602569
character_string_type
25612570
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN {
25622571
return { dataType: t, length: parseInt(l.join(''), 10) };

pegjs/mysql.pegjs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ on_clause
16221622
= KW_ON __ e:expr { return e; }
16231623

16241624
where_clause
1625-
= KW_WHERE __ e:(or_and_where_expr / expr) { return e; }
1625+
= KW_WHERE __ e:or_and_where_expr { return e; }
16261626

16271627
group_by_clause
16281628
= KW_GROUP __ KW_BY __ e:expr_list { return e.value; }
@@ -1893,14 +1893,25 @@ interval_expr
18931893

18941894
case_expr
18951895
= KW_CASE __
1896-
expr:expr? __
18971896
condition_list:case_when_then+ __
18981897
otherwise:case_else? __
18991898
KW_END __ KW_CASE? {
19001899
if (otherwise) condition_list.push(otherwise);
19011900
return {
19021901
type: 'case',
1903-
expr: expr || null,
1902+
expr: null,
1903+
args: condition_list
1904+
};
1905+
}
1906+
/ KW_CASE __
1907+
expr:expr __
1908+
condition_list:case_when_then+ __
1909+
otherwise:case_else? __
1910+
KW_END __ KW_CASE? {
1911+
if (otherwise) condition_list.push(otherwise);
1912+
return {
1913+
type: 'case',
1914+
expr: expr,
19041915
args: condition_list
19051916
};
19061917
}
@@ -1948,7 +1959,7 @@ unary_expr
19481959
}
19491960

19501961
or_and_where_expr
1951-
= head:parentheses_or_expr tail:(__ (KW_AND / KW_OR) __ parentheses_or_expr)* {
1962+
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {
19521963
return createBinaryExprChain(head, tail);
19531964
}
19541965

@@ -1960,13 +1971,7 @@ or_expr
19601971
and_expr
19611972
= head:not_expr tail:(___ KW_AND __ not_expr)* {
19621973
return createBinaryExprChain(head, tail);
1963-
}
1964-
parentheses_or_expr
1965-
= lf:LPAREN __ head:or_expr __ rt:RPAREN {
1966-
head.parentheses = true
1967-
return head
19681974
}
1969-
/ or_expr
19701975
//here we should use `NOT` instead of `comparision_expr` to support chain-expr
19711976
not_expr
19721977
= comparison_expr
@@ -2913,10 +2918,15 @@ data_type
29132918
/ text_type
29142919
/ enum_type
29152920
/ boolean_type
2921+
/ binary_type
29162922

29172923
boolean_type
29182924
= 'boolean'i { return { dataType: 'BOOLEAN' }; }
29192925

2926+
binary_type
2927+
= 'binary'i { return { dataType: 'BINARY' }; }
2928+
/ 'varbinary'i { return { dataType: 'VARBINARY' }; }
2929+
29202930
character_string_type
29212931
= t:(KW_CHAR / KW_VARCHAR) __ LPAREN __ l:[0-9]+ __ RPAREN {
29222932
return { dataType: t, length: parseInt(l.join(''), 10) };

0 commit comments

Comments
 (0)