From a8ff06eeb6bae8fdf38313569ff6460f619dd402 Mon Sep 17 00:00:00 2001 From: Harry Brundage Date: Thu, 14 Dec 2023 22:07:41 -0500 Subject: [PATCH] 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. --- src/syntax/main.ne | 6 ++- src/syntax/select.spec.ts | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/syntax/main.ne b/src/syntax/main.ne index 611df40..e4e213f 100644 --- a/src/syntax/main.ne +++ b/src/syntax/main.ne @@ -68,10 +68,14 @@ statement_noprep | functions_statements -selection -> select_statement {% unwrap %} + +any_selection -> select_statement {% unwrap %} | select_values {% unwrap %} | with_statement {% unwrap %} | with_recursive_statement {% unwrap %} | union_statement {% unwrap %} +selection -> lparen selection rparen {% get(1) %} + | any_selection {% get(0) %} + selection_paren -> lparen selection rparen {% get(1) %} diff --git a/src/syntax/select.spec.ts b/src/syntax/select.spec.ts index c7fab3d..6f4ae9b 100644 --- a/src/syntax/select.spec.ts +++ b/src/syntax/select.spec.ts @@ -1060,4 +1060,91 @@ describe('Select statements', () => { type: 'skip locked', } }); + + checkSelect("(select * from foo)", + { + type: 'select', + "columns": [ + { + "expr": { + "name": "*", + "type": "ref" + } + } + ], + "from": [ + { + "name": { + "name": "foo" + }, + "type": "table" + } + ] + } + ); + + const testSubselect: SelectStatement = { + type: 'select', + "columns": [ + { + "expr": { + "name": "*", + "type": "ref" + } + } + ], + "from": [ + { + "alias": "a", + "statement": { + "columns": [ + { + "expr": { + "name": "*", + "type": "ref" + } + } + ], + "from": [ + { + "name": { + "name": "bar" + }, + "type": "table" + } + ], + "type": "select", + }, + "type": "statement" + } + ] + }; + + checkSelect(` + select * from ( + select * from bar + ) a`, + testSubselect + ); + + checkSelect(` + select * from ( + ( + select * from bar + ) + ) a`, + testSubselect + ); + + checkSelect(` + select * from ( + ( + ( + select * from bar + ) + ) + ) a`, + testSubselect + ); + });