Skip to content

Commit ad77dc6

Browse files
Merge pull request #2245 from taozhi8833998/feat-limit-subquery-pg
feat: support subquery in limit clause
2 parents ab7376b + 665030b commit ad77dc6

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

pegjs/postgresql.pegjs

+11-10
Original file line numberDiff line numberDiff line change
@@ -3118,15 +3118,8 @@ comment_on_stmt
31183118
expr: is,
31193119
}
31203120
}
3121-
select_stmt
3122-
= KW_SELECT __ ';' {
3123-
// => { type: 'select'; }
3124-
return {
3125-
type: 'select',
3126-
}
3127-
}
3128-
/ select_stmt_nake
3129-
/ s:('(' __ select_stmt __ ')') {
3121+
select_stmt_parentheses
3122+
= s:('(' __ select_stmt __ ')') {
31303123
/*
31313124
export interface select_stmt_node extends select_stmt_nake {
31323125
parentheses: true;
@@ -3138,6 +3131,14 @@ select_stmt
31383131
parentheses_symbol: true,
31393132
}
31403133
}
3134+
select_stmt
3135+
= KW_SELECT __ ';' {
3136+
// => { type: 'select'; }
3137+
return {
3138+
type: 'select',
3139+
}
3140+
}
3141+
/ select_stmt_nake / select_stmt_parentheses
31413142

31423143
with_clause
31433144
= KW_WITH __ head:cte_definition tail:(__ COMMA __ cte_definition)* {
@@ -3820,7 +3821,7 @@ number_or_param
38203821
/ param
38213822

38223823
limit_clause
3823-
= l:(KW_LIMIT __ (number_or_param / KW_ALL))? __ tail:(KW_OFFSET __ number_or_param)? {
3824+
= l:(KW_LIMIT __ (number_or_param / KW_ALL / select_stmt_parentheses))? __ tail:(KW_OFFSET __ number_or_param)? {
38243825
// => { separator: 'offset' | ''; value: [number_or_param | { type: 'origin', value: 'all' }, number_or_param?] }
38253826
const res = []
38263827
if (l) res.push(typeof l[2] === 'string' ? { type: 'origin', value: 'all' } : l[2])

test/postgres.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,13 @@ describe('Postgres', () => {
17071707
'CREATE INDEX ON "tableName" (supplier, amount) INCLUDE (id)'
17081708
]
17091709
},
1710+
{
1711+
title: 'limit by select stmt',
1712+
sql: [
1713+
'SELECT * FROM user LIMIT (SELECT COUNT(*) / 2 FROM user);',
1714+
'SELECT * FROM "user" LIMIT (SELECT COUNT(*) / 2 FROM "user")'
1715+
]
1716+
},
17101717
]
17111718
function neatlyNestTestedSQL(sqlList){
17121719
sqlList.forEach(sqlInfo => {

0 commit comments

Comments
 (0)