Skip to content

fix(pretty-print): keep negative constants and FROM casts valid with castStyle=operator - #848

Merged
psteinroe merged 1 commit into
supabase-community:mainfrom
RaphaelFakhri:fix/cast-operator-negative-and-from
Oct 11, 2026
Merged

psteinroe merged 1 commit into
supabase-community:mainfrom
RaphaelFakhri:fix/cast-operator-negative-and-from

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

With castStyle: "operator", the formatter changed the meaning of two kinds of casts.

A negative numeric constant lost its parentheses. CAST(-2147483648 AS int) became -2147483648::int, which casts 2147483648 first and fails with integer out of range. CAST(-12345 AS money) became -12345::money, a negation of a cast instead of a cast of a negative constant.

A cast used as a table function in FROM was written as ::. FROM CAST(1 + 2 AS int) AS a became FROM (1 + 2)::int AS a, which is a syntax error. The safety check rejected both outputs, so the statements were left unformatted.

select cast(-2147483648 as int), cast(-1.5 as numeric);
select * from cast(1 + 2 as int) as a, rows from (cast('x' as text)) as b;

This change parenthesizes a negative numeric constant before ::, and always emits CAST(...) for a cast that is a FROM item, including inside ROWS FROM. It adds the cast_operator_style_negative_constant and cast_operator_style_from_clause fixtures with snapshots generated from a real test run.

Running the Postgres regression statements through the formatter with castStyle: "operator" found 63 failing statements across the Postgres 15, 16, and 17 suites, all from these two causes. With this change, none fail.

Test:

cargo test -p pgls_pretty_print

@psteinroe psteinroe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@psteinroe
psteinroe merged commit 1d5c89a into supabase-community:main Oct 11, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants