Dwrite commented on code in PR #4916:
URL: https://github.com/apache/calcite/pull/4916#discussion_r3227098888
##########
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:
yeah. I completely agree. Adding parentheses like (v :: VARCHAR
ARRAY)[1].FIELD would significantly improve visual clarity and eliminate any
ambiguity regarding whether the subscripts belong to the type or the cast
result.
I will investigate the unparse logic and the precedence settings of the
INFIX_CAST operator. The goal is to ensure that when a cast is an operand of a
higher-precedence postfix operator (like ITEM or DOT), Calcite's SqlWriter
automatically wraps it in parentheses. I will update the PR once I have a
working solution for this."
--
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]