[CALCITE-7475] Babel parser allows postfix access after PostgreSQL-style :: infix cast#4916
[CALCITE-7475] Babel parser allows postfix access after PostgreSQL-style :: infix cast#4916Dwrite wants to merge 1 commit intoapache:mainfrom
Conversation
|
Your jira link is not right, please fix it first. |
sorry,fixed |
| new SqlParserUtil.ToTreeListItem(SqlLibraryOperators.INFIX_CAST, | ||
| s.pos())); | ||
| list.add(dt); | ||
| SqlNode leftOperand = (SqlNode) list.remove(list.size() - 1); |
There was a problem hiding this comment.
maybe the name of this production rule should be changed from InfixCast because now it does more. InfixCastOrFieldAccess?
There was a problem hiding this comment.
I think the current name InfixCast is more appropriate because the added logic is strictly to prevent the 'greedy' consumption of subscripts by the type specification. Since we are essentially fixing the precedence of the :: operator rather than introducing a standalone field access rule, keeping the original name ensures clarity for future maintenance.
|



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.