Skip to content
Closed
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 @@ -172,6 +172,13 @@ public MssqlSqlDialect(Context context) {
final SqlWriter.Frame frame = writer.startFunCall("CEILING");
call.operand(0).unparse(writer, leftPrec, rightPrec);
writer.endFunCall(frame);
} else if (call.getKind() == SqlKind.SAFE_CAST) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually assembling unparsed logic is repetitive and fragile.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I researched other dialects, and they almost all use writer.startFunCall to rewrite functions. I am not sure this is good way, but this is the current reality.

// TRY_CAST is supported but not SAFE_CAST in MS SQL
final SqlWriter.Frame frame = writer.startFunCall("TRY_CAST");
call.operand(0).unparse(writer, leftPrec, rightPrec);
writer.sep("as");
call.operand(1).unparse(writer, leftPrec, rightPrec);
writer.endFunCall(frame);
} else {
switch (call.getKind()) {
case FLOOR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12025,4 +12025,16 @@ public Sql schema(CalciteAssert.SchemaSpec schemaSpec) {
sql(query).withLibrary(SqlLibrary.HIVE).withHive().ok(expectedHive);
sql(query).withLibrary(SqlLibrary.SPARK).withSpark().ok(expectedSpark);
}

/** 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 testTryCastWithMssql() {
final String query = "select TRY_CAST(\"gross_weight\" as bigint) as gross_weight_value\n"
+ "from \"product\"";
final String expectedSql = "SELECT TRY_CAST([gross_weight] AS BIGINT) AS [GROSS_WEIGHT_VALUE]"
+ "\nFROM [foodmart].[product]";

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