From 8db229a4c7a2d7cdbe26f5bdb5cc8195b3a8cbe0 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Fri, 10 May 2024 20:05:49 +0800 Subject: [PATCH] fix: sum expr in mysql --- pegjs/mariadb.pegjs | 2 +- pegjs/mysql.pegjs | 2 +- test/mysql-mariadb.spec.js | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index ea8e17c4..388288ff 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -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, diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 76fa87d7..cc7ddcf4 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -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, diff --git a/test/mysql-mariadb.spec.js b/test/mysql-mariadb.spec.js index 663d8f0f..ed76619e 100644 --- a/test/mysql-mariadb.spec.js +++ b/test/mysql-mariadb.spec.js @@ -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 @@ -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', () => {