sunxiaoguang commented on code in PR #49453: URL: https://github.com/apache/spark/pull/49453#discussion_r1912725632
########## sql/core/src/main/scala/org/apache/spark/sql/jdbc/MySQLDialect.scala: ########## @@ -112,6 +112,19 @@ private case class MySQLDialect() extends JdbcDialect with SQLConfHelper with No } else { super.visitAggregateFunction(funcName, isDistinct, inputs) } + + override def visitCast(expr: String, exprDataType: DataType, dataType: DataType): String = { + val databaseTypeDefinition = dataType match { + // MySQL uses CHAR in the cast function for the type LONGTEXT + case StringType => "CHAR" + // MySQL uses SIGNED INTEGER in the cast function for the types SMALLINT, INTEGER and BIGINT + case ShortType | IntegerType | LongType => "SIGNED INTEGER" Review Comment: You are right, MySQL doesn't support casting to Short and Integer. But luckily, the wire protocol and JDBC driver implementation works with the same BIGINT result for Short, Integer and Long types. ```java import java.sql.*; public class JDBC { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 3306; String username = "root"; String password = ""; String database = "test"; try (Connection conn = DriverManager.getConnection( String.format("jdbc:mysql://%s:%d/%s", host, port, database), username, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT CAST(1 AS SIGNED INTEGER)")) { if (rs.next()) { System.out.println("Short: " + rs.getShort(1)); System.out.println("Integer: " + rs.getInt(1)); System.out.println("Long: " + rs.getLong(1)); } } } } ``` And here is the output ```bash java -cp mysql-connector-java-8.0.25.jar JDBC.java Short: 1 Integer: 1 Long: 1 ``` -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org