Fix issue #217: Wrap FuncCall arguments in parentheses when using :: cast syntax#226
Closed
pyramation wants to merge 3 commits intomainfrom
Closed
Fix issue #217: Wrap FuncCall arguments in parentheses when using :: cast syntax#226pyramation wants to merge 3 commits intomainfrom
pyramation wants to merge 3 commits intomainfrom
Conversation
…cast syntax - Fixes operator precedence issues with double casts like CAST(...AS text)::date - Wraps FuncCall arguments (e.g., AT TIME ZONE expressions) in parentheses - Prevents :: operator from binding to wrong operand - Refactored empty if block to pass lint checks - Updated pg-catalog snapshot to reflect correct parenthesization Co-Authored-By: Dan Lynch <pyramation@gmail.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
3 tasks
Contributor
|
This PR has been superseded by #227 which contains the same fix but without the accidental formatting changes. PR #227 has a minimal diff with only 2 files changed (8 insertions, 3 deletions) making it much easier to review. The functional change is identical - wrapping FuncCall arguments in parentheses when using the Closing this PR in favor of the cleaner version. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)