sunxiaoguang commented on code in PR #49453:
URL: https://github.com/apache/spark/pull/49453#discussion_r1914266781


##########
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:
   Data types are validated as well in the test to make sure in the case MySQL 
only cast to BIGINT, we always get the right types defined from Spark side.
   
   ```scala
   ......
         val shortValue = 0.toShort
         val integerValue = 0
         val longValue = 0L
   ......
         def testCast(castType: String, sourceCol: String, targetCol: String,
                      sourceValue: Any, targetValue: Any): Unit = {
           val sql =
             s"""SELECT $sourceCol, CAST($sourceCol AS $castType) FROM 
$tableName
                |WHERE CAST($sourceCol AS $castType) = $targetCol""".stripMargin
           val rows = spark.sql(sql).collect()
           assert(rows.length === 1, s"Failed to cast $sourceCol to $castType")
           val row = rows(0)
           assert(row.get(0) === sourceValue, s"$sourceCol does not equal to 
$sourceValue")
           assert(row.get(0).getClass == sourceValue.getClass,
             s"$sourceCol has different type from $sourceValue")
           assert(row.get(1) === targetValue,
             s"CAST($sourceCol AS $castType) does not equal to $targetValue")
           assert(row.get(1).getClass == targetValue.getClass,
             s"CAST($sourceCol AS $castType) has different type from 
$targetValue")
         }
   ......
         testCast("SHORT", "string_col", "short_col", stringValue, shortValue)
         testCast("INTEGER", "string_col", "integer_col", stringValue, 
integerValue)
         testCast("LONG", "string_col", "long_col", stringValue, longValue)
   ```



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

Reply via email to