Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.OperandTypes;
Expand Down Expand Up @@ -185,6 +186,13 @@ public MssqlSqlDialect(Context context) {
SqlOperator op = SqlStdOperatorTable.PERCENT_REMAINDER;
SqlSyntax.BINARY.unparse(writer, op, call, leftPrec, rightPrec);
break;
case SAFE_CAST:
// MSSQL uses TRY_CAST instead of SAFE_CAST (BigQuery)
super.unparseCall(writer,
SqlLibraryOperators.TRY_CAST.createCall(
call.getParserPosition(), call.getOperandList()),
leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,27 @@ private SqlDialect nonOrdinalDialect() {
sql(query).withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7456">[CALCITE-7456]
* Enable the TRY_CAST function to support the MSSQL dialect</a>. */
@Test void testMssqlTryCast() {
final String query = "select try_cast(\"product_name\" as date) "
+ "from \"foodmart\".\"product\"";
final String expected = "SELECT TRY_CAST([product_name] AS DATE)\n"
+ "FROM [foodmart].[product]";

sql(query).withLibrary(SqlLibrary.MSSQL).withMssql().ok(expected);
}

@Test void testSafeCastToMssqlTryCast() {
final String query = "select safe_cast(\"product_name\" as date) "
+ "from \"foodmart\".\"product\"";
final String expected = "SELECT TRY_CAST([product_name] AS DATE)\n"
+ "FROM [foodmart].[product]";

sql(query).withLibrary(SqlLibrary.BIG_QUERY).withMssql().ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6150">[CALCITE-6150]
* JDBC adapter for ClickHouse generates incorrect SQL for certain units in
Expand Down
Loading