Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion __fixtures__/generated/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -21330,7 +21330,9 @@
"misc/issues-15.sql": "select \"A\" from \"table_name\"",
"misc/issues-16.sql": "select \"AA\" from \"table_name\"",
"misc/issues-17.sql": "SELECT CAST(t.date AT TIME ZONE $$America/New_York$$ AS text)::date FROM tbl t",
"misc/issues-18.sql": "CREATE TABLE test_exclude_where (\n id uuid PRIMARY KEY,\n database_id uuid NOT NULL,\n status text NOT NULL DEFAULT 'pending',\n EXCLUDE USING btree (database_id WITH =)\n WHERE (status = 'pending')\n)",
"misc/issues-18.sql": "SELECT (- (-10 - -12))::numeric AS delta",
"misc/issues-19.sql": "SELECT (- (a.actual_eur - a.budget_eur))::numeric AS delta_eur FROM accounts a",
"misc/issues-20.sql": "CREATE TABLE test_exclude_where (\n id uuid PRIMARY KEY,\n database_id uuid NOT NULL,\n status text NOT NULL DEFAULT 'pending',\n EXCLUDE USING btree (database_id WITH =)\n WHERE (status = 'pending')\n)",
"misc/inflection-1.sql": "CREATE SCHEMA inflection",
"misc/inflection-2.sql": "GRANT USAGE ON SCHEMA inflection TO PUBLIC",
"misc/inflection-3.sql": "ALTER DEFAULT PRIVILEGES IN SCHEMA inflection \n GRANT EXECUTE ON FUNCTIONS TO PUBLIC",
Expand Down
6 changes: 6 additions & 0 deletions __fixtures__/kitchen-sink/misc/issues.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ select "AA" from "table_name";
-- https://github.com/constructive-io/pgsql-parser/issues/217
SELECT CAST(t.date AT TIME ZONE $$America/New_York$$ AS text)::date FROM tbl t;

-- https://github.com/constructive-io/pgsql-parser/issues/285
-- TypeCast with unary minus loses parentheses around inner expression
-- (- (a - b))::numeric becomes CAST(- a - b AS numeric) which changes the math
SELECT (- (-10 - -12))::numeric AS delta;
SELECT (- (a.actual_eur - a.budget_eur))::numeric AS delta_eur FROM accounts a;

-- https://github.com/constructive-io/pgsql-parser/issues/287
-- EXCLUDE constraint with WHERE clause (partial exclusion constraint)
-- The deparser drops the WHERE clause from EXCLUDE USING ... WHERE (...)
Expand Down
4 changes: 3 additions & 1 deletion packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ it('misc-issues', async () => {
"misc/issues-15.sql",
"misc/issues-16.sql",
"misc/issues-17.sql",
"misc/issues-18.sql"
"misc/issues-18.sql",
"misc/issues-19.sql",
"misc/issues-20.sql"
]);
});
15 changes: 11 additions & 4 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,17 @@ export class Deparser implements DeparserVisitor {

return context.format([leftExpr, operator, rightExpr]);
}else if (rexpr) {
return context.format([
this.deparseOperatorName(name, context),
this.visit(rexpr, context)
]);
// Unary operator (e.g., unary minus: - expr)
const operator = this.deparseOperatorName(name, context);
let rightExpr = this.visit(rexpr, context);

// Wrap in parentheses if rexpr is a binary A_Expr to preserve semantics
// e.g., -(a - b) must stay -(a - b), not become -a - b
if (rexpr && 'A_Expr' in rexpr && rexpr.A_Expr?.kind === 'AEXPR_OP' && rexpr.A_Expr?.lexpr) {
rightExpr = context.parens(rightExpr);
}

return context.format([operator, rightExpr]);
}
break;
case 'AEXPR_OP_ANY':
Expand Down
Loading