Dwrite commented on code in PR #4916:
URL: https://github.com/apache/calcite/pull/4916#discussion_r3227319042


##########
babel/src/test/java/org/apache/calcite/test/BabelParserTest.java:
##########
@@ -290,11 +294,50 @@ class BabelParserTest extends SqlParserTest {
     final String sql = "select -('12' || '.34')::VARCHAR(30)::INTEGER as x\n"
         + "from t";
     final String expected = ""
-        + "SELECT (- ('12' || '.34') :: VARCHAR(30) :: INTEGER) AS `X`\n"
+        + "SELECT (- ('12' || '.34')) :: VARCHAR(30) :: INTEGER AS `X`\n"
         + "FROM `T`";
     sql(sql).ok(expected);
   }
 
+  /**
+   * Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7475";>[CALCITE-7475]
+   * Babel parser allows postfix access after PostgreSQL-style {@code ::} 
infix cast</a>.
+   *
+   * <p>Verifies that PostgreSQL-style infix cast ({@code ::}) correctly binds
+   * tighter than postfix access operators such as array indexing ({@code []})
+   * and field access ({@code .}).
+   */
+  @Test void testParseInfixCastWithPostfixAccess() {
+    final String sql = "select 'test'::varchar array[1].field";
+
+    // 1. Verify the unparsed SQL string.
+    // Calcite's unparser adds parentheses to reflect the correct AST 
precedence.
+    final String expected = "SELECT ('test' :: VARCHAR ARRAY[1].`FIELD`)";

Review Comment:
   That's a great point regarding visual clarity. I have adjusted the operator 
precedence and the unparse frame handling to ensure the cast result is wrapped 
in parentheses when followed by higher-precedence postfix operators like ITEM 
and DOT.
   
   The updated output is now:
   SELECT (('test' :: VARCHAR ARRAY)[1].FIELD)
   
   This explicitly separates the VARCHAR ARRAY type from the [1] subscript and 
the .FIELD member access, matching the intended AST: DOT(ITEM(CAST(...))). The 
PR has been updated with these changes and corresponding test assertions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to