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


##########
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:
   I think the intended AST is correct, `v::varchar array[1].field` should 
parse as `DOT(ITEM(CAST(v, VARCHAR ARRAY), 1), FIELD)`.
   
   However, the expected unparse still prints `v :: VARCHAR ARRAY[1].FIELD`, 
which visually suggests that `[1].FIELD` is part of the type. Could we make the 
unparser emit `(v :: VARCHAR ARRAY)[1].FIELD` instead? That would clearly 
separate the cast type from the postfix access on the cast result.



-- 
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