Skip to content

Commit 1d2c4b5

Browse files
committed
Add support for parenthized subselects
The real postgres parser supports parentheses around a full select expression, or the select expression in a subselect. This adds support to the parser for these parentheses. They don't change the output AST, but they didn't parse before.
1 parent 7b6ce9d commit 1d2c4b5

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

src/syntax/main.ne

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ statement_noprep
6666
| functions_statements
6767

6868

69-
selection -> select_statement {% unwrap %}
69+
70+
any_selection -> select_statement {% unwrap %}
7071
| select_values {% unwrap %}
7172
| with_statement {% unwrap %}
7273
| with_recursive_statement {% unwrap %}
7374
| union_statement {% unwrap %}
7475

76+
selection -> lparen selection rparen {% get(1) %}
77+
| any_selection {% get(0) %}
78+
7579
selection_paren -> lparen selection rparen {% get(1) %}

src/syntax/select.spec.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,91 @@ describe('Select statements', () => {
943943
type: 'skip locked',
944944
}
945945
});
946+
947+
checkSelect("(select * from foo)",
948+
{
949+
type: 'select',
950+
"columns": [
951+
{
952+
"expr": {
953+
"name": "*",
954+
"type": "ref"
955+
}
956+
}
957+
],
958+
"from": [
959+
{
960+
"name": {
961+
"name": "foo"
962+
},
963+
"type": "table"
964+
}
965+
]
966+
}
967+
);
968+
969+
const testSubselect = {
970+
type: 'select',
971+
"columns": [
972+
{
973+
"expr": {
974+
"name": "*",
975+
"type": "ref"
976+
}
977+
}
978+
],
979+
"from": [
980+
{
981+
"alias": "a",
982+
"statement": {
983+
"columns": [
984+
{
985+
"expr": {
986+
"name": "*",
987+
"type": "ref"
988+
}
989+
}
990+
],
991+
"from": [
992+
{
993+
"name": {
994+
"name": "bar"
995+
},
996+
"type": "table"
997+
}
998+
],
999+
"type": "select",
1000+
},
1001+
"type": "statement"
1002+
}
1003+
]
1004+
}
1005+
1006+
checkSelect(`
1007+
select * from (
1008+
select * from bar
1009+
) a`,
1010+
testSubselect
1011+
);
1012+
1013+
checkSelect(`
1014+
select * from (
1015+
(
1016+
select * from bar
1017+
)
1018+
) a`,
1019+
testSubselect
1020+
);
1021+
1022+
checkSelect(`
1023+
select * from (
1024+
(
1025+
(
1026+
select * from bar
1027+
)
1028+
)
1029+
) a`,
1030+
testSubselect
1031+
);
1032+
9461033
});

0 commit comments

Comments
 (0)