Skip to content

Commit e4194d2

Browse files
committed
support funcion parition by
1 parent e1269fa commit e4194d2

5 files changed

Lines changed: 26 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "1.6.20",
3+
"version": "1.6.21",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"types": "index.d.ts",

pegjs/mysql.pegjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,6 @@ KW_SUM_MAX_MIN_AVG
15361536

15371537
aggr_fun_count
15381538
= name:KW_COUNT __ LPAREN __ arg:count_arg __ RPAREN __ KW_OVER __ LPAREN __ KW_PARTITION __ KW_BY __ bc: column_list __ RPAREN __ {
1539-
console.log(bc)
15401539
if (bc) bc.forEach(c => columnList.add(`select::null::${c}`))
15411540
return {
15421541
type: 'aggr_func',
@@ -1562,13 +1561,23 @@ star_expr
15621561
= "*" { return { type: 'star', value: '*' }; }
15631562

15641563
func_call
1565-
= name:ident __ LPAREN __ l:expr_list? __ RPAREN {
1564+
= name:ident __ LPAREN __ l:expr_list? __ RPAREN __ KW_OVER __ LPAREN __ KW_PARTITION __ KW_BY __ bc: column_list __ RPAREN __ {
1565+
if (bc) bc.forEach(c => columnList.add(`select::null::${c}`))
1566+
return {
1567+
type: 'function',
1568+
name: name,
1569+
args: l ? l: { type: 'expr_list', value: [] },
1570+
over: bc
1571+
};
1572+
}
1573+
/ name:ident __ LPAREN __ l:expr_list? __ RPAREN {
15661574
return {
15671575
type: 'function',
15681576
name: name,
15691577
args: l ? l: { type: 'expr_list', value: [] }
15701578
};
15711579
}
1580+
15721581
/ name:scalar_func (__ LPAREN RPAREN __)? {
15731582
return {
15741583
type: 'function',

src/aggregation.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ function aggrToSQL(expr) {
88
let str = exprToSQL(args.expr)
99
const fnName = expr.name
1010
const overStr = over && `OVER (PARTITION BY ${over.map(col => identifierToSql(col)).join(', ')})`
11-
1211
if (fnName === 'COUNT') {
1312
if (has(args, 'distinct') && args.distinct !== null) str = `DISTINCT ${str}`
1413
}

src/func.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { exprToSQL } from './expr'
2+
import { hasVal, identifierToSql } from './util'
23

34
function castToSQL(expr) {
45
const str = expr.target.length ? `(${expr.target.length})` : ''
@@ -14,9 +15,12 @@ function castToSQL(expr) {
1415
}
1516

1617
function funcToSQL(expr) {
17-
if (!expr.args) return expr.name
18-
const str = `${expr.name}(${exprToSQL(expr.args).join(', ')})`
19-
return expr.parentheses ? `(${str})` : str
18+
const { args, name } = expr
19+
if (!args) return name
20+
const { parentheses, over } = expr
21+
const str = `${name}(${exprToSQL(args).join(', ')})`
22+
const overStr = over && `OVER (PARTITION BY ${over.map(col => identifierToSql(col)).join(', ')})`
23+
return [parentheses ? `(${str})` : str, overStr].filter(hasVal).join(' ')
2024
}
2125

2226
export {

test/select.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,13 @@ describe('select', () => {
10031003
const backSQL = parser.sqlify(ast)
10041004
expect(backSQL).to.equal("SELECT `id`, `name`, `gender`, COUNT(`gender`) OVER (PARTITION BY `gender`) AS `Total_students` FROM `student`")
10051005
})
1006+
1007+
it('should support select over function', () => {
1008+
const sql = 'SELECT ROW_NUMBER() OVER (PARTITION BY gender) AS Total_students FROM student'
1009+
const ast = parser.astify(sql)
1010+
const backSQL = parser.sqlify(ast)
1011+
expect(backSQL).to.equal("SELECT ROW_NUMBER() OVER (PARTITION BY `gender`) AS `Total_students` FROM `student`")
1012+
})
10061013
})
10071014

10081015
describe('pg json column', () => {

0 commit comments

Comments
 (0)