Fix issue #217: Wrap FuncCall arguments in parentheses when using :: cast syntax #226
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #217: Wrap FuncCall arguments in parentheses when using :: cast syntax
Summary
Fixes a bug where double type casts with the
::operator were being deparsed incorrectly due to operator precedence issues. The problem occurred when a FuncCall (likeAT TIME ZONE) was cast using the::shorthand syntax, causing the cast to bind to the wrong operand.Example of the bug:
SELECT CAST(t.date AT TIME ZONE 'America/New_York' AS text)::date FROM tbl tSELECT CAST(t.date AT TIME ZONE 'America/New_York'::text AS date) FROM tbl AS t::textwas binding to'America/New_York'instead of the entireCAST(...)expressionThe fix:
When using the
::cast shorthand syntax with a FuncCall argument, wrap the FuncCall in parentheses to ensure correct operator precedence:(t.date AT TIME ZONE 'America/New_York')::textReview & Testing Checklist for Human
packages/deparser/src/deparser.tslines 2256-2268 (the TypeCast function). The key change is wrapping FuncCall arguments in parentheses when using::syntax.pg-catalog.test.ts.snapis correct: function calls likepublic.gen_random_uuid()::textnow become(public.gen_random_uuid())::text. This is intentional to prevent precedence issues.SELECT CAST(t.date AT TIME ZONE 'America/New_York' AS text)::date FROM tbl tnow deparses correctly and can be re-parsed without AST changes.'123'::intstill work correctly (they should, as the fix only affects FuncCall arguments).Test Plan
cd packages/deparser && yarn test(all 654 tests should pass)Notes
misc-issues.test.tsifblock to pass lint checksDevin Session: https://app.devin.ai/sessions/111a29019c274f83a61a873f35081622
Requested by: Dan Lynch (@pyramation)