AlexisCubilla commented on code in PR #5097:
URL: https://github.com/apache/calcite/pull/5097#discussion_r3573341396


##########
core/src/main/java/org/apache/calcite/sql/dialect/MssqlSqlDialect.java:
##########
@@ -92,6 +94,34 @@ public MssqlSqlDialect(Context context) {
     top = context.databaseMajorVersion() < 11;
   }
 
+  @Override public @Nullable SqlNode getCastSpec(RelDataType type) {
+    switch (type.getSqlTypeName()) {
+    case TIMESTAMP:
+      // In SQL Server, TIMESTAMP is a deprecated synonym for ROWVERSION
+      // (a binary, auto-generated type), not a temporal type. The correct
+      // fixed-precision date/time type is DATETIME2 (SQL Server 2008+).
+      return createDatetimeCastSpec("DATETIME2", type);
+    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+      // SQL Server's timezone-aware date/time type.
+      return createDatetimeCastSpec("DATETIMEOFFSET", type);
+    default:
+      return super.getCastSpec(type);
+    }
+  }
+
+  /** Builds a SQL Server date/time cast target such as {@code DATETIME2(3)},
+   * appending the fractional-seconds precision when it is in the valid SQL
+   * Server range [0, 7]. */
+  private static SqlNode createDatetimeCastSpec(String typeAlias, RelDataType 
type) {
+    final int precision = type.getPrecision();
+    final String spec = precision >= 0 && precision <= 7
+        ? typeAlias + "(" + precision + ")"
+        : typeAlias;

Review Comment:
   Thanks for the review!
   
   Type without precision is fine: in SQL Server both DATETIME2 and 
DATETIMEOFFSET without a precision argument default to a fractional-seconds 
precision of 7, so the bare type name is valid.
   Precision limits are identical for both: 0 to 7. That's why the same 
createDatetimeCastSpec helper works for both — it emits TYPE(n) when 0 <= n <= 
7 and otherwise falls back to the bare type name (which defaults to 7).
   I've added a DATETIMEOFFSET test case (TIMESTAMP WITH LOCAL TIME ZONE) and 
updated the Jira description to mention it.



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