diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java index 9d39dbf9cb7..c9b3c4eb093 100644 --- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java +++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java @@ -814,6 +814,7 @@ private SqlNode callToSql(@Nullable RexProgram program, RexCall call0, final List nodeList = toSql(program, call.getOperands()); switch (call.getKind()) { case CAST: + case SAFE_CAST: // CURSOR is used inside CAST, like 'CAST ($0): CURSOR NOT NULL', // convert it to sql call of {@link SqlStdOperatorTable#CURSOR}. final RelDataType dataType = call.getType(); diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java index 1578d0112e1..79b6b0b983e 100644 --- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java +++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java @@ -2251,6 +2251,19 @@ private SqlDialect nonOrdinalDialect() { .ok(expectedBqFormatDatetime); } + /** + * Test that the type of a SAFE_CAST rex call is converted to an argument of the SQL call. + * See [CALCITE-6117]. + */ + @Test void testBigQuerySafeCast() { + final String query = "select safe_cast(\"product_name\" as date) " + + "from \"foodmart\".\"product\""; + final String expected = "SELECT SAFE_CAST(\"product_name\" AS DATE)\n" + + "FROM \"foodmart\".\"product\""; + + sql(query).withLibrary(SqlLibrary.BIG_QUERY).ok(expected); + } + /** Test case for * [CALCITE-3220] * HiveSqlDialect should transform the SQL-standard TRIM function to TRIM,