Skip to content

Commit 774fc26

Browse files
authored
[improve](syntax) compatiable with mysql MOD syntax (#58432)
### What problem does this PR solve? This PR improves compatibility with MySQL arithmetic expression syntax, allow user use `%` and `MOD` to represent modular semantics. Note that the `MOD` is a non-reserved keyword, can be used in arithmetic expressions, MOD function name or to represent aliases in as statements. before: ```sql SELECT 10 MOD 3 as mod; ERROR 1105 (HY000): errCode = 2, detailMessage = mismatched input '3' expecting {<EOF>, ';'}(line 1, pos 19) ``` after: ```sql mysql> SELECT 10 MOD 3 as mod; +------+ | mod | +------+ | 1 | +------+ ``` Issue Number: close #xxx Related PR: #xxx Problem Summary: ### Release note None ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [x] Regression test - [ ] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [ ] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [ ] No. - [ ] Yes. <!-- Add document PR link here. eg: apache/doris-website#1214 --> ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into -->
1 parent f1b6e16 commit 774fc26

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ SUBTRACT: '-';
614614
ASTERISK: '*';
615615
SLASH: '/';
616616
MOD: '%';
617+
MOD_ALT: 'MOD';
617618
TILDE: '~';
618619
AMPERSAND: '&';
619620
LOGICALAND: '&&';

fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ valueExpression
15981598
| operator=(SUBTRACT | PLUS | TILDE) valueExpression #arithmeticUnary
15991599
// split arithmeticBinary from 1 to 5 due to they have different operator precedence
16001600
| left=valueExpression operator=HAT right=valueExpression #arithmeticBinary
1601-
| left=valueExpression operator=(ASTERISK | SLASH | MOD | DIV) right=valueExpression #arithmeticBinary
1601+
| left=valueExpression operator=(ASTERISK | SLASH | MOD | MOD_ALT | DIV) right=valueExpression #arithmeticBinary
16021602
| left=valueExpression operator=(PLUS | SUBTRACT) right=valueExpression #arithmeticBinary
16031603
| left=valueExpression operator=AMPERSAND right=valueExpression #arithmeticBinary
16041604
| left=valueExpression operator=PIPE right=valueExpression #arithmeticBinary
@@ -2102,6 +2102,7 @@ nonReserved
21022102
| MIN
21032103
| MINUTE
21042104
| MINUTES
2105+
| MOD_ALT
21052106
| MODIFY
21062107
| MONTH
21072108
| MTMV

fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,8 @@ public Expression visitArithmeticBinary(ArithmeticBinaryContext ctx) {
30353035
return new Divide(left, right);
30363036
case DorisParser.MOD:
30373037
return new Mod(left, right);
3038+
case DorisParser.MOD_ALT:
3039+
return new Mod(left, right);
30383040
case DorisParser.PLUS:
30393041
return new Add(left, right);
30403042
case DorisParser.SUBTRACT:

regression-test/data/nereids_p0/operator/test_arithmetic_operators.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,13 @@
290290
\N \N \N
291291
\N \N \N
292292

293+
-- !arith_op30 --
294+
0.123 0.1 0.3330001831054688
295+
1.500 0.2680000000000007 0.25
296+
0.325 1 0
297+
298+
-- !arith_op31 --
299+
0.123 0.1 0.3330001831054688
300+
1.500 0.2680000000000007 0.25
301+
0.325 1 0
302+

regression-test/suites/nereids_p0/operator/test_arithmetic_operators.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@ suite("test_arithmetic_operators", "query,p0") {
7676
qt_arith_op27 "select -10.2 / 0.0, -10.2 / 0, -10.2 % 0.0, -10.2 % 0"
7777
qt_arith_op28 "select k5 / 0, k8 / 0, k9 / 0 from ${tableName} order by k1,k2,k3,k4"
7878
qt_arith_op29 "select k5 % 0, k8 % 0, k9 % 0 from ${tableName} order by k1,k2,k3,k4"
79+
80+
qt_arith_op30 "select k5 MOD 3, k8 mod 2, k9 Mod 1 from ${tableName} order by k1,k2,k3,k4"
81+
qt_arith_op31 "select k5 MOD 3, MOD(k8, 2), (k9 MOD 1) as MOD from ${tableName} order by k1,k2,k3,k4"
7982
}

0 commit comments

Comments
 (0)