Skip to content

Commit

Permalink
Merge pull request #1867 from taozhi8833998/count-args-mysql
Browse files Browse the repository at this point in the history
fix: count args in mysql
  • Loading branch information
taozhi8833998 authored Apr 13, 2024
2 parents c5cb54d + b37af77 commit 078262c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
11 changes: 9 additions & 2 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ or_and_where_expr
}
if (seperator === ',') {
const el = { type: 'expr_list' }
el.value = result
el.value = Array.isArray(result) ? result : [result]
return el
}
return result
Expand Down Expand Up @@ -3055,7 +3055,14 @@ count_arg
separator: s
};
}
/ d:KW_DISTINCT? __ c:or_and_expr __ or:order_by_clause? __ s:concat_separator? { return { distinct: d, expr: c, orderby: or, separator: s }; }
/ d:KW_DISTINCT? __ c:or_and_where_expr __ or:order_by_clause? __ s:concat_separator? {
return {
distinct: d,
expr: c,
orderby: or,
separator: s
};
}

star_expr
= "*" { return { type: 'star', value: '*' }; }
Expand Down
11 changes: 9 additions & 2 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2863,7 +2863,7 @@ or_and_where_expr
}
if (seperator === ',') {
const el = { type: 'expr_list' }
el.value = result
el.value = Array.isArray(result) ? result : [result]
return el
}
return result
Expand Down Expand Up @@ -3356,7 +3356,14 @@ count_arg
...getLocationObject()
};
}
/ d:KW_DISTINCT? __ c:or_and_expr __ or:order_by_clause? __ s:concat_separator? { return { distinct: d, expr: c, orderby: or, separator: s, ...getLocationObject() }; }
/ d:KW_DISTINCT? __ c:or_and_where_expr __ or:order_by_clause? __ s:concat_separator? {
return {
distinct: d,
expr: c,
orderby: or,
separator: s
};
}

star_expr
= "*" { return { type: 'star', value: '*' }; }
Expand Down
3 changes: 2 additions & 1 deletion src/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function binaryToSQL(expr) {
if (!isBetween) rstr = `(${rstr.join(', ')})`
}
const escape = expr.right.escape || {}
const str = [exprToSQL(expr.left), operator, rstr, toUpper(escape.type), exprToSQL(escape.value)].filter(hasVal).join(' ')
const leftPart = Array.isArray(expr.left) ? expr.left.map(exprToSQL).join(', ') : exprToSQL(expr.left)
const str = [leftPart, operator, rstr, toUpper(escape.type), exprToSQL(escape.value)].filter(hasVal).join(' ')
const result = [expr.parentheses ? `(${str})` : str]
const { suffix } = expr
if (!suffix) return result.join(' ')
Expand Down
31 changes: 31 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,37 @@ describe('mysql', () => {
"SELECT * FROM `product` WHERE (`id` = '1' OR `id` = '2') COLLATE UTF8MB4_GENERAL_CI"
]
},
{
title: 'count args',
sql: [
`SELECT
DATE( o.date ) AS date,
COUNT( DISTINCT o.user_id, operation_type = 0 OR NULL ) AS operateOpenCount,
COUNT( DISTINCT o.user_id, ( operation_type = 0 AND jump_status = 3 ) OR NULL ) AS realityOpenCount,
COUNT( DISTINCT o.user_id, operation_type = 1 OR NULL ) AS operateCloseCount,
COUNT( DISTINCT o.user_id, ( operation_type = 1 AND jump_status = 3 ) OR NULL ) AS realityCloseCount
FROM
(
SELECT
id,
user_id,
operation_type,
jump_status,
operation_time,
rider_type,
IF
( EXTRACT( HOUR FROM operation_time ) >= 16, DATE_ADD( DATE( operation_time ), INTERVAL 1 DAY ), DATE( operation_time ) ) AS date
FROM
labour_insurance_operation
) AS o
LEFT JOIN labour_user u ON o.user_id = u.id
LEFT JOIN labour_user_group_user gu ON o.user_id = gu.user_id
AND gu.STATUS = 0
GROUP BY
o.date`,
'SELECT DATE(`o`.`date`) AS `date`, COUNT(DISTINCT `o`.`user_id`,`operation_type` = 0 OR NULL) AS `operateOpenCount`, COUNT(DISTINCT `o`.`user_id`, (`operation_type` = 0 AND `jump_status` = 3) OR NULL) AS `realityOpenCount`, COUNT(DISTINCT `o`.`user_id`,`operation_type` = 1 OR NULL) AS `operateCloseCount`, COUNT(DISTINCT `o`.`user_id`, (`operation_type` = 1 AND `jump_status` = 3) OR NULL) AS `realityCloseCount` FROM (SELECT `id`, `user_id`, `operation_type`, `jump_status`, `operation_time`, `rider_type`, IF(EXTRACT(HOUR FROM `operation_time`) >= 16, DATE_ADD(DATE(`operation_time`), INTERVAL 1 DAY), DATE(`operation_time`)) AS `date` FROM `labour_insurance_operation`) AS `o` LEFT JOIN `labour_user` AS `u` ON `o`.`user_id` = `u`.`id` LEFT JOIN `labour_user_group_user` AS `gu` ON `o`.`user_id` = `gu`.`user_id` AND `gu`.`STATUS` = 0 GROUP BY `o`.`date`'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit 078262c

Please sign in to comment.