Skip to content

Commit 6cfb699

Browse files
committed
fix: normalize whitespace after INTO clause insertion
When the parser strips 'INTO <target>' from a SELECT statement, it leaves behind the original whitespace. This caused weird formatting like: SELECT x INTO y FROM z Now we normalize the whitespace: - If original had newlines, use newline + 4-space indent - If original was just spaces/tabs, collapse to single space This fixes the pretty-printing issue with long INTO target lists.
1 parent 6448ff5 commit 6cfb699

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

packages/plpgsql-deparser/__tests__/__snapshots__/deparser-fixes.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ BEGIN
8888
INSERT INTO tokens (id, user_id, access_token_hash)
8989
VALUES (v_token_id, p_user_id, digest(v_plaintext_token, 'sha256'));
9090
SELECT tkn.id, tkn.user_id, v_plaintext_token, tkn.access_token_expires_at, tkn.is_verified, tkn.totp_enabled INTO id, user_id, access_token, access_token_expires_at, is_verified, totp_enabled
91-
FROM tokens AS tkn
91+
FROM tokens AS tkn
9292
WHERE tkn.id = v_token_id;
9393
RETURN;
9494
END"

packages/plpgsql-deparser/__tests__/__snapshots__/hydrate-demo.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ BEGIN
9898
t.orders_scanned,
9999
t.gross_total,
100100
t.avg_total INTO v_orders_scanned, v_gross, v_avg
101-
FROM totals AS t;
101+
FROM totals AS t;
102102
IF p_apply_discount THEN
103103
v_rebate := round(v_gross * GREATEST(LEAST(v_discount_rate + v_jitter, 0.50), 0), p_round_to);
104104
ELSE
@@ -109,7 +109,7 @@ BEGIN
109109
SELECT
110110
oi.sku,
111111
CAST(sum(oi.quantity) AS bigint) AS qty INTO v_top_sku, v_top_sku_qty
112-
FROM app_public.order_item AS oi
112+
FROM app_public.order_item AS oi
113113
JOIN app_public.app_order AS o ON o.id = oi.order_id
114114
WHERE
115115
o.org_id = p_org_id

packages/plpgsql-deparser/__tests__/__snapshots__/schema-rename-mapped.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ exports[`schema rename mapped should transform schema names and snapshot schema
194194
total_count int;
195195
BEGIN
196196
SELECT count(*) INTO total_count
197-
FROM myapp_v2.users AS u
197+
FROM myapp_v2.users AS u
198198
JOIN myapp_v2.orders AS o ON o.user_id = u.id
199199
WHERE
200200
u.id = p_user_id;
@@ -270,7 +270,7 @@ CREATE FUNCTION myapp_v2.calculate_order_total(
270270
discount numeric;
271271
BEGIN
272272
SELECT sum(quantity * price) INTO subtotal
273-
FROM myapp_v2.order_items
273+
FROM myapp_v2.order_items
274274
WHERE
275275
order_id = p_order_id;
276276
tax_amount := myapp_v2.get_tax_rate() * subtotal;

packages/plpgsql-deparser/src/plpgsql-deparser.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,23 @@ export class PLpgSQLDeparser {
14851485
// large gaps like "SELECT x INTO y FROM z"
14861486
const before = sql.slice(0, insertPos);
14871487
let after = sql.slice(insertPos);
1488-
// Collapse leading whitespace (but preserve a single space before the next keyword)
1489-
after = after.replace(/^[ \t]+/, ' ');
1488+
// Normalize whitespace after INTO insertion
1489+
// The parser strips "INTO <target>" but leaves whitespace behind, which can cause
1490+
// weird formatting like "SELECT x INTO y FROM z"
1491+
// We collapse all whitespace to either a single space or a newline with standard indent
1492+
const leadingWsMatch = after.match(/^(\s+)/);
1493+
if (leadingWsMatch) {
1494+
const ws = leadingWsMatch[1];
1495+
const hasNewline = /\n/.test(ws);
1496+
if (hasNewline) {
1497+
// If original had newlines, use a newline with standard 4-space indent
1498+
// This normalizes any weird indentation left by the parser
1499+
after = after.replace(/^\s+/, '\n ');
1500+
} else {
1501+
// If original was just spaces/tabs, collapse to single space
1502+
after = after.replace(/^[ \t]+/, ' ');
1503+
}
1504+
}
14901505
sql = before + intoClause + after;
14911506
} else {
14921507
// -1 means INTO already exists at depth 0, don't add another one

0 commit comments

Comments
 (0)