Skip to content

Commit 72f4d1f

Browse files
committed
fix: resolve union AST mismatch by adding parentheses around set operation operands with ORDER BY/LIMIT clauses
Co-Authored-By: Dan Lynch <[email protected]>
1 parent 3c098cd commit 72f4d1f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

packages/deparser/src/deparser.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff 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));

0 commit comments

Comments
 (0)