Skip to content

[CALCITE-7475] Babel parser allows postfix access after PostgreSQL-style :: infix cast#4916

Open
Dwrite wants to merge 3 commits intoapache:mainfrom
Dwrite:CALCITE-7475
Open

[CALCITE-7475] Babel parser allows postfix access after PostgreSQL-style :: infix cast#4916
Dwrite wants to merge 3 commits intoapache:mainfrom
Dwrite:CALCITE-7475

Conversation

@Dwrite
Copy link
Copy Markdown

@Dwrite Dwrite commented May 4, 2026

Jira Link
CALCITE-7475

Changes Proposed
Summary
This PR fixes an issue in the Babel parser where postfix operators (such as array element access [] and field access .) were incorrectly handled or failed when following a PostgreSQL-style infix cast (::).

Problem
Previously, the parser could not clearly distinguish between a subscript that is part of a type definition (e.g., VARCHAR ARRAY) and a subscript that is an operator acting on the result of the cast. For example, in the expression 'test'::VARCHAR ARRAY[1], the [1] was often greedily or incorrectly consumed, leading to parsing errors or an invalid AST.

Solution
Eager Reduction in Parser: Modified the InfixCast logic to eagerly build the SqlCall for the :: operator. This ensures the cast is correctly scoped before handling any subsequent operators.

Postfix Expression Loop: Introduced a loop within the InfixCast rule to explicitly handle trailing (for ITEM access) and (for field access). This prevents these operators from being lost or incorrectly associated with the type specification.

Refined InfixCastDataType: Created a restricted variant of the data type parser specifically for infix casts. It correctly handles the ARRAY keyword while leaving subscript consumption to the expression layer.

Improved Unparsing: Updated the unparse logic for the :: operator to rely on operator precedence rather than forcing redundant parentheses, ensuring cleaner and more idiomatic PostgreSQL-style SQL output.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 4, 2026

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.

1 participant