predator4ann commented on code in PR #6657:
URL: https://github.com/apache/gravitino/pull/6657#discussion_r2218650504


##########
catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/JdbcTableOperations.java:
##########
@@ -626,4 +633,117 @@ protected JdbcColumn.Builder 
getBasicJdbcColumnInfo(ResultSet column) throws SQL
         .withNullable(nullable)
         .withDefaultValue(defaultValue);
   }
+
+  /**
+   * Calculate the precision for time/datetime/timestamp types.
+   *
+   * <p>This method provides a default behavior returning null for catalogs 
that do not implement
+   * this method. Developers should override this method when their database 
supports precision
+   * specification for datetime types.
+   *
+   * <p><strong>When to return null:</strong>
+   *
+   * <ul>
+   *   <li>When the database does not support precision for datetime types
+   *   <li>When the driver version is incompatible (e.g., MySQL driver &lt; 
8.0.16)
+   *   <li>When the type is not a datetime type (TIME, TIMESTAMP, DATETIME)
+   *   <li>When the precision cannot be accurately calculated from the 
provided parameters
+   * </ul>
+   *
+   * <p><strong>When to return non-null:</strong>
+   *
+   * <ul>
+   *   <li>When the database supports precision for datetime types and the 
driver version is
+   *       compatible
+   *   <li>When the precision can be accurately calculated from columnSize 
(e.g., columnSize -
+   *       format_length)
+   *   <li>For TIME types: return columnSize - 8 (for 'HH:MM:SS' format)
+   *   <li>For TIMESTAMP/DATETIME types: return columnSize - 19 (for 
'YYYY-MM-DD HH:MM:SS' format)
+   * </ul>
+   *
+   * <p><strong>Examples:</strong>
+   *
+   * <ul>
+   *   <li>TIME(3) with columnSize=11: return 3 (11-8)
+   *   <li>TIMESTAMP(6) with columnSize=25: return 6 (25-19)
+   *   <li>DATETIME(0) with columnSize=19: return 0 (19-19)
+   * </ul>
+   *
+   * @param typeName the type name from database (e.g., "TIME", "TIMESTAMP", 
"DATETIME")
+   * @param columnSize the column size from database (total length including 
format and precision)
+   * @param scale the scale from database (usually 0 for datetime types)
+   * @return the precision of the time/datetime/timestamp type, or null if not 
supported/calculable
+   */
+  public Integer calculateDatetimePrecision(String typeName, int columnSize, 
int scale) {
+    return null;
+  }
+
+  /**
+   * Get MySQL driver version from DatabaseMetaData
+   *
+   * @return the driver version string, or null if not available
+   */
+  protected String getMySQLDriverVersion() {
+    try {
+      if (dataSource != null) {
+        try (Connection connection = dataSource.getConnection()) {
+          return connection.getMetaData().getDriverVersion();
+        }
+      }
+    } catch (SQLException e) {
+      LOG.debug("Failed to get driver version", e);
+    }
+    return null;
+  }
+
+  /**
+   * Check if driver version supports accurate columnSize for precision 
calculation. For MySQL
+   * driver: Only versions &gt;= 8.0.16 return accurate columnSize for 
datetime precision. For other
+   * drivers (like OceanBase): Assume they support accurate precision 
calculation
+   *
+   * @param driverVersion the driver version string to check
+   * @return true if the driver version supports accurate precision 
calculation, false otherwise
+   */
+  public boolean isMySQLDriverVersionSupported(String driverVersion) {
+    if (StringUtils.isBlank(driverVersion)) {
+      return false;
+    }
+
+    // For non-MySQL drivers, assume they support accurate precision 
calculation
+    if (!driverVersion.startsWith("mysql-connector-java-")) {
+      LOG.debug(
+          "Non-MySQL driver detected: {}. Assuming it supports accurate 
precision calculation.",
+          driverVersion);
+      return true;
+    }
+
+    // Extract version from driver string like "mysql-connector-java-8.0.19 
(Revision: ...)"
+    String versionPattern = "mysql-connector-java-(\\d+\\.\\d+\\.\\d+)";
+    java.util.regex.Pattern pattern = 
java.util.regex.Pattern.compile(versionPattern);
+    java.util.regex.Matcher matcher = pattern.matcher(driverVersion);

Review Comment:
   done



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