diff --git a/src/syntax/ast.ts b/src/syntax/ast.ts index e405263..4f56411 100644 --- a/src/syntax/ast.ts +++ b/src/syntax/ast.ts @@ -747,7 +747,7 @@ export interface FromTable extends PGNode { export interface FromStatement extends PGNode { type: 'statement'; statement: SelectStatement; - alias: string; + alias?: string; lateral?: true; columnNames?: Name[] | nil; db?: null | nil; diff --git a/src/syntax/select.ne b/src/syntax/select.ne index 638c29c..64bdbad 100644 --- a/src/syntax/select.ne +++ b/src/syntax/select.ne @@ -80,8 +80,8 @@ stb_table -> table_ref stb_opts:? {% x => { } %} -# Selects on subselects MUST have an alias -stb_statement -> %kw_lateral:? selection_paren stb_opts {% x => track(x, { +# Selects on subselects CAN have an alias +stb_statement -> %kw_lateral:? selection_paren stb_opts:? {% x => track(x, { type: 'statement', statement: unwrap(x[1]), ...x[0] && { lateral: true }, diff --git a/src/syntax/select.spec.ts b/src/syntax/select.spec.ts index c7fab3d..25bbb12 100644 --- a/src/syntax/select.spec.ts +++ b/src/syntax/select.spec.ts @@ -349,7 +349,6 @@ describe('Select statements', () => { checkInvalid('select (*) from test'); checkInvalid('select ("*") from test'); checkInvalid('select * from (test)'); - checkInvalid('select * from (select id from test)'); // <== missing alias checkInvalid('select * from sum(DISTINCT whatever)'); checkSelect('select * from (select id from test) d', { @@ -366,6 +365,19 @@ describe('Select statements', () => { }] }) + checkSelect('select * from (select id from test)', { + type: 'select', + columns: columns({ type: 'ref', name: '*' }), + from: [{ + type: 'statement', + statement: { + type: 'select', + from: [tbl('test')], + columns: columns({ type: 'ref', name: 'id' }), + }, + }] + }) + checkSelect(['select * from test group by grp', 'select * from test group by (grp)'], { type: 'select', columns: columns({ type: 'ref', name: '*' }),