Skip to content

Commit a8ff06e

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 8e9ba23 commit a8ff06e

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
@@ -68,10 +68,14 @@ statement_noprep
6868
| functions_statements
6969

7070

71-
selection -> select_statement {% unwrap %}
71+
72+
any_selection -> select_statement {% unwrap %}
7273
| select_values {% unwrap %}
7374
| with_statement {% unwrap %}
7475
| with_recursive_statement {% unwrap %}
7576
| union_statement {% unwrap %}
7677

78+
selection -> lparen selection rparen {% get(1) %}
79+
| any_selection {% get(0) %}
80+
7781
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
@@ -1060,4 +1060,91 @@ describe('Select statements', () => {
10601060
type: 'skip locked',
10611061
}
10621062
});
1063+
1064+
checkSelect("(select * from foo)",
1065+
{
1066+
type: 'select',
1067+
"columns": [
1068+
{
1069+
"expr": {
1070+
"name": "*",
1071+
"type": "ref"
1072+
}
1073+
}
1074+
],
1075+
"from": [
1076+
{
1077+
"name": {
1078+
"name": "foo"
1079+
},
1080+
"type": "table"
1081+
}
1082+
]
1083+
}
1084+
);
1085+
1086+
const testSubselect: SelectStatement = {
1087+
type: 'select',
1088+
"columns": [
1089+
{
1090+
"expr": {
1091+
"name": "*",
1092+
"type": "ref"
1093+
}
1094+
}
1095+
],
1096+
"from": [
1097+
{
1098+
"alias": "a",
1099+
"statement": {
1100+
"columns": [
1101+
{
1102+
"expr": {
1103+
"name": "*",
1104+
"type": "ref"
1105+
}
1106+
}
1107+
],
1108+
"from": [
1109+
{
1110+
"name": {
1111+
"name": "bar"
1112+
},
1113+
"type": "table"
1114+
}
1115+
],
1116+
"type": "select",
1117+
},
1118+
"type": "statement"
1119+
}
1120+
]
1121+
};
1122+
1123+
checkSelect(`
1124+
select * from (
1125+
select * from bar
1126+
) a`,
1127+
testSubselect
1128+
);
1129+
1130+
checkSelect(`
1131+
select * from (
1132+
(
1133+
select * from bar
1134+
)
1135+
) a`,
1136+
testSubselect
1137+
);
1138+
1139+
checkSelect(`
1140+
select * from (
1141+
(
1142+
(
1143+
select * from bar
1144+
)
1145+
)
1146+
) a`,
1147+
testSubselect
1148+
);
1149+
10631150
});

0 commit comments

Comments
 (0)