Skip to content

Commit

Permalink
Merge pull request #1899 from taozhi8833998/fix-sum-expr-mysql
Browse files Browse the repository at this point in the history
fix: sum expr in mysql
  • Loading branch information
taozhi8833998 authored May 11, 2024
2 parents 33dbb14 + 8db229a commit 3ad2aa6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,7 @@ aggr_func
/ aggr_fun_smma

aggr_fun_smma
= name:KW_SUM_MAX_MIN_AVG __ LPAREN __ e:additive_expr __ RPAREN __ bc:over_partition? {
= name:KW_SUM_MAX_MIN_AVG __ LPAREN __ e:expr __ RPAREN __ bc:over_partition? {
return {
type: 'aggr_func',
name: name,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,7 @@ aggr_func
/ aggr_fun_smma

aggr_fun_smma
= name:KW_SUM_MAX_MIN_AVG __ LPAREN __ e:additive_expr __ RPAREN __ bc:over_partition? {
= name:KW_SUM_MAX_MIN_AVG __ LPAREN __ e:expr __ RPAREN __ bc:over_partition? {
return {
type: 'aggr_func',
name: name,
Expand Down
9 changes: 8 additions & 1 deletion test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,13 @@ describe('mysql', () => {
'CREATE TABLE `Pattern` (`IsInterpolated` INT NOT NULL, `Value` DOUBLE, CONSTRAINT `CHK_Value_IsInterpolated` CHECK ((`Value` IS NOT NULL) OR (`IsInterpolated` = 0)))'
]
},
{
title: 'sql',
sql: [
'select a.*,sum(b.f_hits_number) hits,sum(b.f_helpful=1) helpful from t_xxxx a left join t_bbb b on a.f_id = b.question_name_id group by a.f_id',
'SELECT `a`.*, SUM(`b`.`f_hits_number`) AS `hits`, SUM(`b`.`f_helpful` = 1) AS `helpful` FROM `t_xxxx` AS `a` LEFT JOIN `t_bbb` AS `b` ON `a`.`f_id` = `b`.`question_name_id` GROUP BY `a`.`f_id`'
]
}
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand All @@ -1018,7 +1025,7 @@ describe('mysql', () => {
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
sql = 'SELECT AVG(Quantity,age) FROM table1;'
expect(parser.astify.bind(parser, sql)).to.throw('Expected "#", "%", "&", "&&", "(", ")", "*", "+", "-", "--", "->", "->>", ".", "/", "/*", "<<", ">>", "XOR", "^", "div", "|", "||", [ \\t\\n\\r], [A-Za-z0-9_$\\x80-], or [A-Za-z0-9_:] but "," found.')
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "%", "&", "&&", "(", ")", "*", "+", "-", "--", "->", "->>", ".", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "BETWEEN", "IN", "IS", "LIKE", "NOT", "REGEXP", "RLIKE", "XOR", "^", "div", "|", "||", [ \\t\\n\\r], [A-Za-z0-9_$\\x80-], or [A-Za-z0-9_:] but "," found.')
})

it('should join multiple table with comma', () => {
Expand Down

0 comments on commit 3ad2aa6

Please sign in to comment.