File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -118,9 +118,19 @@ export class Deparser implements DeparserVisitor {
118118 const leftStmt = this . SelectStmt ( node . larg as t . SelectStmt , context ) ;
119119 const rightStmt = this . SelectStmt ( node . rarg as t . SelectStmt , context ) ;
120120
121- // Only add parentheses if the operand is itself a set operation
122- const leftNeedsParens = node . larg && ( node . larg as t . SelectStmt ) . op && ( node . larg as t . SelectStmt ) . op !== 'SETOP_NONE' ;
123- const rightNeedsParens = node . rarg && ( node . rarg as t . SelectStmt ) . op && ( node . rarg as t . SelectStmt ) . op !== 'SETOP_NONE' ;
121+ // Add parentheses if the operand is a set operation OR has ORDER BY/LIMIT clauses
122+ const leftNeedsParens = node . larg && (
123+ ( ( node . larg as t . SelectStmt ) . op && ( node . larg as t . SelectStmt ) . op !== 'SETOP_NONE' ) ||
124+ ( node . larg as t . SelectStmt ) . sortClause ||
125+ ( node . larg as t . SelectStmt ) . limitCount ||
126+ ( node . larg as t . SelectStmt ) . limitOffset
127+ ) ;
128+ const rightNeedsParens = node . rarg && (
129+ ( ( node . rarg as t . SelectStmt ) . op && ( node . rarg as t . SelectStmt ) . op !== 'SETOP_NONE' ) ||
130+ ( node . rarg as t . SelectStmt ) . sortClause ||
131+ ( node . rarg as t . SelectStmt ) . limitCount ||
132+ ( node . rarg as t . SelectStmt ) . limitOffset
133+ ) ;
124134
125135 if ( leftNeedsParens ) {
126136 output . push ( this . formatter . parens ( leftStmt ) ) ;
You can’t perform that action at this time.
0 commit comments